xref: /freebsd/sys/dev/bge/if_bge.c (revision ea13bdd522627899418f044835c1ffdbf4c31c3b)
195d67482SBill Paul /*
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  *
3395d67482SBill Paul  * $FreeBSD$
3495d67482SBill Paul  */
3595d67482SBill Paul 
3695d67482SBill Paul /*
3795d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3895d67482SBill Paul  *
3995d67482SBill Paul  * Written by Bill Paul <wpaul@windriver.com>
4095d67482SBill Paul  * Senior Engineer, Wind River Systems
4195d67482SBill Paul  */
4295d67482SBill Paul 
4395d67482SBill Paul /*
4495d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4595d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4695d67482SBill Paul  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
4795d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4895d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4995d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
5095d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
5195d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
5295d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
5395d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5495d67482SBill Paul  * into the driver.
5595d67482SBill Paul  *
5695d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5795d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5895d67482SBill Paul  *
5995d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
6098b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
6195d67482SBill Paul  * does not support external SSRAM.
6295d67482SBill Paul  *
6395d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6495d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6595d67482SBill Paul  *
6695d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6795d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6895d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6995d67482SBill Paul  * result, this driver does not implement any support for the mini RX
7095d67482SBill Paul  * ring.
7195d67482SBill Paul  */
7295d67482SBill Paul 
7395d67482SBill Paul #include <sys/param.h>
7495d67482SBill Paul #include <sys/systm.h>
7595d67482SBill Paul #include <sys/sockio.h>
7695d67482SBill Paul #include <sys/mbuf.h>
7795d67482SBill Paul #include <sys/malloc.h>
7895d67482SBill Paul #include <sys/kernel.h>
7995d67482SBill Paul #include <sys/socket.h>
8095d67482SBill Paul #include <sys/queue.h>
8195d67482SBill Paul 
8295d67482SBill Paul #include <net/if.h>
8395d67482SBill Paul #include <net/if_arp.h>
8495d67482SBill Paul #include <net/ethernet.h>
8595d67482SBill Paul #include <net/if_dl.h>
8695d67482SBill Paul #include <net/if_media.h>
8795d67482SBill Paul 
8895d67482SBill Paul #include <net/bpf.h>
8995d67482SBill Paul 
9095d67482SBill Paul #include <net/if_types.h>
9195d67482SBill Paul #include <net/if_vlan_var.h>
9295d67482SBill Paul 
9395d67482SBill Paul #include <netinet/in_systm.h>
9495d67482SBill Paul #include <netinet/in.h>
9595d67482SBill Paul #include <netinet/ip.h>
9695d67482SBill Paul 
9795d67482SBill Paul #include <vm/vm.h>              /* for vtophys */
9895d67482SBill Paul #include <vm/pmap.h>            /* for vtophys */
9995d67482SBill Paul #include <machine/clock.h>      /* for DELAY */
10095d67482SBill Paul #include <machine/bus_memio.h>
10195d67482SBill Paul #include <machine/bus.h>
10295d67482SBill Paul #include <machine/resource.h>
10395d67482SBill Paul #include <sys/bus.h>
10495d67482SBill Paul #include <sys/rman.h>
10595d67482SBill Paul 
10695d67482SBill Paul #include <dev/mii/mii.h>
10795d67482SBill Paul #include <dev/mii/miivar.h>
10895d67482SBill Paul #include <dev/mii/miidevs.h>
10995d67482SBill Paul #include <dev/mii/brgphyreg.h>
11095d67482SBill Paul 
11195d67482SBill Paul #include <pci/pcireg.h>
11295d67482SBill Paul #include <pci/pcivar.h>
11395d67482SBill Paul 
11495d67482SBill Paul #include <dev/bge/if_bgereg.h>
11595d67482SBill Paul 
1165ddc5794SJohn Polstra #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
11795d67482SBill Paul 
11895d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
11995d67482SBill Paul 
12095d67482SBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
12195d67482SBill Paul #include "miibus_if.h"
12295d67482SBill Paul 
12395d67482SBill Paul #if !defined(lint)
12495d67482SBill Paul static const char rcsid[] =
12595d67482SBill Paul   "$FreeBSD$";
12695d67482SBill Paul #endif
12795d67482SBill Paul 
12895d67482SBill Paul /*
12995d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13095d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13195d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13295d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13395d67482SBill Paul  */
13495d67482SBill Paul 
13595d67482SBill Paul static struct bge_type bge_devs[] = {
13695d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5700,
13795d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
13895d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5701,
13995d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
14095d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5700,
14195d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
14295d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5701,
14395d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
144b1265c1aSJohn Polstra 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703X,
145b1265c1aSJohn Polstra 		"Broadcom BCM5703X Gigabit Ethernet" },
14695d67482SBill Paul 	{ SK_VENDORID, SK_DEVICEID_ALTIMA,
14795d67482SBill Paul 		"SysKonnect Gigabit Ethernet" },
148586d7c2eSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000,
149586d7c2eSJohn Polstra 		"Altima AC1000 Gigabit Ethernet" },
150470bd96aSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100,
151470bd96aSJohn Polstra 		"Altima AC9100 Gigabit Ethernet" },
15295d67482SBill Paul 	{ 0, 0, NULL }
15395d67482SBill Paul };
15495d67482SBill Paul 
155e51a25f8SAlfred Perlstein static int bge_probe		(device_t);
156e51a25f8SAlfred Perlstein static int bge_attach		(device_t);
157e51a25f8SAlfred Perlstein static int bge_detach		(device_t);
15895d67482SBill Paul static void bge_release_resources
159e51a25f8SAlfred Perlstein 				(struct bge_softc *);
160e51a25f8SAlfred Perlstein static void bge_txeof		(struct bge_softc *);
161e51a25f8SAlfred Perlstein static void bge_rxeof		(struct bge_softc *);
16295d67482SBill Paul 
163e51a25f8SAlfred Perlstein static void bge_tick		(void *);
164e51a25f8SAlfred Perlstein static void bge_stats_update	(struct bge_softc *);
165e51a25f8SAlfred Perlstein static int bge_encap		(struct bge_softc *, struct mbuf *,
166e51a25f8SAlfred Perlstein 					u_int32_t *);
16795d67482SBill Paul 
168e51a25f8SAlfred Perlstein static void bge_intr		(void *);
169e51a25f8SAlfred Perlstein static void bge_start		(struct ifnet *);
170e51a25f8SAlfred Perlstein static int bge_ioctl		(struct ifnet *, u_long, caddr_t);
171e51a25f8SAlfred Perlstein static void bge_init		(void *);
172e51a25f8SAlfred Perlstein static void bge_stop		(struct bge_softc *);
173e51a25f8SAlfred Perlstein static void bge_watchdog		(struct ifnet *);
174e51a25f8SAlfred Perlstein static void bge_shutdown		(device_t);
175e51a25f8SAlfred Perlstein static int bge_ifmedia_upd	(struct ifnet *);
176e51a25f8SAlfred Perlstein static void bge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
17795d67482SBill Paul 
178e51a25f8SAlfred Perlstein static u_int8_t	bge_eeprom_getbyte	(struct bge_softc *, int, u_int8_t *);
179e51a25f8SAlfred Perlstein static int bge_read_eeprom	(struct bge_softc *, caddr_t, int, int);
18095d67482SBill Paul 
181e51a25f8SAlfred Perlstein static u_int32_t bge_crc	(caddr_t);
182e51a25f8SAlfred Perlstein static void bge_setmulti	(struct bge_softc *);
18395d67482SBill Paul 
184e51a25f8SAlfred Perlstein static void bge_handle_events	(struct bge_softc *);
185e51a25f8SAlfred Perlstein static int bge_alloc_jumbo_mem	(struct bge_softc *);
186e51a25f8SAlfred Perlstein static void bge_free_jumbo_mem	(struct bge_softc *);
187e51a25f8SAlfred Perlstein static void *bge_jalloc		(struct bge_softc *);
188914596abSAlfred Perlstein static void bge_jfree		(void *, void *);
189e51a25f8SAlfred Perlstein static int bge_newbuf_std	(struct bge_softc *, int, struct mbuf *);
190e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo	(struct bge_softc *, int, struct mbuf *);
191e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std	(struct bge_softc *);
192e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std	(struct bge_softc *);
193e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo	(struct bge_softc *);
194e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo	(struct bge_softc *);
195e51a25f8SAlfred Perlstein static void bge_free_tx_ring	(struct bge_softc *);
196e51a25f8SAlfred Perlstein static int bge_init_tx_ring	(struct bge_softc *);
19795d67482SBill Paul 
198e51a25f8SAlfred Perlstein static int bge_chipinit		(struct bge_softc *);
199e51a25f8SAlfred Perlstein static int bge_blockinit	(struct bge_softc *);
20095d67482SBill Paul 
2011b4a3b2fSPeter Wemm #ifdef notdef
202e51a25f8SAlfred Perlstein static u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
203e51a25f8SAlfred Perlstein static void bge_vpd_read_res	(struct bge_softc *, struct vpd_res *, int);
204e51a25f8SAlfred Perlstein static void bge_vpd_read	(struct bge_softc *);
2051b4a3b2fSPeter Wemm #endif
20695d67482SBill Paul 
20795d67482SBill Paul static u_int32_t bge_readmem_ind
208e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
209e51a25f8SAlfred Perlstein static void bge_writemem_ind	(struct bge_softc *, int, int);
21095d67482SBill Paul #ifdef notdef
21195d67482SBill Paul static u_int32_t bge_readreg_ind
212e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
21395d67482SBill Paul #endif
214e51a25f8SAlfred Perlstein static void bge_writereg_ind	(struct bge_softc *, int, int);
21595d67482SBill Paul 
216e51a25f8SAlfred Perlstein static int bge_miibus_readreg	(device_t, int, int);
217e51a25f8SAlfred Perlstein static int bge_miibus_writereg	(device_t, int, int, int);
218e51a25f8SAlfred Perlstein static void bge_miibus_statchg	(device_t);
21995d67482SBill Paul 
220e51a25f8SAlfred Perlstein static void bge_reset		(struct bge_softc *);
221e51a25f8SAlfred Perlstein static void bge_phy_hack	(struct bge_softc *);
22295d67482SBill Paul 
22395d67482SBill Paul static device_method_t bge_methods[] = {
22495d67482SBill Paul 	/* Device interface */
22595d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
22695d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
22795d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
22895d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
22995d67482SBill Paul 
23095d67482SBill Paul 	/* bus interface */
23195d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
23295d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
23395d67482SBill Paul 
23495d67482SBill Paul 	/* MII interface */
23595d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
23695d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
23795d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
23895d67482SBill Paul 
23995d67482SBill Paul 	{ 0, 0 }
24095d67482SBill Paul };
24195d67482SBill Paul 
24295d67482SBill Paul static driver_t bge_driver = {
24395d67482SBill Paul 	"bge",
24495d67482SBill Paul 	bge_methods,
24595d67482SBill Paul 	sizeof(struct bge_softc)
24695d67482SBill Paul };
24795d67482SBill Paul 
24895d67482SBill Paul static devclass_t bge_devclass;
24995d67482SBill Paul 
25095d67482SBill Paul DRIVER_MODULE(if_bge, pci, bge_driver, bge_devclass, 0, 0);
25195d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
25295d67482SBill Paul 
25395d67482SBill Paul static u_int32_t
25495d67482SBill Paul bge_readmem_ind(sc, off)
25595d67482SBill Paul 	struct bge_softc *sc;
25695d67482SBill Paul 	int off;
25795d67482SBill Paul {
25895d67482SBill Paul 	device_t dev;
25995d67482SBill Paul 
26095d67482SBill Paul 	dev = sc->bge_dev;
26195d67482SBill Paul 
26295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
26395d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
26495d67482SBill Paul }
26595d67482SBill Paul 
26695d67482SBill Paul static void
26795d67482SBill Paul bge_writemem_ind(sc, off, val)
26895d67482SBill Paul 	struct bge_softc *sc;
26995d67482SBill Paul 	int off, val;
27095d67482SBill Paul {
27195d67482SBill Paul 	device_t dev;
27295d67482SBill Paul 
27395d67482SBill Paul 	dev = sc->bge_dev;
27495d67482SBill Paul 
27595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
27695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
27795d67482SBill Paul 
27895d67482SBill Paul 	return;
27995d67482SBill Paul }
28095d67482SBill Paul 
28195d67482SBill Paul #ifdef notdef
28295d67482SBill Paul static u_int32_t
28395d67482SBill Paul bge_readreg_ind(sc, off)
28495d67482SBill Paul 	struct bge_softc *sc;
28595d67482SBill Paul 	int off;
28695d67482SBill Paul {
28795d67482SBill Paul 	device_t dev;
28895d67482SBill Paul 
28995d67482SBill Paul 	dev = sc->bge_dev;
29095d67482SBill Paul 
29195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
29295d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
29395d67482SBill Paul }
29495d67482SBill Paul #endif
29595d67482SBill Paul 
29695d67482SBill Paul static void
29795d67482SBill Paul bge_writereg_ind(sc, off, val)
29895d67482SBill Paul 	struct bge_softc *sc;
29995d67482SBill Paul 	int off, val;
30095d67482SBill Paul {
30195d67482SBill Paul 	device_t dev;
30295d67482SBill Paul 
30395d67482SBill Paul 	dev = sc->bge_dev;
30495d67482SBill Paul 
30595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
30695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
30795d67482SBill Paul 
30895d67482SBill Paul 	return;
30995d67482SBill Paul }
31095d67482SBill Paul 
3111b4a3b2fSPeter Wemm #ifdef notdef
31295d67482SBill Paul static u_int8_t
31395d67482SBill Paul bge_vpd_readbyte(sc, addr)
31495d67482SBill Paul 	struct bge_softc *sc;
31595d67482SBill Paul 	int addr;
31695d67482SBill Paul {
31795d67482SBill Paul 	int i;
31895d67482SBill Paul 	device_t dev;
31995d67482SBill Paul 	u_int32_t val;
32095d67482SBill Paul 
32195d67482SBill Paul 	dev = sc->bge_dev;
32295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
32395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
32495d67482SBill Paul 		DELAY(10);
32595d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
32695d67482SBill Paul 			break;
32795d67482SBill Paul 	}
32895d67482SBill Paul 
32995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
33095d67482SBill Paul 		printf("bge%d: VPD read timed out\n", sc->bge_unit);
33195d67482SBill Paul 		return(0);
33295d67482SBill Paul 	}
33395d67482SBill Paul 
33495d67482SBill Paul 	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
33595d67482SBill Paul 
33695d67482SBill Paul 	return((val >> ((addr % 4) * 8)) & 0xFF);
33795d67482SBill Paul }
33895d67482SBill Paul 
33995d67482SBill Paul static void
34095d67482SBill Paul bge_vpd_read_res(sc, res, addr)
34195d67482SBill Paul 	struct bge_softc *sc;
34295d67482SBill Paul 	struct vpd_res *res;
34395d67482SBill Paul 	int addr;
34495d67482SBill Paul {
34595d67482SBill Paul 	int i;
34695d67482SBill Paul 	u_int8_t *ptr;
34795d67482SBill Paul 
34895d67482SBill Paul 	ptr = (u_int8_t *)res;
34995d67482SBill Paul 	for (i = 0; i < sizeof(struct vpd_res); i++)
35095d67482SBill Paul 		ptr[i] = bge_vpd_readbyte(sc, i + addr);
35195d67482SBill Paul 
35295d67482SBill Paul 	return;
35395d67482SBill Paul }
35495d67482SBill Paul 
35595d67482SBill Paul static void
35695d67482SBill Paul bge_vpd_read(sc)
35795d67482SBill Paul 	struct bge_softc *sc;
35895d67482SBill Paul {
35995d67482SBill Paul 	int pos = 0, i;
36095d67482SBill Paul 	struct vpd_res res;
36195d67482SBill Paul 
36295d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
36395d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
36495d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
36595d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
36695d67482SBill Paul 	sc->bge_vpd_prodname = NULL;
36795d67482SBill Paul 	sc->bge_vpd_readonly = NULL;
36895d67482SBill Paul 
36995d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
37095d67482SBill Paul 
37195d67482SBill Paul 	if (res.vr_id != VPD_RES_ID) {
37295d67482SBill Paul 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
37395d67482SBill Paul 			sc->bge_unit, VPD_RES_ID, res.vr_id);
37495d67482SBill Paul                 return;
37595d67482SBill Paul         }
37695d67482SBill Paul 
37795d67482SBill Paul 	pos += sizeof(res);
37895d67482SBill Paul 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
37995d67482SBill Paul 	for (i = 0; i < res.vr_len; i++)
38095d67482SBill Paul 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
38195d67482SBill Paul 	sc->bge_vpd_prodname[i] = '\0';
38295d67482SBill Paul 	pos += i;
38395d67482SBill Paul 
38495d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
38595d67482SBill Paul 
38695d67482SBill Paul 	if (res.vr_id != VPD_RES_READ) {
38795d67482SBill Paul 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
38895d67482SBill Paul 		    sc->bge_unit, VPD_RES_READ, res.vr_id);
38995d67482SBill Paul 		return;
39095d67482SBill Paul 	}
39195d67482SBill Paul 
39295d67482SBill Paul 	pos += sizeof(res);
39395d67482SBill Paul 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
39495d67482SBill Paul 	for (i = 0; i < res.vr_len + 1; i++)
39595d67482SBill Paul 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
39695d67482SBill Paul 
39795d67482SBill Paul 	return;
39895d67482SBill Paul }
3991b4a3b2fSPeter Wemm #endif
40095d67482SBill Paul 
40195d67482SBill Paul /*
40295d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
40395d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
40495d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
40595d67482SBill Paul  * access method.
40695d67482SBill Paul  */
40795d67482SBill Paul static u_int8_t
40895d67482SBill Paul bge_eeprom_getbyte(sc, addr, dest)
40995d67482SBill Paul 	struct bge_softc *sc;
41095d67482SBill Paul 	int addr;
41195d67482SBill Paul 	u_int8_t *dest;
41295d67482SBill Paul {
41395d67482SBill Paul 	int i;
41495d67482SBill Paul 	u_int32_t byte = 0;
41595d67482SBill Paul 
41695d67482SBill Paul 	/*
41795d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
41895d67482SBill Paul 	 * having to use the bitbang method.
41995d67482SBill Paul 	 */
42095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
42195d67482SBill Paul 
42295d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
42395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
42495d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
42595d67482SBill Paul 	DELAY(20);
42695d67482SBill Paul 
42795d67482SBill Paul 	/* Issue the read EEPROM command. */
42895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
42995d67482SBill Paul 
43095d67482SBill Paul 	/* Wait for completion */
43195d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
43295d67482SBill Paul 		DELAY(10);
43395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
43495d67482SBill Paul 			break;
43595d67482SBill Paul 	}
43695d67482SBill Paul 
43795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
43895d67482SBill Paul 		printf("bge%d: eeprom read timed out\n", sc->bge_unit);
43995d67482SBill Paul 		return(0);
44095d67482SBill Paul 	}
44195d67482SBill Paul 
44295d67482SBill Paul 	/* Get result. */
44395d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
44495d67482SBill Paul 
44595d67482SBill Paul         *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
44695d67482SBill Paul 
44795d67482SBill Paul 	return(0);
44895d67482SBill Paul }
44995d67482SBill Paul 
45095d67482SBill Paul /*
45195d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
45295d67482SBill Paul  */
45395d67482SBill Paul static int
45495d67482SBill Paul bge_read_eeprom(sc, dest, off, cnt)
45595d67482SBill Paul 	struct bge_softc *sc;
45695d67482SBill Paul 	caddr_t dest;
45795d67482SBill Paul 	int off;
45895d67482SBill Paul 	int cnt;
45995d67482SBill Paul {
46095d67482SBill Paul 	int err = 0, i;
46195d67482SBill Paul 	u_int8_t byte = 0;
46295d67482SBill Paul 
46395d67482SBill Paul 	for (i = 0; i < cnt; i++) {
46495d67482SBill Paul 		err = bge_eeprom_getbyte(sc, off + i, &byte);
46595d67482SBill Paul 		if (err)
46695d67482SBill Paul 			break;
46795d67482SBill Paul 		*(dest + i) = byte;
46895d67482SBill Paul 	}
46995d67482SBill Paul 
47095d67482SBill Paul 	return(err ? 1 : 0);
47195d67482SBill Paul }
47295d67482SBill Paul 
47395d67482SBill Paul static int
47495d67482SBill Paul bge_miibus_readreg(dev, phy, reg)
47595d67482SBill Paul 	device_t dev;
47695d67482SBill Paul 	int phy, reg;
47795d67482SBill Paul {
47895d67482SBill Paul 	struct bge_softc *sc;
47995d67482SBill Paul 	struct ifnet *ifp;
48095d67482SBill Paul 	u_int32_t val;
48195d67482SBill Paul 	int i;
48295d67482SBill Paul 
48395d67482SBill Paul 	sc = device_get_softc(dev);
48495d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
48595d67482SBill Paul 
486b1265c1aSJohn Polstra 	if (phy != 1)
487b1265c1aSJohn Polstra 		switch(sc->bge_asicrev) {
488b1265c1aSJohn Polstra 		case BGE_ASICREV_BCM5701_B5:
489b1265c1aSJohn Polstra 		case BGE_ASICREV_BCM5703_A2:
49098b28ee5SBill Paul 			return(0);
491b1265c1aSJohn Polstra 		}
49298b28ee5SBill Paul 
49395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
49495d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
49595d67482SBill Paul 
49695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
49795d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
49895d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
49995d67482SBill Paul 			break;
50095d67482SBill Paul 	}
50195d67482SBill Paul 
50295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
50395d67482SBill Paul 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
50495d67482SBill Paul 		return(0);
50595d67482SBill Paul 	}
50695d67482SBill Paul 
50795d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
50895d67482SBill Paul 
50995d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
51095d67482SBill Paul 		return(0);
51195d67482SBill Paul 
51295d67482SBill Paul 	return(val & 0xFFFF);
51395d67482SBill Paul }
51495d67482SBill Paul 
51595d67482SBill Paul static int
51695d67482SBill Paul bge_miibus_writereg(dev, phy, reg, val)
51795d67482SBill Paul 	device_t dev;
51895d67482SBill Paul 	int phy, reg, val;
51995d67482SBill Paul {
52095d67482SBill Paul 	struct bge_softc *sc;
52195d67482SBill Paul 	int i;
52295d67482SBill Paul 
52395d67482SBill Paul 	sc = device_get_softc(dev);
52495d67482SBill Paul 
52595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
52695d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
52795d67482SBill Paul 
52895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
52995d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
53095d67482SBill Paul 			break;
53195d67482SBill Paul 	}
53295d67482SBill Paul 
53395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
53495d67482SBill Paul 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
53595d67482SBill Paul 		return(0);
53695d67482SBill Paul 	}
53795d67482SBill Paul 
53895d67482SBill Paul 	return(0);
53995d67482SBill Paul }
54095d67482SBill Paul 
54195d67482SBill Paul static void
54295d67482SBill Paul bge_miibus_statchg(dev)
54395d67482SBill Paul 	device_t dev;
54495d67482SBill Paul {
54595d67482SBill Paul 	struct bge_softc *sc;
54695d67482SBill Paul 	struct mii_data *mii;
54795d67482SBill Paul 
54895d67482SBill Paul 	sc = device_get_softc(dev);
54995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
55095d67482SBill Paul 
55195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
552b418ad5cSPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
55395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
55495d67482SBill Paul 	} else {
55595d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
55695d67482SBill Paul 	}
55795d67482SBill Paul 
55895d67482SBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
55995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
56095d67482SBill Paul 	} else {
56195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
56295d67482SBill Paul 	}
56395d67482SBill Paul 
56495d67482SBill Paul 	bge_phy_hack(sc);
56595d67482SBill Paul 
56695d67482SBill Paul 	return;
56795d67482SBill Paul }
56895d67482SBill Paul 
56995d67482SBill Paul /*
57095d67482SBill Paul  * Handle events that have triggered interrupts.
57195d67482SBill Paul  */
57295d67482SBill Paul static void
57395d67482SBill Paul bge_handle_events(sc)
57495d67482SBill Paul 	struct bge_softc		*sc;
57595d67482SBill Paul {
57695d67482SBill Paul 
57795d67482SBill Paul 	return;
57895d67482SBill Paul }
57995d67482SBill Paul 
58095d67482SBill Paul /*
58195d67482SBill Paul  * Memory management for jumbo frames.
58295d67482SBill Paul  */
58395d67482SBill Paul 
58495d67482SBill Paul static int
58595d67482SBill Paul bge_alloc_jumbo_mem(sc)
58695d67482SBill Paul 	struct bge_softc		*sc;
58795d67482SBill Paul {
58895d67482SBill Paul 	caddr_t			ptr;
58995d67482SBill Paul 	register int		i;
59095d67482SBill Paul 	struct bge_jpool_entry   *entry;
59195d67482SBill Paul 
59295d67482SBill Paul 	/* Grab a big chunk o' storage. */
59395d67482SBill Paul 	sc->bge_cdata.bge_jumbo_buf = contigmalloc(BGE_JMEM, M_DEVBUF,
59495d67482SBill Paul 		M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
59595d67482SBill Paul 
59695d67482SBill Paul 	if (sc->bge_cdata.bge_jumbo_buf == NULL) {
59795d67482SBill Paul 		printf("bge%d: no memory for jumbo buffers!\n", sc->bge_unit);
59895d67482SBill Paul 		return(ENOBUFS);
59995d67482SBill Paul 	}
60095d67482SBill Paul 
60195d67482SBill Paul 	SLIST_INIT(&sc->bge_jfree_listhead);
60295d67482SBill Paul 	SLIST_INIT(&sc->bge_jinuse_listhead);
60395d67482SBill Paul 
60495d67482SBill Paul 	/*
60595d67482SBill Paul 	 * Now divide it up into 9K pieces and save the addresses
60695d67482SBill Paul 	 * in an array.
60795d67482SBill Paul 	 */
60895d67482SBill Paul 	ptr = sc->bge_cdata.bge_jumbo_buf;
60995d67482SBill Paul 	for (i = 0; i < BGE_JSLOTS; i++) {
61095d67482SBill Paul 		sc->bge_cdata.bge_jslots[i] = ptr;
61195d67482SBill Paul 		ptr += BGE_JLEN;
61295d67482SBill Paul 		entry = malloc(sizeof(struct bge_jpool_entry),
61395d67482SBill Paul 		    M_DEVBUF, M_NOWAIT);
61495d67482SBill Paul 		if (entry == NULL) {
61595d67482SBill Paul 			contigfree(sc->bge_cdata.bge_jumbo_buf,
61695d67482SBill Paul 			    BGE_JMEM, M_DEVBUF);
61795d67482SBill Paul 			sc->bge_cdata.bge_jumbo_buf = NULL;
61895d67482SBill Paul 			printf("bge%d: no memory for jumbo "
61995d67482SBill Paul 			    "buffer queue!\n", sc->bge_unit);
62095d67482SBill Paul 			return(ENOBUFS);
62195d67482SBill Paul 		}
62295d67482SBill Paul 		entry->slot = i;
62395d67482SBill Paul 		SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
62495d67482SBill Paul 		    entry, jpool_entries);
62595d67482SBill Paul 	}
62695d67482SBill Paul 
62795d67482SBill Paul 	return(0);
62895d67482SBill Paul }
62995d67482SBill Paul 
63095d67482SBill Paul static void
63195d67482SBill Paul bge_free_jumbo_mem(sc)
63295d67482SBill Paul         struct bge_softc *sc;
63395d67482SBill Paul {
63495d67482SBill Paul         int i;
63595d67482SBill Paul         struct bge_jpool_entry *entry;
63695d67482SBill Paul 
63795d67482SBill Paul 	for (i = 0; i < BGE_JSLOTS; i++) {
63895d67482SBill Paul 		entry = SLIST_FIRST(&sc->bge_jfree_listhead);
63995d67482SBill Paul 		SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
64095d67482SBill Paul 		free(entry, M_DEVBUF);
64195d67482SBill Paul 	}
64295d67482SBill Paul 
64395d67482SBill Paul 	contigfree(sc->bge_cdata.bge_jumbo_buf, BGE_JMEM, M_DEVBUF);
64495d67482SBill Paul 
64595d67482SBill Paul         return;
64695d67482SBill Paul }
64795d67482SBill Paul 
64895d67482SBill Paul /*
64995d67482SBill Paul  * Allocate a jumbo buffer.
65095d67482SBill Paul  */
65195d67482SBill Paul static void *
65295d67482SBill Paul bge_jalloc(sc)
65395d67482SBill Paul 	struct bge_softc		*sc;
65495d67482SBill Paul {
65595d67482SBill Paul 	struct bge_jpool_entry   *entry;
65695d67482SBill Paul 
65795d67482SBill Paul 	entry = SLIST_FIRST(&sc->bge_jfree_listhead);
65895d67482SBill Paul 
65995d67482SBill Paul 	if (entry == NULL) {
66095d67482SBill Paul 		printf("bge%d: no free jumbo buffers\n", sc->bge_unit);
66195d67482SBill Paul 		return(NULL);
66295d67482SBill Paul 	}
66395d67482SBill Paul 
66495d67482SBill Paul 	SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
66595d67482SBill Paul 	SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
66695d67482SBill Paul 	return(sc->bge_cdata.bge_jslots[entry->slot]);
66795d67482SBill Paul }
66895d67482SBill Paul 
66995d67482SBill Paul /*
67095d67482SBill Paul  * Release a jumbo buffer.
67195d67482SBill Paul  */
67295d67482SBill Paul static void
67395d67482SBill Paul bge_jfree(buf, args)
674914596abSAlfred Perlstein 	void *buf;
67595d67482SBill Paul 	void *args;
67695d67482SBill Paul {
67795d67482SBill Paul 	struct bge_jpool_entry *entry;
67895d67482SBill Paul 	struct bge_softc *sc;
67995d67482SBill Paul 	int i;
68095d67482SBill Paul 
68195d67482SBill Paul 	/* Extract the softc struct pointer. */
68295d67482SBill Paul 	sc = (struct bge_softc *)args;
68395d67482SBill Paul 
68495d67482SBill Paul 	if (sc == NULL)
68595d67482SBill Paul 		panic("bge_jfree: can't find softc pointer!");
68695d67482SBill Paul 
68795d67482SBill Paul 	/* calculate the slot this buffer belongs to */
68895d67482SBill Paul 
68995d67482SBill Paul 	i = ((vm_offset_t)buf
69095d67482SBill Paul 	     - (vm_offset_t)sc->bge_cdata.bge_jumbo_buf) / BGE_JLEN;
69195d67482SBill Paul 
69295d67482SBill Paul 	if ((i < 0) || (i >= BGE_JSLOTS))
69395d67482SBill Paul 		panic("bge_jfree: asked to free buffer that we don't manage!");
69495d67482SBill Paul 
69595d67482SBill Paul 	entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
69695d67482SBill Paul 	if (entry == NULL)
69795d67482SBill Paul 		panic("bge_jfree: buffer not in use!");
69895d67482SBill Paul 	entry->slot = i;
69995d67482SBill Paul 	SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
70095d67482SBill Paul 	SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
70195d67482SBill Paul 
70295d67482SBill Paul 	return;
70395d67482SBill Paul }
70495d67482SBill Paul 
70595d67482SBill Paul 
70695d67482SBill Paul /*
70795d67482SBill Paul  * Intialize a standard receive ring descriptor.
70895d67482SBill Paul  */
70995d67482SBill Paul static int
71095d67482SBill Paul bge_newbuf_std(sc, i, m)
71195d67482SBill Paul 	struct bge_softc	*sc;
71295d67482SBill Paul 	int			i;
71395d67482SBill Paul 	struct mbuf		*m;
71495d67482SBill Paul {
71595d67482SBill Paul 	struct mbuf		*m_new = NULL;
71695d67482SBill Paul 	struct bge_rx_bd	*r;
71795d67482SBill Paul 
71895d67482SBill Paul 	if (m == NULL) {
71995d67482SBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
72095d67482SBill Paul 		if (m_new == NULL) {
72195d67482SBill Paul 			return(ENOBUFS);
72295d67482SBill Paul 		}
72395d67482SBill Paul 
72495d67482SBill Paul 		MCLGET(m_new, M_DONTWAIT);
72595d67482SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
72695d67482SBill Paul 			m_freem(m_new);
72795d67482SBill Paul 			return(ENOBUFS);
72895d67482SBill Paul 		}
72995d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
73095d67482SBill Paul 	} else {
73195d67482SBill Paul 		m_new = m;
73295d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
73395d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
73495d67482SBill Paul 	}
73595d67482SBill Paul 
736e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
73795d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
73895d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
73995d67482SBill Paul 	r = &sc->bge_rdata->bge_rx_std_ring[i];
74095d67482SBill Paul 	BGE_HOSTADDR(r->bge_addr) = vtophys(mtod(m_new, caddr_t));
74195d67482SBill Paul 	r->bge_flags = BGE_RXBDFLAG_END;
74295d67482SBill Paul 	r->bge_len = m_new->m_len;
74395d67482SBill Paul 	r->bge_idx = i;
74495d67482SBill Paul 
74595d67482SBill Paul 	return(0);
74695d67482SBill Paul }
74795d67482SBill Paul 
74895d67482SBill Paul /*
74995d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
75095d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
75195d67482SBill Paul  */
75295d67482SBill Paul static int
75395d67482SBill Paul bge_newbuf_jumbo(sc, i, m)
75495d67482SBill Paul 	struct bge_softc *sc;
75595d67482SBill Paul 	int i;
75695d67482SBill Paul 	struct mbuf *m;
75795d67482SBill Paul {
75895d67482SBill Paul 	struct mbuf *m_new = NULL;
75995d67482SBill Paul 	struct bge_rx_bd *r;
76095d67482SBill Paul 
76195d67482SBill Paul 	if (m == NULL) {
76295d67482SBill Paul 		caddr_t			*buf = NULL;
76395d67482SBill Paul 
76495d67482SBill Paul 		/* Allocate the mbuf. */
76595d67482SBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
76695d67482SBill Paul 		if (m_new == NULL) {
76795d67482SBill Paul 			return(ENOBUFS);
76895d67482SBill Paul 		}
76995d67482SBill Paul 
77095d67482SBill Paul 		/* Allocate the jumbo buffer */
77195d67482SBill Paul 		buf = bge_jalloc(sc);
77295d67482SBill Paul 		if (buf == NULL) {
77395d67482SBill Paul 			m_freem(m_new);
77495d67482SBill Paul 			printf("bge%d: jumbo allocation failed "
77595d67482SBill Paul 			    "-- packet dropped!\n", sc->bge_unit);
77695d67482SBill Paul 			return(ENOBUFS);
77795d67482SBill Paul 		}
77895d67482SBill Paul 
77995d67482SBill Paul 		/* Attach the buffer to the mbuf. */
78095d67482SBill Paul 		m_new->m_data = (void *) buf;
78195d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
78295d67482SBill Paul 		MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, bge_jfree,
78395d67482SBill Paul 		    (struct bge_softc *)sc, 0, EXT_NET_DRV);
78495d67482SBill Paul 	} else {
78595d67482SBill Paul 		m_new = m;
78695d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
78795d67482SBill Paul 		m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
78895d67482SBill Paul 	}
78995d67482SBill Paul 
790e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
79195d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
79295d67482SBill Paul 	/* Set up the descriptor. */
79395d67482SBill Paul 	r = &sc->bge_rdata->bge_rx_jumbo_ring[i];
79495d67482SBill Paul 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
79595d67482SBill Paul 	BGE_HOSTADDR(r->bge_addr) = vtophys(mtod(m_new, caddr_t));
79695d67482SBill Paul 	r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
79795d67482SBill Paul 	r->bge_len = m_new->m_len;
79895d67482SBill Paul 	r->bge_idx = i;
79995d67482SBill Paul 
80095d67482SBill Paul 	return(0);
80195d67482SBill Paul }
80295d67482SBill Paul 
80395d67482SBill Paul /*
80495d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
80595d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
80695d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
80795d67482SBill Paul  * the NIC.
80895d67482SBill Paul  */
80995d67482SBill Paul static int
81095d67482SBill Paul bge_init_rx_ring_std(sc)
81195d67482SBill Paul 	struct bge_softc *sc;
81295d67482SBill Paul {
81395d67482SBill Paul 	int i;
81495d67482SBill Paul 
81595d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
81695d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
81795d67482SBill Paul 			return(ENOBUFS);
81895d67482SBill Paul 	};
81995d67482SBill Paul 
82095d67482SBill Paul 	sc->bge_std = i - 1;
82195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
82295d67482SBill Paul 
82395d67482SBill Paul 	return(0);
82495d67482SBill Paul }
82595d67482SBill Paul 
82695d67482SBill Paul static void
82795d67482SBill Paul bge_free_rx_ring_std(sc)
82895d67482SBill Paul 	struct bge_softc *sc;
82995d67482SBill Paul {
83095d67482SBill Paul 	int i;
83195d67482SBill Paul 
83295d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
83395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
83495d67482SBill Paul 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
83595d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
83695d67482SBill Paul 		}
83795d67482SBill Paul 		bzero((char *)&sc->bge_rdata->bge_rx_std_ring[i],
83895d67482SBill Paul 		    sizeof(struct bge_rx_bd));
83995d67482SBill Paul 	}
84095d67482SBill Paul 
84195d67482SBill Paul 	return;
84295d67482SBill Paul }
84395d67482SBill Paul 
84495d67482SBill Paul static int
84595d67482SBill Paul bge_init_rx_ring_jumbo(sc)
84695d67482SBill Paul 	struct bge_softc *sc;
84795d67482SBill Paul {
84895d67482SBill Paul 	int i;
84995d67482SBill Paul 	struct bge_rcb *rcb;
85095d67482SBill Paul 	struct bge_rcb_opaque *rcbo;
85195d67482SBill Paul 
85295d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
85395d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
85495d67482SBill Paul 			return(ENOBUFS);
85595d67482SBill Paul 	};
85695d67482SBill Paul 
85795d67482SBill Paul 	sc->bge_jumbo = i - 1;
85895d67482SBill Paul 
85995d67482SBill Paul 	rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
86095d67482SBill Paul 	rcbo = (struct bge_rcb_opaque *)rcb;
86195d67482SBill Paul 	rcb->bge_flags = 0;
86295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
86395d67482SBill Paul 
86495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
86595d67482SBill Paul 
86695d67482SBill Paul 	return(0);
86795d67482SBill Paul }
86895d67482SBill Paul 
86995d67482SBill Paul static void
87095d67482SBill Paul bge_free_rx_ring_jumbo(sc)
87195d67482SBill Paul 	struct bge_softc *sc;
87295d67482SBill Paul {
87395d67482SBill Paul 	int i;
87495d67482SBill Paul 
87595d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
87695d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
87795d67482SBill Paul 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
87895d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
87995d67482SBill Paul 		}
88095d67482SBill Paul 		bzero((char *)&sc->bge_rdata->bge_rx_jumbo_ring[i],
88195d67482SBill Paul 		    sizeof(struct bge_rx_bd));
88295d67482SBill Paul 	}
88395d67482SBill Paul 
88495d67482SBill Paul 	return;
88595d67482SBill Paul }
88695d67482SBill Paul 
88795d67482SBill Paul static void
88895d67482SBill Paul bge_free_tx_ring(sc)
88995d67482SBill Paul 	struct bge_softc *sc;
89095d67482SBill Paul {
89195d67482SBill Paul 	int i;
89295d67482SBill Paul 
89395d67482SBill Paul 	if (sc->bge_rdata->bge_tx_ring == NULL)
89495d67482SBill Paul 		return;
89595d67482SBill Paul 
89695d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
89795d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
89895d67482SBill Paul 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
89995d67482SBill Paul 			sc->bge_cdata.bge_tx_chain[i] = NULL;
90095d67482SBill Paul 		}
90195d67482SBill Paul 		bzero((char *)&sc->bge_rdata->bge_tx_ring[i],
90295d67482SBill Paul 		    sizeof(struct bge_tx_bd));
90395d67482SBill Paul 	}
90495d67482SBill Paul 
90595d67482SBill Paul 	return;
90695d67482SBill Paul }
90795d67482SBill Paul 
90895d67482SBill Paul static int
90995d67482SBill Paul bge_init_tx_ring(sc)
91095d67482SBill Paul 	struct bge_softc *sc;
91195d67482SBill Paul {
91295d67482SBill Paul 	sc->bge_txcnt = 0;
91395d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
91495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
91595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
91695d67482SBill Paul 
91795d67482SBill Paul 	return(0);
91895d67482SBill Paul }
91995d67482SBill Paul 
92095d67482SBill Paul #define BGE_POLY	0xEDB88320
92195d67482SBill Paul 
92295d67482SBill Paul static u_int32_t
92395d67482SBill Paul bge_crc(addr)
92495d67482SBill Paul 	caddr_t addr;
92595d67482SBill Paul {
92695d67482SBill Paul 	u_int32_t idx, bit, data, crc;
92795d67482SBill Paul 
92895d67482SBill Paul 	/* Compute CRC for the address value. */
92995d67482SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
93095d67482SBill Paul 
93195d67482SBill Paul 	for (idx = 0; idx < 6; idx++) {
93295d67482SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
93395d67482SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? BGE_POLY : 0);
93495d67482SBill Paul 	}
93595d67482SBill Paul 
93695d67482SBill Paul 	return(crc & 0x7F);
93795d67482SBill Paul }
93895d67482SBill Paul 
93995d67482SBill Paul static void
94095d67482SBill Paul bge_setmulti(sc)
94195d67482SBill Paul 	struct bge_softc *sc;
94295d67482SBill Paul {
94395d67482SBill Paul 	struct ifnet *ifp;
94495d67482SBill Paul 	struct ifmultiaddr *ifma;
94595d67482SBill Paul 	u_int32_t hashes[4] = { 0, 0, 0, 0 };
94695d67482SBill Paul 	int h, i;
94795d67482SBill Paul 
94895d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
94995d67482SBill Paul 
95095d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
95195d67482SBill Paul 		for (i = 0; i < 4; i++)
95295d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
95395d67482SBill Paul 		return;
95495d67482SBill Paul 	}
95595d67482SBill Paul 
95695d67482SBill Paul 	/* First, zot all the existing filters. */
95795d67482SBill Paul 	for (i = 0; i < 4; i++)
95895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
95995d67482SBill Paul 
96095d67482SBill Paul 	/* Now program new ones. */
96195d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
96295d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
96395d67482SBill Paul 			continue;
96495d67482SBill Paul 		h = bge_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
96595d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
96695d67482SBill Paul 	}
96795d67482SBill Paul 
96895d67482SBill Paul 	for (i = 0; i < 4; i++)
96995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
97095d67482SBill Paul 
97195d67482SBill Paul 	return;
97295d67482SBill Paul }
97395d67482SBill Paul 
97495d67482SBill Paul /*
97595d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
97695d67482SBill Paul  * self-test results.
97795d67482SBill Paul  */
97895d67482SBill Paul static int
97995d67482SBill Paul bge_chipinit(sc)
98095d67482SBill Paul 	struct bge_softc *sc;
98195d67482SBill Paul {
98295d67482SBill Paul 	int			i;
98395d67482SBill Paul 
98495d67482SBill Paul 	/* Set endianness before we access any non-PCI registers. */
98595d67482SBill Paul #if BYTE_ORDER == BIG_ENDIAN
98695d67482SBill Paul 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
98795d67482SBill Paul 	    BGE_BIGENDIAN_INIT, 4);
98895d67482SBill Paul #else
98995d67482SBill Paul 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
99095d67482SBill Paul 	    BGE_LITTLEENDIAN_INIT, 4);
99195d67482SBill Paul #endif
99295d67482SBill Paul 
99395d67482SBill Paul 	/*
99495d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
99595d67482SBill Paul 	 * self-tests passed.
99695d67482SBill Paul 	 */
99795d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
99895d67482SBill Paul 		printf("bge%d: RX CPU self-diagnostics failed!\n",
99995d67482SBill Paul 		    sc->bge_unit);
100095d67482SBill Paul 		return(ENODEV);
100195d67482SBill Paul 	}
100295d67482SBill Paul 
100395d67482SBill Paul 	/* Clear the MAC control register */
100495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
100595d67482SBill Paul 
100695d67482SBill Paul 	/*
100795d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
100895d67482SBill Paul 	 * internal memory.
100995d67482SBill Paul 	 */
101095d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
101195d67482SBill Paul 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
101295d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
101395d67482SBill Paul 
101495d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
101595d67482SBill Paul 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
101695d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
101795d67482SBill Paul 
101895d67482SBill Paul 	/* Set up the PCI DMA control register. */
10198287860eSJohn Polstra 	if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
10208287860eSJohn Polstra 	    BGE_PCISTATE_PCI_BUSMODE) {
10218287860eSJohn Polstra 		/* Conventional PCI bus */
102295d67482SBill Paul 		pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
10238287860eSJohn Polstra 		    BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD|0x3F000F, 4);
10248287860eSJohn Polstra 	} else {
10258287860eSJohn Polstra 		/* PCI-X bus */
10268287860eSJohn Polstra 		pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
10278287860eSJohn Polstra 		    BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD|0x1B000F, 4);
10288287860eSJohn Polstra 	}
102995d67482SBill Paul 
103095d67482SBill Paul 	/*
103195d67482SBill Paul 	 * Set up general mode register.
103295d67482SBill Paul 	 */
103395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_WORDSWAP_NONFRAME|
103495d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
103595d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
10360189c944SBill Paul 	    BGE_MODECTL_NO_RX_CRC|BGE_MODECTL_TX_NO_PHDR_CSUM|
10370189c944SBill Paul 	    BGE_MODECTL_RX_NO_PHDR_CSUM);
103895d67482SBill Paul 
103995d67482SBill Paul 	/*
1040ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1041ea13bdd5SJohn Polstra 	 * properly by these devices.
104295d67482SBill Paul 	 */
1043ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
104495d67482SBill Paul 
104595d67482SBill Paul #ifdef __brokenalpha__
104695d67482SBill Paul 	/*
104795d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
104895d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
104995d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
105095d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
105195d67482SBill Paul 	 */
105295d67482SBill Paul 	PCI_SETBIT(sc, BGE_PCI_DMA_RW_CTL, BGE_PCI_READ_BNDRY_1024, 4);
105395d67482SBill Paul #endif
105495d67482SBill Paul 
105595d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
105695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
105795d67482SBill Paul 
105895d67482SBill Paul 	return(0);
105995d67482SBill Paul }
106095d67482SBill Paul 
106195d67482SBill Paul static int
106295d67482SBill Paul bge_blockinit(sc)
106395d67482SBill Paul 	struct bge_softc *sc;
106495d67482SBill Paul {
106595d67482SBill Paul 	struct bge_rcb *rcb;
106695d67482SBill Paul 	struct bge_rcb_opaque *rcbo;
106795d67482SBill Paul 	int i;
106895d67482SBill Paul 
106995d67482SBill Paul 	/*
107095d67482SBill Paul 	 * Initialize the memory window pointer register so that
107195d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
107295d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
107395d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
107495d67482SBill Paul 	 */
107595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
107695d67482SBill Paul 
107795d67482SBill Paul 	/* Configure mbuf memory pool */
107895d67482SBill Paul 	if (sc->bge_extram) {
107995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_EXT_SSRAM);
108095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
108195d67482SBill Paul 	} else {
108295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
108395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
108495d67482SBill Paul 	}
108595d67482SBill Paul 
108695d67482SBill Paul 	/* Configure DMA resource pool */
108795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR, BGE_DMA_DESCRIPTORS);
108895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
108995d67482SBill Paul 
109095d67482SBill Paul 	/* Configure mbuf pool watermarks */
109195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 24);
109295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 24);
109395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 48);
109495d67482SBill Paul 
109595d67482SBill Paul 	/* Configure DMA resource watermarks */
109695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
109795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
109895d67482SBill Paul 
109995d67482SBill Paul 	/* Enable buffer manager */
110095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_MODE,
110195d67482SBill Paul 	    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
110295d67482SBill Paul 
110395d67482SBill Paul 	/* Poll for buffer manager start indication */
110495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
110595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
110695d67482SBill Paul 			break;
110795d67482SBill Paul 		DELAY(10);
110895d67482SBill Paul 	}
110995d67482SBill Paul 
111095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
111195d67482SBill Paul 		printf("bge%d: buffer manager failed to start\n",
111295d67482SBill Paul 		    sc->bge_unit);
111395d67482SBill Paul 		return(ENXIO);
111495d67482SBill Paul 	}
111595d67482SBill Paul 
111695d67482SBill Paul 	/* Enable flow-through queues */
111795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
111895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
111995d67482SBill Paul 
112095d67482SBill Paul 	/* Wait until queue initialization is complete */
112195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
112295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
112395d67482SBill Paul 			break;
112495d67482SBill Paul 		DELAY(10);
112595d67482SBill Paul 	}
112695d67482SBill Paul 
112795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
112895d67482SBill Paul 		printf("bge%d: flow-through queue init failed\n",
112995d67482SBill Paul 		    sc->bge_unit);
113095d67482SBill Paul 		return(ENXIO);
113195d67482SBill Paul 	}
113295d67482SBill Paul 
113395d67482SBill Paul 	/* Initialize the standard RX ring control block */
113495d67482SBill Paul 	rcb = &sc->bge_rdata->bge_info.bge_std_rx_rcb;
113595d67482SBill Paul 	BGE_HOSTADDR(rcb->bge_hostaddr) =
113695d67482SBill Paul 	    vtophys(&sc->bge_rdata->bge_rx_std_ring);
113795d67482SBill Paul 	rcb->bge_max_len = BGE_MAX_FRAMELEN;
113895d67482SBill Paul 	if (sc->bge_extram)
113995d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
114095d67482SBill Paul 	else
114195d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
114295d67482SBill Paul 	rcb->bge_flags = 0;
114395d67482SBill Paul 	rcbo = (struct bge_rcb_opaque *)rcb;
114495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcbo->bge_reg0);
114595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcbo->bge_reg1);
114695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
114795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcbo->bge_reg3);
114895d67482SBill Paul 
114995d67482SBill Paul 	/*
115095d67482SBill Paul 	 * Initialize the jumbo RX ring control block
115195d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
115295d67482SBill Paul 	 * field until we're actually ready to start
115395d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
115495d67482SBill Paul 	 * high enough to require it).
115595d67482SBill Paul 	 */
115695d67482SBill Paul 	rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
115795d67482SBill Paul 	BGE_HOSTADDR(rcb->bge_hostaddr) =
115895d67482SBill Paul 	    vtophys(&sc->bge_rdata->bge_rx_jumbo_ring);
115995d67482SBill Paul 	rcb->bge_max_len = BGE_MAX_FRAMELEN;
116095d67482SBill Paul 	if (sc->bge_extram)
116195d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
116295d67482SBill Paul 	else
116395d67482SBill Paul 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
116495d67482SBill Paul 	rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED;
116595d67482SBill Paul 
116695d67482SBill Paul 	rcbo = (struct bge_rcb_opaque *)rcb;
116795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI, rcbo->bge_reg0);
116895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO, rcbo->bge_reg1);
116995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
117095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcbo->bge_reg3);
117195d67482SBill Paul 
117295d67482SBill Paul 	/* Set up dummy disabled mini ring RCB */
117395d67482SBill Paul 	rcb = &sc->bge_rdata->bge_info.bge_mini_rx_rcb;
117495d67482SBill Paul 	rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED;
117595d67482SBill Paul 	rcbo = (struct bge_rcb_opaque *)rcb;
117695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
117795d67482SBill Paul 
117895d67482SBill Paul 	/*
117995d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
118095d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
118195d67482SBill Paul 	 * each ring.
118295d67482SBill Paul 	 */
118395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
118495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
118595d67482SBill Paul 
118695d67482SBill Paul 	/*
118795d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
118895d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
118995d67482SBill Paul 	 * These are located in NIC memory.
119095d67482SBill Paul 	 */
119195d67482SBill Paul 	rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
119295d67482SBill Paul 	    BGE_SEND_RING_RCB);
119395d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
119495d67482SBill Paul 		rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED;
119595d67482SBill Paul 		rcb->bge_max_len = 0;
119695d67482SBill Paul 		rcb->bge_nicaddr = 0;
119795d67482SBill Paul 		rcb++;
119895d67482SBill Paul 	}
119995d67482SBill Paul 
120095d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
120195d67482SBill Paul 	rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
120295d67482SBill Paul 	    BGE_SEND_RING_RCB);
120395d67482SBill Paul 	rcb->bge_hostaddr.bge_addr_hi = 0;
120495d67482SBill Paul 	BGE_HOSTADDR(rcb->bge_hostaddr) =
120595d67482SBill Paul 	    vtophys(&sc->bge_rdata->bge_tx_ring);
120695d67482SBill Paul 	rcb->bge_nicaddr = BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT);
120795d67482SBill Paul 	rcb->bge_max_len = BGE_TX_RING_CNT;
120895d67482SBill Paul 	rcb->bge_flags = 0;
120995d67482SBill Paul 
121095d67482SBill Paul 	/* Disable all unused RX return rings */
121195d67482SBill Paul 	rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
121295d67482SBill Paul 	    BGE_RX_RETURN_RING_RCB);
121395d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
121495d67482SBill Paul 		rcb->bge_hostaddr.bge_addr_hi = 0;
121595d67482SBill Paul 		rcb->bge_hostaddr.bge_addr_lo = 0;
121695d67482SBill Paul 		rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED;
121795d67482SBill Paul 		rcb->bge_max_len = BGE_RETURN_RING_CNT;
121895d67482SBill Paul 		rcb->bge_nicaddr = 0;
121995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
122095d67482SBill Paul 		    (i * (sizeof(u_int64_t))), 0);
122195d67482SBill Paul 		rcb++;
122295d67482SBill Paul 	}
122395d67482SBill Paul 
122495d67482SBill Paul 	/* Initialize RX ring indexes */
122595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
122695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
122795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
122895d67482SBill Paul 
122995d67482SBill Paul 	/*
123095d67482SBill Paul 	 * Set up RX return ring 0
123195d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
123295d67482SBill Paul 	 * The return rings live entirely within the host, so the
123395d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
123495d67482SBill Paul 	 */
123595d67482SBill Paul 	rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
123695d67482SBill Paul 	    BGE_RX_RETURN_RING_RCB);
123795d67482SBill Paul 	rcb->bge_hostaddr.bge_addr_hi = 0;
123895d67482SBill Paul 	BGE_HOSTADDR(rcb->bge_hostaddr) =
123995d67482SBill Paul 	    vtophys(&sc->bge_rdata->bge_rx_return_ring);
124095d67482SBill Paul 	rcb->bge_nicaddr = 0x00000000;
124195d67482SBill Paul 	rcb->bge_max_len = BGE_RETURN_RING_CNT;
124295d67482SBill Paul 	rcb->bge_flags = 0;
124395d67482SBill Paul 
124495d67482SBill Paul 	/* Set random backoff seed for TX */
124595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
124695d67482SBill Paul 	    sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
124795d67482SBill Paul 	    sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
124895d67482SBill Paul 	    sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] +
124995d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
125095d67482SBill Paul 
125195d67482SBill Paul 	/* Set inter-packet gap */
125295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
125395d67482SBill Paul 
125495d67482SBill Paul 	/*
125595d67482SBill Paul 	 * Specify which ring to use for packets that don't match
125695d67482SBill Paul 	 * any RX rules.
125795d67482SBill Paul 	 */
125895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
125995d67482SBill Paul 
126095d67482SBill Paul 	/*
126195d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
126295d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
126395d67482SBill Paul 	 */
126495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
126595d67482SBill Paul 
126695d67482SBill Paul 	/* Inialize RX list placement stats mask. */
126795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
126895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
126995d67482SBill Paul 
127095d67482SBill Paul 	/* Disable host coalescing until we get it set up */
127195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
127295d67482SBill Paul 
127395d67482SBill Paul 	/* Poll to make sure it's shut down. */
127495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
127595d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
127695d67482SBill Paul 			break;
127795d67482SBill Paul 		DELAY(10);
127895d67482SBill Paul 	}
127995d67482SBill Paul 
128095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
128195d67482SBill Paul 		printf("bge%d: host coalescing engine failed to idle\n",
128295d67482SBill Paul 		    sc->bge_unit);
128395d67482SBill Paul 		return(ENXIO);
128495d67482SBill Paul 	}
128595d67482SBill Paul 
128695d67482SBill Paul 	/* Set up host coalescing defaults */
128795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
128895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
128995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
129095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
129195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
129295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
129395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
129495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
129595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
129695d67482SBill Paul 
129795d67482SBill Paul 	/* Set up address of statistics block */
129895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
129995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI, 0);
130095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
130195d67482SBill Paul 	    vtophys(&sc->bge_rdata->bge_info.bge_stats));
130295d67482SBill Paul 
130395d67482SBill Paul 	/* Set up address of status block */
130495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
130595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI, 0);
130695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
130795d67482SBill Paul 	    vtophys(&sc->bge_rdata->bge_status_block));
130895d67482SBill Paul 	sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx = 0;
130995d67482SBill Paul 	sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx = 0;
131095d67482SBill Paul 
131195d67482SBill Paul 	/* Turn on host coalescing state machine */
131295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
131395d67482SBill Paul 
131495d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
131595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
131695d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
131795d67482SBill Paul 
131895d67482SBill Paul 	/* Turn on RX list placement state machine */
131995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
132095d67482SBill Paul 
132195d67482SBill Paul 	/* Turn on RX list selector state machine. */
132295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
132395d67482SBill Paul 
132495d67482SBill Paul 	/* Turn on DMA, clear stats */
132595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
132695d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
132795d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
132895d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
132995d67482SBill Paul 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
133095d67482SBill Paul 
133195d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
133295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
133395d67482SBill Paul 
133495d67482SBill Paul #ifdef notdef
133595d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
133695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
133795d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
133895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
133995d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
134095d67482SBill Paul #endif
134195d67482SBill Paul 
134295d67482SBill Paul 	/* Turn on DMA completion state machine */
134395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
134495d67482SBill Paul 
134595d67482SBill Paul 	/* Turn on write DMA state machine */
134695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
134795d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
134895d67482SBill Paul 
134995d67482SBill Paul 	/* Turn on read DMA state machine */
135095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
135195d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
135295d67482SBill Paul 
135395d67482SBill Paul 	/* Turn on RX data completion state machine */
135495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
135595d67482SBill Paul 
135695d67482SBill Paul 	/* Turn on RX BD initiator state machine */
135795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
135895d67482SBill Paul 
135995d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
136095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
136195d67482SBill Paul 
136295d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
136395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
136495d67482SBill Paul 
136595d67482SBill Paul 	/* Turn on send BD completion state machine */
136695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
136795d67482SBill Paul 
136895d67482SBill Paul 	/* Turn on send data completion state machine */
136995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
137095d67482SBill Paul 
137195d67482SBill Paul 	/* Turn on send data initiator state machine */
137295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
137395d67482SBill Paul 
137495d67482SBill Paul 	/* Turn on send BD initiator state machine */
137595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
137695d67482SBill Paul 
137795d67482SBill Paul 	/* Turn on send BD selector state machine */
137895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
137995d67482SBill Paul 
138095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
138195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
138295d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
138395d67482SBill Paul 
138495d67482SBill Paul 	/* init LED register */
138595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_LED_CTL, 0x00000000);
138695d67482SBill Paul 
138795d67482SBill Paul 	/* ack/clear link change events */
138895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
138995d67482SBill Paul 	    BGE_MACSTAT_CFG_CHANGED);
139095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
139195d67482SBill Paul 
139295d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
139395d67482SBill Paul 	if (sc->bge_tbi) {
139495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1395a1d52896SBill Paul  	} else {
139695d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
1397a1d52896SBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700)
1398a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1399a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1400a1d52896SBill Paul 	}
140195d67482SBill Paul 
140295d67482SBill Paul 	/* Enable link state change attentions. */
140395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
140495d67482SBill Paul 
140595d67482SBill Paul 	return(0);
140695d67482SBill Paul }
140795d67482SBill Paul 
140895d67482SBill Paul /*
140995d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
141095d67482SBill Paul  * against our list and return its name if we find a match. Note
141195d67482SBill Paul  * that since the Broadcom controller contains VPD support, we
141295d67482SBill Paul  * can get the device name string from the controller itself instead
141395d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
141495d67482SBill Paul  * we'll always announce the right product name.
141595d67482SBill Paul  */
141695d67482SBill Paul static int
141795d67482SBill Paul bge_probe(dev)
141895d67482SBill Paul 	device_t dev;
141995d67482SBill Paul {
142095d67482SBill Paul 	struct bge_type *t;
142195d67482SBill Paul 	struct bge_softc *sc;
142295d67482SBill Paul 
142395d67482SBill Paul 	t = bge_devs;
142495d67482SBill Paul 
142595d67482SBill Paul 	sc = device_get_softc(dev);
142695d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
142795d67482SBill Paul 	sc->bge_unit = device_get_unit(dev);
142895d67482SBill Paul 	sc->bge_dev = dev;
142995d67482SBill Paul 
143095d67482SBill Paul 	while(t->bge_name != NULL) {
143195d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
143295d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
143395d67482SBill Paul #ifdef notdef
143495d67482SBill Paul 			bge_vpd_read(sc);
143595d67482SBill Paul 			device_set_desc(dev, sc->bge_vpd_prodname);
143695d67482SBill Paul #endif
143795d67482SBill Paul 			device_set_desc(dev, t->bge_name);
143895d67482SBill Paul 			return(0);
143995d67482SBill Paul 		}
144095d67482SBill Paul 		t++;
144195d67482SBill Paul 	}
144295d67482SBill Paul 
144395d67482SBill Paul 	return(ENXIO);
144495d67482SBill Paul }
144595d67482SBill Paul 
144695d67482SBill Paul static int
144795d67482SBill Paul bge_attach(dev)
144895d67482SBill Paul 	device_t dev;
144995d67482SBill Paul {
145095d67482SBill Paul 	int s;
145195d67482SBill Paul 	u_int32_t command;
145295d67482SBill Paul 	struct ifnet *ifp;
145395d67482SBill Paul 	struct bge_softc *sc;
1454a1d52896SBill Paul 	u_int32_t hwcfg = 0;
1455b1265c1aSJohn Polstra 	u_int32_t mac_addr = 0;
145695d67482SBill Paul 	int unit, error = 0, rid;
145795d67482SBill Paul 
145895d67482SBill Paul 	s = splimp();
145995d67482SBill Paul 
146095d67482SBill Paul 	sc = device_get_softc(dev);
146195d67482SBill Paul 	unit = device_get_unit(dev);
146295d67482SBill Paul 	sc->bge_dev = dev;
146395d67482SBill Paul 	sc->bge_unit = unit;
146495d67482SBill Paul 
146595d67482SBill Paul 	/*
146695d67482SBill Paul 	 * Map control/status registers.
146795d67482SBill Paul 	 */
146895d67482SBill Paul 	pci_enable_busmaster(dev);
146995d67482SBill Paul 	pci_enable_io(dev, SYS_RES_MEMORY);
147095d67482SBill Paul 	command = pci_read_config(dev, PCIR_COMMAND, 4);
147195d67482SBill Paul 
147295d67482SBill Paul 	if (!(command & PCIM_CMD_MEMEN)) {
147395d67482SBill Paul 		printf("bge%d: failed to enable memory mapping!\n", unit);
147495d67482SBill Paul 		error = ENXIO;
147595d67482SBill Paul 		goto fail;
147695d67482SBill Paul 	}
147795d67482SBill Paul 
147895d67482SBill Paul 	rid = BGE_PCI_BAR0;
147995d67482SBill Paul 	sc->bge_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
148095d67482SBill Paul 	    0, ~0, 1, RF_ACTIVE);
148195d67482SBill Paul 
148295d67482SBill Paul 	if (sc->bge_res == NULL) {
148395d67482SBill Paul 		printf ("bge%d: couldn't map memory\n", unit);
148495d67482SBill Paul 		error = ENXIO;
148595d67482SBill Paul 		goto fail;
148695d67482SBill Paul 	}
148795d67482SBill Paul 
148895d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
148995d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
149095d67482SBill Paul 	sc->bge_vhandle = (vm_offset_t)rman_get_virtual(sc->bge_res);
149195d67482SBill Paul 
149295d67482SBill Paul 	/*
149395d67482SBill Paul 	 * XXX FIXME: rman_get_virtual() on the alpha is currently
149495d67482SBill Paul 	 * broken and returns a physical address instead of a kernel
149595d67482SBill Paul 	 * virtual address. Consequently, we need to do a little
149695d67482SBill Paul 	 * extra mangling of the vhandle on the alpha. This should
149795d67482SBill Paul 	 * eventually be fixed! The whole idea here is to get rid
149895d67482SBill Paul 	 * of platform dependencies.
149995d67482SBill Paul 	 */
150095d67482SBill Paul #ifdef __alpha__
150195d67482SBill Paul 	if (pci_cvt_to_bwx(sc->bge_vhandle))
150295d67482SBill Paul 		sc->bge_vhandle = pci_cvt_to_bwx(sc->bge_vhandle);
150395d67482SBill Paul 	else
150495d67482SBill Paul 		sc->bge_vhandle = pci_cvt_to_dense(sc->bge_vhandle);
150595d67482SBill Paul 	sc->bge_vhandle = ALPHA_PHYS_TO_K0SEG(sc->bge_vhandle);
150695d67482SBill Paul #endif
150795d67482SBill Paul 
150895d67482SBill Paul 	/* Allocate interrupt */
150995d67482SBill Paul 	rid = 0;
151095d67482SBill Paul 
151195d67482SBill Paul 	sc->bge_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
151295d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
151395d67482SBill Paul 
151495d67482SBill Paul 	if (sc->bge_irq == NULL) {
151595d67482SBill Paul 		printf("bge%d: couldn't map interrupt\n", unit);
151695d67482SBill Paul 		error = ENXIO;
151795d67482SBill Paul 		goto fail;
151895d67482SBill Paul 	}
151995d67482SBill Paul 
152095d67482SBill Paul 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET,
152195d67482SBill Paul 	   bge_intr, sc, &sc->bge_intrhand);
152295d67482SBill Paul 
152395d67482SBill Paul 	if (error) {
152495d67482SBill Paul 		bge_release_resources(sc);
152595d67482SBill Paul 		printf("bge%d: couldn't set up irq\n", unit);
152695d67482SBill Paul 		goto fail;
152795d67482SBill Paul 	}
152895d67482SBill Paul 
152995d67482SBill Paul 	sc->bge_unit = unit;
153095d67482SBill Paul 
153195d67482SBill Paul 	/* Try to reset the chip. */
153295d67482SBill Paul 	bge_reset(sc);
153395d67482SBill Paul 
153495d67482SBill Paul 	if (bge_chipinit(sc)) {
153595d67482SBill Paul 		printf("bge%d: chip initialization failed\n", sc->bge_unit);
153695d67482SBill Paul 		bge_release_resources(sc);
153795d67482SBill Paul 		error = ENXIO;
153895d67482SBill Paul 		goto fail;
153995d67482SBill Paul 	}
154095d67482SBill Paul 
154195d67482SBill Paul 	/*
154295d67482SBill Paul 	 * Get station address from the EEPROM.
154395d67482SBill Paul 	 */
1544b1265c1aSJohn Polstra 	mac_addr = bge_readmem_ind(sc, 0x0c14);
1545b1265c1aSJohn Polstra 	if ((mac_addr >> 16) == 0x484b) {
1546b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[0] = (u_char)(mac_addr >> 8);
1547b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[1] = (u_char)mac_addr;
1548b1265c1aSJohn Polstra 		mac_addr = bge_readmem_ind(sc, 0x0c18);
1549b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[2] = (u_char)(mac_addr >> 24);
1550b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[3] = (u_char)(mac_addr >> 16);
1551b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[4] = (u_char)(mac_addr >> 8);
1552b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[5] = (u_char)mac_addr;
1553b1265c1aSJohn Polstra 	} else if (bge_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
155495d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
155595d67482SBill Paul 		printf("bge%d: failed to read station address\n", unit);
155695d67482SBill Paul 		bge_release_resources(sc);
155795d67482SBill Paul 		error = ENXIO;
155895d67482SBill Paul 		goto fail;
155995d67482SBill Paul 	}
156095d67482SBill Paul 
156195d67482SBill Paul 	/*
156295d67482SBill Paul 	 * A Broadcom chip was detected. Inform the world.
156395d67482SBill Paul 	 */
156495d67482SBill Paul 	printf("bge%d: Ethernet address: %6D\n", unit,
156595d67482SBill Paul 	    sc->arpcom.ac_enaddr, ":");
156695d67482SBill Paul 
156795d67482SBill Paul 	/* Allocate the general information block and ring buffers. */
156895d67482SBill Paul 	sc->bge_rdata = contigmalloc(sizeof(struct bge_ring_data), M_DEVBUF,
156995d67482SBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
157095d67482SBill Paul 
157195d67482SBill Paul 	if (sc->bge_rdata == NULL) {
157295d67482SBill Paul 		bge_release_resources(sc);
157395d67482SBill Paul 		error = ENXIO;
157495d67482SBill Paul 		printf("bge%d: no memory for list buffers!\n", sc->bge_unit);
157595d67482SBill Paul 		goto fail;
157695d67482SBill Paul 	}
157795d67482SBill Paul 
157895d67482SBill Paul 	bzero(sc->bge_rdata, sizeof(struct bge_ring_data));
157995d67482SBill Paul 
158095d67482SBill Paul 	/* Try to allocate memory for jumbo buffers. */
158195d67482SBill Paul 	if (bge_alloc_jumbo_mem(sc)) {
158295d67482SBill Paul 		printf("bge%d: jumbo buffer allocation "
158395d67482SBill Paul 		    "failed\n", sc->bge_unit);
158495d67482SBill Paul 		bge_release_resources(sc);
158595d67482SBill Paul 		error = ENXIO;
158695d67482SBill Paul 		goto fail;
158795d67482SBill Paul 	}
158895d67482SBill Paul 
158995d67482SBill Paul 	/* Set default tuneable values. */
159095d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
159195d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
159295d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
159395d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
159495d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
159595d67482SBill Paul 
159695d67482SBill Paul 	/* Set up ifnet structure */
159795d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
159895d67482SBill Paul 	ifp->if_softc = sc;
159995d67482SBill Paul 	ifp->if_unit = sc->bge_unit;
160095d67482SBill Paul 	ifp->if_name = "bge";
160195d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
160295d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
160395d67482SBill Paul 	ifp->if_output = ether_output;
160495d67482SBill Paul 	ifp->if_start = bge_start;
160595d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
160695d67482SBill Paul 	ifp->if_init = bge_init;
160795d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
160895d67482SBill Paul 	ifp->if_snd.ifq_maxlen = BGE_TX_RING_CNT - 1;
160995d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
161095d67482SBill Paul 	ifp->if_capabilities = IFCAP_HWCSUM;
161195d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
161295d67482SBill Paul 
161398b28ee5SBill Paul 	/* Save ASIC rev. */
161498b28ee5SBill Paul 
161598b28ee5SBill Paul 	sc->bge_asicrev =
161698b28ee5SBill Paul 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
161798b28ee5SBill Paul 	    BGE_PCIMISCCTL_ASICREV;
161898b28ee5SBill Paul 
1619a1d52896SBill Paul 	/* Pretend all 5700s are the same */
1620a1d52896SBill Paul 	if ((sc->bge_asicrev & 0xFF000000) == BGE_ASICREV_BCM5700)
1621a1d52896SBill Paul 		sc->bge_asicrev = BGE_ASICREV_BCM5700;
1622a1d52896SBill Paul 
1623a1d52896SBill Paul 	/*
1624a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
1625a1d52896SBill Paul 	 * hardware config word in the EEPROM. Note: on some BCM5700
1626a1d52896SBill Paul 	 * cards, this value appears to be unset. If that's the
1627a1d52896SBill Paul 	 * case, we have to rely on identifying the NIC by its PCI
1628a1d52896SBill Paul 	 * subsystem ID, as we do below for the SysKonnect SK-9D41.
1629a1d52896SBill Paul 	 */
1630a1d52896SBill Paul 	bge_read_eeprom(sc, (caddr_t)&hwcfg,
1631a1d52896SBill Paul 		    BGE_EE_HWCFG_OFFSET, sizeof(hwcfg));
1632a1d52896SBill Paul 	if ((ntohl(hwcfg) & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
1633a1d52896SBill Paul 		sc->bge_tbi = 1;
1634a1d52896SBill Paul 
163595d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
163695d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
163795d67482SBill Paul 		sc->bge_tbi = 1;
163895d67482SBill Paul 
163995d67482SBill Paul 	if (sc->bge_tbi) {
164095d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
164195d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
164295d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
164395d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
164495d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
164595d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
164695d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
164795d67482SBill Paul 	} else {
164895d67482SBill Paul 		/*
164995d67482SBill Paul 		 * Do transceiver setup.
165095d67482SBill Paul 		 */
165195d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
165295d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
165395d67482SBill Paul 			printf("bge%d: MII without any PHY!\n", sc->bge_unit);
165495d67482SBill Paul 			bge_release_resources(sc);
165595d67482SBill Paul 			bge_free_jumbo_mem(sc);
165695d67482SBill Paul 			error = ENXIO;
165795d67482SBill Paul 			goto fail;
165895d67482SBill Paul 		}
165995d67482SBill Paul 	}
166095d67482SBill Paul 
166195d67482SBill Paul 	/*
1662e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
1663e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
1664e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
1665e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
1666e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
1667e255b776SJohn Polstra 	 * payloads by copying the received packets.
1668e255b776SJohn Polstra 	 */
1669e255b776SJohn Polstra 	switch (sc->bge_asicrev) {
1670e255b776SJohn Polstra 	case BGE_ASICREV_BCM5701_A0:
1671e255b776SJohn Polstra 	case BGE_ASICREV_BCM5701_B0:
1672e255b776SJohn Polstra 	case BGE_ASICREV_BCM5701_B2:
1673e255b776SJohn Polstra 	case BGE_ASICREV_BCM5701_B5:
1674e255b776SJohn Polstra 		/* If in PCI-X mode, work around the alignment bug. */
1675e255b776SJohn Polstra 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
1676e255b776SJohn Polstra 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
1677e255b776SJohn Polstra 		    BGE_PCISTATE_PCI_BUSSPEED)
1678e255b776SJohn Polstra 			sc->bge_rx_alignment_bug = 1;
1679e255b776SJohn Polstra 		break;
1680e255b776SJohn Polstra 	}
1681e255b776SJohn Polstra 
1682e255b776SJohn Polstra 	/*
168395d67482SBill Paul 	 * Call MI attach routine.
168495d67482SBill Paul 	 */
168595d67482SBill Paul 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
168695d67482SBill Paul 	callout_handle_init(&sc->bge_stat_ch);
168795d67482SBill Paul 
168895d67482SBill Paul fail:
168995d67482SBill Paul 	splx(s);
169095d67482SBill Paul 
169195d67482SBill Paul 	return(error);
169295d67482SBill Paul }
169395d67482SBill Paul 
169495d67482SBill Paul static int
169595d67482SBill Paul bge_detach(dev)
169695d67482SBill Paul 	device_t dev;
169795d67482SBill Paul {
169895d67482SBill Paul 	struct bge_softc *sc;
169995d67482SBill Paul 	struct ifnet *ifp;
170095d67482SBill Paul 	int s;
170195d67482SBill Paul 
170295d67482SBill Paul 	s = splimp();
170395d67482SBill Paul 
170495d67482SBill Paul 	sc = device_get_softc(dev);
170595d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
170695d67482SBill Paul 
170795d67482SBill Paul 	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
170895d67482SBill Paul 	bge_stop(sc);
170995d67482SBill Paul 	bge_reset(sc);
171095d67482SBill Paul 
171195d67482SBill Paul 	if (sc->bge_tbi) {
171295d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
171395d67482SBill Paul 	} else {
171495d67482SBill Paul 		bus_generic_detach(dev);
171595d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
171695d67482SBill Paul 	}
171795d67482SBill Paul 
171895d67482SBill Paul 	bge_release_resources(sc);
171995d67482SBill Paul 	bge_free_jumbo_mem(sc);
172095d67482SBill Paul 
172195d67482SBill Paul 	splx(s);
172295d67482SBill Paul 
172395d67482SBill Paul 	return(0);
172495d67482SBill Paul }
172595d67482SBill Paul 
172695d67482SBill Paul static void
172795d67482SBill Paul bge_release_resources(sc)
172895d67482SBill Paul 	struct bge_softc *sc;
172995d67482SBill Paul {
173095d67482SBill Paul         device_t dev;
173195d67482SBill Paul 
173295d67482SBill Paul         dev = sc->bge_dev;
173395d67482SBill Paul 
173495d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
173595d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
173695d67482SBill Paul 
173795d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
173895d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
173995d67482SBill Paul 
174095d67482SBill Paul         if (sc->bge_intrhand != NULL)
174195d67482SBill Paul                 bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
174295d67482SBill Paul 
174395d67482SBill Paul         if (sc->bge_irq != NULL)
174495d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
174595d67482SBill Paul 
174695d67482SBill Paul         if (sc->bge_res != NULL)
174795d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
174895d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
174995d67482SBill Paul 
175095d67482SBill Paul         if (sc->bge_rdata != NULL)
175195d67482SBill Paul 		contigfree(sc->bge_rdata,
175295d67482SBill Paul 		    sizeof(struct bge_ring_data), M_DEVBUF);
175395d67482SBill Paul 
175495d67482SBill Paul         return;
175595d67482SBill Paul }
175695d67482SBill Paul 
175795d67482SBill Paul static void
175895d67482SBill Paul bge_reset(sc)
175995d67482SBill Paul 	struct bge_softc *sc;
176095d67482SBill Paul {
176195d67482SBill Paul 	device_t dev;
176295d67482SBill Paul 	u_int32_t cachesize, command, pcistate;
176395d67482SBill Paul 	int i, val = 0;
176495d67482SBill Paul 
176595d67482SBill Paul 	dev = sc->bge_dev;
176695d67482SBill Paul 
176795d67482SBill Paul 	/* Save some important PCI state. */
176895d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
176995d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
177095d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
177195d67482SBill Paul 
177295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
177395d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
177495d67482SBill Paul 	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
177595d67482SBill Paul 
177695d67482SBill Paul 	/* Issue global reset */
177795d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG,
177895d67482SBill Paul 	    BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1));
177995d67482SBill Paul 
178095d67482SBill Paul 	DELAY(1000);
178195d67482SBill Paul 
178295d67482SBill Paul 	/* Reset some of the PCI state that got zapped by reset */
178395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
178495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
178595d67482SBill Paul 	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
178695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
178795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
178895d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
178995d67482SBill Paul 
179095d67482SBill Paul 	/*
179195d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
179295d67482SBill Paul 	 * general communications memory at 0xB50.
179395d67482SBill Paul 	 */
179495d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
179595d67482SBill Paul 	/*
179695d67482SBill Paul 	 * Poll the value location we just wrote until
179795d67482SBill Paul 	 * we see the 1's complement of the magic number.
179895d67482SBill Paul 	 * This indicates that the firmware initialization
179995d67482SBill Paul 	 * is complete.
180095d67482SBill Paul 	 */
180195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
180295d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
180395d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
180495d67482SBill Paul 			break;
180595d67482SBill Paul 		DELAY(10);
180695d67482SBill Paul 	}
180795d67482SBill Paul 
180895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
180995d67482SBill Paul 		printf("bge%d: firmware handshake timed out\n", sc->bge_unit);
181095d67482SBill Paul 		return;
181195d67482SBill Paul 	}
181295d67482SBill Paul 
181395d67482SBill Paul 	/*
181495d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
181595d67482SBill Paul 	 * return to its original pre-reset state. This is a
181695d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
181795d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
181895d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
181995d67482SBill Paul 	 * results.
182095d67482SBill Paul 	 */
182195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
182295d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
182395d67482SBill Paul 			break;
182495d67482SBill Paul 		DELAY(10);
182595d67482SBill Paul 	}
182695d67482SBill Paul 
182795d67482SBill Paul 	/* Enable memory arbiter. */
182895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
182995d67482SBill Paul 
183095d67482SBill Paul 	/* Fix up byte swapping */
183195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_BYTESWAP_NONFRAME|
183295d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
183395d67482SBill Paul 
183495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
183595d67482SBill Paul 
183695d67482SBill Paul 	DELAY(10000);
183795d67482SBill Paul 
183895d67482SBill Paul 	return;
183995d67482SBill Paul }
184095d67482SBill Paul 
184195d67482SBill Paul /*
184295d67482SBill Paul  * Frame reception handling. This is called if there's a frame
184395d67482SBill Paul  * on the receive return list.
184495d67482SBill Paul  *
184595d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
184695d67482SBill Paul  * 1) the frame is from the jumbo recieve ring
184795d67482SBill Paul  * 2) the frame is from the standard receive ring
184895d67482SBill Paul  */
184995d67482SBill Paul 
185095d67482SBill Paul static void
185195d67482SBill Paul bge_rxeof(sc)
185295d67482SBill Paul 	struct bge_softc *sc;
185395d67482SBill Paul {
185495d67482SBill Paul 	struct ifnet *ifp;
185595d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
185695d67482SBill Paul 
185795d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
185895d67482SBill Paul 
185995d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
186095d67482SBill Paul 	    sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx) {
186195d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
186295d67482SBill Paul 		u_int32_t		rxidx;
186395d67482SBill Paul 		struct ether_header	*eh;
186495d67482SBill Paul 		struct mbuf		*m = NULL;
186595d67482SBill Paul 		u_int16_t		vlan_tag = 0;
186695d67482SBill Paul 		int			have_tag = 0;
186795d67482SBill Paul 
186895d67482SBill Paul 		cur_rx =
186995d67482SBill Paul 	    &sc->bge_rdata->bge_rx_return_ring[sc->bge_rx_saved_considx];
187095d67482SBill Paul 
187195d67482SBill Paul 		rxidx = cur_rx->bge_idx;
187295d67482SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, BGE_RETURN_RING_CNT);
187395d67482SBill Paul 
187495d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
187595d67482SBill Paul 			have_tag = 1;
187695d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
187795d67482SBill Paul 		}
187895d67482SBill Paul 
187995d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
188095d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
188195d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
188295d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
188395d67482SBill Paul 			jumbocnt++;
188495d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
188595d67482SBill Paul 				ifp->if_ierrors++;
188695d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
188795d67482SBill Paul 				continue;
188895d67482SBill Paul 			}
188995d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
189095d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
189195d67482SBill Paul 				ifp->if_ierrors++;
189295d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
189395d67482SBill Paul 				continue;
189495d67482SBill Paul 			}
189595d67482SBill Paul 		} else {
189695d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
189795d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
189895d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
189995d67482SBill Paul 			stdcnt++;
190095d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
190195d67482SBill Paul 				ifp->if_ierrors++;
190295d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
190395d67482SBill Paul 				continue;
190495d67482SBill Paul 			}
190595d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
190695d67482SBill Paul 			    NULL) == ENOBUFS) {
190795d67482SBill Paul 				ifp->if_ierrors++;
190895d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
190995d67482SBill Paul 				continue;
191095d67482SBill Paul 			}
191195d67482SBill Paul 		}
191295d67482SBill Paul 
191395d67482SBill Paul 		ifp->if_ipackets++;
1914e255b776SJohn Polstra #ifndef __i386__
1915e255b776SJohn Polstra 		/*
1916e255b776SJohn Polstra 		 * The i386 allows unaligned accesses, but for other
1917e255b776SJohn Polstra 		 * platforms we must make sure the payload is aligned.
1918e255b776SJohn Polstra 		 */
1919e255b776SJohn Polstra 		if (sc->bge_rx_alignment_bug) {
1920e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
1921e255b776SJohn Polstra 			    cur_rx->bge_len);
1922e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
1923e255b776SJohn Polstra 		}
1924e255b776SJohn Polstra #endif
192595d67482SBill Paul 		eh = mtod(m, struct ether_header *);
192695d67482SBill Paul 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len;
192795d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
192895d67482SBill Paul 
192995d67482SBill Paul 		/* Remove header from mbuf and pass it on. */
193095d67482SBill Paul 		m_adj(m, sizeof(struct ether_header));
193195d67482SBill Paul 
1932eb48892eSDavid Greenman #if 0 /* currently broken for some packets, possibly related to TCP options */
193395d67482SBill Paul 		if (ifp->if_hwassist) {
193495d67482SBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
193595d67482SBill Paul 			if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
193695d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
193795d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
193895d67482SBill Paul 				m->m_pkthdr.csum_data =
193995d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
19400189c944SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
194195d67482SBill Paul 			}
194295d67482SBill Paul 		}
1943eb48892eSDavid Greenman #endif
194495d67482SBill Paul 
194595d67482SBill Paul 		/*
194695d67482SBill Paul 		 * If we received a packet with a vlan tag, pass it
194795d67482SBill Paul 		 * to vlan_input() instead of ether_input().
194895d67482SBill Paul 		 */
194995d67482SBill Paul 		if (have_tag) {
1950437e48e9SBrooks Davis 			VLAN_INPUT_TAG(eh, m, vlan_tag);
195195d67482SBill Paul 			have_tag = vlan_tag = 0;
195295d67482SBill Paul 			continue;
195395d67482SBill Paul 		}
195495d67482SBill Paul 
195595d67482SBill Paul 		ether_input(ifp, eh, m);
195695d67482SBill Paul 	}
195795d67482SBill Paul 
195895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
195995d67482SBill Paul 	if (stdcnt)
196095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
196195d67482SBill Paul 	if (jumbocnt)
196295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
196395d67482SBill Paul 
196495d67482SBill Paul 	return;
196595d67482SBill Paul }
196695d67482SBill Paul 
196795d67482SBill Paul static void
196895d67482SBill Paul bge_txeof(sc)
196995d67482SBill Paul 	struct bge_softc *sc;
197095d67482SBill Paul {
197195d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
197295d67482SBill Paul 	struct ifnet *ifp;
197395d67482SBill Paul 
197495d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
197595d67482SBill Paul 
197695d67482SBill Paul 	/*
197795d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
197895d67482SBill Paul 	 * frames that have been sent.
197995d67482SBill Paul 	 */
198095d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
198195d67482SBill Paul 	    sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx) {
198295d67482SBill Paul 		u_int32_t		idx = 0;
198395d67482SBill Paul 
198495d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
198595d67482SBill Paul 		cur_tx = &sc->bge_rdata->bge_tx_ring[idx];
198695d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
198795d67482SBill Paul 			ifp->if_opackets++;
198895d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
198995d67482SBill Paul 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
199095d67482SBill Paul 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
199195d67482SBill Paul 		}
199295d67482SBill Paul 		sc->bge_txcnt--;
199395d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
199495d67482SBill Paul 		ifp->if_timer = 0;
199595d67482SBill Paul 	}
199695d67482SBill Paul 
199795d67482SBill Paul 	if (cur_tx != NULL)
199895d67482SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
199995d67482SBill Paul 
200095d67482SBill Paul 	return;
200195d67482SBill Paul }
200295d67482SBill Paul 
200395d67482SBill Paul static void
200495d67482SBill Paul bge_intr(xsc)
200595d67482SBill Paul 	void *xsc;
200695d67482SBill Paul {
200795d67482SBill Paul 	struct bge_softc *sc;
200895d67482SBill Paul 	struct ifnet *ifp;
200995d67482SBill Paul 
201095d67482SBill Paul 	sc = xsc;
201195d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
201295d67482SBill Paul 
201395d67482SBill Paul #ifdef notdef
201495d67482SBill Paul 	/* Avoid this for now -- checking this register is expensive. */
201595d67482SBill Paul 	/* Make sure this is really our interrupt. */
201695d67482SBill Paul 	if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
201795d67482SBill Paul 		return;
201895d67482SBill Paul #endif
201995d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
202095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
202195d67482SBill Paul 
2022a1d52896SBill Paul 	/*
2023a1d52896SBill Paul 	 * Process link state changes.
2024a1d52896SBill Paul 	 * Grrr. The link status word in the status block does
2025a1d52896SBill Paul 	 * not work correctly on the BCM5700 rev AX and BX chips,
2026a1d52896SBill Paul 	 * according to all avaibable information. Hence, we have
2027a1d52896SBill Paul 	 * to enable MII interrupts in order to properly obtain
2028a1d52896SBill Paul 	 * async link changes. Unfortunately, this also means that
2029a1d52896SBill Paul 	 * we have to read the MAC status register to detect link
2030a1d52896SBill Paul 	 * changes, thereby adding an additional register access to
2031a1d52896SBill Paul 	 * the interrupt handler.
2032a1d52896SBill Paul 	 */
2033a1d52896SBill Paul 
2034a1d52896SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700) {
2035a1d52896SBill Paul 		u_int32_t		status;
2036a1d52896SBill Paul 
2037a1d52896SBill Paul 		status = CSR_READ_4(sc, BGE_MAC_STS);
2038a1d52896SBill Paul 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
203995d67482SBill Paul 			sc->bge_link = 0;
204095d67482SBill Paul 			untimeout(bge_tick, sc, sc->bge_stat_ch);
204195d67482SBill Paul 			bge_tick(sc);
2042a1d52896SBill Paul 			/* Clear the interrupt */
2043a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2044a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2045a1d52896SBill Paul 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
2046a1d52896SBill Paul 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
2047a1d52896SBill Paul 			    BRGPHY_INTRS);
204898b28ee5SBill Paul 		}
2049a1d52896SBill Paul 	} else {
2050a1d52896SBill Paul 		if (sc->bge_rdata->bge_status_block.bge_status &
2051a1d52896SBill Paul 		    BGE_STATFLAG_LINKSTATE_CHANGED) {
2052a1d52896SBill Paul 			sc->bge_link = 0;
2053a1d52896SBill Paul 			untimeout(bge_tick, sc, sc->bge_stat_ch);
2054a1d52896SBill Paul 			bge_tick(sc);
2055a1d52896SBill Paul 			/* Clear the interrupt */
205695d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
205795d67482SBill Paul 			    BGE_MACSTAT_CFG_CHANGED);
2058a1d52896SBill Paul 		}
205995d67482SBill Paul 	}
206095d67482SBill Paul 
206195d67482SBill Paul 	if (ifp->if_flags & IFF_RUNNING) {
206295d67482SBill Paul 		/* Check RX return ring producer/consumer */
206395d67482SBill Paul 		bge_rxeof(sc);
206495d67482SBill Paul 
206595d67482SBill Paul 		/* Check TX ring producer/consumer */
206695d67482SBill Paul 		bge_txeof(sc);
206795d67482SBill Paul 	}
206895d67482SBill Paul 
206995d67482SBill Paul 	bge_handle_events(sc);
207095d67482SBill Paul 
207195d67482SBill Paul 	/* Re-enable interrupts. */
207295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
207395d67482SBill Paul 
207495d67482SBill Paul 	if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
207595d67482SBill Paul 		bge_start(ifp);
207695d67482SBill Paul 
207795d67482SBill Paul 	return;
207895d67482SBill Paul }
207995d67482SBill Paul 
208095d67482SBill Paul static void
208195d67482SBill Paul bge_tick(xsc)
208295d67482SBill Paul 	void *xsc;
208395d67482SBill Paul {
208495d67482SBill Paul 	struct bge_softc *sc;
208595d67482SBill Paul 	struct mii_data *mii = NULL;
208695d67482SBill Paul 	struct ifmedia *ifm = NULL;
208795d67482SBill Paul 	struct ifnet *ifp;
208895d67482SBill Paul 	int s;
208995d67482SBill Paul 
209095d67482SBill Paul 	sc = xsc;
209195d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
209295d67482SBill Paul 
209395d67482SBill Paul 	s = splimp();
209495d67482SBill Paul 
209595d67482SBill Paul 	bge_stats_update(sc);
209695d67482SBill Paul 	sc->bge_stat_ch = timeout(bge_tick, sc, hz);
20971c33cc4bSJohn Polstra 	if (sc->bge_link) {
20981c33cc4bSJohn Polstra 		splx(s);
209995d67482SBill Paul 		return;
21001c33cc4bSJohn Polstra 	}
210195d67482SBill Paul 
210295d67482SBill Paul 	if (sc->bge_tbi) {
210395d67482SBill Paul 		ifm = &sc->bge_ifmedia;
210495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
210595d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED) {
210695d67482SBill Paul 			sc->bge_link++;
210795d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
210895d67482SBill Paul 			printf("bge%d: gigabit link up\n", sc->bge_unit);
210995d67482SBill Paul 			if (ifp->if_snd.ifq_head != NULL)
211095d67482SBill Paul 				bge_start(ifp);
211195d67482SBill Paul 		}
21121c33cc4bSJohn Polstra 		splx(s);
211395d67482SBill Paul 		return;
211495d67482SBill Paul 	}
211595d67482SBill Paul 
211695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
211795d67482SBill Paul 	mii_tick(mii);
211895d67482SBill Paul 
2119b2561871SJonathan Lemon 	if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE &&
212095d67482SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
212195d67482SBill Paul 		sc->bge_link++;
2122b418ad5cSPoul-Henning Kamp 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
212395d67482SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
212495d67482SBill Paul 			printf("bge%d: gigabit link up\n",
212595d67482SBill Paul 			   sc->bge_unit);
212695d67482SBill Paul 		if (ifp->if_snd.ifq_head != NULL)
212795d67482SBill Paul 			bge_start(ifp);
212895d67482SBill Paul 	}
212995d67482SBill Paul 
213095d67482SBill Paul 	splx(s);
213195d67482SBill Paul 
213295d67482SBill Paul 	return;
213395d67482SBill Paul }
213495d67482SBill Paul 
213595d67482SBill Paul static void
213695d67482SBill Paul bge_stats_update(sc)
213795d67482SBill Paul 	struct bge_softc *sc;
213895d67482SBill Paul {
213995d67482SBill Paul 	struct ifnet *ifp;
214095d67482SBill Paul 	struct bge_stats *stats;
214195d67482SBill Paul 
214295d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
214395d67482SBill Paul 
214495d67482SBill Paul 	stats = (struct bge_stats *)(sc->bge_vhandle +
214595d67482SBill Paul 	    BGE_MEMWIN_START + BGE_STATS_BLOCK);
214695d67482SBill Paul 
214795d67482SBill Paul 	ifp->if_collisions +=
214895d67482SBill Paul 	   (stats->dot3StatsSingleCollisionFrames.bge_addr_lo +
214995d67482SBill Paul 	   stats->dot3StatsMultipleCollisionFrames.bge_addr_lo +
215095d67482SBill Paul 	   stats->dot3StatsExcessiveCollisions.bge_addr_lo +
215195d67482SBill Paul 	   stats->dot3StatsLateCollisions.bge_addr_lo) -
215295d67482SBill Paul 	   ifp->if_collisions;
215395d67482SBill Paul 
215495d67482SBill Paul #ifdef notdef
215595d67482SBill Paul 	ifp->if_collisions +=
215695d67482SBill Paul 	   (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
215795d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
215895d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
215995d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
216095d67482SBill Paul 	   ifp->if_collisions;
216195d67482SBill Paul #endif
216295d67482SBill Paul 
216395d67482SBill Paul 	return;
216495d67482SBill Paul }
216595d67482SBill Paul 
216695d67482SBill Paul /*
216795d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
216895d67482SBill Paul  * pointers to descriptors.
216995d67482SBill Paul  */
217095d67482SBill Paul static int
217195d67482SBill Paul bge_encap(sc, m_head, txidx)
217295d67482SBill Paul 	struct bge_softc *sc;
217395d67482SBill Paul 	struct mbuf *m_head;
217495d67482SBill Paul 	u_int32_t *txidx;
217595d67482SBill Paul {
217695d67482SBill Paul 	struct bge_tx_bd	*f = NULL;
217795d67482SBill Paul 	struct mbuf		*m;
217895d67482SBill Paul 	u_int32_t		frag, cur, cnt = 0;
217995d67482SBill Paul 	u_int16_t		csum_flags = 0;
218095d67482SBill Paul 	struct ifvlan		*ifv = NULL;
218195d67482SBill Paul 
218295d67482SBill Paul 	if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) &&
218395d67482SBill Paul 	    m_head->m_pkthdr.rcvif != NULL &&
218495d67482SBill Paul 	    m_head->m_pkthdr.rcvif->if_type == IFT_L2VLAN)
218595d67482SBill Paul 		ifv = m_head->m_pkthdr.rcvif->if_softc;
218695d67482SBill Paul 
218795d67482SBill Paul 	m = m_head;
218895d67482SBill Paul 	cur = frag = *txidx;
218995d67482SBill Paul 
219095d67482SBill Paul 	if (m_head->m_pkthdr.csum_flags) {
219195d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
219295d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
219395d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
219495d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
219595d67482SBill Paul 		if (m_head->m_flags & M_LASTFRAG)
219695d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
219795d67482SBill Paul 		else if (m_head->m_flags & M_FRAG)
219895d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
219995d67482SBill Paul 	}
220095d67482SBill Paul 
220195d67482SBill Paul 	/*
220295d67482SBill Paul  	 * Start packing the mbufs in this chain into
220395d67482SBill Paul 	 * the fragment pointers. Stop when we run out
220495d67482SBill Paul  	 * of fragments or hit the end of the mbuf chain.
220595d67482SBill Paul 	 */
220695d67482SBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
220795d67482SBill Paul 		if (m->m_len != 0) {
220895d67482SBill Paul 			f = &sc->bge_rdata->bge_tx_ring[frag];
220995d67482SBill Paul 			if (sc->bge_cdata.bge_tx_chain[frag] != NULL)
221095d67482SBill Paul 				break;
221195d67482SBill Paul 			BGE_HOSTADDR(f->bge_addr) =
221295d67482SBill Paul 			   vtophys(mtod(m, vm_offset_t));
221395d67482SBill Paul 			f->bge_len = m->m_len;
221495d67482SBill Paul 			f->bge_flags = csum_flags;
221595d67482SBill Paul 			if (ifv != NULL) {
221695d67482SBill Paul 				f->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
221795d67482SBill Paul 				f->bge_vlan_tag = ifv->ifv_tag;
221895d67482SBill Paul 			} else {
221995d67482SBill Paul 				f->bge_vlan_tag = 0;
222095d67482SBill Paul 			}
222195d67482SBill Paul 			/*
222295d67482SBill Paul 			 * Sanity check: avoid coming within 16 descriptors
222395d67482SBill Paul 			 * of the end of the ring.
222495d67482SBill Paul 			 */
222595d67482SBill Paul 			if ((BGE_TX_RING_CNT - (sc->bge_txcnt + cnt)) < 16)
222695d67482SBill Paul 				return(ENOBUFS);
222795d67482SBill Paul 			cur = frag;
222895d67482SBill Paul 			BGE_INC(frag, BGE_TX_RING_CNT);
222995d67482SBill Paul 			cnt++;
223095d67482SBill Paul 		}
223195d67482SBill Paul 	}
223295d67482SBill Paul 
223395d67482SBill Paul 	if (m != NULL)
223495d67482SBill Paul 		return(ENOBUFS);
223595d67482SBill Paul 
223695d67482SBill Paul 	if (frag == sc->bge_tx_saved_considx)
223795d67482SBill Paul 		return(ENOBUFS);
223895d67482SBill Paul 
223995d67482SBill Paul 	sc->bge_rdata->bge_tx_ring[cur].bge_flags |= BGE_TXBDFLAG_END;
224095d67482SBill Paul 	sc->bge_cdata.bge_tx_chain[cur] = m_head;
224195d67482SBill Paul 	sc->bge_txcnt += cnt;
224295d67482SBill Paul 
224395d67482SBill Paul 	*txidx = frag;
224495d67482SBill Paul 
224595d67482SBill Paul 	return(0);
224695d67482SBill Paul }
224795d67482SBill Paul 
224895d67482SBill Paul /*
224995d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
225095d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
225195d67482SBill Paul  */
225295d67482SBill Paul static void
225395d67482SBill Paul bge_start(ifp)
225495d67482SBill Paul 	struct ifnet *ifp;
225595d67482SBill Paul {
225695d67482SBill Paul 	struct bge_softc *sc;
225795d67482SBill Paul 	struct mbuf *m_head = NULL;
225895d67482SBill Paul 	u_int32_t prodidx = 0;
225995d67482SBill Paul 
226095d67482SBill Paul 	sc = ifp->if_softc;
226195d67482SBill Paul 
226295d67482SBill Paul 	if (!sc->bge_link && ifp->if_snd.ifq_len < 10)
226395d67482SBill Paul 		return;
226495d67482SBill Paul 
226595d67482SBill Paul 	prodidx = CSR_READ_4(sc, BGE_MBX_TX_HOST_PROD0_LO);
226695d67482SBill Paul 
226795d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
226895d67482SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
226995d67482SBill Paul 		if (m_head == NULL)
227095d67482SBill Paul 			break;
227195d67482SBill Paul 
227295d67482SBill Paul 		/*
227395d67482SBill Paul 		 * XXX
227495d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
227595d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
227695d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
227795d67482SBill Paul 		 * chain at once.
227895d67482SBill Paul 		 * (paranoia -- may not actually be needed)
227995d67482SBill Paul 		 */
228095d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
228195d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
228295d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
228395d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
228495d67482SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
228595d67482SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
228695d67482SBill Paul 				break;
228795d67482SBill Paul 			}
228895d67482SBill Paul 		}
228995d67482SBill Paul 
229095d67482SBill Paul 		/*
229195d67482SBill Paul 		 * Pack the data into the transmit ring. If we
229295d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
229395d67482SBill Paul 		 * for the NIC to drain the ring.
229495d67482SBill Paul 		 */
229595d67482SBill Paul 		if (bge_encap(sc, m_head, &prodidx)) {
229695d67482SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
229795d67482SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
229895d67482SBill Paul 			break;
229995d67482SBill Paul 		}
230095d67482SBill Paul 
230195d67482SBill Paul 		/*
230295d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
230395d67482SBill Paul 		 * to him.
230495d67482SBill Paul 		 */
230595d67482SBill Paul 		if (ifp->if_bpf)
230695d67482SBill Paul 			bpf_mtap(ifp, m_head);
230795d67482SBill Paul 	}
230895d67482SBill Paul 
230995d67482SBill Paul 	/* Transmit */
231095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
231195d67482SBill Paul 
231295d67482SBill Paul 	/*
231395d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
231495d67482SBill Paul 	 */
231595d67482SBill Paul 	ifp->if_timer = 5;
231695d67482SBill Paul 
231795d67482SBill Paul 	return;
231895d67482SBill Paul }
231995d67482SBill Paul 
232095d67482SBill Paul /*
232195d67482SBill Paul  * If we have a BCM5400 or BCM5401 PHY, we need to properly
232295d67482SBill Paul  * program its internal DSP. Failing to do this can result in
232395d67482SBill Paul  * massive packet loss at 1Gb speeds.
232495d67482SBill Paul  */
232595d67482SBill Paul static void
232695d67482SBill Paul bge_phy_hack(sc)
232795d67482SBill Paul 	struct bge_softc *sc;
232895d67482SBill Paul {
232995d67482SBill Paul 	struct bge_bcom_hack bhack[] = {
233095d67482SBill Paul 	{ BRGPHY_MII_AUXCTL, 0x4C20 },
233195d67482SBill Paul 	{ BRGPHY_MII_DSP_ADDR_REG, 0x0012 },
233295d67482SBill Paul 	{ BRGPHY_MII_DSP_RW_PORT, 0x1804 },
233395d67482SBill Paul 	{ BRGPHY_MII_DSP_ADDR_REG, 0x0013 },
233495d67482SBill Paul 	{ BRGPHY_MII_DSP_RW_PORT, 0x1204 },
233595d67482SBill Paul 	{ BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
233695d67482SBill Paul 	{ BRGPHY_MII_DSP_RW_PORT, 0x0132 },
233795d67482SBill Paul 	{ BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
233895d67482SBill Paul 	{ BRGPHY_MII_DSP_RW_PORT, 0x0232 },
233995d67482SBill Paul 	{ BRGPHY_MII_DSP_ADDR_REG, 0x201F },
234095d67482SBill Paul 	{ BRGPHY_MII_DSP_RW_PORT, 0x0A20 },
234195d67482SBill Paul 	{ 0, 0 } };
234295d67482SBill Paul 	u_int16_t vid, did;
234395d67482SBill Paul 	int i;
234495d67482SBill Paul 
234595d67482SBill Paul 	vid = bge_miibus_readreg(sc->bge_dev, 1, MII_PHYIDR1);
234695d67482SBill Paul 	did = bge_miibus_readreg(sc->bge_dev, 1, MII_PHYIDR2);
234795d67482SBill Paul 
234895d67482SBill Paul 	if (MII_OUI(vid, did) == MII_OUI_xxBROADCOM &&
234995d67482SBill Paul 	    (MII_MODEL(did) == MII_MODEL_xxBROADCOM_BCM5400 ||
235095d67482SBill Paul 	    MII_MODEL(did) == MII_MODEL_xxBROADCOM_BCM5401)) {
235195d67482SBill Paul 		i = 0;
235295d67482SBill Paul 		while(bhack[i].reg) {
235395d67482SBill Paul 			bge_miibus_writereg(sc->bge_dev, 1, bhack[i].reg,
235495d67482SBill Paul 			    bhack[i].val);
235595d67482SBill Paul 			i++;
235695d67482SBill Paul 		}
235795d67482SBill Paul 	}
235895d67482SBill Paul 
235995d67482SBill Paul 	return;
236095d67482SBill Paul }
236195d67482SBill Paul 
236295d67482SBill Paul static void
236395d67482SBill Paul bge_init(xsc)
236495d67482SBill Paul 	void *xsc;
236595d67482SBill Paul {
236695d67482SBill Paul 	struct bge_softc *sc = xsc;
236795d67482SBill Paul 	struct ifnet *ifp;
236895d67482SBill Paul 	u_int16_t *m;
236995d67482SBill Paul         int s;
237095d67482SBill Paul 
237195d67482SBill Paul 	s = splimp();
237295d67482SBill Paul 
237395d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
237495d67482SBill Paul 
237544081f9bSMark Peek 	if (ifp->if_flags & IFF_RUNNING) {
237644081f9bSMark Peek 		splx(s);
237795d67482SBill Paul 		return;
237844081f9bSMark Peek 	}
237995d67482SBill Paul 
238095d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
238195d67482SBill Paul 	bge_stop(sc);
238295d67482SBill Paul 	bge_reset(sc);
238395d67482SBill Paul 	bge_chipinit(sc);
238495d67482SBill Paul 
238595d67482SBill Paul 	/*
238695d67482SBill Paul 	 * Init the various state machines, ring
238795d67482SBill Paul 	 * control blocks and firmware.
238895d67482SBill Paul 	 */
238995d67482SBill Paul 	if (bge_blockinit(sc)) {
239095d67482SBill Paul 		printf("bge%d: initialization failure\n", sc->bge_unit);
239195d67482SBill Paul 		splx(s);
239295d67482SBill Paul 		return;
239395d67482SBill Paul 	}
239495d67482SBill Paul 
239595d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
239695d67482SBill Paul 
239795d67482SBill Paul 	/* Specify MTU. */
239895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
239995d67482SBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN);
240095d67482SBill Paul 
240195d67482SBill Paul 	/* Load our MAC address. */
240295d67482SBill Paul 	m = (u_int16_t *)&sc->arpcom.ac_enaddr[0];
240395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
240495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
240595d67482SBill Paul 
240695d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
240795d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
240895d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
240995d67482SBill Paul 	} else {
241095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
241195d67482SBill Paul 	}
241295d67482SBill Paul 
241395d67482SBill Paul 	/* Program multicast filter. */
241495d67482SBill Paul 	bge_setmulti(sc);
241595d67482SBill Paul 
241695d67482SBill Paul 	/* Init RX ring. */
241795d67482SBill Paul 	bge_init_rx_ring_std(sc);
241895d67482SBill Paul 
241995d67482SBill Paul 	/* Init jumbo RX ring. */
242095d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
242195d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
242295d67482SBill Paul 
242395d67482SBill Paul 	/* Init our RX return ring index */
242495d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
242595d67482SBill Paul 
242695d67482SBill Paul 	/* Init TX ring. */
242795d67482SBill Paul 	bge_init_tx_ring(sc);
242895d67482SBill Paul 
242995d67482SBill Paul 	/* Turn on transmitter */
243095d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
243195d67482SBill Paul 
243295d67482SBill Paul 	/* Turn on receiver */
243395d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
243495d67482SBill Paul 
243595d67482SBill Paul 	/* Tell firmware we're alive. */
243695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
243795d67482SBill Paul 
243895d67482SBill Paul 	/* Enable host interrupts. */
243995d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
244095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
244195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
244295d67482SBill Paul 
244395d67482SBill Paul 	bge_ifmedia_upd(ifp);
244495d67482SBill Paul 
244595d67482SBill Paul 	ifp->if_flags |= IFF_RUNNING;
244695d67482SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
244795d67482SBill Paul 
244895d67482SBill Paul 	splx(s);
244995d67482SBill Paul 
245095d67482SBill Paul 	sc->bge_stat_ch = timeout(bge_tick, sc, hz);
245195d67482SBill Paul 
245295d67482SBill Paul 	return;
245395d67482SBill Paul }
245495d67482SBill Paul 
245595d67482SBill Paul /*
245695d67482SBill Paul  * Set media options.
245795d67482SBill Paul  */
245895d67482SBill Paul static int
245995d67482SBill Paul bge_ifmedia_upd(ifp)
246095d67482SBill Paul 	struct ifnet *ifp;
246195d67482SBill Paul {
246295d67482SBill Paul 	struct bge_softc *sc;
246395d67482SBill Paul 	struct mii_data *mii;
246495d67482SBill Paul 	struct ifmedia *ifm;
246595d67482SBill Paul 
246695d67482SBill Paul 	sc = ifp->if_softc;
246795d67482SBill Paul 	ifm = &sc->bge_ifmedia;
246895d67482SBill Paul 
246995d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
247095d67482SBill Paul 	if (sc->bge_tbi) {
247195d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
247295d67482SBill Paul 			return(EINVAL);
247395d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
247495d67482SBill Paul 		case IFM_AUTO:
247595d67482SBill Paul 			break;
247695d67482SBill Paul 		case IFM_1000_SX:
247795d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
247895d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
247995d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
248095d67482SBill Paul 			} else {
248195d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
248295d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
248395d67482SBill Paul 			}
248495d67482SBill Paul 			break;
248595d67482SBill Paul 		default:
248695d67482SBill Paul 			return(EINVAL);
248795d67482SBill Paul 		}
248895d67482SBill Paul 		return(0);
248995d67482SBill Paul 	}
249095d67482SBill Paul 
249195d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
249295d67482SBill Paul 	sc->bge_link = 0;
249395d67482SBill Paul 	if (mii->mii_instance) {
249495d67482SBill Paul 		struct mii_softc *miisc;
249595d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
249695d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
249795d67482SBill Paul 			mii_phy_reset(miisc);
249895d67482SBill Paul 	}
249995d67482SBill Paul 	bge_phy_hack(sc);
250095d67482SBill Paul 	mii_mediachg(mii);
250195d67482SBill Paul 
250295d67482SBill Paul 	return(0);
250395d67482SBill Paul }
250495d67482SBill Paul 
250595d67482SBill Paul /*
250695d67482SBill Paul  * Report current media status.
250795d67482SBill Paul  */
250895d67482SBill Paul static void
250995d67482SBill Paul bge_ifmedia_sts(ifp, ifmr)
251095d67482SBill Paul 	struct ifnet *ifp;
251195d67482SBill Paul 	struct ifmediareq *ifmr;
251295d67482SBill Paul {
251395d67482SBill Paul 	struct bge_softc *sc;
251495d67482SBill Paul 	struct mii_data *mii;
251595d67482SBill Paul 
251695d67482SBill Paul 	sc = ifp->if_softc;
251795d67482SBill Paul 
251895d67482SBill Paul 	if (sc->bge_tbi) {
251995d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
252095d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
252195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
252295d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
252395d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
252495d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
252595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
252695d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
252795d67482SBill Paul 		else
252895d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
252995d67482SBill Paul 		return;
253095d67482SBill Paul 	}
253195d67482SBill Paul 
253295d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
253395d67482SBill Paul 	mii_pollstat(mii);
253495d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
253595d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
253695d67482SBill Paul 
253795d67482SBill Paul 	return;
253895d67482SBill Paul }
253995d67482SBill Paul 
254095d67482SBill Paul static int
254195d67482SBill Paul bge_ioctl(ifp, command, data)
254295d67482SBill Paul 	struct ifnet *ifp;
254395d67482SBill Paul 	u_long command;
254495d67482SBill Paul 	caddr_t data;
254595d67482SBill Paul {
254695d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
254795d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
254895d67482SBill Paul 	int s, mask, error = 0;
254995d67482SBill Paul 	struct mii_data *mii;
255095d67482SBill Paul 
255195d67482SBill Paul 	s = splimp();
255295d67482SBill Paul 
255395d67482SBill Paul 	switch(command) {
255495d67482SBill Paul 	case SIOCSIFADDR:
255595d67482SBill Paul 	case SIOCGIFADDR:
255695d67482SBill Paul 		error = ether_ioctl(ifp, command, data);
255795d67482SBill Paul 		break;
255895d67482SBill Paul 	case SIOCSIFMTU:
255995d67482SBill Paul 		if (ifr->ifr_mtu > BGE_JUMBO_MTU)
256095d67482SBill Paul 			error = EINVAL;
256195d67482SBill Paul 		else {
256295d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
256395d67482SBill Paul 			ifp->if_flags &= ~IFF_RUNNING;
256495d67482SBill Paul 			bge_init(sc);
256595d67482SBill Paul 		}
256695d67482SBill Paul 		break;
256795d67482SBill Paul 	case SIOCSIFFLAGS:
256895d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
256995d67482SBill Paul 			/*
257095d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
257195d67482SBill Paul 			 * then just use the 'set promisc mode' command
257295d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
257395d67482SBill Paul 			 * a full re-init means reloading the firmware and
257495d67482SBill Paul 			 * waiting for it to start up, which may take a
257595d67482SBill Paul 			 * second or two.
257695d67482SBill Paul 			 */
257795d67482SBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
257895d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
257995d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
258095d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
258195d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
258295d67482SBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
258395d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
258495d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
258595d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
258695d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
258795d67482SBill Paul 			} else
258895d67482SBill Paul 				bge_init(sc);
258995d67482SBill Paul 		} else {
259095d67482SBill Paul 			if (ifp->if_flags & IFF_RUNNING) {
259195d67482SBill Paul 				bge_stop(sc);
259295d67482SBill Paul 			}
259395d67482SBill Paul 		}
259495d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
259595d67482SBill Paul 		error = 0;
259695d67482SBill Paul 		break;
259795d67482SBill Paul 	case SIOCADDMULTI:
259895d67482SBill Paul 	case SIOCDELMULTI:
259995d67482SBill Paul 		if (ifp->if_flags & IFF_RUNNING) {
260095d67482SBill Paul 			bge_setmulti(sc);
260195d67482SBill Paul 			error = 0;
260295d67482SBill Paul 		}
260395d67482SBill Paul 		break;
260495d67482SBill Paul 	case SIOCSIFMEDIA:
260595d67482SBill Paul 	case SIOCGIFMEDIA:
260695d67482SBill Paul 		if (sc->bge_tbi) {
260795d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
260895d67482SBill Paul 			    &sc->bge_ifmedia, command);
260995d67482SBill Paul 		} else {
261095d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
261195d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
261295d67482SBill Paul 			    &mii->mii_media, command);
261395d67482SBill Paul 		}
261495d67482SBill Paul 		break;
261595d67482SBill Paul         case SIOCSIFCAP:
261695d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
261795d67482SBill Paul 		if (mask & IFCAP_HWCSUM) {
261895d67482SBill Paul 			if (IFCAP_HWCSUM & ifp->if_capenable)
261995d67482SBill Paul 				ifp->if_capenable &= ~IFCAP_HWCSUM;
262095d67482SBill Paul 			else
262195d67482SBill Paul 				ifp->if_capenable |= IFCAP_HWCSUM;
262295d67482SBill Paul 		}
262395d67482SBill Paul 		error = 0;
262495d67482SBill Paul 		break;
262595d67482SBill Paul 	default:
262695d67482SBill Paul 		error = EINVAL;
262795d67482SBill Paul 		break;
262895d67482SBill Paul 	}
262995d67482SBill Paul 
263095d67482SBill Paul 	(void)splx(s);
263195d67482SBill Paul 
263295d67482SBill Paul 	return(error);
263395d67482SBill Paul }
263495d67482SBill Paul 
263595d67482SBill Paul static void
263695d67482SBill Paul bge_watchdog(ifp)
263795d67482SBill Paul 	struct ifnet *ifp;
263895d67482SBill Paul {
263995d67482SBill Paul 	struct bge_softc *sc;
264095d67482SBill Paul 
264195d67482SBill Paul 	sc = ifp->if_softc;
264295d67482SBill Paul 
264395d67482SBill Paul 	printf("bge%d: watchdog timeout -- resetting\n", sc->bge_unit);
264495d67482SBill Paul 
264595d67482SBill Paul 	ifp->if_flags &= ~IFF_RUNNING;
264695d67482SBill Paul 	bge_init(sc);
264795d67482SBill Paul 
264895d67482SBill Paul 	ifp->if_oerrors++;
264995d67482SBill Paul 
265095d67482SBill Paul 	return;
265195d67482SBill Paul }
265295d67482SBill Paul 
265395d67482SBill Paul /*
265495d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
265595d67482SBill Paul  * RX and TX lists.
265695d67482SBill Paul  */
265795d67482SBill Paul static void
265895d67482SBill Paul bge_stop(sc)
265995d67482SBill Paul 	struct bge_softc *sc;
266095d67482SBill Paul {
266195d67482SBill Paul 	struct ifnet *ifp;
266295d67482SBill Paul 	struct ifmedia_entry *ifm;
266395d67482SBill Paul 	struct mii_data *mii = NULL;
266495d67482SBill Paul 	int mtmp, itmp;
266595d67482SBill Paul 
266695d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
266795d67482SBill Paul 
266895d67482SBill Paul 	if (!sc->bge_tbi)
266995d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
267095d67482SBill Paul 
267195d67482SBill Paul 	untimeout(bge_tick, sc, sc->bge_stat_ch);
267295d67482SBill Paul 
267395d67482SBill Paul 	/*
267495d67482SBill Paul 	 * Disable all of the receiver blocks
267595d67482SBill Paul 	 */
267695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
267795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
267895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
267995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
268095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
268195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
268295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
268395d67482SBill Paul 
268495d67482SBill Paul 	/*
268595d67482SBill Paul 	 * Disable all of the transmit blocks
268695d67482SBill Paul 	 */
268795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
268895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
268995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
269095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
269195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
269295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
269395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
269495d67482SBill Paul 
269595d67482SBill Paul 	/*
269695d67482SBill Paul 	 * Shut down all of the memory managers and related
269795d67482SBill Paul 	 * state machines.
269895d67482SBill Paul 	 */
269995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
270095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
270195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
270295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
270395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
270495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
270595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
270695d67482SBill Paul 
270795d67482SBill Paul 	/* Disable host interrupts. */
270895d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
270995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
271095d67482SBill Paul 
271195d67482SBill Paul 	/*
271295d67482SBill Paul 	 * Tell firmware we're shutting down.
271395d67482SBill Paul 	 */
271495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
271595d67482SBill Paul 
271695d67482SBill Paul 	/* Free the RX lists. */
271795d67482SBill Paul 	bge_free_rx_ring_std(sc);
271895d67482SBill Paul 
271995d67482SBill Paul 	/* Free jumbo RX list. */
272095d67482SBill Paul 	bge_free_rx_ring_jumbo(sc);
272195d67482SBill Paul 
272295d67482SBill Paul 	/* Free TX buffers. */
272395d67482SBill Paul 	bge_free_tx_ring(sc);
272495d67482SBill Paul 
272595d67482SBill Paul 	/*
272695d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
272795d67482SBill Paul 	 * unchanged so that things will be put back to normal when
272895d67482SBill Paul 	 * we bring the interface back up.
272995d67482SBill Paul 	 */
273095d67482SBill Paul 	if (!sc->bge_tbi) {
273195d67482SBill Paul 		itmp = ifp->if_flags;
273295d67482SBill Paul 		ifp->if_flags |= IFF_UP;
273395d67482SBill Paul 		ifm = mii->mii_media.ifm_cur;
273495d67482SBill Paul 		mtmp = ifm->ifm_media;
273595d67482SBill Paul 		ifm->ifm_media = IFM_ETHER|IFM_NONE;
273695d67482SBill Paul 		mii_mediachg(mii);
273795d67482SBill Paul 		ifm->ifm_media = mtmp;
273895d67482SBill Paul 		ifp->if_flags = itmp;
273995d67482SBill Paul 	}
274095d67482SBill Paul 
274195d67482SBill Paul 	sc->bge_link = 0;
274295d67482SBill Paul 
274395d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
274495d67482SBill Paul 
274595d67482SBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
274695d67482SBill Paul 
274795d67482SBill Paul 	return;
274895d67482SBill Paul }
274995d67482SBill Paul 
275095d67482SBill Paul /*
275195d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
275295d67482SBill Paul  * get confused by errant DMAs when rebooting.
275395d67482SBill Paul  */
275495d67482SBill Paul static void
275595d67482SBill Paul bge_shutdown(dev)
275695d67482SBill Paul 	device_t dev;
275795d67482SBill Paul {
275895d67482SBill Paul 	struct bge_softc *sc;
275995d67482SBill Paul 
276095d67482SBill Paul 	sc = device_get_softc(dev);
276195d67482SBill Paul 
276295d67482SBill Paul 	bge_stop(sc);
276395d67482SBill Paul 	bge_reset(sc);
276495d67482SBill Paul 
276595d67482SBill Paul 	return;
276695d67482SBill Paul }
2767