xref: /freebsd/sys/dev/bge/if_bge.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
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  * $FreeBSD$
34  */
35 
36 /*
37  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
38  *
39  * Written by Bill Paul <wpaul@windriver.com>
40  * Senior Engineer, Wind River Systems
41  */
42 
43 /*
44  * The Broadcom BCM5700 is based on technology originally developed by
45  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
46  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
47  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
48  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
49  * frames, highly configurable RX filtering, and 16 RX and TX queues
50  * (which, along with RX filter rules, can be used for QOS applications).
51  * Other features, such as TCP segmentation, may be available as part
52  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
53  * firmware images can be stored in hardware and need not be compiled
54  * into the driver.
55  *
56  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
57  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
58  *
59  * The BCM5701 is a single-chip solution incorporating both the BCM5700
60  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
61  * does not support external SSRAM.
62  *
63  * Broadcom also produces a variation of the BCM5700 under the "Altima"
64  * brand name, which is functionally similar but lacks PCI-X support.
65  *
66  * Without external SSRAM, you can only have at most 4 TX rings,
67  * and the use of the mini RX ring is disabled. This seems to imply
68  * that these features are simply not available on the BCM5701. As a
69  * result, this driver does not implement any support for the mini RX
70  * ring.
71  */
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/sockio.h>
76 #include <sys/mbuf.h>
77 #include <sys/malloc.h>
78 #include <sys/kernel.h>
79 #include <sys/socket.h>
80 #include <sys/queue.h>
81 
82 #include <net/if.h>
83 #include <net/if_arp.h>
84 #include <net/ethernet.h>
85 #include <net/if_dl.h>
86 #include <net/if_media.h>
87 
88 #include <net/bpf.h>
89 
90 #include <net/if_types.h>
91 #include <net/if_vlan_var.h>
92 
93 #include <netinet/in_systm.h>
94 #include <netinet/in.h>
95 #include <netinet/ip.h>
96 
97 #include <vm/vm.h>              /* for vtophys */
98 #include <vm/pmap.h>            /* for vtophys */
99 #include <machine/clock.h>      /* for DELAY */
100 #include <machine/bus_memio.h>
101 #include <machine/bus.h>
102 #include <machine/resource.h>
103 #include <sys/bus.h>
104 #include <sys/rman.h>
105 
106 #include <dev/mii/mii.h>
107 #include <dev/mii/miivar.h>
108 #include <dev/mii/miidevs.h>
109 #include <dev/mii/brgphyreg.h>
110 
111 #include <pci/pcireg.h>
112 #include <pci/pcivar.h>
113 
114 #include <dev/bge/if_bgereg.h>
115 
116 #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
117 
118 MODULE_DEPEND(bge, miibus, 1, 1, 1);
119 
120 /* "controller miibus0" required.  See GENERIC if you get errors here. */
121 #include "miibus_if.h"
122 
123 #if !defined(lint)
124 static const char rcsid[] =
125   "$FreeBSD$";
126 #endif
127 
128 /*
129  * Various supported device vendors/types and their names. Note: the
130  * spec seems to indicate that the hardware still has Alteon's vendor
131  * ID burned into it, though it will always be overriden by the vendor
132  * ID in the EEPROM. Just to be safe, we cover all possibilities.
133  */
134 
135 static struct bge_type bge_devs[] = {
136 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5700,
137 		"Broadcom BCM5700 Gigabit Ethernet" },
138 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5701,
139 		"Broadcom BCM5701 Gigabit Ethernet" },
140 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5700,
141 		"Broadcom BCM5700 Gigabit Ethernet" },
142 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5701,
143 		"Broadcom BCM5701 Gigabit Ethernet" },
144 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702X,
145 		"Broadcom BCM5702X Gigabit Ethernet" },
146 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703X,
147 		"Broadcom BCM5703X Gigabit Ethernet" },
148 	{ SK_VENDORID, SK_DEVICEID_ALTIMA,
149 		"SysKonnect Gigabit Ethernet" },
150 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000,
151 		"Altima AC1000 Gigabit Ethernet" },
152 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100,
153 		"Altima AC9100 Gigabit Ethernet" },
154 	{ 0, 0, NULL }
155 };
156 
157 static int bge_probe		(device_t);
158 static int bge_attach		(device_t);
159 static int bge_detach		(device_t);
160 static void bge_release_resources
161 				(struct bge_softc *);
162 static void bge_txeof		(struct bge_softc *);
163 static void bge_rxeof		(struct bge_softc *);
164 
165 static void bge_tick		(void *);
166 static void bge_stats_update	(struct bge_softc *);
167 static int bge_encap		(struct bge_softc *, struct mbuf *,
168 					u_int32_t *);
169 
170 static void bge_intr		(void *);
171 static void bge_start		(struct ifnet *);
172 static int bge_ioctl		(struct ifnet *, u_long, caddr_t);
173 static void bge_init		(void *);
174 static void bge_stop		(struct bge_softc *);
175 static void bge_watchdog		(struct ifnet *);
176 static void bge_shutdown		(device_t);
177 static int bge_ifmedia_upd	(struct ifnet *);
178 static void bge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
179 
180 static u_int8_t	bge_eeprom_getbyte	(struct bge_softc *, int, u_int8_t *);
181 static int bge_read_eeprom	(struct bge_softc *, caddr_t, int, int);
182 
183 static u_int32_t bge_crc	(caddr_t);
184 static void bge_setmulti	(struct bge_softc *);
185 
186 static void bge_handle_events	(struct bge_softc *);
187 static int bge_alloc_jumbo_mem	(struct bge_softc *);
188 static void bge_free_jumbo_mem	(struct bge_softc *);
189 static void *bge_jalloc		(struct bge_softc *);
190 static void bge_jfree		(void *, void *);
191 static int bge_newbuf_std	(struct bge_softc *, int, struct mbuf *);
192 static int bge_newbuf_jumbo	(struct bge_softc *, int, struct mbuf *);
193 static int bge_init_rx_ring_std	(struct bge_softc *);
194 static void bge_free_rx_ring_std	(struct bge_softc *);
195 static int bge_init_rx_ring_jumbo	(struct bge_softc *);
196 static void bge_free_rx_ring_jumbo	(struct bge_softc *);
197 static void bge_free_tx_ring	(struct bge_softc *);
198 static int bge_init_tx_ring	(struct bge_softc *);
199 
200 static int bge_chipinit		(struct bge_softc *);
201 static int bge_blockinit	(struct bge_softc *);
202 
203 #ifdef notdef
204 static u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
205 static void bge_vpd_read_res	(struct bge_softc *, struct vpd_res *, int);
206 static void bge_vpd_read	(struct bge_softc *);
207 #endif
208 
209 static u_int32_t bge_readmem_ind
210 				(struct bge_softc *, int);
211 static void bge_writemem_ind	(struct bge_softc *, int, int);
212 #ifdef notdef
213 static u_int32_t bge_readreg_ind
214 				(struct bge_softc *, int);
215 #endif
216 static void bge_writereg_ind	(struct bge_softc *, int, int);
217 
218 static int bge_miibus_readreg	(device_t, int, int);
219 static int bge_miibus_writereg	(device_t, int, int, int);
220 static void bge_miibus_statchg	(device_t);
221 
222 static void bge_reset		(struct bge_softc *);
223 static void bge_phy_hack	(struct bge_softc *);
224 
225 static device_method_t bge_methods[] = {
226 	/* Device interface */
227 	DEVMETHOD(device_probe,		bge_probe),
228 	DEVMETHOD(device_attach,	bge_attach),
229 	DEVMETHOD(device_detach,	bge_detach),
230 	DEVMETHOD(device_shutdown,	bge_shutdown),
231 
232 	/* bus interface */
233 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
234 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
235 
236 	/* MII interface */
237 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
238 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
239 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
240 
241 	{ 0, 0 }
242 };
243 
244 static driver_t bge_driver = {
245 	"bge",
246 	bge_methods,
247 	sizeof(struct bge_softc)
248 };
249 
250 static devclass_t bge_devclass;
251 
252 DRIVER_MODULE(if_bge, pci, bge_driver, bge_devclass, 0, 0);
253 DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
254 
255 static u_int32_t
256 bge_readmem_ind(sc, off)
257 	struct bge_softc *sc;
258 	int off;
259 {
260 	device_t dev;
261 
262 	dev = sc->bge_dev;
263 
264 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
265 	return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
266 }
267 
268 static void
269 bge_writemem_ind(sc, off, val)
270 	struct bge_softc *sc;
271 	int off, val;
272 {
273 	device_t dev;
274 
275 	dev = sc->bge_dev;
276 
277 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
278 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
279 
280 	return;
281 }
282 
283 #ifdef notdef
284 static u_int32_t
285 bge_readreg_ind(sc, off)
286 	struct bge_softc *sc;
287 	int off;
288 {
289 	device_t dev;
290 
291 	dev = sc->bge_dev;
292 
293 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
294 	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
295 }
296 #endif
297 
298 static void
299 bge_writereg_ind(sc, off, val)
300 	struct bge_softc *sc;
301 	int off, val;
302 {
303 	device_t dev;
304 
305 	dev = sc->bge_dev;
306 
307 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
308 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
309 
310 	return;
311 }
312 
313 #ifdef notdef
314 static u_int8_t
315 bge_vpd_readbyte(sc, addr)
316 	struct bge_softc *sc;
317 	int addr;
318 {
319 	int i;
320 	device_t dev;
321 	u_int32_t val;
322 
323 	dev = sc->bge_dev;
324 	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
325 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
326 		DELAY(10);
327 		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
328 			break;
329 	}
330 
331 	if (i == BGE_TIMEOUT) {
332 		printf("bge%d: VPD read timed out\n", sc->bge_unit);
333 		return(0);
334 	}
335 
336 	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
337 
338 	return((val >> ((addr % 4) * 8)) & 0xFF);
339 }
340 
341 static void
342 bge_vpd_read_res(sc, res, addr)
343 	struct bge_softc *sc;
344 	struct vpd_res *res;
345 	int addr;
346 {
347 	int i;
348 	u_int8_t *ptr;
349 
350 	ptr = (u_int8_t *)res;
351 	for (i = 0; i < sizeof(struct vpd_res); i++)
352 		ptr[i] = bge_vpd_readbyte(sc, i + addr);
353 
354 	return;
355 }
356 
357 static void
358 bge_vpd_read(sc)
359 	struct bge_softc *sc;
360 {
361 	int pos = 0, i;
362 	struct vpd_res res;
363 
364 	if (sc->bge_vpd_prodname != NULL)
365 		free(sc->bge_vpd_prodname, M_DEVBUF);
366 	if (sc->bge_vpd_readonly != NULL)
367 		free(sc->bge_vpd_readonly, M_DEVBUF);
368 	sc->bge_vpd_prodname = NULL;
369 	sc->bge_vpd_readonly = NULL;
370 
371 	bge_vpd_read_res(sc, &res, pos);
372 
373 	if (res.vr_id != VPD_RES_ID) {
374 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
375 			sc->bge_unit, VPD_RES_ID, res.vr_id);
376                 return;
377         }
378 
379 	pos += sizeof(res);
380 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
381 	for (i = 0; i < res.vr_len; i++)
382 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
383 	sc->bge_vpd_prodname[i] = '\0';
384 	pos += i;
385 
386 	bge_vpd_read_res(sc, &res, pos);
387 
388 	if (res.vr_id != VPD_RES_READ) {
389 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
390 		    sc->bge_unit, VPD_RES_READ, res.vr_id);
391 		return;
392 	}
393 
394 	pos += sizeof(res);
395 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
396 	for (i = 0; i < res.vr_len + 1; i++)
397 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
398 
399 	return;
400 }
401 #endif
402 
403 /*
404  * Read a byte of data stored in the EEPROM at address 'addr.' The
405  * BCM570x supports both the traditional bitbang interface and an
406  * auto access interface for reading the EEPROM. We use the auto
407  * access method.
408  */
409 static u_int8_t
410 bge_eeprom_getbyte(sc, addr, dest)
411 	struct bge_softc *sc;
412 	int addr;
413 	u_int8_t *dest;
414 {
415 	int i;
416 	u_int32_t byte = 0;
417 
418 	/*
419 	 * Enable use of auto EEPROM access so we can avoid
420 	 * having to use the bitbang method.
421 	 */
422 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
423 
424 	/* Reset the EEPROM, load the clock period. */
425 	CSR_WRITE_4(sc, BGE_EE_ADDR,
426 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
427 	DELAY(20);
428 
429 	/* Issue the read EEPROM command. */
430 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
431 
432 	/* Wait for completion */
433 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
434 		DELAY(10);
435 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
436 			break;
437 	}
438 
439 	if (i == BGE_TIMEOUT) {
440 		printf("bge%d: eeprom read timed out\n", sc->bge_unit);
441 		return(0);
442 	}
443 
444 	/* Get result. */
445 	byte = CSR_READ_4(sc, BGE_EE_DATA);
446 
447         *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
448 
449 	return(0);
450 }
451 
452 /*
453  * Read a sequence of bytes from the EEPROM.
454  */
455 static int
456 bge_read_eeprom(sc, dest, off, cnt)
457 	struct bge_softc *sc;
458 	caddr_t dest;
459 	int off;
460 	int cnt;
461 {
462 	int err = 0, i;
463 	u_int8_t byte = 0;
464 
465 	for (i = 0; i < cnt; i++) {
466 		err = bge_eeprom_getbyte(sc, off + i, &byte);
467 		if (err)
468 			break;
469 		*(dest + i) = byte;
470 	}
471 
472 	return(err ? 1 : 0);
473 }
474 
475 static int
476 bge_miibus_readreg(dev, phy, reg)
477 	device_t dev;
478 	int phy, reg;
479 {
480 	struct bge_softc *sc;
481 	struct ifnet *ifp;
482 	u_int32_t val;
483 	int i;
484 
485 	sc = device_get_softc(dev);
486 	ifp = &sc->arpcom.ac_if;
487 
488 	if (phy != 1)
489 		switch(sc->bge_asicrev) {
490 		case BGE_ASICREV_BCM5701_B5:
491 		case BGE_ASICREV_BCM5703_A2:
492 			return(0);
493 		}
494 
495 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
496 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
497 
498 	for (i = 0; i < BGE_TIMEOUT; i++) {
499 		val = CSR_READ_4(sc, BGE_MI_COMM);
500 		if (!(val & BGE_MICOMM_BUSY))
501 			break;
502 	}
503 
504 	if (i == BGE_TIMEOUT) {
505 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
506 		return(0);
507 	}
508 
509 	val = CSR_READ_4(sc, BGE_MI_COMM);
510 
511 	if (val & BGE_MICOMM_READFAIL)
512 		return(0);
513 
514 	return(val & 0xFFFF);
515 }
516 
517 static int
518 bge_miibus_writereg(dev, phy, reg, val)
519 	device_t dev;
520 	int phy, reg, val;
521 {
522 	struct bge_softc *sc;
523 	int i;
524 
525 	sc = device_get_softc(dev);
526 
527 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
528 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
529 
530 	for (i = 0; i < BGE_TIMEOUT; i++) {
531 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
532 			break;
533 	}
534 
535 	if (i == BGE_TIMEOUT) {
536 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
537 		return(0);
538 	}
539 
540 	return(0);
541 }
542 
543 static void
544 bge_miibus_statchg(dev)
545 	device_t dev;
546 {
547 	struct bge_softc *sc;
548 	struct mii_data *mii;
549 
550 	sc = device_get_softc(dev);
551 	mii = device_get_softc(sc->bge_miibus);
552 
553 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
554 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
555 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
556 	} else {
557 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
558 	}
559 
560 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
561 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
562 	} else {
563 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
564 	}
565 
566 	bge_phy_hack(sc);
567 
568 	return;
569 }
570 
571 /*
572  * Handle events that have triggered interrupts.
573  */
574 static void
575 bge_handle_events(sc)
576 	struct bge_softc		*sc;
577 {
578 
579 	return;
580 }
581 
582 /*
583  * Memory management for jumbo frames.
584  */
585 
586 static int
587 bge_alloc_jumbo_mem(sc)
588 	struct bge_softc		*sc;
589 {
590 	caddr_t			ptr;
591 	register int		i;
592 	struct bge_jpool_entry   *entry;
593 
594 	/* Grab a big chunk o' storage. */
595 	sc->bge_cdata.bge_jumbo_buf = contigmalloc(BGE_JMEM, M_DEVBUF,
596 		M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
597 
598 	if (sc->bge_cdata.bge_jumbo_buf == NULL) {
599 		printf("bge%d: no memory for jumbo buffers!\n", sc->bge_unit);
600 		return(ENOBUFS);
601 	}
602 
603 	SLIST_INIT(&sc->bge_jfree_listhead);
604 	SLIST_INIT(&sc->bge_jinuse_listhead);
605 
606 	/*
607 	 * Now divide it up into 9K pieces and save the addresses
608 	 * in an array.
609 	 */
610 	ptr = sc->bge_cdata.bge_jumbo_buf;
611 	for (i = 0; i < BGE_JSLOTS; i++) {
612 		sc->bge_cdata.bge_jslots[i] = ptr;
613 		ptr += BGE_JLEN;
614 		entry = malloc(sizeof(struct bge_jpool_entry),
615 		    M_DEVBUF, M_NOWAIT);
616 		if (entry == NULL) {
617 			contigfree(sc->bge_cdata.bge_jumbo_buf,
618 			    BGE_JMEM, M_DEVBUF);
619 			sc->bge_cdata.bge_jumbo_buf = NULL;
620 			printf("bge%d: no memory for jumbo "
621 			    "buffer queue!\n", sc->bge_unit);
622 			return(ENOBUFS);
623 		}
624 		entry->slot = i;
625 		SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
626 		    entry, jpool_entries);
627 	}
628 
629 	return(0);
630 }
631 
632 static void
633 bge_free_jumbo_mem(sc)
634         struct bge_softc *sc;
635 {
636         int i;
637         struct bge_jpool_entry *entry;
638 
639 	for (i = 0; i < BGE_JSLOTS; i++) {
640 		entry = SLIST_FIRST(&sc->bge_jfree_listhead);
641 		SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
642 		free(entry, M_DEVBUF);
643 	}
644 
645 	contigfree(sc->bge_cdata.bge_jumbo_buf, BGE_JMEM, M_DEVBUF);
646 
647         return;
648 }
649 
650 /*
651  * Allocate a jumbo buffer.
652  */
653 static void *
654 bge_jalloc(sc)
655 	struct bge_softc		*sc;
656 {
657 	struct bge_jpool_entry   *entry;
658 
659 	entry = SLIST_FIRST(&sc->bge_jfree_listhead);
660 
661 	if (entry == NULL) {
662 		printf("bge%d: no free jumbo buffers\n", sc->bge_unit);
663 		return(NULL);
664 	}
665 
666 	SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
667 	SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
668 	return(sc->bge_cdata.bge_jslots[entry->slot]);
669 }
670 
671 /*
672  * Release a jumbo buffer.
673  */
674 static void
675 bge_jfree(buf, args)
676 	void *buf;
677 	void *args;
678 {
679 	struct bge_jpool_entry *entry;
680 	struct bge_softc *sc;
681 	int i;
682 
683 	/* Extract the softc struct pointer. */
684 	sc = (struct bge_softc *)args;
685 
686 	if (sc == NULL)
687 		panic("bge_jfree: can't find softc pointer!");
688 
689 	/* calculate the slot this buffer belongs to */
690 
691 	i = ((vm_offset_t)buf
692 	     - (vm_offset_t)sc->bge_cdata.bge_jumbo_buf) / BGE_JLEN;
693 
694 	if ((i < 0) || (i >= BGE_JSLOTS))
695 		panic("bge_jfree: asked to free buffer that we don't manage!");
696 
697 	entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
698 	if (entry == NULL)
699 		panic("bge_jfree: buffer not in use!");
700 	entry->slot = i;
701 	SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
702 	SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
703 
704 	return;
705 }
706 
707 
708 /*
709  * Intialize a standard receive ring descriptor.
710  */
711 static int
712 bge_newbuf_std(sc, i, m)
713 	struct bge_softc	*sc;
714 	int			i;
715 	struct mbuf		*m;
716 {
717 	struct mbuf		*m_new = NULL;
718 	struct bge_rx_bd	*r;
719 
720 	if (m == NULL) {
721 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
722 		if (m_new == NULL) {
723 			return(ENOBUFS);
724 		}
725 
726 		MCLGET(m_new, M_DONTWAIT);
727 		if (!(m_new->m_flags & M_EXT)) {
728 			m_freem(m_new);
729 			return(ENOBUFS);
730 		}
731 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
732 	} else {
733 		m_new = m;
734 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
735 		m_new->m_data = m_new->m_ext.ext_buf;
736 	}
737 
738 	if (!sc->bge_rx_alignment_bug)
739 		m_adj(m_new, ETHER_ALIGN);
740 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
741 	r = &sc->bge_rdata->bge_rx_std_ring[i];
742 	BGE_HOSTADDR(r->bge_addr) = vtophys(mtod(m_new, caddr_t));
743 	r->bge_flags = BGE_RXBDFLAG_END;
744 	r->bge_len = m_new->m_len;
745 	r->bge_idx = i;
746 
747 	return(0);
748 }
749 
750 /*
751  * Initialize a jumbo receive ring descriptor. This allocates
752  * a jumbo buffer from the pool managed internally by the driver.
753  */
754 static int
755 bge_newbuf_jumbo(sc, i, m)
756 	struct bge_softc *sc;
757 	int i;
758 	struct mbuf *m;
759 {
760 	struct mbuf *m_new = NULL;
761 	struct bge_rx_bd *r;
762 
763 	if (m == NULL) {
764 		caddr_t			*buf = NULL;
765 
766 		/* Allocate the mbuf. */
767 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
768 		if (m_new == NULL) {
769 			return(ENOBUFS);
770 		}
771 
772 		/* Allocate the jumbo buffer */
773 		buf = bge_jalloc(sc);
774 		if (buf == NULL) {
775 			m_freem(m_new);
776 			printf("bge%d: jumbo allocation failed "
777 			    "-- packet dropped!\n", sc->bge_unit);
778 			return(ENOBUFS);
779 		}
780 
781 		/* Attach the buffer to the mbuf. */
782 		m_new->m_data = (void *) buf;
783 		m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
784 		MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, bge_jfree,
785 		    (struct bge_softc *)sc, 0, EXT_NET_DRV);
786 	} else {
787 		m_new = m;
788 		m_new->m_data = m_new->m_ext.ext_buf;
789 		m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
790 	}
791 
792 	if (!sc->bge_rx_alignment_bug)
793 		m_adj(m_new, ETHER_ALIGN);
794 	/* Set up the descriptor. */
795 	r = &sc->bge_rdata->bge_rx_jumbo_ring[i];
796 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
797 	BGE_HOSTADDR(r->bge_addr) = vtophys(mtod(m_new, caddr_t));
798 	r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
799 	r->bge_len = m_new->m_len;
800 	r->bge_idx = i;
801 
802 	return(0);
803 }
804 
805 /*
806  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
807  * that's 1MB or memory, which is a lot. For now, we fill only the first
808  * 256 ring entries and hope that our CPU is fast enough to keep up with
809  * the NIC.
810  */
811 static int
812 bge_init_rx_ring_std(sc)
813 	struct bge_softc *sc;
814 {
815 	int i;
816 
817 	for (i = 0; i < BGE_SSLOTS; i++) {
818 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
819 			return(ENOBUFS);
820 	};
821 
822 	sc->bge_std = i - 1;
823 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
824 
825 	return(0);
826 }
827 
828 static void
829 bge_free_rx_ring_std(sc)
830 	struct bge_softc *sc;
831 {
832 	int i;
833 
834 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
835 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
836 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
837 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
838 		}
839 		bzero((char *)&sc->bge_rdata->bge_rx_std_ring[i],
840 		    sizeof(struct bge_rx_bd));
841 	}
842 
843 	return;
844 }
845 
846 static int
847 bge_init_rx_ring_jumbo(sc)
848 	struct bge_softc *sc;
849 {
850 	int i;
851 	struct bge_rcb *rcb;
852 	struct bge_rcb_opaque *rcbo;
853 
854 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
855 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
856 			return(ENOBUFS);
857 	};
858 
859 	sc->bge_jumbo = i - 1;
860 
861 	rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
862 	rcbo = (struct bge_rcb_opaque *)rcb;
863 	rcb->bge_flags = 0;
864 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
865 
866 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
867 
868 	return(0);
869 }
870 
871 static void
872 bge_free_rx_ring_jumbo(sc)
873 	struct bge_softc *sc;
874 {
875 	int i;
876 
877 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
878 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
879 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
880 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
881 		}
882 		bzero((char *)&sc->bge_rdata->bge_rx_jumbo_ring[i],
883 		    sizeof(struct bge_rx_bd));
884 	}
885 
886 	return;
887 }
888 
889 static void
890 bge_free_tx_ring(sc)
891 	struct bge_softc *sc;
892 {
893 	int i;
894 
895 	if (sc->bge_rdata->bge_tx_ring == NULL)
896 		return;
897 
898 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
899 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
900 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
901 			sc->bge_cdata.bge_tx_chain[i] = NULL;
902 		}
903 		bzero((char *)&sc->bge_rdata->bge_tx_ring[i],
904 		    sizeof(struct bge_tx_bd));
905 	}
906 
907 	return;
908 }
909 
910 static int
911 bge_init_tx_ring(sc)
912 	struct bge_softc *sc;
913 {
914 	sc->bge_txcnt = 0;
915 	sc->bge_tx_saved_considx = 0;
916 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
917 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
918 
919 	return(0);
920 }
921 
922 #define BGE_POLY	0xEDB88320
923 
924 static u_int32_t
925 bge_crc(addr)
926 	caddr_t addr;
927 {
928 	u_int32_t idx, bit, data, crc;
929 
930 	/* Compute CRC for the address value. */
931 	crc = 0xFFFFFFFF; /* initial value */
932 
933 	for (idx = 0; idx < 6; idx++) {
934 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
935 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? BGE_POLY : 0);
936 	}
937 
938 	return(crc & 0x7F);
939 }
940 
941 static void
942 bge_setmulti(sc)
943 	struct bge_softc *sc;
944 {
945 	struct ifnet *ifp;
946 	struct ifmultiaddr *ifma;
947 	u_int32_t hashes[4] = { 0, 0, 0, 0 };
948 	int h, i;
949 
950 	ifp = &sc->arpcom.ac_if;
951 
952 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
953 		for (i = 0; i < 4; i++)
954 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
955 		return;
956 	}
957 
958 	/* First, zot all the existing filters. */
959 	for (i = 0; i < 4; i++)
960 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
961 
962 	/* Now program new ones. */
963 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
964 		if (ifma->ifma_addr->sa_family != AF_LINK)
965 			continue;
966 		h = bge_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
967 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
968 	}
969 
970 	for (i = 0; i < 4; i++)
971 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
972 
973 	return;
974 }
975 
976 /*
977  * Do endian, PCI and DMA initialization. Also check the on-board ROM
978  * self-test results.
979  */
980 static int
981 bge_chipinit(sc)
982 	struct bge_softc *sc;
983 {
984 	int			i;
985 
986 	/* Set endianness before we access any non-PCI registers. */
987 #if BYTE_ORDER == BIG_ENDIAN
988 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
989 	    BGE_BIGENDIAN_INIT, 4);
990 #else
991 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
992 	    BGE_LITTLEENDIAN_INIT, 4);
993 #endif
994 
995 	/*
996 	 * Check the 'ROM failed' bit on the RX CPU to see if
997 	 * self-tests passed.
998 	 */
999 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1000 		printf("bge%d: RX CPU self-diagnostics failed!\n",
1001 		    sc->bge_unit);
1002 		return(ENODEV);
1003 	}
1004 
1005 	/* Clear the MAC control register */
1006 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1007 
1008 	/*
1009 	 * Clear the MAC statistics block in the NIC's
1010 	 * internal memory.
1011 	 */
1012 	for (i = BGE_STATS_BLOCK;
1013 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
1014 		BGE_MEMWIN_WRITE(sc, i, 0);
1015 
1016 	for (i = BGE_STATUS_BLOCK;
1017 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
1018 		BGE_MEMWIN_WRITE(sc, i, 0);
1019 
1020 	/* Set up the PCI DMA control register. */
1021 	if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
1022 	    BGE_PCISTATE_PCI_BUSMODE) {
1023 		/* Conventional PCI bus */
1024 		pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
1025 		    BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD|0x3F000F, 4);
1026 	} else {
1027 		/* PCI-X bus */
1028 		pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
1029 		    BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD|0x1B000F, 4);
1030 	}
1031 
1032 	/*
1033 	 * Set up general mode register.
1034 	 */
1035 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_WORDSWAP_NONFRAME|
1036 	    BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1037 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1038 	    BGE_MODECTL_NO_RX_CRC|BGE_MODECTL_TX_NO_PHDR_CSUM|
1039 	    BGE_MODECTL_RX_NO_PHDR_CSUM);
1040 
1041 	/*
1042 	 * Disable memory write invalidate.  Apparently it is not supported
1043 	 * properly by these devices.
1044 	 */
1045 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
1046 
1047 #ifdef __brokenalpha__
1048 	/*
1049 	 * Must insure that we do not cross an 8K (bytes) boundary
1050 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
1051 	 * restriction on some ALPHA platforms with early revision
1052 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
1053 	 */
1054 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
1055 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
1056 #endif
1057 
1058 	/* Set the timer prescaler (always 66Mhz) */
1059 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1060 
1061 	return(0);
1062 }
1063 
1064 static int
1065 bge_blockinit(sc)
1066 	struct bge_softc *sc;
1067 {
1068 	struct bge_rcb *rcb;
1069 	struct bge_rcb_opaque *rcbo;
1070 	int i;
1071 
1072 	/*
1073 	 * Initialize the memory window pointer register so that
1074 	 * we can access the first 32K of internal NIC RAM. This will
1075 	 * allow us to set up the TX send ring RCBs and the RX return
1076 	 * ring RCBs, plus other things which live in NIC memory.
1077 	 */
1078 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
1079 
1080 	/* Configure mbuf memory pool */
1081 	if (sc->bge_extram) {
1082 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_EXT_SSRAM);
1083 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1084 	} else {
1085 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1086 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1087 	}
1088 
1089 	/* Configure DMA resource pool */
1090 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR, BGE_DMA_DESCRIPTORS);
1091 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
1092 
1093 	/* Configure mbuf pool watermarks */
1094 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 24);
1095 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 24);
1096 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 48);
1097 
1098 	/* Configure DMA resource watermarks */
1099 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1100 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1101 
1102 	/* Enable buffer manager */
1103 	CSR_WRITE_4(sc, BGE_BMAN_MODE,
1104 	    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
1105 
1106 	/* Poll for buffer manager start indication */
1107 	for (i = 0; i < BGE_TIMEOUT; i++) {
1108 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1109 			break;
1110 		DELAY(10);
1111 	}
1112 
1113 	if (i == BGE_TIMEOUT) {
1114 		printf("bge%d: buffer manager failed to start\n",
1115 		    sc->bge_unit);
1116 		return(ENXIO);
1117 	}
1118 
1119 	/* Enable flow-through queues */
1120 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1121 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1122 
1123 	/* Wait until queue initialization is complete */
1124 	for (i = 0; i < BGE_TIMEOUT; i++) {
1125 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1126 			break;
1127 		DELAY(10);
1128 	}
1129 
1130 	if (i == BGE_TIMEOUT) {
1131 		printf("bge%d: flow-through queue init failed\n",
1132 		    sc->bge_unit);
1133 		return(ENXIO);
1134 	}
1135 
1136 	/* Initialize the standard RX ring control block */
1137 	rcb = &sc->bge_rdata->bge_info.bge_std_rx_rcb;
1138 	BGE_HOSTADDR(rcb->bge_hostaddr) =
1139 	    vtophys(&sc->bge_rdata->bge_rx_std_ring);
1140 	rcb->bge_max_len = BGE_MAX_FRAMELEN;
1141 	if (sc->bge_extram)
1142 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
1143 	else
1144 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1145 	rcb->bge_flags = 0;
1146 	rcbo = (struct bge_rcb_opaque *)rcb;
1147 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcbo->bge_reg0);
1148 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcbo->bge_reg1);
1149 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
1150 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcbo->bge_reg3);
1151 
1152 	/*
1153 	 * Initialize the jumbo RX ring control block
1154 	 * We set the 'ring disabled' bit in the flags
1155 	 * field until we're actually ready to start
1156 	 * using this ring (i.e. once we set the MTU
1157 	 * high enough to require it).
1158 	 */
1159 	rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
1160 	BGE_HOSTADDR(rcb->bge_hostaddr) =
1161 	    vtophys(&sc->bge_rdata->bge_rx_jumbo_ring);
1162 	rcb->bge_max_len = BGE_MAX_FRAMELEN;
1163 	if (sc->bge_extram)
1164 		rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
1165 	else
1166 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1167 	rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED;
1168 
1169 	rcbo = (struct bge_rcb_opaque *)rcb;
1170 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI, rcbo->bge_reg0);
1171 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO, rcbo->bge_reg1);
1172 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
1173 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcbo->bge_reg3);
1174 
1175 	/* Set up dummy disabled mini ring RCB */
1176 	rcb = &sc->bge_rdata->bge_info.bge_mini_rx_rcb;
1177 	rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED;
1178 	rcbo = (struct bge_rcb_opaque *)rcb;
1179 	CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
1180 
1181 	/*
1182 	 * Set the BD ring replentish thresholds. The recommended
1183 	 * values are 1/8th the number of descriptors allocated to
1184 	 * each ring.
1185 	 */
1186 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
1187 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
1188 
1189 	/*
1190 	 * Disable all unused send rings by setting the 'ring disabled'
1191 	 * bit in the flags field of all the TX send ring control blocks.
1192 	 * These are located in NIC memory.
1193 	 */
1194 	rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1195 	    BGE_SEND_RING_RCB);
1196 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1197 		rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED;
1198 		rcb->bge_max_len = 0;
1199 		rcb->bge_nicaddr = 0;
1200 		rcb++;
1201 	}
1202 
1203 	/* Configure TX RCB 0 (we use only the first ring) */
1204 	rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1205 	    BGE_SEND_RING_RCB);
1206 	rcb->bge_hostaddr.bge_addr_hi = 0;
1207 	BGE_HOSTADDR(rcb->bge_hostaddr) =
1208 	    vtophys(&sc->bge_rdata->bge_tx_ring);
1209 	rcb->bge_nicaddr = BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT);
1210 	rcb->bge_max_len = BGE_TX_RING_CNT;
1211 	rcb->bge_flags = 0;
1212 
1213 	/* Disable all unused RX return rings */
1214 	rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1215 	    BGE_RX_RETURN_RING_RCB);
1216 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1217 		rcb->bge_hostaddr.bge_addr_hi = 0;
1218 		rcb->bge_hostaddr.bge_addr_lo = 0;
1219 		rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED;
1220 		rcb->bge_max_len = BGE_RETURN_RING_CNT;
1221 		rcb->bge_nicaddr = 0;
1222 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
1223 		    (i * (sizeof(u_int64_t))), 0);
1224 		rcb++;
1225 	}
1226 
1227 	/* Initialize RX ring indexes */
1228 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1229 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1230 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
1231 
1232 	/*
1233 	 * Set up RX return ring 0
1234 	 * Note that the NIC address for RX return rings is 0x00000000.
1235 	 * The return rings live entirely within the host, so the
1236 	 * nicaddr field in the RCB isn't used.
1237 	 */
1238 	rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1239 	    BGE_RX_RETURN_RING_RCB);
1240 	rcb->bge_hostaddr.bge_addr_hi = 0;
1241 	BGE_HOSTADDR(rcb->bge_hostaddr) =
1242 	    vtophys(&sc->bge_rdata->bge_rx_return_ring);
1243 	rcb->bge_nicaddr = 0x00000000;
1244 	rcb->bge_max_len = BGE_RETURN_RING_CNT;
1245 	rcb->bge_flags = 0;
1246 
1247 	/* Set random backoff seed for TX */
1248 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
1249 	    sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
1250 	    sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
1251 	    sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] +
1252 	    BGE_TX_BACKOFF_SEED_MASK);
1253 
1254 	/* Set inter-packet gap */
1255 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
1256 
1257 	/*
1258 	 * Specify which ring to use for packets that don't match
1259 	 * any RX rules.
1260 	 */
1261 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
1262 
1263 	/*
1264 	 * Configure number of RX lists. One interrupt distribution
1265 	 * list, sixteen active lists, one bad frames class.
1266 	 */
1267 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
1268 
1269 	/* Inialize RX list placement stats mask. */
1270 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
1271 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
1272 
1273 	/* Disable host coalescing until we get it set up */
1274 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
1275 
1276 	/* Poll to make sure it's shut down. */
1277 	for (i = 0; i < BGE_TIMEOUT; i++) {
1278 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
1279 			break;
1280 		DELAY(10);
1281 	}
1282 
1283 	if (i == BGE_TIMEOUT) {
1284 		printf("bge%d: host coalescing engine failed to idle\n",
1285 		    sc->bge_unit);
1286 		return(ENXIO);
1287 	}
1288 
1289 	/* Set up host coalescing defaults */
1290 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
1291 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
1292 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
1293 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
1294 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
1295 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
1296 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
1297 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
1298 	CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
1299 
1300 	/* Set up address of statistics block */
1301 	CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
1302 	CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI, 0);
1303 	CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1304 	    vtophys(&sc->bge_rdata->bge_info.bge_stats));
1305 
1306 	/* Set up address of status block */
1307 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
1308 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI, 0);
1309 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1310 	    vtophys(&sc->bge_rdata->bge_status_block));
1311 	sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx = 0;
1312 	sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx = 0;
1313 
1314 	/* Turn on host coalescing state machine */
1315 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
1316 
1317 	/* Turn on RX BD completion state machine and enable attentions */
1318 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
1319 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
1320 
1321 	/* Turn on RX list placement state machine */
1322 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
1323 
1324 	/* Turn on RX list selector state machine. */
1325 	CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
1326 
1327 	/* Turn on DMA, clear stats */
1328 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
1329 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
1330 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
1331 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
1332 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
1333 
1334 	/* Set misc. local control, enable interrupts on attentions */
1335 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
1336 
1337 #ifdef notdef
1338 	/* Assert GPIO pins for PHY reset */
1339 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
1340 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
1341 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
1342 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
1343 #endif
1344 
1345 	/* Turn on DMA completion state machine */
1346 	CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
1347 
1348 	/* Turn on write DMA state machine */
1349 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
1350 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
1351 
1352 	/* Turn on read DMA state machine */
1353 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
1354 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
1355 
1356 	/* Turn on RX data completion state machine */
1357 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
1358 
1359 	/* Turn on RX BD initiator state machine */
1360 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
1361 
1362 	/* Turn on RX data and RX BD initiator state machine */
1363 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
1364 
1365 	/* Turn on Mbuf cluster free state machine */
1366 	CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
1367 
1368 	/* Turn on send BD completion state machine */
1369 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
1370 
1371 	/* Turn on send data completion state machine */
1372 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
1373 
1374 	/* Turn on send data initiator state machine */
1375 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
1376 
1377 	/* Turn on send BD initiator state machine */
1378 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
1379 
1380 	/* Turn on send BD selector state machine */
1381 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
1382 
1383 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
1384 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
1385 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
1386 
1387 	/* init LED register */
1388 	CSR_WRITE_4(sc, BGE_MAC_LED_CTL, 0x00000000);
1389 
1390 	/* ack/clear link change events */
1391 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1392 	    BGE_MACSTAT_CFG_CHANGED);
1393 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
1394 
1395 	/* Enable PHY auto polling (for MII/GMII only) */
1396 	if (sc->bge_tbi) {
1397 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1398  	} else {
1399 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
1400 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700)
1401 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1402 			    BGE_EVTENB_MI_INTERRUPT);
1403 	}
1404 
1405 	/* Enable link state change attentions. */
1406 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
1407 
1408 	return(0);
1409 }
1410 
1411 /*
1412  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
1413  * against our list and return its name if we find a match. Note
1414  * that since the Broadcom controller contains VPD support, we
1415  * can get the device name string from the controller itself instead
1416  * of the compiled-in string. This is a little slow, but it guarantees
1417  * we'll always announce the right product name.
1418  */
1419 static int
1420 bge_probe(dev)
1421 	device_t dev;
1422 {
1423 	struct bge_type *t;
1424 	struct bge_softc *sc;
1425 
1426 	t = bge_devs;
1427 
1428 	sc = device_get_softc(dev);
1429 	bzero(sc, sizeof(struct bge_softc));
1430 	sc->bge_unit = device_get_unit(dev);
1431 	sc->bge_dev = dev;
1432 
1433 	while(t->bge_name != NULL) {
1434 		if ((pci_get_vendor(dev) == t->bge_vid) &&
1435 		    (pci_get_device(dev) == t->bge_did)) {
1436 #ifdef notdef
1437 			bge_vpd_read(sc);
1438 			device_set_desc(dev, sc->bge_vpd_prodname);
1439 #endif
1440 			device_set_desc(dev, t->bge_name);
1441 			return(0);
1442 		}
1443 		t++;
1444 	}
1445 
1446 	return(ENXIO);
1447 }
1448 
1449 static int
1450 bge_attach(dev)
1451 	device_t dev;
1452 {
1453 	int s;
1454 	u_int32_t command;
1455 	struct ifnet *ifp;
1456 	struct bge_softc *sc;
1457 	u_int32_t hwcfg = 0;
1458 	u_int32_t mac_addr = 0;
1459 	int unit, error = 0, rid;
1460 
1461 	s = splimp();
1462 
1463 	sc = device_get_softc(dev);
1464 	unit = device_get_unit(dev);
1465 	sc->bge_dev = dev;
1466 	sc->bge_unit = unit;
1467 
1468 	/*
1469 	 * Map control/status registers.
1470 	 */
1471 	pci_enable_busmaster(dev);
1472 	pci_enable_io(dev, SYS_RES_MEMORY);
1473 	command = pci_read_config(dev, PCIR_COMMAND, 4);
1474 
1475 	if (!(command & PCIM_CMD_MEMEN)) {
1476 		printf("bge%d: failed to enable memory mapping!\n", unit);
1477 		error = ENXIO;
1478 		goto fail;
1479 	}
1480 
1481 	rid = BGE_PCI_BAR0;
1482 	sc->bge_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
1483 	    0, ~0, 1, RF_ACTIVE|PCI_RF_DENSE);
1484 
1485 	if (sc->bge_res == NULL) {
1486 		printf ("bge%d: couldn't map memory\n", unit);
1487 		error = ENXIO;
1488 		goto fail;
1489 	}
1490 
1491 	sc->bge_btag = rman_get_bustag(sc->bge_res);
1492 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
1493 	sc->bge_vhandle = (vm_offset_t)rman_get_virtual(sc->bge_res);
1494 
1495 	/* Allocate interrupt */
1496 	rid = 0;
1497 
1498 	sc->bge_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1499 	    RF_SHAREABLE | RF_ACTIVE);
1500 
1501 	if (sc->bge_irq == NULL) {
1502 		printf("bge%d: couldn't map interrupt\n", unit);
1503 		error = ENXIO;
1504 		goto fail;
1505 	}
1506 
1507 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET,
1508 	   bge_intr, sc, &sc->bge_intrhand);
1509 
1510 	if (error) {
1511 		bge_release_resources(sc);
1512 		printf("bge%d: couldn't set up irq\n", unit);
1513 		goto fail;
1514 	}
1515 
1516 	sc->bge_unit = unit;
1517 
1518 	/* Try to reset the chip. */
1519 	bge_reset(sc);
1520 
1521 	if (bge_chipinit(sc)) {
1522 		printf("bge%d: chip initialization failed\n", sc->bge_unit);
1523 		bge_release_resources(sc);
1524 		error = ENXIO;
1525 		goto fail;
1526 	}
1527 
1528 	/*
1529 	 * Get station address from the EEPROM.
1530 	 */
1531 	mac_addr = bge_readmem_ind(sc, 0x0c14);
1532 	if ((mac_addr >> 16) == 0x484b) {
1533 		sc->arpcom.ac_enaddr[0] = (u_char)(mac_addr >> 8);
1534 		sc->arpcom.ac_enaddr[1] = (u_char)mac_addr;
1535 		mac_addr = bge_readmem_ind(sc, 0x0c18);
1536 		sc->arpcom.ac_enaddr[2] = (u_char)(mac_addr >> 24);
1537 		sc->arpcom.ac_enaddr[3] = (u_char)(mac_addr >> 16);
1538 		sc->arpcom.ac_enaddr[4] = (u_char)(mac_addr >> 8);
1539 		sc->arpcom.ac_enaddr[5] = (u_char)mac_addr;
1540 	} else if (bge_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
1541 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
1542 		printf("bge%d: failed to read station address\n", unit);
1543 		bge_release_resources(sc);
1544 		error = ENXIO;
1545 		goto fail;
1546 	}
1547 
1548 	/*
1549 	 * A Broadcom chip was detected. Inform the world.
1550 	 */
1551 	printf("bge%d: Ethernet address: %6D\n", unit,
1552 	    sc->arpcom.ac_enaddr, ":");
1553 
1554 	/* Allocate the general information block and ring buffers. */
1555 	sc->bge_rdata = contigmalloc(sizeof(struct bge_ring_data), M_DEVBUF,
1556 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1557 
1558 	if (sc->bge_rdata == NULL) {
1559 		bge_release_resources(sc);
1560 		error = ENXIO;
1561 		printf("bge%d: no memory for list buffers!\n", sc->bge_unit);
1562 		goto fail;
1563 	}
1564 
1565 	bzero(sc->bge_rdata, sizeof(struct bge_ring_data));
1566 
1567 	/* Try to allocate memory for jumbo buffers. */
1568 	if (bge_alloc_jumbo_mem(sc)) {
1569 		printf("bge%d: jumbo buffer allocation "
1570 		    "failed\n", sc->bge_unit);
1571 		bge_release_resources(sc);
1572 		error = ENXIO;
1573 		goto fail;
1574 	}
1575 
1576 	/* Set default tuneable values. */
1577 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
1578 	sc->bge_rx_coal_ticks = 150;
1579 	sc->bge_tx_coal_ticks = 150;
1580 	sc->bge_rx_max_coal_bds = 64;
1581 	sc->bge_tx_max_coal_bds = 128;
1582 
1583 	/* Set up ifnet structure */
1584 	ifp = &sc->arpcom.ac_if;
1585 	ifp->if_softc = sc;
1586 	ifp->if_unit = sc->bge_unit;
1587 	ifp->if_name = "bge";
1588 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1589 	ifp->if_ioctl = bge_ioctl;
1590 	ifp->if_output = ether_output;
1591 	ifp->if_start = bge_start;
1592 	ifp->if_watchdog = bge_watchdog;
1593 	ifp->if_init = bge_init;
1594 	ifp->if_mtu = ETHERMTU;
1595 	ifp->if_snd.ifq_maxlen = BGE_TX_RING_CNT - 1;
1596 	ifp->if_hwassist = BGE_CSUM_FEATURES;
1597 	ifp->if_capabilities = IFCAP_HWCSUM;
1598 	ifp->if_capenable = ifp->if_capabilities;
1599 
1600 	/* Save ASIC rev. */
1601 
1602 	sc->bge_asicrev =
1603 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
1604 	    BGE_PCIMISCCTL_ASICREV;
1605 
1606 	/* Pretend all 5700s are the same */
1607 	if ((sc->bge_asicrev & 0xFF000000) == BGE_ASICREV_BCM5700)
1608 		sc->bge_asicrev = BGE_ASICREV_BCM5700;
1609 
1610 	/*
1611 	 * Figure out what sort of media we have by checking the
1612 	 * hardware config word in the EEPROM. Note: on some BCM5700
1613 	 * cards, this value appears to be unset. If that's the
1614 	 * case, we have to rely on identifying the NIC by its PCI
1615 	 * subsystem ID, as we do below for the SysKonnect SK-9D41.
1616 	 */
1617 	bge_read_eeprom(sc, (caddr_t)&hwcfg,
1618 		    BGE_EE_HWCFG_OFFSET, sizeof(hwcfg));
1619 	if ((ntohl(hwcfg) & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
1620 		sc->bge_tbi = 1;
1621 
1622 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
1623 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
1624 		sc->bge_tbi = 1;
1625 
1626 	if (sc->bge_tbi) {
1627 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
1628 		    bge_ifmedia_upd, bge_ifmedia_sts);
1629 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
1630 		ifmedia_add(&sc->bge_ifmedia,
1631 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
1632 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
1633 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
1634 	} else {
1635 		/*
1636 		 * Do transceiver setup.
1637 		 */
1638 		if (mii_phy_probe(dev, &sc->bge_miibus,
1639 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
1640 			printf("bge%d: MII without any PHY!\n", sc->bge_unit);
1641 			bge_release_resources(sc);
1642 			bge_free_jumbo_mem(sc);
1643 			error = ENXIO;
1644 			goto fail;
1645 		}
1646 	}
1647 
1648 	/*
1649 	 * When using the BCM5701 in PCI-X mode, data corruption has
1650 	 * been observed in the first few bytes of some received packets.
1651 	 * Aligning the packet buffer in memory eliminates the corruption.
1652 	 * Unfortunately, this misaligns the packet payloads.  On platforms
1653 	 * which do not support unaligned accesses, we will realign the
1654 	 * payloads by copying the received packets.
1655 	 */
1656 	switch (sc->bge_asicrev) {
1657 	case BGE_ASICREV_BCM5701_A0:
1658 	case BGE_ASICREV_BCM5701_B0:
1659 	case BGE_ASICREV_BCM5701_B2:
1660 	case BGE_ASICREV_BCM5701_B5:
1661 		/* If in PCI-X mode, work around the alignment bug. */
1662 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
1663 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
1664 		    BGE_PCISTATE_PCI_BUSSPEED)
1665 			sc->bge_rx_alignment_bug = 1;
1666 		break;
1667 	}
1668 
1669 	/*
1670 	 * Call MI attach routine.
1671 	 */
1672 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1673 	callout_handle_init(&sc->bge_stat_ch);
1674 
1675 fail:
1676 	splx(s);
1677 
1678 	return(error);
1679 }
1680 
1681 static int
1682 bge_detach(dev)
1683 	device_t dev;
1684 {
1685 	struct bge_softc *sc;
1686 	struct ifnet *ifp;
1687 	int s;
1688 
1689 	s = splimp();
1690 
1691 	sc = device_get_softc(dev);
1692 	ifp = &sc->arpcom.ac_if;
1693 
1694 	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
1695 	bge_stop(sc);
1696 	bge_reset(sc);
1697 
1698 	if (sc->bge_tbi) {
1699 		ifmedia_removeall(&sc->bge_ifmedia);
1700 	} else {
1701 		bus_generic_detach(dev);
1702 		device_delete_child(dev, sc->bge_miibus);
1703 	}
1704 
1705 	bge_release_resources(sc);
1706 	bge_free_jumbo_mem(sc);
1707 
1708 	splx(s);
1709 
1710 	return(0);
1711 }
1712 
1713 static void
1714 bge_release_resources(sc)
1715 	struct bge_softc *sc;
1716 {
1717         device_t dev;
1718 
1719         dev = sc->bge_dev;
1720 
1721 	if (sc->bge_vpd_prodname != NULL)
1722 		free(sc->bge_vpd_prodname, M_DEVBUF);
1723 
1724 	if (sc->bge_vpd_readonly != NULL)
1725 		free(sc->bge_vpd_readonly, M_DEVBUF);
1726 
1727         if (sc->bge_intrhand != NULL)
1728                 bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
1729 
1730         if (sc->bge_irq != NULL)
1731 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
1732 
1733         if (sc->bge_res != NULL)
1734 		bus_release_resource(dev, SYS_RES_MEMORY,
1735 		    BGE_PCI_BAR0, sc->bge_res);
1736 
1737         if (sc->bge_rdata != NULL)
1738 		contigfree(sc->bge_rdata,
1739 		    sizeof(struct bge_ring_data), M_DEVBUF);
1740 
1741         return;
1742 }
1743 
1744 static void
1745 bge_reset(sc)
1746 	struct bge_softc *sc;
1747 {
1748 	device_t dev;
1749 	u_int32_t cachesize, command, pcistate;
1750 	int i, val = 0;
1751 
1752 	dev = sc->bge_dev;
1753 
1754 	/* Save some important PCI state. */
1755 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
1756 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
1757 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
1758 
1759 	pci_write_config(dev, BGE_PCI_MISC_CTL,
1760 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
1761 	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
1762 
1763 	/* Issue global reset */
1764 	bge_writereg_ind(sc, BGE_MISC_CFG,
1765 	    BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1));
1766 
1767 	DELAY(1000);
1768 
1769 	/* Reset some of the PCI state that got zapped by reset */
1770 	pci_write_config(dev, BGE_PCI_MISC_CTL,
1771 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
1772 	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
1773 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
1774 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
1775 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
1776 
1777 	/*
1778 	 * Prevent PXE restart: write a magic number to the
1779 	 * general communications memory at 0xB50.
1780 	 */
1781 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
1782 	/*
1783 	 * Poll the value location we just wrote until
1784 	 * we see the 1's complement of the magic number.
1785 	 * This indicates that the firmware initialization
1786 	 * is complete.
1787 	 */
1788 	for (i = 0; i < BGE_TIMEOUT; i++) {
1789 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
1790 		if (val == ~BGE_MAGIC_NUMBER)
1791 			break;
1792 		DELAY(10);
1793 	}
1794 
1795 	if (i == BGE_TIMEOUT) {
1796 		printf("bge%d: firmware handshake timed out\n", sc->bge_unit);
1797 		return;
1798 	}
1799 
1800 	/*
1801 	 * XXX Wait for the value of the PCISTATE register to
1802 	 * return to its original pre-reset state. This is a
1803 	 * fairly good indicator of reset completion. If we don't
1804 	 * wait for the reset to fully complete, trying to read
1805 	 * from the device's non-PCI registers may yield garbage
1806 	 * results.
1807 	 */
1808 	for (i = 0; i < BGE_TIMEOUT; i++) {
1809 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
1810 			break;
1811 		DELAY(10);
1812 	}
1813 
1814 	/* Enable memory arbiter. */
1815 	CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
1816 
1817 	/* Fix up byte swapping */
1818 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_BYTESWAP_NONFRAME|
1819 	    BGE_MODECTL_BYTESWAP_DATA);
1820 
1821 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1822 
1823 	DELAY(10000);
1824 
1825 	return;
1826 }
1827 
1828 /*
1829  * Frame reception handling. This is called if there's a frame
1830  * on the receive return list.
1831  *
1832  * Note: we have to be able to handle two possibilities here:
1833  * 1) the frame is from the jumbo recieve ring
1834  * 2) the frame is from the standard receive ring
1835  */
1836 
1837 static void
1838 bge_rxeof(sc)
1839 	struct bge_softc *sc;
1840 {
1841 	struct ifnet *ifp;
1842 	int stdcnt = 0, jumbocnt = 0;
1843 
1844 	ifp = &sc->arpcom.ac_if;
1845 
1846 	while(sc->bge_rx_saved_considx !=
1847 	    sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx) {
1848 		struct bge_rx_bd	*cur_rx;
1849 		u_int32_t		rxidx;
1850 		struct ether_header	*eh;
1851 		struct mbuf		*m = NULL;
1852 		u_int16_t		vlan_tag = 0;
1853 		int			have_tag = 0;
1854 
1855 		cur_rx =
1856 	    &sc->bge_rdata->bge_rx_return_ring[sc->bge_rx_saved_considx];
1857 
1858 		rxidx = cur_rx->bge_idx;
1859 		BGE_INC(sc->bge_rx_saved_considx, BGE_RETURN_RING_CNT);
1860 
1861 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
1862 			have_tag = 1;
1863 			vlan_tag = cur_rx->bge_vlan_tag;
1864 		}
1865 
1866 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
1867 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
1868 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
1869 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
1870 			jumbocnt++;
1871 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
1872 				ifp->if_ierrors++;
1873 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
1874 				continue;
1875 			}
1876 			if (bge_newbuf_jumbo(sc,
1877 			    sc->bge_jumbo, NULL) == ENOBUFS) {
1878 				ifp->if_ierrors++;
1879 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
1880 				continue;
1881 			}
1882 		} else {
1883 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
1884 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
1885 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
1886 			stdcnt++;
1887 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
1888 				ifp->if_ierrors++;
1889 				bge_newbuf_std(sc, sc->bge_std, m);
1890 				continue;
1891 			}
1892 			if (bge_newbuf_std(sc, sc->bge_std,
1893 			    NULL) == ENOBUFS) {
1894 				ifp->if_ierrors++;
1895 				bge_newbuf_std(sc, sc->bge_std, m);
1896 				continue;
1897 			}
1898 		}
1899 
1900 		ifp->if_ipackets++;
1901 #ifndef __i386__
1902 		/*
1903 		 * The i386 allows unaligned accesses, but for other
1904 		 * platforms we must make sure the payload is aligned.
1905 		 */
1906 		if (sc->bge_rx_alignment_bug) {
1907 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
1908 			    cur_rx->bge_len);
1909 			m->m_data += ETHER_ALIGN;
1910 		}
1911 #endif
1912 		eh = mtod(m, struct ether_header *);
1913 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len;
1914 		m->m_pkthdr.rcvif = ifp;
1915 
1916 		/* Remove header from mbuf and pass it on. */
1917 		m_adj(m, sizeof(struct ether_header));
1918 
1919 #if 0 /* currently broken for some packets, possibly related to TCP options */
1920 		if (ifp->if_hwassist) {
1921 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1922 			if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
1923 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1924 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
1925 				m->m_pkthdr.csum_data =
1926 				    cur_rx->bge_tcp_udp_csum;
1927 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1928 			}
1929 		}
1930 #endif
1931 
1932 		/*
1933 		 * If we received a packet with a vlan tag, pass it
1934 		 * to vlan_input() instead of ether_input().
1935 		 */
1936 		if (have_tag) {
1937 			VLAN_INPUT_TAG(eh, m, vlan_tag);
1938 			have_tag = vlan_tag = 0;
1939 			continue;
1940 		}
1941 
1942 		ether_input(ifp, eh, m);
1943 	}
1944 
1945 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
1946 	if (stdcnt)
1947 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
1948 	if (jumbocnt)
1949 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
1950 
1951 	return;
1952 }
1953 
1954 static void
1955 bge_txeof(sc)
1956 	struct bge_softc *sc;
1957 {
1958 	struct bge_tx_bd *cur_tx = NULL;
1959 	struct ifnet *ifp;
1960 
1961 	ifp = &sc->arpcom.ac_if;
1962 
1963 	/*
1964 	 * Go through our tx ring and free mbufs for those
1965 	 * frames that have been sent.
1966 	 */
1967 	while (sc->bge_tx_saved_considx !=
1968 	    sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx) {
1969 		u_int32_t		idx = 0;
1970 
1971 		idx = sc->bge_tx_saved_considx;
1972 		cur_tx = &sc->bge_rdata->bge_tx_ring[idx];
1973 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
1974 			ifp->if_opackets++;
1975 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
1976 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
1977 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
1978 		}
1979 		sc->bge_txcnt--;
1980 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
1981 		ifp->if_timer = 0;
1982 	}
1983 
1984 	if (cur_tx != NULL)
1985 		ifp->if_flags &= ~IFF_OACTIVE;
1986 
1987 	return;
1988 }
1989 
1990 static void
1991 bge_intr(xsc)
1992 	void *xsc;
1993 {
1994 	struct bge_softc *sc;
1995 	struct ifnet *ifp;
1996 
1997 	sc = xsc;
1998 	ifp = &sc->arpcom.ac_if;
1999 
2000 #ifdef notdef
2001 	/* Avoid this for now -- checking this register is expensive. */
2002 	/* Make sure this is really our interrupt. */
2003 	if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
2004 		return;
2005 #endif
2006 	/* Ack interrupt and stop others from occuring. */
2007 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
2008 
2009 	/*
2010 	 * Process link state changes.
2011 	 * Grrr. The link status word in the status block does
2012 	 * not work correctly on the BCM5700 rev AX and BX chips,
2013 	 * according to all avaibable information. Hence, we have
2014 	 * to enable MII interrupts in order to properly obtain
2015 	 * async link changes. Unfortunately, this also means that
2016 	 * we have to read the MAC status register to detect link
2017 	 * changes, thereby adding an additional register access to
2018 	 * the interrupt handler.
2019 	 */
2020 
2021 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700) {
2022 		u_int32_t		status;
2023 
2024 		status = CSR_READ_4(sc, BGE_MAC_STS);
2025 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
2026 			sc->bge_link = 0;
2027 			untimeout(bge_tick, sc, sc->bge_stat_ch);
2028 			bge_tick(sc);
2029 			/* Clear the interrupt */
2030 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2031 			    BGE_EVTENB_MI_INTERRUPT);
2032 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
2033 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
2034 			    BRGPHY_INTRS);
2035 		}
2036 	} else {
2037 		if (sc->bge_rdata->bge_status_block.bge_status &
2038 		    BGE_STATFLAG_LINKSTATE_CHANGED) {
2039 			sc->bge_link = 0;
2040 			untimeout(bge_tick, sc, sc->bge_stat_ch);
2041 			bge_tick(sc);
2042 			/* Clear the interrupt */
2043 			CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
2044 			    BGE_MACSTAT_CFG_CHANGED);
2045 		}
2046 	}
2047 
2048 	if (ifp->if_flags & IFF_RUNNING) {
2049 		/* Check RX return ring producer/consumer */
2050 		bge_rxeof(sc);
2051 
2052 		/* Check TX ring producer/consumer */
2053 		bge_txeof(sc);
2054 	}
2055 
2056 	bge_handle_events(sc);
2057 
2058 	/* Re-enable interrupts. */
2059 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
2060 
2061 	if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
2062 		bge_start(ifp);
2063 
2064 	return;
2065 }
2066 
2067 static void
2068 bge_tick(xsc)
2069 	void *xsc;
2070 {
2071 	struct bge_softc *sc;
2072 	struct mii_data *mii = NULL;
2073 	struct ifmedia *ifm = NULL;
2074 	struct ifnet *ifp;
2075 	int s;
2076 
2077 	sc = xsc;
2078 	ifp = &sc->arpcom.ac_if;
2079 
2080 	s = splimp();
2081 
2082 	bge_stats_update(sc);
2083 	sc->bge_stat_ch = timeout(bge_tick, sc, hz);
2084 	if (sc->bge_link) {
2085 		splx(s);
2086 		return;
2087 	}
2088 
2089 	if (sc->bge_tbi) {
2090 		ifm = &sc->bge_ifmedia;
2091 		if (CSR_READ_4(sc, BGE_MAC_STS) &
2092 		    BGE_MACSTAT_TBI_PCS_SYNCHED) {
2093 			sc->bge_link++;
2094 			CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
2095 			printf("bge%d: gigabit link up\n", sc->bge_unit);
2096 			if (ifp->if_snd.ifq_head != NULL)
2097 				bge_start(ifp);
2098 		}
2099 		splx(s);
2100 		return;
2101 	}
2102 
2103 	mii = device_get_softc(sc->bge_miibus);
2104 	mii_tick(mii);
2105 
2106 	if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE &&
2107 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
2108 		sc->bge_link++;
2109 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
2110 		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
2111 			printf("bge%d: gigabit link up\n",
2112 			   sc->bge_unit);
2113 		if (ifp->if_snd.ifq_head != NULL)
2114 			bge_start(ifp);
2115 	}
2116 
2117 	splx(s);
2118 
2119 	return;
2120 }
2121 
2122 static void
2123 bge_stats_update(sc)
2124 	struct bge_softc *sc;
2125 {
2126 	struct ifnet *ifp;
2127 	struct bge_stats *stats;
2128 
2129 	ifp = &sc->arpcom.ac_if;
2130 
2131 	stats = (struct bge_stats *)(sc->bge_vhandle +
2132 	    BGE_MEMWIN_START + BGE_STATS_BLOCK);
2133 
2134 	ifp->if_collisions +=
2135 	   (stats->dot3StatsSingleCollisionFrames.bge_addr_lo +
2136 	   stats->dot3StatsMultipleCollisionFrames.bge_addr_lo +
2137 	   stats->dot3StatsExcessiveCollisions.bge_addr_lo +
2138 	   stats->dot3StatsLateCollisions.bge_addr_lo) -
2139 	   ifp->if_collisions;
2140 
2141 #ifdef notdef
2142 	ifp->if_collisions +=
2143 	   (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
2144 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
2145 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
2146 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
2147 	   ifp->if_collisions;
2148 #endif
2149 
2150 	return;
2151 }
2152 
2153 /*
2154  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
2155  * pointers to descriptors.
2156  */
2157 static int
2158 bge_encap(sc, m_head, txidx)
2159 	struct bge_softc *sc;
2160 	struct mbuf *m_head;
2161 	u_int32_t *txidx;
2162 {
2163 	struct bge_tx_bd	*f = NULL;
2164 	struct mbuf		*m;
2165 	u_int32_t		frag, cur, cnt = 0;
2166 	u_int16_t		csum_flags = 0;
2167 	struct ifvlan		*ifv = NULL;
2168 
2169 	if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) &&
2170 	    m_head->m_pkthdr.rcvif != NULL &&
2171 	    m_head->m_pkthdr.rcvif->if_type == IFT_L2VLAN)
2172 		ifv = m_head->m_pkthdr.rcvif->if_softc;
2173 
2174 	m = m_head;
2175 	cur = frag = *txidx;
2176 
2177 	if (m_head->m_pkthdr.csum_flags) {
2178 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
2179 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
2180 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
2181 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
2182 		if (m_head->m_flags & M_LASTFRAG)
2183 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
2184 		else if (m_head->m_flags & M_FRAG)
2185 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
2186 	}
2187 
2188 	/*
2189  	 * Start packing the mbufs in this chain into
2190 	 * the fragment pointers. Stop when we run out
2191  	 * of fragments or hit the end of the mbuf chain.
2192 	 */
2193 	for (m = m_head; m != NULL; m = m->m_next) {
2194 		if (m->m_len != 0) {
2195 			f = &sc->bge_rdata->bge_tx_ring[frag];
2196 			if (sc->bge_cdata.bge_tx_chain[frag] != NULL)
2197 				break;
2198 			BGE_HOSTADDR(f->bge_addr) =
2199 			   vtophys(mtod(m, vm_offset_t));
2200 			f->bge_len = m->m_len;
2201 			f->bge_flags = csum_flags;
2202 			if (ifv != NULL) {
2203 				f->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
2204 				f->bge_vlan_tag = ifv->ifv_tag;
2205 			} else {
2206 				f->bge_vlan_tag = 0;
2207 			}
2208 			/*
2209 			 * Sanity check: avoid coming within 16 descriptors
2210 			 * of the end of the ring.
2211 			 */
2212 			if ((BGE_TX_RING_CNT - (sc->bge_txcnt + cnt)) < 16)
2213 				return(ENOBUFS);
2214 			cur = frag;
2215 			BGE_INC(frag, BGE_TX_RING_CNT);
2216 			cnt++;
2217 		}
2218 	}
2219 
2220 	if (m != NULL)
2221 		return(ENOBUFS);
2222 
2223 	if (frag == sc->bge_tx_saved_considx)
2224 		return(ENOBUFS);
2225 
2226 	sc->bge_rdata->bge_tx_ring[cur].bge_flags |= BGE_TXBDFLAG_END;
2227 	sc->bge_cdata.bge_tx_chain[cur] = m_head;
2228 	sc->bge_txcnt += cnt;
2229 
2230 	*txidx = frag;
2231 
2232 	return(0);
2233 }
2234 
2235 /*
2236  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
2237  * to the mbuf data regions directly in the transmit descriptors.
2238  */
2239 static void
2240 bge_start(ifp)
2241 	struct ifnet *ifp;
2242 {
2243 	struct bge_softc *sc;
2244 	struct mbuf *m_head = NULL;
2245 	u_int32_t prodidx = 0;
2246 
2247 	sc = ifp->if_softc;
2248 
2249 	if (!sc->bge_link && ifp->if_snd.ifq_len < 10)
2250 		return;
2251 
2252 	prodidx = CSR_READ_4(sc, BGE_MBX_TX_HOST_PROD0_LO);
2253 
2254 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
2255 		IF_DEQUEUE(&ifp->if_snd, m_head);
2256 		if (m_head == NULL)
2257 			break;
2258 
2259 		/*
2260 		 * XXX
2261 		 * safety overkill.  If this is a fragmented packet chain
2262 		 * with delayed TCP/UDP checksums, then only encapsulate
2263 		 * it if we have enough descriptors to handle the entire
2264 		 * chain at once.
2265 		 * (paranoia -- may not actually be needed)
2266 		 */
2267 		if (m_head->m_flags & M_FIRSTFRAG &&
2268 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
2269 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
2270 			    m_head->m_pkthdr.csum_data + 16) {
2271 				IF_PREPEND(&ifp->if_snd, m_head);
2272 				ifp->if_flags |= IFF_OACTIVE;
2273 				break;
2274 			}
2275 		}
2276 
2277 		/*
2278 		 * Pack the data into the transmit ring. If we
2279 		 * don't have room, set the OACTIVE flag and wait
2280 		 * for the NIC to drain the ring.
2281 		 */
2282 		if (bge_encap(sc, m_head, &prodidx)) {
2283 			IF_PREPEND(&ifp->if_snd, m_head);
2284 			ifp->if_flags |= IFF_OACTIVE;
2285 			break;
2286 		}
2287 
2288 		/*
2289 		 * If there's a BPF listener, bounce a copy of this frame
2290 		 * to him.
2291 		 */
2292 		if (ifp->if_bpf)
2293 			bpf_mtap(ifp, m_head);
2294 	}
2295 
2296 	/* Transmit */
2297 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
2298 
2299 	/*
2300 	 * Set a timeout in case the chip goes out to lunch.
2301 	 */
2302 	ifp->if_timer = 5;
2303 
2304 	return;
2305 }
2306 
2307 /*
2308  * If we have a BCM5400 or BCM5401 PHY, we need to properly
2309  * program its internal DSP. Failing to do this can result in
2310  * massive packet loss at 1Gb speeds.
2311  */
2312 static void
2313 bge_phy_hack(sc)
2314 	struct bge_softc *sc;
2315 {
2316 	struct bge_bcom_hack bhack[] = {
2317 	{ BRGPHY_MII_AUXCTL, 0x4C20 },
2318 	{ BRGPHY_MII_DSP_ADDR_REG, 0x0012 },
2319 	{ BRGPHY_MII_DSP_RW_PORT, 0x1804 },
2320 	{ BRGPHY_MII_DSP_ADDR_REG, 0x0013 },
2321 	{ BRGPHY_MII_DSP_RW_PORT, 0x1204 },
2322 	{ BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
2323 	{ BRGPHY_MII_DSP_RW_PORT, 0x0132 },
2324 	{ BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
2325 	{ BRGPHY_MII_DSP_RW_PORT, 0x0232 },
2326 	{ BRGPHY_MII_DSP_ADDR_REG, 0x201F },
2327 	{ BRGPHY_MII_DSP_RW_PORT, 0x0A20 },
2328 	{ 0, 0 } };
2329 	u_int16_t vid, did;
2330 	int i;
2331 
2332 	vid = bge_miibus_readreg(sc->bge_dev, 1, MII_PHYIDR1);
2333 	did = bge_miibus_readreg(sc->bge_dev, 1, MII_PHYIDR2);
2334 
2335 	if (MII_OUI(vid, did) == MII_OUI_xxBROADCOM &&
2336 	    (MII_MODEL(did) == MII_MODEL_xxBROADCOM_BCM5400 ||
2337 	    MII_MODEL(did) == MII_MODEL_xxBROADCOM_BCM5401)) {
2338 		i = 0;
2339 		while(bhack[i].reg) {
2340 			bge_miibus_writereg(sc->bge_dev, 1, bhack[i].reg,
2341 			    bhack[i].val);
2342 			i++;
2343 		}
2344 	}
2345 
2346 	return;
2347 }
2348 
2349 static void
2350 bge_init(xsc)
2351 	void *xsc;
2352 {
2353 	struct bge_softc *sc = xsc;
2354 	struct ifnet *ifp;
2355 	u_int16_t *m;
2356         int s;
2357 
2358 	s = splimp();
2359 
2360 	ifp = &sc->arpcom.ac_if;
2361 
2362 	if (ifp->if_flags & IFF_RUNNING) {
2363 		splx(s);
2364 		return;
2365 	}
2366 
2367 	/* Cancel pending I/O and flush buffers. */
2368 	bge_stop(sc);
2369 	bge_reset(sc);
2370 	bge_chipinit(sc);
2371 
2372 	/*
2373 	 * Init the various state machines, ring
2374 	 * control blocks and firmware.
2375 	 */
2376 	if (bge_blockinit(sc)) {
2377 		printf("bge%d: initialization failure\n", sc->bge_unit);
2378 		splx(s);
2379 		return;
2380 	}
2381 
2382 	ifp = &sc->arpcom.ac_if;
2383 
2384 	/* Specify MTU. */
2385 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
2386 	    ETHER_HDR_LEN + ETHER_CRC_LEN);
2387 
2388 	/* Load our MAC address. */
2389 	m = (u_int16_t *)&sc->arpcom.ac_enaddr[0];
2390 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
2391 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
2392 
2393 	/* Enable or disable promiscuous mode as needed. */
2394 	if (ifp->if_flags & IFF_PROMISC) {
2395 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
2396 	} else {
2397 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
2398 	}
2399 
2400 	/* Program multicast filter. */
2401 	bge_setmulti(sc);
2402 
2403 	/* Init RX ring. */
2404 	bge_init_rx_ring_std(sc);
2405 
2406 	/* Init jumbo RX ring. */
2407 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2408 		bge_init_rx_ring_jumbo(sc);
2409 
2410 	/* Init our RX return ring index */
2411 	sc->bge_rx_saved_considx = 0;
2412 
2413 	/* Init TX ring. */
2414 	bge_init_tx_ring(sc);
2415 
2416 	/* Turn on transmitter */
2417 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
2418 
2419 	/* Turn on receiver */
2420 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
2421 
2422 	/* Tell firmware we're alive. */
2423 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
2424 
2425 	/* Enable host interrupts. */
2426 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
2427 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
2428 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
2429 
2430 	bge_ifmedia_upd(ifp);
2431 
2432 	ifp->if_flags |= IFF_RUNNING;
2433 	ifp->if_flags &= ~IFF_OACTIVE;
2434 
2435 	splx(s);
2436 
2437 	sc->bge_stat_ch = timeout(bge_tick, sc, hz);
2438 
2439 	return;
2440 }
2441 
2442 /*
2443  * Set media options.
2444  */
2445 static int
2446 bge_ifmedia_upd(ifp)
2447 	struct ifnet *ifp;
2448 {
2449 	struct bge_softc *sc;
2450 	struct mii_data *mii;
2451 	struct ifmedia *ifm;
2452 
2453 	sc = ifp->if_softc;
2454 	ifm = &sc->bge_ifmedia;
2455 
2456 	/* If this is a 1000baseX NIC, enable the TBI port. */
2457 	if (sc->bge_tbi) {
2458 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2459 			return(EINVAL);
2460 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
2461 		case IFM_AUTO:
2462 			break;
2463 		case IFM_1000_SX:
2464 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
2465 				BGE_CLRBIT(sc, BGE_MAC_MODE,
2466 				    BGE_MACMODE_HALF_DUPLEX);
2467 			} else {
2468 				BGE_SETBIT(sc, BGE_MAC_MODE,
2469 				    BGE_MACMODE_HALF_DUPLEX);
2470 			}
2471 			break;
2472 		default:
2473 			return(EINVAL);
2474 		}
2475 		return(0);
2476 	}
2477 
2478 	mii = device_get_softc(sc->bge_miibus);
2479 	sc->bge_link = 0;
2480 	if (mii->mii_instance) {
2481 		struct mii_softc *miisc;
2482 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
2483 		    miisc = LIST_NEXT(miisc, mii_list))
2484 			mii_phy_reset(miisc);
2485 	}
2486 	bge_phy_hack(sc);
2487 	mii_mediachg(mii);
2488 
2489 	return(0);
2490 }
2491 
2492 /*
2493  * Report current media status.
2494  */
2495 static void
2496 bge_ifmedia_sts(ifp, ifmr)
2497 	struct ifnet *ifp;
2498 	struct ifmediareq *ifmr;
2499 {
2500 	struct bge_softc *sc;
2501 	struct mii_data *mii;
2502 
2503 	sc = ifp->if_softc;
2504 
2505 	if (sc->bge_tbi) {
2506 		ifmr->ifm_status = IFM_AVALID;
2507 		ifmr->ifm_active = IFM_ETHER;
2508 		if (CSR_READ_4(sc, BGE_MAC_STS) &
2509 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
2510 			ifmr->ifm_status |= IFM_ACTIVE;
2511 		ifmr->ifm_active |= IFM_1000_SX;
2512 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
2513 			ifmr->ifm_active |= IFM_HDX;
2514 		else
2515 			ifmr->ifm_active |= IFM_FDX;
2516 		return;
2517 	}
2518 
2519 	mii = device_get_softc(sc->bge_miibus);
2520 	mii_pollstat(mii);
2521 	ifmr->ifm_active = mii->mii_media_active;
2522 	ifmr->ifm_status = mii->mii_media_status;
2523 
2524 	return;
2525 }
2526 
2527 static int
2528 bge_ioctl(ifp, command, data)
2529 	struct ifnet *ifp;
2530 	u_long command;
2531 	caddr_t data;
2532 {
2533 	struct bge_softc *sc = ifp->if_softc;
2534 	struct ifreq *ifr = (struct ifreq *) data;
2535 	int s, mask, error = 0;
2536 	struct mii_data *mii;
2537 
2538 	s = splimp();
2539 
2540 	switch(command) {
2541 	case SIOCSIFADDR:
2542 	case SIOCGIFADDR:
2543 		error = ether_ioctl(ifp, command, data);
2544 		break;
2545 	case SIOCSIFMTU:
2546 		if (ifr->ifr_mtu > BGE_JUMBO_MTU)
2547 			error = EINVAL;
2548 		else {
2549 			ifp->if_mtu = ifr->ifr_mtu;
2550 			ifp->if_flags &= ~IFF_RUNNING;
2551 			bge_init(sc);
2552 		}
2553 		break;
2554 	case SIOCSIFFLAGS:
2555 		if (ifp->if_flags & IFF_UP) {
2556 			/*
2557 			 * If only the state of the PROMISC flag changed,
2558 			 * then just use the 'set promisc mode' command
2559 			 * instead of reinitializing the entire NIC. Doing
2560 			 * a full re-init means reloading the firmware and
2561 			 * waiting for it to start up, which may take a
2562 			 * second or two.
2563 			 */
2564 			if (ifp->if_flags & IFF_RUNNING &&
2565 			    ifp->if_flags & IFF_PROMISC &&
2566 			    !(sc->bge_if_flags & IFF_PROMISC)) {
2567 				BGE_SETBIT(sc, BGE_RX_MODE,
2568 				    BGE_RXMODE_RX_PROMISC);
2569 			} else if (ifp->if_flags & IFF_RUNNING &&
2570 			    !(ifp->if_flags & IFF_PROMISC) &&
2571 			    sc->bge_if_flags & IFF_PROMISC) {
2572 				BGE_CLRBIT(sc, BGE_RX_MODE,
2573 				    BGE_RXMODE_RX_PROMISC);
2574 			} else
2575 				bge_init(sc);
2576 		} else {
2577 			if (ifp->if_flags & IFF_RUNNING) {
2578 				bge_stop(sc);
2579 			}
2580 		}
2581 		sc->bge_if_flags = ifp->if_flags;
2582 		error = 0;
2583 		break;
2584 	case SIOCADDMULTI:
2585 	case SIOCDELMULTI:
2586 		if (ifp->if_flags & IFF_RUNNING) {
2587 			bge_setmulti(sc);
2588 			error = 0;
2589 		}
2590 		break;
2591 	case SIOCSIFMEDIA:
2592 	case SIOCGIFMEDIA:
2593 		if (sc->bge_tbi) {
2594 			error = ifmedia_ioctl(ifp, ifr,
2595 			    &sc->bge_ifmedia, command);
2596 		} else {
2597 			mii = device_get_softc(sc->bge_miibus);
2598 			error = ifmedia_ioctl(ifp, ifr,
2599 			    &mii->mii_media, command);
2600 		}
2601 		break;
2602         case SIOCSIFCAP:
2603 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2604 		if (mask & IFCAP_HWCSUM) {
2605 			if (IFCAP_HWCSUM & ifp->if_capenable)
2606 				ifp->if_capenable &= ~IFCAP_HWCSUM;
2607 			else
2608 				ifp->if_capenable |= IFCAP_HWCSUM;
2609 		}
2610 		error = 0;
2611 		break;
2612 	default:
2613 		error = EINVAL;
2614 		break;
2615 	}
2616 
2617 	(void)splx(s);
2618 
2619 	return(error);
2620 }
2621 
2622 static void
2623 bge_watchdog(ifp)
2624 	struct ifnet *ifp;
2625 {
2626 	struct bge_softc *sc;
2627 
2628 	sc = ifp->if_softc;
2629 
2630 	printf("bge%d: watchdog timeout -- resetting\n", sc->bge_unit);
2631 
2632 	ifp->if_flags &= ~IFF_RUNNING;
2633 	bge_init(sc);
2634 
2635 	ifp->if_oerrors++;
2636 
2637 	return;
2638 }
2639 
2640 /*
2641  * Stop the adapter and free any mbufs allocated to the
2642  * RX and TX lists.
2643  */
2644 static void
2645 bge_stop(sc)
2646 	struct bge_softc *sc;
2647 {
2648 	struct ifnet *ifp;
2649 	struct ifmedia_entry *ifm;
2650 	struct mii_data *mii = NULL;
2651 	int mtmp, itmp;
2652 
2653 	ifp = &sc->arpcom.ac_if;
2654 
2655 	if (!sc->bge_tbi)
2656 		mii = device_get_softc(sc->bge_miibus);
2657 
2658 	untimeout(bge_tick, sc, sc->bge_stat_ch);
2659 
2660 	/*
2661 	 * Disable all of the receiver blocks
2662 	 */
2663 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
2664 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
2665 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
2666 	BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
2667 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
2668 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
2669 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
2670 
2671 	/*
2672 	 * Disable all of the transmit blocks
2673 	 */
2674 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
2675 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
2676 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
2677 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
2678 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
2679 	BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
2680 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
2681 
2682 	/*
2683 	 * Shut down all of the memory managers and related
2684 	 * state machines.
2685 	 */
2686 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
2687 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
2688 	BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
2689 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
2690 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
2691 	BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
2692 	BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2693 
2694 	/* Disable host interrupts. */
2695 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
2696 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
2697 
2698 	/*
2699 	 * Tell firmware we're shutting down.
2700 	 */
2701 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
2702 
2703 	/* Free the RX lists. */
2704 	bge_free_rx_ring_std(sc);
2705 
2706 	/* Free jumbo RX list. */
2707 	bge_free_rx_ring_jumbo(sc);
2708 
2709 	/* Free TX buffers. */
2710 	bge_free_tx_ring(sc);
2711 
2712 	/*
2713 	 * Isolate/power down the PHY, but leave the media selection
2714 	 * unchanged so that things will be put back to normal when
2715 	 * we bring the interface back up.
2716 	 */
2717 	if (!sc->bge_tbi) {
2718 		itmp = ifp->if_flags;
2719 		ifp->if_flags |= IFF_UP;
2720 		ifm = mii->mii_media.ifm_cur;
2721 		mtmp = ifm->ifm_media;
2722 		ifm->ifm_media = IFM_ETHER|IFM_NONE;
2723 		mii_mediachg(mii);
2724 		ifm->ifm_media = mtmp;
2725 		ifp->if_flags = itmp;
2726 	}
2727 
2728 	sc->bge_link = 0;
2729 
2730 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
2731 
2732 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2733 
2734 	return;
2735 }
2736 
2737 /*
2738  * Stop all chip I/O so that the kernel's probe routines don't
2739  * get confused by errant DMAs when rebooting.
2740  */
2741 static void
2742 bge_shutdown(dev)
2743 	device_t dev;
2744 {
2745 	struct bge_softc *sc;
2746 
2747 	sc = device_get_softc(dev);
2748 
2749 	bge_stop(sc);
2750 	bge_reset(sc);
2751 
2752 	return;
2753 }
2754