xref: /freebsd/sys/dev/bge/if_bge.c (revision f41ac2be93ccf628f8b0f1a3922a3f436c2b35cc)
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 
3495d67482SBill Paul /*
3595d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3695d67482SBill Paul  *
3795d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
3895d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
3995d67482SBill Paul  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
4095d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4195d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4295d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4395d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4495d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4595d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4695d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
4795d67482SBill Paul  * into the driver.
4895d67482SBill Paul  *
4995d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5095d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5398b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5495d67482SBill Paul  * does not support external SSRAM.
5595d67482SBill Paul  *
5695d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
5795d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6095d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6195d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6295d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6395d67482SBill Paul  * ring.
6495d67482SBill Paul  */
6595d67482SBill Paul 
668368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
678368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$");
688368cf8fSDavid E. O'Brien 
6995d67482SBill Paul #include <sys/param.h>
70f41ac2beSBill Paul #include <sys/endian.h>
7195d67482SBill Paul #include <sys/systm.h>
7295d67482SBill Paul #include <sys/sockio.h>
7395d67482SBill Paul #include <sys/mbuf.h>
7495d67482SBill Paul #include <sys/malloc.h>
7595d67482SBill Paul #include <sys/kernel.h>
7695d67482SBill Paul #include <sys/socket.h>
7795d67482SBill Paul #include <sys/queue.h>
7895d67482SBill Paul 
7995d67482SBill Paul #include <net/if.h>
8095d67482SBill Paul #include <net/if_arp.h>
8195d67482SBill Paul #include <net/ethernet.h>
8295d67482SBill Paul #include <net/if_dl.h>
8395d67482SBill Paul #include <net/if_media.h>
8495d67482SBill Paul 
8595d67482SBill Paul #include <net/bpf.h>
8695d67482SBill Paul 
8795d67482SBill Paul #include <net/if_types.h>
8895d67482SBill Paul #include <net/if_vlan_var.h>
8995d67482SBill Paul 
9095d67482SBill Paul #include <netinet/in_systm.h>
9195d67482SBill Paul #include <netinet/in.h>
9295d67482SBill Paul #include <netinet/ip.h>
9395d67482SBill Paul 
9495d67482SBill Paul #include <vm/vm.h>              /* for vtophys */
9595d67482SBill Paul #include <vm/pmap.h>            /* for vtophys */
9695d67482SBill Paul #include <machine/clock.h>      /* for DELAY */
9795d67482SBill Paul #include <machine/bus_memio.h>
9895d67482SBill Paul #include <machine/bus.h>
9995d67482SBill Paul #include <machine/resource.h>
10095d67482SBill Paul #include <sys/bus.h>
10195d67482SBill Paul #include <sys/rman.h>
10295d67482SBill Paul 
10395d67482SBill Paul #include <dev/mii/mii.h>
10495d67482SBill Paul #include <dev/mii/miivar.h>
1052d3ce713SDavid E. O'Brien #include "miidevs.h"
10695d67482SBill Paul #include <dev/mii/brgphyreg.h>
10795d67482SBill Paul 
10895d67482SBill Paul #include <pci/pcireg.h>
10995d67482SBill Paul #include <pci/pcivar.h>
11095d67482SBill Paul 
11195d67482SBill Paul #include <dev/bge/if_bgereg.h>
11295d67482SBill Paul 
1135ddc5794SJohn Polstra #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
11495d67482SBill Paul 
115f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
116f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
11795d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
11895d67482SBill Paul 
11995d67482SBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
12095d67482SBill Paul #include "miibus_if.h"
12195d67482SBill Paul 
12295d67482SBill Paul /*
12395d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
12495d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
12595d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
12695d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
12795d67482SBill Paul  */
128029e2ee3SJohn Polstra #define BGE_DEVDESC_MAX		64	/* Maximum device description length */
12995d67482SBill Paul 
13095d67482SBill Paul static struct bge_type bge_devs[] = {
13195d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5700,
13295d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
13395d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5701,
13495d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
13595d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5700,
13695d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
13795d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5701,
13895d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
1390434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702,
1400434d1b8SBill Paul 		"Broadcom BCM5702 Gigabit Ethernet" },
14101598b8dSMitsuru IWASAKI 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702X,
14201598b8dSMitsuru IWASAKI 		"Broadcom BCM5702X Gigabit Ethernet" },
1430434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703,
1440434d1b8SBill Paul 		"Broadcom BCM5703 Gigabit Ethernet" },
145b1265c1aSJohn Polstra 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703X,
146b1265c1aSJohn Polstra 		"Broadcom BCM5703X Gigabit Ethernet" },
1476ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704C,
1486ac6d2c8SPaul Saab 		"Broadcom BCM5704C Dual Gigabit Ethernet" },
1496ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704S,
1506ac6d2c8SPaul Saab 		"Broadcom BCM5704S Dual Gigabit Ethernet" },
1510434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705,
1520434d1b8SBill Paul 		"Broadcom BCM5705 Gigabit Ethernet" },
1530434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M,
1540434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
1550434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M_ALT,
1560434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
1570434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5782,
1580434d1b8SBill Paul 		"Broadcom BCM5782 Gigabit Ethernet" },
15995d67482SBill Paul 	{ SK_VENDORID, SK_DEVICEID_ALTIMA,
16095d67482SBill Paul 		"SysKonnect Gigabit Ethernet" },
161586d7c2eSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000,
162586d7c2eSJohn Polstra 		"Altima AC1000 Gigabit Ethernet" },
163470bd96aSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100,
164470bd96aSJohn Polstra 		"Altima AC9100 Gigabit Ethernet" },
16595d67482SBill Paul 	{ 0, 0, NULL }
16695d67482SBill Paul };
16795d67482SBill Paul 
168e51a25f8SAlfred Perlstein static int bge_probe		(device_t);
169e51a25f8SAlfred Perlstein static int bge_attach		(device_t);
170e51a25f8SAlfred Perlstein static int bge_detach		(device_t);
17195d67482SBill Paul static void bge_release_resources
172e51a25f8SAlfred Perlstein 				(struct bge_softc *);
173f41ac2beSBill Paul static void bge_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
174f41ac2beSBill Paul static void bge_dma_map_tx_desc	(void *, bus_dma_segment_t *, int,
175f41ac2beSBill Paul 				    bus_size_t, int);
176f41ac2beSBill Paul static int bge_dma_alloc	(device_t);
177f41ac2beSBill Paul static void bge_dma_free	(struct bge_softc *);
178f41ac2beSBill Paul 
179e51a25f8SAlfred Perlstein static void bge_txeof		(struct bge_softc *);
180e51a25f8SAlfred Perlstein static void bge_rxeof		(struct bge_softc *);
18195d67482SBill Paul 
182e51a25f8SAlfred Perlstein static void bge_tick		(void *);
183e51a25f8SAlfred Perlstein static void bge_stats_update	(struct bge_softc *);
1840434d1b8SBill Paul static void bge_stats_update_regs
1850434d1b8SBill Paul 				(struct bge_softc *);
186e51a25f8SAlfred Perlstein static int bge_encap		(struct bge_softc *, struct mbuf *,
187e51a25f8SAlfred Perlstein 					u_int32_t *);
18895d67482SBill Paul 
189e51a25f8SAlfred Perlstein static void bge_intr		(void *);
190e51a25f8SAlfred Perlstein static void bge_start		(struct ifnet *);
191e51a25f8SAlfred Perlstein static int bge_ioctl		(struct ifnet *, u_long, caddr_t);
192e51a25f8SAlfred Perlstein static void bge_init		(void *);
193e51a25f8SAlfred Perlstein static void bge_stop		(struct bge_softc *);
194e51a25f8SAlfred Perlstein static void bge_watchdog		(struct ifnet *);
195e51a25f8SAlfred Perlstein static void bge_shutdown		(device_t);
196e51a25f8SAlfred Perlstein static int bge_ifmedia_upd	(struct ifnet *);
197e51a25f8SAlfred Perlstein static void bge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
19895d67482SBill Paul 
199e51a25f8SAlfred Perlstein static u_int8_t	bge_eeprom_getbyte	(struct bge_softc *, int, u_int8_t *);
200e51a25f8SAlfred Perlstein static int bge_read_eeprom	(struct bge_softc *, caddr_t, int, int);
20195d67482SBill Paul 
202e51a25f8SAlfred Perlstein static u_int32_t bge_crc	(caddr_t);
203e51a25f8SAlfred Perlstein static void bge_setmulti	(struct bge_softc *);
20495d67482SBill Paul 
205e51a25f8SAlfred Perlstein static void bge_handle_events	(struct bge_softc *);
206e51a25f8SAlfred Perlstein static int bge_alloc_jumbo_mem	(struct bge_softc *);
207e51a25f8SAlfred Perlstein static void bge_free_jumbo_mem	(struct bge_softc *);
208e51a25f8SAlfred Perlstein static void *bge_jalloc		(struct bge_softc *);
209914596abSAlfred Perlstein static void bge_jfree		(void *, void *);
210e51a25f8SAlfred Perlstein static int bge_newbuf_std	(struct bge_softc *, int, struct mbuf *);
211e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo	(struct bge_softc *, int, struct mbuf *);
212e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std	(struct bge_softc *);
213e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std	(struct bge_softc *);
214e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo	(struct bge_softc *);
215e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo	(struct bge_softc *);
216e51a25f8SAlfred Perlstein static void bge_free_tx_ring	(struct bge_softc *);
217e51a25f8SAlfred Perlstein static int bge_init_tx_ring	(struct bge_softc *);
21895d67482SBill Paul 
219e51a25f8SAlfred Perlstein static int bge_chipinit		(struct bge_softc *);
220e51a25f8SAlfred Perlstein static int bge_blockinit	(struct bge_softc *);
22195d67482SBill Paul 
2221b4a3b2fSPeter Wemm #ifdef notdef
223e51a25f8SAlfred Perlstein static u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
224e51a25f8SAlfred Perlstein static void bge_vpd_read_res	(struct bge_softc *, struct vpd_res *, int);
225e51a25f8SAlfred Perlstein static void bge_vpd_read	(struct bge_softc *);
2261b4a3b2fSPeter Wemm #endif
22795d67482SBill Paul 
22895d67482SBill Paul static u_int32_t bge_readmem_ind
229e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
230e51a25f8SAlfred Perlstein static void bge_writemem_ind	(struct bge_softc *, int, int);
23195d67482SBill Paul #ifdef notdef
23295d67482SBill Paul static u_int32_t bge_readreg_ind
233e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
23495d67482SBill Paul #endif
235e51a25f8SAlfred Perlstein static void bge_writereg_ind	(struct bge_softc *, int, int);
23695d67482SBill Paul 
237e51a25f8SAlfred Perlstein static int bge_miibus_readreg	(device_t, int, int);
238e51a25f8SAlfred Perlstein static int bge_miibus_writereg	(device_t, int, int, int);
239e51a25f8SAlfred Perlstein static void bge_miibus_statchg	(device_t);
24095d67482SBill Paul 
241e51a25f8SAlfred Perlstein static void bge_reset		(struct bge_softc *);
24295d67482SBill Paul 
24395d67482SBill Paul static device_method_t bge_methods[] = {
24495d67482SBill Paul 	/* Device interface */
24595d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
24695d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
24795d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
24895d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
24995d67482SBill Paul 
25095d67482SBill Paul 	/* bus interface */
25195d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
25295d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
25395d67482SBill Paul 
25495d67482SBill Paul 	/* MII interface */
25595d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
25695d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
25795d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
25895d67482SBill Paul 
25995d67482SBill Paul 	{ 0, 0 }
26095d67482SBill Paul };
26195d67482SBill Paul 
26295d67482SBill Paul static driver_t bge_driver = {
26395d67482SBill Paul 	"bge",
26495d67482SBill Paul 	bge_methods,
26595d67482SBill Paul 	sizeof(struct bge_softc)
26695d67482SBill Paul };
26795d67482SBill Paul 
26895d67482SBill Paul static devclass_t bge_devclass;
26995d67482SBill Paul 
270f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
27195d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
27295d67482SBill Paul 
27395d67482SBill Paul static u_int32_t
27495d67482SBill Paul bge_readmem_ind(sc, off)
27595d67482SBill Paul 	struct bge_softc *sc;
27695d67482SBill Paul 	int off;
27795d67482SBill Paul {
27895d67482SBill Paul 	device_t dev;
27995d67482SBill Paul 
28095d67482SBill Paul 	dev = sc->bge_dev;
28195d67482SBill Paul 
28295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
28395d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
28495d67482SBill Paul }
28595d67482SBill Paul 
28695d67482SBill Paul static void
28795d67482SBill Paul bge_writemem_ind(sc, off, val)
28895d67482SBill Paul 	struct bge_softc *sc;
28995d67482SBill Paul 	int off, val;
29095d67482SBill Paul {
29195d67482SBill Paul 	device_t dev;
29295d67482SBill Paul 
29395d67482SBill Paul 	dev = sc->bge_dev;
29495d67482SBill Paul 
29595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
29695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
29795d67482SBill Paul 
29895d67482SBill Paul 	return;
29995d67482SBill Paul }
30095d67482SBill Paul 
30195d67482SBill Paul #ifdef notdef
30295d67482SBill Paul static u_int32_t
30395d67482SBill Paul bge_readreg_ind(sc, off)
30495d67482SBill Paul 	struct bge_softc *sc;
30595d67482SBill Paul 	int off;
30695d67482SBill Paul {
30795d67482SBill Paul 	device_t dev;
30895d67482SBill Paul 
30995d67482SBill Paul 	dev = sc->bge_dev;
31095d67482SBill Paul 
31195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
31295d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
31395d67482SBill Paul }
31495d67482SBill Paul #endif
31595d67482SBill Paul 
31695d67482SBill Paul static void
31795d67482SBill Paul bge_writereg_ind(sc, off, val)
31895d67482SBill Paul 	struct bge_softc *sc;
31995d67482SBill Paul 	int off, val;
32095d67482SBill Paul {
32195d67482SBill Paul 	device_t dev;
32295d67482SBill Paul 
32395d67482SBill Paul 	dev = sc->bge_dev;
32495d67482SBill Paul 
32595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
32695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
32795d67482SBill Paul 
32895d67482SBill Paul 	return;
32995d67482SBill Paul }
33095d67482SBill Paul 
331f41ac2beSBill Paul /*
332f41ac2beSBill Paul  * Map a single buffer address.
333f41ac2beSBill Paul  */
334f41ac2beSBill Paul 
335f41ac2beSBill Paul static void
336f41ac2beSBill Paul bge_dma_map_addr(arg, segs, nseg, error)
337f41ac2beSBill Paul 	void *arg;
338f41ac2beSBill Paul 	bus_dma_segment_t *segs;
339f41ac2beSBill Paul 	int nseg;
340f41ac2beSBill Paul 	int error;
341f41ac2beSBill Paul {
342f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
343f41ac2beSBill Paul 
344f41ac2beSBill Paul 	if (error)
345f41ac2beSBill Paul 		return;
346f41ac2beSBill Paul 
347f41ac2beSBill Paul 	ctx = arg;
348f41ac2beSBill Paul 
349f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
350f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
351f41ac2beSBill Paul 		return;
352f41ac2beSBill Paul 	}
353f41ac2beSBill Paul 
354f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
355f41ac2beSBill Paul 
356f41ac2beSBill Paul 	return;
357f41ac2beSBill Paul }
358f41ac2beSBill Paul 
359f41ac2beSBill Paul /*
360f41ac2beSBill Paul  * Map an mbuf chain into an TX ring.
361f41ac2beSBill Paul  */
362f41ac2beSBill Paul 
363f41ac2beSBill Paul static void
364f41ac2beSBill Paul bge_dma_map_tx_desc(arg, segs, nseg, mapsize, error)
365f41ac2beSBill Paul 	void *arg;
366f41ac2beSBill Paul 	bus_dma_segment_t *segs;
367f41ac2beSBill Paul 	int nseg;
368f41ac2beSBill Paul 	bus_size_t mapsize;
369f41ac2beSBill Paul 	int error;
370f41ac2beSBill Paul {
371f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
372f41ac2beSBill Paul 	struct bge_tx_bd *d = NULL;
373f41ac2beSBill Paul 	int i = 0, idx;
374f41ac2beSBill Paul 
375f41ac2beSBill Paul 	if (error)
376f41ac2beSBill Paul 		return;
377f41ac2beSBill Paul 
378f41ac2beSBill Paul 	ctx = arg;
379f41ac2beSBill Paul 
380f41ac2beSBill Paul 	/* Signal error to caller if there's too many segments */
381f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
382f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
383f41ac2beSBill Paul 		return;
384f41ac2beSBill Paul 	}
385f41ac2beSBill Paul 
386f41ac2beSBill Paul 	idx = ctx->bge_idx;
387f41ac2beSBill Paul 	while(1) {
388f41ac2beSBill Paul 		d = &ctx->bge_ring[idx];
389f41ac2beSBill Paul 		d->bge_addr.bge_addr_lo =
390f41ac2beSBill Paul 		    htole32(BGE_ADDR_LO(segs[i].ds_addr));
391f41ac2beSBill Paul 		d->bge_addr.bge_addr_hi =
392f41ac2beSBill Paul 		    htole32(BGE_ADDR_HI(segs[i].ds_addr));
393f41ac2beSBill Paul 		d->bge_len = htole16(segs[i].ds_len);
394f41ac2beSBill Paul 		d->bge_flags = htole16(ctx->bge_flags);
395f41ac2beSBill Paul                 i++;
396f41ac2beSBill Paul 		if (i == nseg)
397f41ac2beSBill Paul 			break;
398f41ac2beSBill Paul 		BGE_INC(idx, BGE_TX_RING_CNT);
399f41ac2beSBill Paul 	}
400f41ac2beSBill Paul 
401f41ac2beSBill Paul 	d->bge_flags |= htole16(BGE_TXBDFLAG_END);
402f41ac2beSBill Paul 	ctx->bge_maxsegs = nseg;
403f41ac2beSBill Paul 	ctx->bge_idx = idx;
404f41ac2beSBill Paul 
405f41ac2beSBill Paul 	return;
406f41ac2beSBill Paul }
407f41ac2beSBill Paul 
408f41ac2beSBill Paul 
4091b4a3b2fSPeter Wemm #ifdef notdef
41095d67482SBill Paul static u_int8_t
41195d67482SBill Paul bge_vpd_readbyte(sc, addr)
41295d67482SBill Paul 	struct bge_softc *sc;
41395d67482SBill Paul 	int addr;
41495d67482SBill Paul {
41595d67482SBill Paul 	int i;
41695d67482SBill Paul 	device_t dev;
41795d67482SBill Paul 	u_int32_t val;
41895d67482SBill Paul 
41995d67482SBill Paul 	dev = sc->bge_dev;
42095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
42195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
42295d67482SBill Paul 		DELAY(10);
42395d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
42495d67482SBill Paul 			break;
42595d67482SBill Paul 	}
42695d67482SBill Paul 
42795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
42895d67482SBill Paul 		printf("bge%d: VPD read timed out\n", sc->bge_unit);
42995d67482SBill Paul 		return(0);
43095d67482SBill Paul 	}
43195d67482SBill Paul 
43295d67482SBill Paul 	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
43395d67482SBill Paul 
43495d67482SBill Paul 	return((val >> ((addr % 4) * 8)) & 0xFF);
43595d67482SBill Paul }
43695d67482SBill Paul 
43795d67482SBill Paul static void
43895d67482SBill Paul bge_vpd_read_res(sc, res, addr)
43995d67482SBill Paul 	struct bge_softc *sc;
44095d67482SBill Paul 	struct vpd_res *res;
44195d67482SBill Paul 	int addr;
44295d67482SBill Paul {
44395d67482SBill Paul 	int i;
44495d67482SBill Paul 	u_int8_t *ptr;
44595d67482SBill Paul 
44695d67482SBill Paul 	ptr = (u_int8_t *)res;
44795d67482SBill Paul 	for (i = 0; i < sizeof(struct vpd_res); i++)
44895d67482SBill Paul 		ptr[i] = bge_vpd_readbyte(sc, i + addr);
44995d67482SBill Paul 
45095d67482SBill Paul 	return;
45195d67482SBill Paul }
45295d67482SBill Paul 
45395d67482SBill Paul static void
45495d67482SBill Paul bge_vpd_read(sc)
45595d67482SBill Paul 	struct bge_softc *sc;
45695d67482SBill Paul {
45795d67482SBill Paul 	int pos = 0, i;
45895d67482SBill Paul 	struct vpd_res res;
45995d67482SBill Paul 
46095d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
46195d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
46295d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
46395d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
46495d67482SBill Paul 	sc->bge_vpd_prodname = NULL;
46595d67482SBill Paul 	sc->bge_vpd_readonly = NULL;
46695d67482SBill Paul 
46795d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
46895d67482SBill Paul 
46995d67482SBill Paul 	if (res.vr_id != VPD_RES_ID) {
47095d67482SBill Paul 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
47195d67482SBill Paul 			sc->bge_unit, VPD_RES_ID, res.vr_id);
47295d67482SBill Paul                 return;
47395d67482SBill Paul         }
47495d67482SBill Paul 
47595d67482SBill Paul 	pos += sizeof(res);
47695d67482SBill Paul 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
47795d67482SBill Paul 	for (i = 0; i < res.vr_len; i++)
47895d67482SBill Paul 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
47995d67482SBill Paul 	sc->bge_vpd_prodname[i] = '\0';
48095d67482SBill Paul 	pos += i;
48195d67482SBill Paul 
48295d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
48395d67482SBill Paul 
48495d67482SBill Paul 	if (res.vr_id != VPD_RES_READ) {
48595d67482SBill Paul 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
48695d67482SBill Paul 		    sc->bge_unit, VPD_RES_READ, res.vr_id);
48795d67482SBill Paul 		return;
48895d67482SBill Paul 	}
48995d67482SBill Paul 
49095d67482SBill Paul 	pos += sizeof(res);
49195d67482SBill Paul 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
49295d67482SBill Paul 	for (i = 0; i < res.vr_len + 1; i++)
49395d67482SBill Paul 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
49495d67482SBill Paul 
49595d67482SBill Paul 	return;
49695d67482SBill Paul }
4971b4a3b2fSPeter Wemm #endif
49895d67482SBill Paul 
49995d67482SBill Paul /*
50095d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
50195d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
50295d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
50395d67482SBill Paul  * access method.
50495d67482SBill Paul  */
50595d67482SBill Paul static u_int8_t
50695d67482SBill Paul bge_eeprom_getbyte(sc, addr, dest)
50795d67482SBill Paul 	struct bge_softc *sc;
50895d67482SBill Paul 	int addr;
50995d67482SBill Paul 	u_int8_t *dest;
51095d67482SBill Paul {
51195d67482SBill Paul 	int i;
51295d67482SBill Paul 	u_int32_t byte = 0;
51395d67482SBill Paul 
51495d67482SBill Paul 	/*
51595d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
51695d67482SBill Paul 	 * having to use the bitbang method.
51795d67482SBill Paul 	 */
51895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
51995d67482SBill Paul 
52095d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
52195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
52295d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
52395d67482SBill Paul 	DELAY(20);
52495d67482SBill Paul 
52595d67482SBill Paul 	/* Issue the read EEPROM command. */
52695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
52795d67482SBill Paul 
52895d67482SBill Paul 	/* Wait for completion */
52995d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
53095d67482SBill Paul 		DELAY(10);
53195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
53295d67482SBill Paul 			break;
53395d67482SBill Paul 	}
53495d67482SBill Paul 
53595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
53695d67482SBill Paul 		printf("bge%d: eeprom read timed out\n", sc->bge_unit);
53795d67482SBill Paul 		return(0);
53895d67482SBill Paul 	}
53995d67482SBill Paul 
54095d67482SBill Paul 	/* Get result. */
54195d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
54295d67482SBill Paul 
54395d67482SBill Paul         *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
54495d67482SBill Paul 
54595d67482SBill Paul 	return(0);
54695d67482SBill Paul }
54795d67482SBill Paul 
54895d67482SBill Paul /*
54995d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
55095d67482SBill Paul  */
55195d67482SBill Paul static int
55295d67482SBill Paul bge_read_eeprom(sc, dest, off, cnt)
55395d67482SBill Paul 	struct bge_softc *sc;
55495d67482SBill Paul 	caddr_t dest;
55595d67482SBill Paul 	int off;
55695d67482SBill Paul 	int cnt;
55795d67482SBill Paul {
55895d67482SBill Paul 	int err = 0, i;
55995d67482SBill Paul 	u_int8_t byte = 0;
56095d67482SBill Paul 
56195d67482SBill Paul 	for (i = 0; i < cnt; i++) {
56295d67482SBill Paul 		err = bge_eeprom_getbyte(sc, off + i, &byte);
56395d67482SBill Paul 		if (err)
56495d67482SBill Paul 			break;
56595d67482SBill Paul 		*(dest + i) = byte;
56695d67482SBill Paul 	}
56795d67482SBill Paul 
56895d67482SBill Paul 	return(err ? 1 : 0);
56995d67482SBill Paul }
57095d67482SBill Paul 
57195d67482SBill Paul static int
57295d67482SBill Paul bge_miibus_readreg(dev, phy, reg)
57395d67482SBill Paul 	device_t dev;
57495d67482SBill Paul 	int phy, reg;
57595d67482SBill Paul {
57695d67482SBill Paul 	struct bge_softc *sc;
57737ceeb4dSPaul Saab 	u_int32_t val, autopoll;
57895d67482SBill Paul 	int i;
57995d67482SBill Paul 
58095d67482SBill Paul 	sc = device_get_softc(dev);
58195d67482SBill Paul 
5820434d1b8SBill Paul 	/*
5830434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5840434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5850434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5860434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5870434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5880434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5890434d1b8SBill Paul 	 * special-cased.
5900434d1b8SBill Paul 	 */
591b1265c1aSJohn Polstra 	if (phy != 1)
59298b28ee5SBill Paul 		return(0);
59398b28ee5SBill Paul 
59437ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
59537ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
59637ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
59737ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
59837ceeb4dSPaul Saab 		DELAY(40);
59937ceeb4dSPaul Saab 	}
60037ceeb4dSPaul Saab 
60195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
60295d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
60395d67482SBill Paul 
60495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
60595d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
60695d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
60795d67482SBill Paul 			break;
60895d67482SBill Paul 	}
60995d67482SBill Paul 
61095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
61195d67482SBill Paul 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
61237ceeb4dSPaul Saab 		val = 0;
61337ceeb4dSPaul Saab 		goto done;
61495d67482SBill Paul 	}
61595d67482SBill Paul 
61695d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
61795d67482SBill Paul 
61837ceeb4dSPaul Saab done:
61937ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
62037ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
62137ceeb4dSPaul Saab 		DELAY(40);
62237ceeb4dSPaul Saab 	}
62337ceeb4dSPaul Saab 
62495d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
62595d67482SBill Paul 		return(0);
62695d67482SBill Paul 
62795d67482SBill Paul 	return(val & 0xFFFF);
62895d67482SBill Paul }
62995d67482SBill Paul 
63095d67482SBill Paul static int
63195d67482SBill Paul bge_miibus_writereg(dev, phy, reg, val)
63295d67482SBill Paul 	device_t dev;
63395d67482SBill Paul 	int phy, reg, val;
63495d67482SBill Paul {
63595d67482SBill Paul 	struct bge_softc *sc;
63637ceeb4dSPaul Saab 	u_int32_t autopoll;
63795d67482SBill Paul 	int i;
63895d67482SBill Paul 
63995d67482SBill Paul 	sc = device_get_softc(dev);
64095d67482SBill Paul 
64137ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
64237ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
64337ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
64437ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
64537ceeb4dSPaul Saab 		DELAY(40);
64637ceeb4dSPaul Saab 	}
64737ceeb4dSPaul Saab 
64895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
64995d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
65095d67482SBill Paul 
65195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
65295d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
65395d67482SBill Paul 			break;
65495d67482SBill Paul 	}
65595d67482SBill Paul 
65637ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
65737ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
65837ceeb4dSPaul Saab 		DELAY(40);
65937ceeb4dSPaul Saab 	}
66037ceeb4dSPaul Saab 
66195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
66295d67482SBill Paul 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
66395d67482SBill Paul 		return(0);
66495d67482SBill Paul 	}
66595d67482SBill Paul 
66695d67482SBill Paul 	return(0);
66795d67482SBill Paul }
66895d67482SBill Paul 
66995d67482SBill Paul static void
67095d67482SBill Paul bge_miibus_statchg(dev)
67195d67482SBill Paul 	device_t dev;
67295d67482SBill Paul {
67395d67482SBill Paul 	struct bge_softc *sc;
67495d67482SBill Paul 	struct mii_data *mii;
67595d67482SBill Paul 
67695d67482SBill Paul 	sc = device_get_softc(dev);
67795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
67895d67482SBill Paul 
67995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
680b418ad5cSPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
68195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
68295d67482SBill Paul 	} else {
68395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
68495d67482SBill Paul 	}
68595d67482SBill Paul 
68695d67482SBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
68795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
68895d67482SBill Paul 	} else {
68995d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
69095d67482SBill Paul 	}
69195d67482SBill Paul 
69295d67482SBill Paul 	return;
69395d67482SBill Paul }
69495d67482SBill Paul 
69595d67482SBill Paul /*
69695d67482SBill Paul  * Handle events that have triggered interrupts.
69795d67482SBill Paul  */
69895d67482SBill Paul static void
69995d67482SBill Paul bge_handle_events(sc)
70095d67482SBill Paul 	struct bge_softc		*sc;
70195d67482SBill Paul {
70295d67482SBill Paul 
70395d67482SBill Paul 	return;
70495d67482SBill Paul }
70595d67482SBill Paul 
70695d67482SBill Paul /*
70795d67482SBill Paul  * Memory management for jumbo frames.
70895d67482SBill Paul  */
70995d67482SBill Paul 
71095d67482SBill Paul static int
71195d67482SBill Paul bge_alloc_jumbo_mem(sc)
71295d67482SBill Paul 	struct bge_softc		*sc;
71395d67482SBill Paul {
71495d67482SBill Paul 	caddr_t			ptr;
715f41ac2beSBill Paul 	register int		i, error;
71695d67482SBill Paul 	struct bge_jpool_entry   *entry;
71795d67482SBill Paul 
718f41ac2beSBill Paul 	/* Create tag for jumbo buffer block */
71995d67482SBill Paul 
720f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
721f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
722f41ac2beSBill Paul 	    NULL, BGE_JMEM, 1, BGE_JMEM, 0, NULL, NULL,
723f41ac2beSBill Paul 	    &sc->bge_cdata.bge_jumbo_tag);
724f41ac2beSBill Paul 
725f41ac2beSBill Paul 	if (error) {
726f41ac2beSBill Paul 		printf("bge%d: could not allocate jumbo dma tag\n",
727f41ac2beSBill Paul 		    sc->bge_unit);
728f41ac2beSBill Paul 		return (ENOMEM);
72995d67482SBill Paul 	}
73095d67482SBill Paul 
731f41ac2beSBill Paul 	/* Allocate DMA'able memory for jumbo buffer block */
732f41ac2beSBill Paul 
733f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_jumbo_tag,
734f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_jumbo_buf, BUS_DMA_NOWAIT,
735f41ac2beSBill Paul 	    &sc->bge_cdata.bge_jumbo_map);
736f41ac2beSBill Paul 
737f41ac2beSBill Paul 	if (error)
738f41ac2beSBill Paul                 return (ENOMEM);
739f41ac2beSBill Paul 
74095d67482SBill Paul 	SLIST_INIT(&sc->bge_jfree_listhead);
74195d67482SBill Paul 	SLIST_INIT(&sc->bge_jinuse_listhead);
74295d67482SBill Paul 
74395d67482SBill Paul 	/*
74495d67482SBill Paul 	 * Now divide it up into 9K pieces and save the addresses
74595d67482SBill Paul 	 * in an array.
74695d67482SBill Paul 	 */
747f41ac2beSBill Paul 	ptr = sc->bge_ldata.bge_jumbo_buf;
74895d67482SBill Paul 	for (i = 0; i < BGE_JSLOTS; i++) {
74995d67482SBill Paul 		sc->bge_cdata.bge_jslots[i] = ptr;
75095d67482SBill Paul 		ptr += BGE_JLEN;
75195d67482SBill Paul 		entry = malloc(sizeof(struct bge_jpool_entry),
75295d67482SBill Paul 		    M_DEVBUF, M_NOWAIT);
75395d67482SBill Paul 		if (entry == NULL) {
754f41ac2beSBill Paul 			bge_free_jumbo_mem(sc);
755f41ac2beSBill Paul 			sc->bge_ldata.bge_jumbo_buf = NULL;
75695d67482SBill Paul 			printf("bge%d: no memory for jumbo "
75795d67482SBill Paul 			    "buffer queue!\n", sc->bge_unit);
75895d67482SBill Paul 			return(ENOBUFS);
75995d67482SBill Paul 		}
76095d67482SBill Paul 		entry->slot = i;
76195d67482SBill Paul 		SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
76295d67482SBill Paul 		    entry, jpool_entries);
76395d67482SBill Paul 	}
76495d67482SBill Paul 
76595d67482SBill Paul 	return(0);
76695d67482SBill Paul }
76795d67482SBill Paul 
76895d67482SBill Paul static void
76995d67482SBill Paul bge_free_jumbo_mem(sc)
77095d67482SBill Paul         struct bge_softc *sc;
77195d67482SBill Paul {
77295d67482SBill Paul         int i;
77395d67482SBill Paul         struct bge_jpool_entry *entry;
77495d67482SBill Paul 
77595d67482SBill Paul 	for (i = 0; i < BGE_JSLOTS; i++) {
77695d67482SBill Paul 		entry = SLIST_FIRST(&sc->bge_jfree_listhead);
77795d67482SBill Paul 		SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
77895d67482SBill Paul 		free(entry, M_DEVBUF);
77995d67482SBill Paul 	}
78095d67482SBill Paul 
781f41ac2beSBill Paul 	/* Destroy jumbo buffer block */
782f41ac2beSBill Paul 
783f41ac2beSBill Paul 	if (sc->bge_ldata.bge_rx_jumbo_ring)
784f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_jumbo_tag,
785f41ac2beSBill Paul 		    sc->bge_ldata.bge_jumbo_buf,
786f41ac2beSBill Paul 		    sc->bge_cdata.bge_jumbo_map);
787f41ac2beSBill Paul 
788f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
789f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_jumbo_tag,
790f41ac2beSBill Paul 		    sc->bge_cdata.bge_jumbo_map);
791f41ac2beSBill Paul 
792f41ac2beSBill Paul 	if (sc->bge_cdata.bge_jumbo_tag)
793f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_jumbo_tag);
79495d67482SBill Paul 
79595d67482SBill Paul         return;
79695d67482SBill Paul }
79795d67482SBill Paul 
79895d67482SBill Paul /*
79995d67482SBill Paul  * Allocate a jumbo buffer.
80095d67482SBill Paul  */
80195d67482SBill Paul static void *
80295d67482SBill Paul bge_jalloc(sc)
80395d67482SBill Paul 	struct bge_softc		*sc;
80495d67482SBill Paul {
80595d67482SBill Paul 	struct bge_jpool_entry   *entry;
80695d67482SBill Paul 
80795d67482SBill Paul 	entry = SLIST_FIRST(&sc->bge_jfree_listhead);
80895d67482SBill Paul 
80995d67482SBill Paul 	if (entry == NULL) {
81095d67482SBill Paul 		printf("bge%d: no free jumbo buffers\n", sc->bge_unit);
81195d67482SBill Paul 		return(NULL);
81295d67482SBill Paul 	}
81395d67482SBill Paul 
81495d67482SBill Paul 	SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
81595d67482SBill Paul 	SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
81695d67482SBill Paul 	return(sc->bge_cdata.bge_jslots[entry->slot]);
81795d67482SBill Paul }
81895d67482SBill Paul 
81995d67482SBill Paul /*
82095d67482SBill Paul  * Release a jumbo buffer.
82195d67482SBill Paul  */
82295d67482SBill Paul static void
82395d67482SBill Paul bge_jfree(buf, args)
824914596abSAlfred Perlstein 	void *buf;
82595d67482SBill Paul 	void *args;
82695d67482SBill Paul {
82795d67482SBill Paul 	struct bge_jpool_entry *entry;
82895d67482SBill Paul 	struct bge_softc *sc;
82995d67482SBill Paul 	int i;
83095d67482SBill Paul 
83195d67482SBill Paul 	/* Extract the softc struct pointer. */
83295d67482SBill Paul 	sc = (struct bge_softc *)args;
83395d67482SBill Paul 
83495d67482SBill Paul 	if (sc == NULL)
83595d67482SBill Paul 		panic("bge_jfree: can't find softc pointer!");
83695d67482SBill Paul 
83795d67482SBill Paul 	/* calculate the slot this buffer belongs to */
83895d67482SBill Paul 
83995d67482SBill Paul 	i = ((vm_offset_t)buf
840f41ac2beSBill Paul 	     - (vm_offset_t)sc->bge_ldata.bge_jumbo_buf) / BGE_JLEN;
84195d67482SBill Paul 
84295d67482SBill Paul 	if ((i < 0) || (i >= BGE_JSLOTS))
84395d67482SBill Paul 		panic("bge_jfree: asked to free buffer that we don't manage!");
84495d67482SBill Paul 
84595d67482SBill Paul 	entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
84695d67482SBill Paul 	if (entry == NULL)
84795d67482SBill Paul 		panic("bge_jfree: buffer not in use!");
84895d67482SBill Paul 	entry->slot = i;
84995d67482SBill Paul 	SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
85095d67482SBill Paul 	SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
85195d67482SBill Paul 
85295d67482SBill Paul 	return;
85395d67482SBill Paul }
85495d67482SBill Paul 
85595d67482SBill Paul 
85695d67482SBill Paul /*
85795d67482SBill Paul  * Intialize a standard receive ring descriptor.
85895d67482SBill Paul  */
85995d67482SBill Paul static int
86095d67482SBill Paul bge_newbuf_std(sc, i, m)
86195d67482SBill Paul 	struct bge_softc	*sc;
86295d67482SBill Paul 	int			i;
86395d67482SBill Paul 	struct mbuf		*m;
86495d67482SBill Paul {
86595d67482SBill Paul 	struct mbuf		*m_new = NULL;
86695d67482SBill Paul 	struct bge_rx_bd	*r;
867f41ac2beSBill Paul 	struct bge_dmamap_arg	ctx;
868f41ac2beSBill Paul 	int			error;
86995d67482SBill Paul 
87095d67482SBill Paul 	if (m == NULL) {
871a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
87295d67482SBill Paul 		if (m_new == NULL) {
87395d67482SBill Paul 			return(ENOBUFS);
87495d67482SBill Paul 		}
87595d67482SBill Paul 
876a163d034SWarner Losh 		MCLGET(m_new, M_DONTWAIT);
87795d67482SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
87895d67482SBill Paul 			m_freem(m_new);
87995d67482SBill Paul 			return(ENOBUFS);
88095d67482SBill Paul 		}
88195d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
88295d67482SBill Paul 	} else {
88395d67482SBill Paul 		m_new = m;
88495d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
88595d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
88695d67482SBill Paul 	}
88795d67482SBill Paul 
888e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
88995d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
89095d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
891f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
892f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
893f41ac2beSBill Paul 	ctx.sc = sc;
894f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
895f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
896f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
897f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
898f41ac2beSBill Paul 		if (m == NULL)
899f41ac2beSBill Paul 			m_freem(m_new);
900f41ac2beSBill Paul 		return(ENOMEM);
901f41ac2beSBill Paul 	}
902f41ac2beSBill Paul 	r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr));
903f41ac2beSBill Paul 	r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr));
904f41ac2beSBill Paul 	r->bge_flags = htole16(BGE_RXBDFLAG_END);
905f41ac2beSBill Paul 	r->bge_len = htole16(m_new->m_len);
906f41ac2beSBill Paul 	r->bge_idx = htole16(i);
907f41ac2beSBill Paul 
908f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
909f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
910f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
91195d67482SBill Paul 
91295d67482SBill Paul 	return(0);
91395d67482SBill Paul }
91495d67482SBill Paul 
91595d67482SBill Paul /*
91695d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
91795d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
91895d67482SBill Paul  */
91995d67482SBill Paul static int
92095d67482SBill Paul bge_newbuf_jumbo(sc, i, m)
92195d67482SBill Paul 	struct bge_softc *sc;
92295d67482SBill Paul 	int i;
92395d67482SBill Paul 	struct mbuf *m;
92495d67482SBill Paul {
92595d67482SBill Paul 	struct mbuf *m_new = NULL;
92695d67482SBill Paul 	struct bge_rx_bd *r;
927f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
928f41ac2beSBill Paul 	int error;
92995d67482SBill Paul 
93095d67482SBill Paul 	if (m == NULL) {
93195d67482SBill Paul 		caddr_t			*buf = NULL;
93295d67482SBill Paul 
93395d67482SBill Paul 		/* Allocate the mbuf. */
934a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
93595d67482SBill Paul 		if (m_new == NULL) {
93695d67482SBill Paul 			return(ENOBUFS);
93795d67482SBill Paul 		}
93895d67482SBill Paul 
93995d67482SBill Paul 		/* Allocate the jumbo buffer */
94095d67482SBill Paul 		buf = bge_jalloc(sc);
94195d67482SBill Paul 		if (buf == NULL) {
94295d67482SBill Paul 			m_freem(m_new);
94395d67482SBill Paul 			printf("bge%d: jumbo allocation failed "
94495d67482SBill Paul 			    "-- packet dropped!\n", sc->bge_unit);
94595d67482SBill Paul 			return(ENOBUFS);
94695d67482SBill Paul 		}
94795d67482SBill Paul 
94895d67482SBill Paul 		/* Attach the buffer to the mbuf. */
94995d67482SBill Paul 		m_new->m_data = (void *) buf;
95095d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
95195d67482SBill Paul 		MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, bge_jfree,
95295d67482SBill Paul 		    (struct bge_softc *)sc, 0, EXT_NET_DRV);
95395d67482SBill Paul 	} else {
95495d67482SBill Paul 		m_new = m;
95595d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
95695d67482SBill Paul 		m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
95795d67482SBill Paul 	}
95895d67482SBill Paul 
959e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
96095d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
96195d67482SBill Paul 	/* Set up the descriptor. */
96295d67482SBill Paul 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
963f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
964f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
965f41ac2beSBill Paul 	ctx.sc = sc;
966f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag_jumbo,
967f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], mtod(m_new, void *),
968f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
969f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
970f41ac2beSBill Paul 		if (m == NULL)
971f41ac2beSBill Paul 			m_freem(m_new);
972f41ac2beSBill Paul 		return(ENOMEM);
973f41ac2beSBill Paul 	}
974f41ac2beSBill Paul 	r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr));
975f41ac2beSBill Paul 	r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr));
976f41ac2beSBill Paul 	r->bge_flags = htole16(BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING);
977f41ac2beSBill Paul 	r->bge_len = htole16(m_new->m_len);
978f41ac2beSBill Paul 	r->bge_idx = htole16(i);
979f41ac2beSBill Paul 
980f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
981f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
982f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
98395d67482SBill Paul 
98495d67482SBill Paul 	return(0);
98595d67482SBill Paul }
98695d67482SBill Paul 
98795d67482SBill Paul /*
98895d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
98995d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
99095d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
99195d67482SBill Paul  * the NIC.
99295d67482SBill Paul  */
99395d67482SBill Paul static int
99495d67482SBill Paul bge_init_rx_ring_std(sc)
99595d67482SBill Paul 	struct bge_softc *sc;
99695d67482SBill Paul {
99795d67482SBill Paul 	int i;
99895d67482SBill Paul 
99995d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
100095d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
100195d67482SBill Paul 			return(ENOBUFS);
100295d67482SBill Paul 	};
100395d67482SBill Paul 
1004f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1005f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
1006f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1007f41ac2beSBill Paul 
100895d67482SBill Paul 	sc->bge_std = i - 1;
100995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
101095d67482SBill Paul 
101195d67482SBill Paul 	return(0);
101295d67482SBill Paul }
101395d67482SBill Paul 
101495d67482SBill Paul static void
101595d67482SBill Paul bge_free_rx_ring_std(sc)
101695d67482SBill Paul 	struct bge_softc *sc;
101795d67482SBill Paul {
101895d67482SBill Paul 	int i;
101995d67482SBill Paul 
102095d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
102195d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
102295d67482SBill Paul 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
102395d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
1024f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1025f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
102695d67482SBill Paul 		}
1027f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
102895d67482SBill Paul 		    sizeof(struct bge_rx_bd));
102995d67482SBill Paul 	}
103095d67482SBill Paul 
103195d67482SBill Paul 	return;
103295d67482SBill Paul }
103395d67482SBill Paul 
103495d67482SBill Paul static int
103595d67482SBill Paul bge_init_rx_ring_jumbo(sc)
103695d67482SBill Paul 	struct bge_softc *sc;
103795d67482SBill Paul {
103895d67482SBill Paul 	int i;
103995d67482SBill Paul 	struct bge_rcb *rcb;
104095d67482SBill Paul 
104195d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
104295d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
104395d67482SBill Paul 			return(ENOBUFS);
104495d67482SBill Paul 	};
104595d67482SBill Paul 
1046f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1047f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
1048f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1049f41ac2beSBill Paul 
105095d67482SBill Paul 	sc->bge_jumbo = i - 1;
105195d67482SBill Paul 
1052f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
105367111612SJohn Polstra 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
105467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
105595d67482SBill Paul 
105695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
105795d67482SBill Paul 
105895d67482SBill Paul 	return(0);
105995d67482SBill Paul }
106095d67482SBill Paul 
106195d67482SBill Paul static void
106295d67482SBill Paul bge_free_rx_ring_jumbo(sc)
106395d67482SBill Paul 	struct bge_softc *sc;
106495d67482SBill Paul {
106595d67482SBill Paul 	int i;
106695d67482SBill Paul 
106795d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
106895d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
106995d67482SBill Paul 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
107095d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1071f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1072f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
107395d67482SBill Paul 		}
1074f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
107595d67482SBill Paul 		    sizeof(struct bge_rx_bd));
107695d67482SBill Paul 	}
107795d67482SBill Paul 
107895d67482SBill Paul 	return;
107995d67482SBill Paul }
108095d67482SBill Paul 
108195d67482SBill Paul static void
108295d67482SBill Paul bge_free_tx_ring(sc)
108395d67482SBill Paul 	struct bge_softc *sc;
108495d67482SBill Paul {
108595d67482SBill Paul 	int i;
108695d67482SBill Paul 
1087f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
108895d67482SBill Paul 		return;
108995d67482SBill Paul 
109095d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
109195d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
109295d67482SBill Paul 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
109395d67482SBill Paul 			sc->bge_cdata.bge_tx_chain[i] = NULL;
1094f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1095f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
109695d67482SBill Paul 		}
1097f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
109895d67482SBill Paul 		    sizeof(struct bge_tx_bd));
109995d67482SBill Paul 	}
110095d67482SBill Paul 
110195d67482SBill Paul 	return;
110295d67482SBill Paul }
110395d67482SBill Paul 
110495d67482SBill Paul static int
110595d67482SBill Paul bge_init_tx_ring(sc)
110695d67482SBill Paul 	struct bge_softc *sc;
110795d67482SBill Paul {
110895d67482SBill Paul 	sc->bge_txcnt = 0;
110995d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
11103927098fSPaul Saab 
111195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
11123927098fSPaul Saab 	/* 5700 b2 errata */
1113e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
11143927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
11153927098fSPaul Saab 
11163927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
11173927098fSPaul Saab 	/* 5700 b2 errata */
1118e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
111995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
112095d67482SBill Paul 
112195d67482SBill Paul 	return(0);
112295d67482SBill Paul }
112395d67482SBill Paul 
112495d67482SBill Paul #define BGE_POLY	0xEDB88320
112595d67482SBill Paul 
112695d67482SBill Paul static u_int32_t
112795d67482SBill Paul bge_crc(addr)
112895d67482SBill Paul 	caddr_t addr;
112995d67482SBill Paul {
113095d67482SBill Paul 	u_int32_t idx, bit, data, crc;
113195d67482SBill Paul 
113295d67482SBill Paul 	/* Compute CRC for the address value. */
113395d67482SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
113495d67482SBill Paul 
113595d67482SBill Paul 	for (idx = 0; idx < 6; idx++) {
113695d67482SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
113795d67482SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? BGE_POLY : 0);
113895d67482SBill Paul 	}
113995d67482SBill Paul 
114095d67482SBill Paul 	return(crc & 0x7F);
114195d67482SBill Paul }
114295d67482SBill Paul 
114395d67482SBill Paul static void
114495d67482SBill Paul bge_setmulti(sc)
114595d67482SBill Paul 	struct bge_softc *sc;
114695d67482SBill Paul {
114795d67482SBill Paul 	struct ifnet *ifp;
114895d67482SBill Paul 	struct ifmultiaddr *ifma;
114995d67482SBill Paul 	u_int32_t hashes[4] = { 0, 0, 0, 0 };
115095d67482SBill Paul 	int h, i;
115195d67482SBill Paul 
115295d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
115395d67482SBill Paul 
115495d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
115595d67482SBill Paul 		for (i = 0; i < 4; i++)
115695d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
115795d67482SBill Paul 		return;
115895d67482SBill Paul 	}
115995d67482SBill Paul 
116095d67482SBill Paul 	/* First, zot all the existing filters. */
116195d67482SBill Paul 	for (i = 0; i < 4; i++)
116295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
116395d67482SBill Paul 
116495d67482SBill Paul 	/* Now program new ones. */
116595d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
116695d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
116795d67482SBill Paul 			continue;
116895d67482SBill Paul 		h = bge_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
116995d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
117095d67482SBill Paul 	}
117195d67482SBill Paul 
117295d67482SBill Paul 	for (i = 0; i < 4; i++)
117395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
117495d67482SBill Paul 
117595d67482SBill Paul 	return;
117695d67482SBill Paul }
117795d67482SBill Paul 
117895d67482SBill Paul /*
117995d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
118095d67482SBill Paul  * self-test results.
118195d67482SBill Paul  */
118295d67482SBill Paul static int
118395d67482SBill Paul bge_chipinit(sc)
118495d67482SBill Paul 	struct bge_softc *sc;
118595d67482SBill Paul {
118695d67482SBill Paul 	int			i;
11875cba12d3SPaul Saab 	u_int32_t		dma_rw_ctl;
118895d67482SBill Paul 
118995d67482SBill Paul 	/* Set endianness before we access any non-PCI registers. */
119095d67482SBill Paul #if BYTE_ORDER == BIG_ENDIAN
119195d67482SBill Paul 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
119295d67482SBill Paul 	    BGE_BIGENDIAN_INIT, 4);
119395d67482SBill Paul #else
119495d67482SBill Paul 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
119595d67482SBill Paul 	    BGE_LITTLEENDIAN_INIT, 4);
119695d67482SBill Paul #endif
119795d67482SBill Paul 
119895d67482SBill Paul 	/*
119995d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
120095d67482SBill Paul 	 * self-tests passed.
120195d67482SBill Paul 	 */
120295d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
120395d67482SBill Paul 		printf("bge%d: RX CPU self-diagnostics failed!\n",
120495d67482SBill Paul 		    sc->bge_unit);
120595d67482SBill Paul 		return(ENODEV);
120695d67482SBill Paul 	}
120795d67482SBill Paul 
120895d67482SBill Paul 	/* Clear the MAC control register */
120995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
121095d67482SBill Paul 
121195d67482SBill Paul 	/*
121295d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
121395d67482SBill Paul 	 * internal memory.
121495d67482SBill Paul 	 */
121595d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
121695d67482SBill Paul 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
121795d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
121895d67482SBill Paul 
121995d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
122095d67482SBill Paul 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
122195d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
122295d67482SBill Paul 
122395d67482SBill Paul 	/* Set up the PCI DMA control register. */
12248287860eSJohn Polstra 	if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
12258287860eSJohn Polstra 	    BGE_PCISTATE_PCI_BUSMODE) {
12268287860eSJohn Polstra 		/* Conventional PCI bus */
12275cba12d3SPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
12285cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
12295cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
12305cba12d3SPaul Saab 		    (0x0F);
12318287860eSJohn Polstra 	} else {
12328287860eSJohn Polstra 		/* PCI-X bus */
12335cba12d3SPaul Saab 		/*
12345cba12d3SPaul Saab 		 * The 5704 uses a different encoding of read/write
12355cba12d3SPaul Saab 		 * watermarks.
12365cba12d3SPaul Saab 		 */
1237e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
12385cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
12395cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
12405cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
12415cba12d3SPaul Saab 		else
12425cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
12435cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
12445cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
12455cba12d3SPaul Saab 			    (0x0F);
12465cba12d3SPaul Saab 
12475cba12d3SPaul Saab 		/*
12485cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
12495cba12d3SPaul Saab 		 * for hardware bugs.
12505cba12d3SPaul Saab 		 */
1251e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1252e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
12535cba12d3SPaul Saab 			u_int32_t tmp;
12545cba12d3SPaul Saab 
12555cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
12565cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
12575cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
12588287860eSJohn Polstra 		}
12595cba12d3SPaul Saab 	}
12605cba12d3SPaul Saab 
1261e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
12620434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
12630434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5705)
12645cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
12655cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
126695d67482SBill Paul 
126795d67482SBill Paul 	/*
126895d67482SBill Paul 	 * Set up general mode register.
126995d67482SBill Paul 	 */
127095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_WORDSWAP_NONFRAME|
127195d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
127295d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
12730189c944SBill Paul 	    BGE_MODECTL_NO_RX_CRC|BGE_MODECTL_TX_NO_PHDR_CSUM|
12740189c944SBill Paul 	    BGE_MODECTL_RX_NO_PHDR_CSUM);
127595d67482SBill Paul 
127695d67482SBill Paul 	/*
1277ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1278ea13bdd5SJohn Polstra 	 * properly by these devices.
127995d67482SBill Paul 	 */
1280ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
128195d67482SBill Paul 
128295d67482SBill Paul #ifdef __brokenalpha__
128395d67482SBill Paul 	/*
128495d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
128595d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
128695d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
128795d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
128895d67482SBill Paul 	 */
128962f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
129062f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
129195d67482SBill Paul #endif
129295d67482SBill Paul 
129395d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
129495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
129595d67482SBill Paul 
129695d67482SBill Paul 	return(0);
129795d67482SBill Paul }
129895d67482SBill Paul 
129995d67482SBill Paul static int
130095d67482SBill Paul bge_blockinit(sc)
130195d67482SBill Paul 	struct bge_softc *sc;
130295d67482SBill Paul {
130395d67482SBill Paul 	struct bge_rcb *rcb;
130467111612SJohn Polstra 	volatile struct bge_rcb *vrcb;
130595d67482SBill Paul 	int i;
130695d67482SBill Paul 
130795d67482SBill Paul 	/*
130895d67482SBill Paul 	 * Initialize the memory window pointer register so that
130995d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
131095d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
131195d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
131295d67482SBill Paul 	 */
131395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
131495d67482SBill Paul 
13150434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
131695d67482SBill Paul 		/* Configure mbuf memory pool */
131795d67482SBill Paul 		if (sc->bge_extram) {
13180434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
13190434d1b8SBill Paul 			    BGE_EXT_SSRAM);
132095d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
132195d67482SBill Paul 		} else {
13220434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
13230434d1b8SBill Paul 			    BGE_BUFFPOOL_1);
132495d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
132595d67482SBill Paul 		}
132695d67482SBill Paul 
132795d67482SBill Paul 		/* Configure DMA resource pool */
13280434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
13290434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
133095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
13310434d1b8SBill Paul 	}
133295d67482SBill Paul 
133395d67482SBill Paul 	/* Configure mbuf pool watermarks */
13340434d1b8SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
13350434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
13360434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
13370434d1b8SBill Paul 	} else {
1338fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1339fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
13400434d1b8SBill Paul 	}
1341fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
134295d67482SBill Paul 
134395d67482SBill Paul 	/* Configure DMA resource watermarks */
134495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
134595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
134695d67482SBill Paul 
134795d67482SBill Paul 	/* Enable buffer manager */
13480434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
134995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
135095d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
135195d67482SBill Paul 
135295d67482SBill Paul 		/* Poll for buffer manager start indication */
135395d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
135495d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
135595d67482SBill Paul 				break;
135695d67482SBill Paul 			DELAY(10);
135795d67482SBill Paul 		}
135895d67482SBill Paul 
135995d67482SBill Paul 		if (i == BGE_TIMEOUT) {
136095d67482SBill Paul 			printf("bge%d: buffer manager failed to start\n",
136195d67482SBill Paul 			    sc->bge_unit);
136295d67482SBill Paul 			return(ENXIO);
136395d67482SBill Paul 		}
13640434d1b8SBill Paul 	}
136595d67482SBill Paul 
136695d67482SBill Paul 	/* Enable flow-through queues */
136795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
136895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
136995d67482SBill Paul 
137095d67482SBill Paul 	/* Wait until queue initialization is complete */
137195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
137295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
137395d67482SBill Paul 			break;
137495d67482SBill Paul 		DELAY(10);
137595d67482SBill Paul 	}
137695d67482SBill Paul 
137795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
137895d67482SBill Paul 		printf("bge%d: flow-through queue init failed\n",
137995d67482SBill Paul 		    sc->bge_unit);
138095d67482SBill Paul 		return(ENXIO);
138195d67482SBill Paul 	}
138295d67482SBill Paul 
138395d67482SBill Paul 	/* Initialize the standard RX ring control block */
1384f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1385f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1386f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1387f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1388f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1389f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1390f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
13910434d1b8SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705)
13920434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
13930434d1b8SBill Paul 	else
13940434d1b8SBill Paul 		rcb->bge_maxlen_flags =
13950434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
139695d67482SBill Paul 	if (sc->bge_extram)
139795d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
139895d67482SBill Paul 	else
139995d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
140067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
140167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1402f41ac2beSBill Paul 
140367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
140467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
140595d67482SBill Paul 
140695d67482SBill Paul 	/*
140795d67482SBill Paul 	 * Initialize the jumbo RX ring control block
140895d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
140995d67482SBill Paul 	 * field until we're actually ready to start
141095d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
141195d67482SBill Paul 	 * high enough to require it).
141295d67482SBill Paul 	 */
14130434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
1414f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1415f41ac2beSBill Paul 
1416f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1417f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1418f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1419f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1420f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1421f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1422f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
142367111612SJohn Polstra 		rcb->bge_maxlen_flags =
14240434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN,
14250434d1b8SBill Paul 		    BGE_RCB_FLAG_RING_DISABLED);
142695d67482SBill Paul 		if (sc->bge_extram)
142795d67482SBill Paul 			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
142895d67482SBill Paul 		else
142995d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
143067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
143167111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
143267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
143367111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1434f41ac2beSBill Paul 
14350434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
14360434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
143767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
143895d67482SBill Paul 
143995d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1440f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
144167111612SJohn Polstra 		rcb->bge_maxlen_flags =
144267111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
14430434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
14440434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
14450434d1b8SBill Paul 	}
144695d67482SBill Paul 
144795d67482SBill Paul 	/*
144895d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
144995d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
145095d67482SBill Paul 	 * each ring.
145195d67482SBill Paul 	 */
145295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
145395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
145495d67482SBill Paul 
145595d67482SBill Paul 	/*
145695d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
145795d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
145895d67482SBill Paul 	 * These are located in NIC memory.
145995d67482SBill Paul 	 */
146067111612SJohn Polstra 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
146195d67482SBill Paul 	    BGE_SEND_RING_RCB);
146295d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
146367111612SJohn Polstra 		vrcb->bge_maxlen_flags =
146467111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
146567111612SJohn Polstra 		vrcb->bge_nicaddr = 0;
146667111612SJohn Polstra 		vrcb++;
146795d67482SBill Paul 	}
146895d67482SBill Paul 
146995d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
147067111612SJohn Polstra 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
147195d67482SBill Paul 	    BGE_SEND_RING_RCB);
1472f41ac2beSBill Paul 	vrcb->bge_hostaddr.bge_addr_lo =
1473f41ac2beSBill Paul 	    htole32(BGE_ADDR_LO(sc->bge_ldata.bge_tx_ring_paddr));
1474f41ac2beSBill Paul 	vrcb->bge_hostaddr.bge_addr_hi =
1475f41ac2beSBill Paul 	    htole32(BGE_ADDR_HI(sc->bge_ldata.bge_tx_ring_paddr));
147667111612SJohn Polstra 	vrcb->bge_nicaddr = BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT);
14770434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
14780434d1b8SBill Paul 		vrcb->bge_maxlen_flags =
14790434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0);
148095d67482SBill Paul 
148195d67482SBill Paul 	/* Disable all unused RX return rings */
148267111612SJohn Polstra 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
148395d67482SBill Paul 	    BGE_RX_RETURN_RING_RCB);
148495d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
148567111612SJohn Polstra 		vrcb->bge_hostaddr.bge_addr_hi = 0;
148667111612SJohn Polstra 		vrcb->bge_hostaddr.bge_addr_lo = 0;
148767111612SJohn Polstra 		vrcb->bge_maxlen_flags =
14880434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
148967111612SJohn Polstra 		    BGE_RCB_FLAG_RING_DISABLED);
149067111612SJohn Polstra 		vrcb->bge_nicaddr = 0;
149195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
149295d67482SBill Paul 		    (i * (sizeof(u_int64_t))), 0);
149367111612SJohn Polstra 		vrcb++;
149495d67482SBill Paul 	}
149595d67482SBill Paul 
149695d67482SBill Paul 	/* Initialize RX ring indexes */
149795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
149895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
149995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
150095d67482SBill Paul 
150195d67482SBill Paul 	/*
150295d67482SBill Paul 	 * Set up RX return ring 0
150395d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
150495d67482SBill Paul 	 * The return rings live entirely within the host, so the
150595d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
150695d67482SBill Paul 	 */
150767111612SJohn Polstra 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
150895d67482SBill Paul 	    BGE_RX_RETURN_RING_RCB);
1509f41ac2beSBill Paul 	vrcb->bge_hostaddr.bge_addr_lo =
1510f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_return_ring_paddr);
1511f41ac2beSBill Paul 	vrcb->bge_hostaddr.bge_addr_hi =
1512f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_return_ring_paddr);
1513f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
1514f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREWRITE);
151567111612SJohn Polstra 	vrcb->bge_nicaddr = 0x00000000;
15160434d1b8SBill Paul 	vrcb->bge_maxlen_flags =
15170434d1b8SBill Paul 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0);
151895d67482SBill Paul 
151995d67482SBill Paul 	/* Set random backoff seed for TX */
152095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
152195d67482SBill Paul 	    sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
152295d67482SBill Paul 	    sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
152395d67482SBill Paul 	    sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] +
152495d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
152595d67482SBill Paul 
152695d67482SBill Paul 	/* Set inter-packet gap */
152795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
152895d67482SBill Paul 
152995d67482SBill Paul 	/*
153095d67482SBill Paul 	 * Specify which ring to use for packets that don't match
153195d67482SBill Paul 	 * any RX rules.
153295d67482SBill Paul 	 */
153395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
153495d67482SBill Paul 
153595d67482SBill Paul 	/*
153695d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
153795d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
153895d67482SBill Paul 	 */
153995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
154095d67482SBill Paul 
154195d67482SBill Paul 	/* Inialize RX list placement stats mask. */
154295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
154395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
154495d67482SBill Paul 
154595d67482SBill Paul 	/* Disable host coalescing until we get it set up */
154695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
154795d67482SBill Paul 
154895d67482SBill Paul 	/* Poll to make sure it's shut down. */
154995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
155095d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
155195d67482SBill Paul 			break;
155295d67482SBill Paul 		DELAY(10);
155395d67482SBill Paul 	}
155495d67482SBill Paul 
155595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
155695d67482SBill Paul 		printf("bge%d: host coalescing engine failed to idle\n",
155795d67482SBill Paul 		    sc->bge_unit);
155895d67482SBill Paul 		return(ENXIO);
155995d67482SBill Paul 	}
156095d67482SBill Paul 
156195d67482SBill Paul 	/* Set up host coalescing defaults */
156295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
156395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
156495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
156595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
15660434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
156795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
156895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
15690434d1b8SBill Paul 	}
157095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
157195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
157295d67482SBill Paul 
157395d67482SBill Paul 	/* Set up address of statistics block */
15740434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
1575f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1576f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
157795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1578f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
15790434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
158095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
15810434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
15820434d1b8SBill Paul 	}
15830434d1b8SBill Paul 
15840434d1b8SBill Paul 	/* Set up address of status block */
1585f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1586f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
158795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1588f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1589f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
1590f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREWRITE);
1591f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1592f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
159395d67482SBill Paul 
159495d67482SBill Paul 	/* Turn on host coalescing state machine */
159595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
159695d67482SBill Paul 
159795d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
159895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
159995d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
160095d67482SBill Paul 
160195d67482SBill Paul 	/* Turn on RX list placement state machine */
160295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
160395d67482SBill Paul 
160495d67482SBill Paul 	/* Turn on RX list selector state machine. */
16050434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
160695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
160795d67482SBill Paul 
160895d67482SBill Paul 	/* Turn on DMA, clear stats */
160995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
161095d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
161195d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
161295d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
161395d67482SBill Paul 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
161495d67482SBill Paul 
161595d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
161695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
161795d67482SBill Paul 
161895d67482SBill Paul #ifdef notdef
161995d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
162095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
162195d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
162295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
162395d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
162495d67482SBill Paul #endif
162595d67482SBill Paul 
162695d67482SBill Paul 	/* Turn on DMA completion state machine */
16270434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
162895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
162995d67482SBill Paul 
163095d67482SBill Paul 	/* Turn on write DMA state machine */
163195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
163295d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
163395d67482SBill Paul 
163495d67482SBill Paul 	/* Turn on read DMA state machine */
163595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
163695d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
163795d67482SBill Paul 
163895d67482SBill Paul 	/* Turn on RX data completion state machine */
163995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
164095d67482SBill Paul 
164195d67482SBill Paul 	/* Turn on RX BD initiator state machine */
164295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
164395d67482SBill Paul 
164495d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
164595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
164695d67482SBill Paul 
164795d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
16480434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
164995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
165095d67482SBill Paul 
165195d67482SBill Paul 	/* Turn on send BD completion state machine */
165295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
165395d67482SBill Paul 
165495d67482SBill Paul 	/* Turn on send data completion state machine */
165595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
165695d67482SBill Paul 
165795d67482SBill Paul 	/* Turn on send data initiator state machine */
165895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
165995d67482SBill Paul 
166095d67482SBill Paul 	/* Turn on send BD initiator state machine */
166195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
166295d67482SBill Paul 
166395d67482SBill Paul 	/* Turn on send BD selector state machine */
166495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
166595d67482SBill Paul 
166695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
166795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
166895d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
166995d67482SBill Paul 
167095d67482SBill Paul 	/* ack/clear link change events */
167195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
16720434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
16730434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1674f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
167595d67482SBill Paul 
167695d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
167795d67482SBill Paul 	if (sc->bge_tbi) {
167895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1679a1d52896SBill Paul  	} else {
168095d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
1681e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700)
1682a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1683a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1684a1d52896SBill Paul 	}
168595d67482SBill Paul 
168695d67482SBill Paul 	/* Enable link state change attentions. */
168795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
168895d67482SBill Paul 
168995d67482SBill Paul 	return(0);
169095d67482SBill Paul }
169195d67482SBill Paul 
169295d67482SBill Paul /*
169395d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
169495d67482SBill Paul  * against our list and return its name if we find a match. Note
169595d67482SBill Paul  * that since the Broadcom controller contains VPD support, we
169695d67482SBill Paul  * can get the device name string from the controller itself instead
169795d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
169895d67482SBill Paul  * we'll always announce the right product name.
169995d67482SBill Paul  */
170095d67482SBill Paul static int
170195d67482SBill Paul bge_probe(dev)
170295d67482SBill Paul 	device_t dev;
170395d67482SBill Paul {
170495d67482SBill Paul 	struct bge_type *t;
170595d67482SBill Paul 	struct bge_softc *sc;
1706029e2ee3SJohn Polstra 	char *descbuf;
170795d67482SBill Paul 
170895d67482SBill Paul 	t = bge_devs;
170995d67482SBill Paul 
171095d67482SBill Paul 	sc = device_get_softc(dev);
171195d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
171295d67482SBill Paul 	sc->bge_unit = device_get_unit(dev);
171395d67482SBill Paul 	sc->bge_dev = dev;
171495d67482SBill Paul 
171595d67482SBill Paul 	while(t->bge_name != NULL) {
171695d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
171795d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
171895d67482SBill Paul #ifdef notdef
171995d67482SBill Paul 			bge_vpd_read(sc);
172095d67482SBill Paul 			device_set_desc(dev, sc->bge_vpd_prodname);
172195d67482SBill Paul #endif
1722029e2ee3SJohn Polstra 			descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
1723029e2ee3SJohn Polstra 			if (descbuf == NULL)
1724029e2ee3SJohn Polstra 				return(ENOMEM);
1725029e2ee3SJohn Polstra 			snprintf(descbuf, BGE_DEVDESC_MAX,
1726029e2ee3SJohn Polstra 			    "%s, ASIC rev. %#04x", t->bge_name,
1727029e2ee3SJohn Polstra 			    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16);
1728029e2ee3SJohn Polstra 			device_set_desc_copy(dev, descbuf);
1729029e2ee3SJohn Polstra 			free(descbuf, M_TEMP);
173095d67482SBill Paul 			return(0);
173195d67482SBill Paul 		}
173295d67482SBill Paul 		t++;
173395d67482SBill Paul 	}
173495d67482SBill Paul 
173595d67482SBill Paul 	return(ENXIO);
173695d67482SBill Paul }
173795d67482SBill Paul 
1738f41ac2beSBill Paul static void
1739f41ac2beSBill Paul bge_dma_free(sc)
1740f41ac2beSBill Paul 	struct bge_softc *sc;
1741f41ac2beSBill Paul {
1742f41ac2beSBill Paul 	int i;
1743f41ac2beSBill Paul 
1744f41ac2beSBill Paul 
1745f41ac2beSBill Paul 	/* Destroy DMA maps for RX buffers */
1746f41ac2beSBill Paul 
1747f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1748f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1749f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1750f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1751f41ac2beSBill Paul 	}
1752f41ac2beSBill Paul 
1753f41ac2beSBill Paul 	/* Destroy DMA maps for jumbo RX buffers */
1754f41ac2beSBill Paul 
1755f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1756f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1757f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1758f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1759f41ac2beSBill Paul 	}
1760f41ac2beSBill Paul 
1761f41ac2beSBill Paul 	/* Destroy DMA maps for TX buffers */
1762f41ac2beSBill Paul 
1763f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1764f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1765f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1766f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1767f41ac2beSBill Paul 	}
1768f41ac2beSBill Paul 
1769f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1770f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1771f41ac2beSBill Paul 
1772f41ac2beSBill Paul 
1773f41ac2beSBill Paul 	/* Destroy standard RX ring */
1774f41ac2beSBill Paul 
1775f41ac2beSBill Paul 	if (sc->bge_ldata.bge_rx_std_ring)
1776f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1777f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1778f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1779f41ac2beSBill Paul 
1780f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_map) {
1781f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1782f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1783f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_std_ring_tag,
1784f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1785f41ac2beSBill Paul 	}
1786f41ac2beSBill Paul 
1787f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1788f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1789f41ac2beSBill Paul 
1790f41ac2beSBill Paul 	/* Destroy jumbo RX ring */
1791f41ac2beSBill Paul 
1792f41ac2beSBill Paul 	if (sc->bge_ldata.bge_rx_jumbo_ring)
1793f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1794f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1795f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1796f41ac2beSBill Paul 
1797f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_map) {
1798f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1799f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1800f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1801f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1802f41ac2beSBill Paul 	}
1803f41ac2beSBill Paul 
1804f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1805f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1806f41ac2beSBill Paul 
1807f41ac2beSBill Paul 	/* Destroy RX return ring */
1808f41ac2beSBill Paul 
1809f41ac2beSBill Paul 	if (sc->bge_ldata.bge_rx_return_ring)
1810f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1811f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1812f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1813f41ac2beSBill Paul 
1814f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_map) {
1815f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1816f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1817f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_return_ring_tag,
1818f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1819f41ac2beSBill Paul 	}
1820f41ac2beSBill Paul 
1821f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1822f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1823f41ac2beSBill Paul 
1824f41ac2beSBill Paul 	/* Destroy TX ring */
1825f41ac2beSBill Paul 
1826f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring)
1827f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1828f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1829f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1830f41ac2beSBill Paul 
1831f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_map) {
1832f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1833f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1834f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_tx_ring_tag,
1835f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1836f41ac2beSBill Paul 	}
1837f41ac2beSBill Paul 
1838f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1839f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1840f41ac2beSBill Paul 
1841f41ac2beSBill Paul 	/* Destroy status block */
1842f41ac2beSBill Paul 
1843f41ac2beSBill Paul 	if (sc->bge_ldata.bge_status_block)
1844f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1845f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1846f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1847f41ac2beSBill Paul 
1848f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_map) {
1849f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1850f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1851f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_status_tag,
1852f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1853f41ac2beSBill Paul 	}
1854f41ac2beSBill Paul 
1855f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1856f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1857f41ac2beSBill Paul 
1858f41ac2beSBill Paul 	/* Destroy statistics block */
1859f41ac2beSBill Paul 
1860f41ac2beSBill Paul 	if (sc->bge_ldata.bge_stats)
1861f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1862f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1863f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1864f41ac2beSBill Paul 
1865f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_map) {
1866f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1867f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1868f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_stats_tag,
1869f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1870f41ac2beSBill Paul 	}
1871f41ac2beSBill Paul 
1872f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1873f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1874f41ac2beSBill Paul 
1875f41ac2beSBill Paul 	/* Destroy the parent tag */
1876f41ac2beSBill Paul 
1877f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1878f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1879f41ac2beSBill Paul 
1880f41ac2beSBill Paul 	return;
1881f41ac2beSBill Paul }
1882f41ac2beSBill Paul 
1883f41ac2beSBill Paul static int
1884f41ac2beSBill Paul bge_dma_alloc(dev)
1885f41ac2beSBill Paul 	device_t dev;
1886f41ac2beSBill Paul {
1887f41ac2beSBill Paul 	struct bge_softc *sc;
1888f41ac2beSBill Paul 	int nseg, i, error;
1889f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
1890f41ac2beSBill Paul 
1891f41ac2beSBill Paul 	sc = device_get_softc(dev);
1892f41ac2beSBill Paul 
1893f41ac2beSBill Paul 	/*
1894f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1895f41ac2beSBill Paul 	 */
1896f41ac2beSBill Paul #define BGE_NSEG_NEW 32
1897f41ac2beSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1898f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1899f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
1900f41ac2beSBill Paul 			BUS_SPACE_MAXADDR_32BIT,/* highaddr */
1901f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1902f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1903f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1904f41ac2beSBill Paul                         BUS_DMA_ALLOCNOW,	/* flags */
1905f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1906f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1907f41ac2beSBill Paul 
1908f41ac2beSBill Paul 	/*
1909f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1910f41ac2beSBill Paul 	 */
1911f41ac2beSBill Paul 	nseg = 32;
1912f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, ETHER_ALIGN,
1913f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1914f41ac2beSBill Paul 	    NULL, MCLBYTES * nseg, nseg, MCLBYTES, 0, NULL, NULL,
1915f41ac2beSBill Paul 	    &sc->bge_cdata.bge_mtag);
1916f41ac2beSBill Paul 
1917f41ac2beSBill Paul 	if (error) {
1918f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1919f41ac2beSBill Paul 		return (ENOMEM);
1920f41ac2beSBill Paul 	}
1921f41ac2beSBill Paul 
1922f41ac2beSBill Paul 	/* Create DMA maps for RX buffers */
1923f41ac2beSBill Paul 
1924f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1925f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1926f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1927f41ac2beSBill Paul 		if (error) {
1928f41ac2beSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1929f41ac2beSBill Paul 			return(ENOMEM);
1930f41ac2beSBill Paul 		}
1931f41ac2beSBill Paul 	}
1932f41ac2beSBill Paul 
1933f41ac2beSBill Paul 	/* Create DMA maps for TX buffers */
1934f41ac2beSBill Paul 
1935f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1936f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1937f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1938f41ac2beSBill Paul 		if (error) {
1939f41ac2beSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1940f41ac2beSBill Paul 			return(ENOMEM);
1941f41ac2beSBill Paul 		}
1942f41ac2beSBill Paul 	}
1943f41ac2beSBill Paul 
1944f41ac2beSBill Paul 	/* Create tag for standard RX ring */
1945f41ac2beSBill Paul 
1946f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1947f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1948f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1949f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1950f41ac2beSBill Paul 
1951f41ac2beSBill Paul 	if (error) {
1952f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1953f41ac2beSBill Paul 		return (ENOMEM);
1954f41ac2beSBill Paul 	}
1955f41ac2beSBill Paul 
1956f41ac2beSBill Paul 	/* Allocate DMA'able memory for standard RX ring */
1957f41ac2beSBill Paul 
1958f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1959f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1960f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1961f41ac2beSBill Paul         if (error)
1962f41ac2beSBill Paul                 return (ENOMEM);
1963f41ac2beSBill Paul 
1964f41ac2beSBill Paul         bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1965f41ac2beSBill Paul 
1966f41ac2beSBill Paul 	/* Load the address of the standard RX ring */
1967f41ac2beSBill Paul 
1968f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1969f41ac2beSBill Paul 	ctx.sc = sc;
1970f41ac2beSBill Paul 
1971f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1972f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1973f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1974f41ac2beSBill Paul 
1975f41ac2beSBill Paul 	if (error)
1976f41ac2beSBill Paul 		return (ENOMEM);
1977f41ac2beSBill Paul 
1978f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1979f41ac2beSBill Paul 
1980f41ac2beSBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
1981f41ac2beSBill Paul 
1982f41ac2beSBill Paul 		/*
1983f41ac2beSBill Paul 		 * Create tag for jumbo mbufs.
1984f41ac2beSBill Paul 		 * This is really a bit of a kludge. We allocate a special
1985f41ac2beSBill Paul 		 * jumbo buffer pool which (thanks to the way our DMA
1986f41ac2beSBill Paul 		 * memory allocation works) will consist of contiguous
1987f41ac2beSBill Paul 		 * pages. This means that even though a jumbo buffer might
1988f41ac2beSBill Paul 		 * be larger than a page size, we don't really need to
1989f41ac2beSBill Paul 		 * map it into more than one DMA segment. However, the
1990f41ac2beSBill Paul 		 * default mbuf tag will result in multi-segment mappings,
1991f41ac2beSBill Paul 		 * so we have to create a special jumbo mbuf tag that
1992f41ac2beSBill Paul 		 * lets us get away with mapping the jumbo buffers as
1993f41ac2beSBill Paul 		 * a single segment. I think eventually the driver should
1994f41ac2beSBill Paul 		 * be changed so that it uses ordinary mbufs and cluster
1995f41ac2beSBill Paul 		 * buffers, i.e. jumbo frames can span multiple DMA
1996f41ac2beSBill Paul 		 * descriptors. But that's a project for another day.
1997f41ac2beSBill Paul 		 */
1998f41ac2beSBill Paul 
1999f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2000f41ac2beSBill Paul 		    ETHER_ALIGN, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2001f41ac2beSBill Paul 		    NULL, MCLBYTES * nseg, nseg, BGE_JLEN, 0, NULL, NULL,
2002f41ac2beSBill Paul 		    &sc->bge_cdata.bge_mtag_jumbo);
2003f41ac2beSBill Paul 
2004f41ac2beSBill Paul 		if (error) {
2005f41ac2beSBill Paul 			device_printf(dev, "could not allocate dma tag\n");
2006f41ac2beSBill Paul 			return (ENOMEM);
2007f41ac2beSBill Paul 		}
2008f41ac2beSBill Paul 
2009f41ac2beSBill Paul 		/* Create tag for jumbo RX ring */
2010f41ac2beSBill Paul 
2011f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2012f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2013f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
2014f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
2015f41ac2beSBill Paul 
2016f41ac2beSBill Paul 		if (error) {
2017f41ac2beSBill Paul 			device_printf(dev, "could not allocate dma tag\n");
2018f41ac2beSBill Paul 			return (ENOMEM);
2019f41ac2beSBill Paul 		}
2020f41ac2beSBill Paul 
2021f41ac2beSBill Paul 		/* Allocate DMA'able memory for jumbo RX ring */
2022f41ac2beSBill Paul 
2023f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2024f41ac2beSBill Paul 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring, BUS_DMA_NOWAIT,
2025f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
2026f41ac2beSBill Paul 		if (error)
2027f41ac2beSBill Paul 			return (ENOMEM);
2028f41ac2beSBill Paul 
2029f41ac2beSBill Paul 		bzero((char *)sc->bge_ldata.bge_rx_jumbo_ring,
2030f41ac2beSBill Paul 		    BGE_JUMBO_RX_RING_SZ);
2031f41ac2beSBill Paul 
2032f41ac2beSBill Paul 		/* Load the address of the jumbo RX ring */
2033f41ac2beSBill Paul 
2034f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
2035f41ac2beSBill Paul 		ctx.sc = sc;
2036f41ac2beSBill Paul 
2037f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2038f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2039f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
2040f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2041f41ac2beSBill Paul 
2042f41ac2beSBill Paul 		if (error)
2043f41ac2beSBill Paul 			return (ENOMEM);
2044f41ac2beSBill Paul 
2045f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
2046f41ac2beSBill Paul 
2047f41ac2beSBill Paul 		/* Create DMA maps for jumbo RX buffers */
2048f41ac2beSBill Paul 
2049f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2050f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2051f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2052f41ac2beSBill Paul 			if (error) {
2053f41ac2beSBill Paul 				device_printf(dev,
2054f41ac2beSBill Paul 				    "can't create DMA map for RX\n");
2055f41ac2beSBill Paul 				return(ENOMEM);
2056f41ac2beSBill Paul 			}
2057f41ac2beSBill Paul 		}
2058f41ac2beSBill Paul 
2059f41ac2beSBill Paul 	}
2060f41ac2beSBill Paul 
2061f41ac2beSBill Paul 	/* Create tag for RX return ring */
2062f41ac2beSBill Paul 
2063f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2064f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2065f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
2066f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
2067f41ac2beSBill Paul 
2068f41ac2beSBill Paul 	if (error) {
2069f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
2070f41ac2beSBill Paul 		return (ENOMEM);
2071f41ac2beSBill Paul 	}
2072f41ac2beSBill Paul 
2073f41ac2beSBill Paul 	/* Allocate DMA'able memory for RX return ring */
2074f41ac2beSBill Paul 
2075f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
2076f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
2077f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
2078f41ac2beSBill Paul         if (error)
2079f41ac2beSBill Paul                 return (ENOMEM);
2080f41ac2beSBill Paul 
2081f41ac2beSBill Paul         bzero((char *)sc->bge_ldata.bge_rx_return_ring,
2082f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
2083f41ac2beSBill Paul 
2084f41ac2beSBill Paul 	/* Load the address of the RX return ring */
2085f41ac2beSBill Paul 
2086f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2087f41ac2beSBill Paul 	ctx.sc = sc;
2088f41ac2beSBill Paul 
2089f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
2090f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
2091f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
2092f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2093f41ac2beSBill Paul 
2094f41ac2beSBill Paul 	if (error)
2095f41ac2beSBill Paul 		return (ENOMEM);
2096f41ac2beSBill Paul 
2097f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2098f41ac2beSBill Paul 
2099f41ac2beSBill Paul 	/* Create tag for TX ring */
2100f41ac2beSBill Paul 
2101f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2102f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2103f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2104f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
2105f41ac2beSBill Paul 
2106f41ac2beSBill Paul 	if (error) {
2107f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
2108f41ac2beSBill Paul 		return (ENOMEM);
2109f41ac2beSBill Paul 	}
2110f41ac2beSBill Paul 
2111f41ac2beSBill Paul 	/* Allocate DMA'able memory for TX ring */
2112f41ac2beSBill Paul 
2113f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2114f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2115f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
2116f41ac2beSBill Paul         if (error)
2117f41ac2beSBill Paul                 return (ENOMEM);
2118f41ac2beSBill Paul 
2119f41ac2beSBill Paul         bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2120f41ac2beSBill Paul 
2121f41ac2beSBill Paul 	/* Load the address of the TX ring */
2122f41ac2beSBill Paul 
2123f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2124f41ac2beSBill Paul 	ctx.sc = sc;
2125f41ac2beSBill Paul 
2126f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2127f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2128f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2129f41ac2beSBill Paul 
2130f41ac2beSBill Paul 	if (error)
2131f41ac2beSBill Paul 		return (ENOMEM);
2132f41ac2beSBill Paul 
2133f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2134f41ac2beSBill Paul 
2135f41ac2beSBill Paul 	/* Create tag for status block */
2136f41ac2beSBill Paul 
2137f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2138f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2139f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2140f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2141f41ac2beSBill Paul 
2142f41ac2beSBill Paul 	if (error) {
2143f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
2144f41ac2beSBill Paul 		return (ENOMEM);
2145f41ac2beSBill Paul 	}
2146f41ac2beSBill Paul 
2147f41ac2beSBill Paul 	/* Allocate DMA'able memory for status block */
2148f41ac2beSBill Paul 
2149f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2150f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2151f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
2152f41ac2beSBill Paul         if (error)
2153f41ac2beSBill Paul                 return (ENOMEM);
2154f41ac2beSBill Paul 
2155f41ac2beSBill Paul         bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2156f41ac2beSBill Paul 
2157f41ac2beSBill Paul 	/* Load the address of the status block */
2158f41ac2beSBill Paul 
2159f41ac2beSBill Paul 	ctx.sc = sc;
2160f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2161f41ac2beSBill Paul 
2162f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2163f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2164f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2165f41ac2beSBill Paul 
2166f41ac2beSBill Paul 	if (error)
2167f41ac2beSBill Paul 		return (ENOMEM);
2168f41ac2beSBill Paul 
2169f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2170f41ac2beSBill Paul 
2171f41ac2beSBill Paul 	/* Create tag for statistics block */
2172f41ac2beSBill Paul 
2173f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2174f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2175f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2176f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2177f41ac2beSBill Paul 
2178f41ac2beSBill Paul 	if (error) {
2179f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
2180f41ac2beSBill Paul 		return (ENOMEM);
2181f41ac2beSBill Paul 	}
2182f41ac2beSBill Paul 
2183f41ac2beSBill Paul 	/* Allocate DMA'able memory for statistics block */
2184f41ac2beSBill Paul 
2185f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2186f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2187f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2188f41ac2beSBill Paul         if (error)
2189f41ac2beSBill Paul                 return (ENOMEM);
2190f41ac2beSBill Paul 
2191f41ac2beSBill Paul         bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2192f41ac2beSBill Paul 
2193f41ac2beSBill Paul 	/* Load the address of the statstics block */
2194f41ac2beSBill Paul 
2195f41ac2beSBill Paul 	ctx.sc = sc;
2196f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2197f41ac2beSBill Paul 
2198f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2199f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2200f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2201f41ac2beSBill Paul 
2202f41ac2beSBill Paul 	if (error)
2203f41ac2beSBill Paul 		return (ENOMEM);
2204f41ac2beSBill Paul 
2205f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2206f41ac2beSBill Paul 
2207f41ac2beSBill Paul 	return(0);
2208f41ac2beSBill Paul }
2209f41ac2beSBill Paul 
221095d67482SBill Paul static int
221195d67482SBill Paul bge_attach(dev)
221295d67482SBill Paul 	device_t dev;
221395d67482SBill Paul {
221495d67482SBill Paul 	int s;
221595d67482SBill Paul 	struct ifnet *ifp;
221695d67482SBill Paul 	struct bge_softc *sc;
2217a1d52896SBill Paul 	u_int32_t hwcfg = 0;
2218b1265c1aSJohn Polstra 	u_int32_t mac_addr = 0;
221995d67482SBill Paul 	int unit, error = 0, rid;
222095d67482SBill Paul 
222195d67482SBill Paul 	s = splimp();
222295d67482SBill Paul 
222395d67482SBill Paul 	sc = device_get_softc(dev);
222495d67482SBill Paul 	unit = device_get_unit(dev);
222595d67482SBill Paul 	sc->bge_dev = dev;
222695d67482SBill Paul 	sc->bge_unit = unit;
222795d67482SBill Paul 
222895d67482SBill Paul 	/*
222995d67482SBill Paul 	 * Map control/status registers.
223095d67482SBill Paul 	 */
223195d67482SBill Paul 	pci_enable_busmaster(dev);
223295d67482SBill Paul 
223395d67482SBill Paul 	rid = BGE_PCI_BAR0;
223495d67482SBill Paul 	sc->bge_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
2235306a2090SMatt Jacob 	    0, ~0, 1, RF_ACTIVE|PCI_RF_DENSE);
223695d67482SBill Paul 
223795d67482SBill Paul 	if (sc->bge_res == NULL) {
223895d67482SBill Paul 		printf ("bge%d: couldn't map memory\n", unit);
223995d67482SBill Paul 		error = ENXIO;
224095d67482SBill Paul 		goto fail;
224195d67482SBill Paul 	}
224295d67482SBill Paul 
224395d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
224495d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
224595d67482SBill Paul 	sc->bge_vhandle = (vm_offset_t)rman_get_virtual(sc->bge_res);
224695d67482SBill Paul 
224795d67482SBill Paul 	/* Allocate interrupt */
224895d67482SBill Paul 	rid = 0;
224995d67482SBill Paul 
225095d67482SBill Paul 	sc->bge_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
225195d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
225295d67482SBill Paul 
225395d67482SBill Paul 	if (sc->bge_irq == NULL) {
225495d67482SBill Paul 		printf("bge%d: couldn't map interrupt\n", unit);
225595d67482SBill Paul 		error = ENXIO;
225695d67482SBill Paul 		goto fail;
225795d67482SBill Paul 	}
225895d67482SBill Paul 
225995d67482SBill Paul 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET,
226095d67482SBill Paul 	   bge_intr, sc, &sc->bge_intrhand);
226195d67482SBill Paul 
226295d67482SBill Paul 	if (error) {
226395d67482SBill Paul 		bge_release_resources(sc);
226495d67482SBill Paul 		printf("bge%d: couldn't set up irq\n", unit);
226595d67482SBill Paul 		goto fail;
226695d67482SBill Paul 	}
226795d67482SBill Paul 
226895d67482SBill Paul 	sc->bge_unit = unit;
226995d67482SBill Paul 
227095d67482SBill Paul 	/* Try to reset the chip. */
227195d67482SBill Paul 	bge_reset(sc);
227295d67482SBill Paul 
227395d67482SBill Paul 	if (bge_chipinit(sc)) {
227495d67482SBill Paul 		printf("bge%d: chip initialization failed\n", sc->bge_unit);
227595d67482SBill Paul 		bge_release_resources(sc);
227695d67482SBill Paul 		error = ENXIO;
227795d67482SBill Paul 		goto fail;
227895d67482SBill Paul 	}
227995d67482SBill Paul 
228095d67482SBill Paul 	/*
228195d67482SBill Paul 	 * Get station address from the EEPROM.
228295d67482SBill Paul 	 */
2283b1265c1aSJohn Polstra 	mac_addr = bge_readmem_ind(sc, 0x0c14);
2284b1265c1aSJohn Polstra 	if ((mac_addr >> 16) == 0x484b) {
2285b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[0] = (u_char)(mac_addr >> 8);
2286b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[1] = (u_char)mac_addr;
2287b1265c1aSJohn Polstra 		mac_addr = bge_readmem_ind(sc, 0x0c18);
2288b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[2] = (u_char)(mac_addr >> 24);
2289b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[3] = (u_char)(mac_addr >> 16);
2290b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[4] = (u_char)(mac_addr >> 8);
2291b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[5] = (u_char)mac_addr;
2292b1265c1aSJohn Polstra 	} else if (bge_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
229395d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
229495d67482SBill Paul 		printf("bge%d: failed to read station address\n", unit);
229595d67482SBill Paul 		bge_release_resources(sc);
229695d67482SBill Paul 		error = ENXIO;
229795d67482SBill Paul 		goto fail;
229895d67482SBill Paul 	}
229995d67482SBill Paul 
230095d67482SBill Paul 	/*
230195d67482SBill Paul 	 * A Broadcom chip was detected. Inform the world.
230295d67482SBill Paul 	 */
230395d67482SBill Paul 	printf("bge%d: Ethernet address: %6D\n", unit,
230495d67482SBill Paul 	    sc->arpcom.ac_enaddr, ":");
230595d67482SBill Paul 
23060434d1b8SBill Paul 	/* Save ASIC rev. */
23070434d1b8SBill Paul 
23080434d1b8SBill Paul 	sc->bge_chipid =
23090434d1b8SBill Paul 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
23100434d1b8SBill Paul 	    BGE_PCIMISCCTL_ASICREV;
23110434d1b8SBill Paul 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
23120434d1b8SBill Paul 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
23130434d1b8SBill Paul 
2314f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
2315f41ac2beSBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705)
2316f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2317f41ac2beSBill Paul 	else
2318f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2319f41ac2beSBill Paul 
2320f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2321f41ac2beSBill Paul 		printf ("bge%d: failed to allocate DMA resources\n",
2322f41ac2beSBill Paul 		    sc->bge_unit);
2323f41ac2beSBill Paul 		bge_release_resources(sc);
2324f41ac2beSBill Paul 		error = ENXIO;
2325f41ac2beSBill Paul 		goto fail;
2326f41ac2beSBill Paul 	}
2327f41ac2beSBill Paul 
23280434d1b8SBill Paul 	/*
23290434d1b8SBill Paul 	 * Try to allocate memory for jumbo buffers.
23300434d1b8SBill Paul 	 * The 5705 does not appear to support jumbo frames.
23310434d1b8SBill Paul 	 */
23320434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
233395d67482SBill Paul 		if (bge_alloc_jumbo_mem(sc)) {
233495d67482SBill Paul 			printf("bge%d: jumbo buffer allocation "
233595d67482SBill Paul 			    "failed\n", sc->bge_unit);
233695d67482SBill Paul 			bge_release_resources(sc);
233795d67482SBill Paul 			error = ENXIO;
233895d67482SBill Paul 			goto fail;
233995d67482SBill Paul 		}
23400434d1b8SBill Paul 	}
234195d67482SBill Paul 
234295d67482SBill Paul 	/* Set default tuneable values. */
234395d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
234495d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
234595d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
234695d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
234795d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
234895d67482SBill Paul 
234995d67482SBill Paul 	/* Set up ifnet structure */
235095d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
235195d67482SBill Paul 	ifp->if_softc = sc;
235295d67482SBill Paul 	ifp->if_unit = sc->bge_unit;
235395d67482SBill Paul 	ifp->if_name = "bge";
235495d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
235595d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
235695d67482SBill Paul 	ifp->if_output = ether_output;
235795d67482SBill Paul 	ifp->if_start = bge_start;
235895d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
235995d67482SBill Paul 	ifp->if_init = bge_init;
236095d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
236195d67482SBill Paul 	ifp->if_snd.ifq_maxlen = BGE_TX_RING_CNT - 1;
236295d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
23630434d1b8SBill Paul 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
23640434d1b8SBill Paul 	    IFCAP_VLAN_MTU;
236595d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
236695d67482SBill Paul 
2367a1d52896SBill Paul 	/*
2368a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
236941abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
237041abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
237141abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
237241abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
237341abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
237441abcc1bSPaul Saab 	 * SK-9D41.
2375a1d52896SBill Paul 	 */
237641abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
237741abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
237841abcc1bSPaul Saab 	else {
2379a1d52896SBill Paul 		bge_read_eeprom(sc, (caddr_t)&hwcfg,
2380a1d52896SBill Paul 				BGE_EE_HWCFG_OFFSET, sizeof(hwcfg));
238141abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
238241abcc1bSPaul Saab 	}
238341abcc1bSPaul Saab 
238441abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2385a1d52896SBill Paul 		sc->bge_tbi = 1;
2386a1d52896SBill Paul 
238795d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
238895d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
238995d67482SBill Paul 		sc->bge_tbi = 1;
239095d67482SBill Paul 
239195d67482SBill Paul 	if (sc->bge_tbi) {
239295d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
239395d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
239495d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
239595d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
239695d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
239795d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
239895d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
239995d67482SBill Paul 	} else {
240095d67482SBill Paul 		/*
240195d67482SBill Paul 		 * Do transceiver setup.
240295d67482SBill Paul 		 */
240395d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
240495d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
240595d67482SBill Paul 			printf("bge%d: MII without any PHY!\n", sc->bge_unit);
240695d67482SBill Paul 			bge_release_resources(sc);
240795d67482SBill Paul 			bge_free_jumbo_mem(sc);
240895d67482SBill Paul 			error = ENXIO;
240995d67482SBill Paul 			goto fail;
241095d67482SBill Paul 		}
241195d67482SBill Paul 	}
241295d67482SBill Paul 
241395d67482SBill Paul 	/*
2414e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2415e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2416e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2417e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2418e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2419e255b776SJohn Polstra 	 * payloads by copying the received packets.
2420e255b776SJohn Polstra 	 */
2421e0ced696SPaul Saab 	switch (sc->bge_chipid) {
2422e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_A0:
2423e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B0:
2424e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B2:
2425e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B5:
2426e255b776SJohn Polstra 		/* If in PCI-X mode, work around the alignment bug. */
2427e255b776SJohn Polstra 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
2428e255b776SJohn Polstra 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2429e255b776SJohn Polstra 		    BGE_PCISTATE_PCI_BUSSPEED)
2430e255b776SJohn Polstra 			sc->bge_rx_alignment_bug = 1;
2431e255b776SJohn Polstra 		break;
2432e255b776SJohn Polstra 	}
2433e255b776SJohn Polstra 
2434e255b776SJohn Polstra 	/*
243595d67482SBill Paul 	 * Call MI attach routine.
243695d67482SBill Paul 	 */
2437673d9191SSam Leffler 	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
243895d67482SBill Paul 	callout_handle_init(&sc->bge_stat_ch);
243995d67482SBill Paul 
244095d67482SBill Paul fail:
244195d67482SBill Paul 	splx(s);
244295d67482SBill Paul 
244395d67482SBill Paul 	return(error);
244495d67482SBill Paul }
244595d67482SBill Paul 
244695d67482SBill Paul static int
244795d67482SBill Paul bge_detach(dev)
244895d67482SBill Paul 	device_t dev;
244995d67482SBill Paul {
245095d67482SBill Paul 	struct bge_softc *sc;
245195d67482SBill Paul 	struct ifnet *ifp;
245295d67482SBill Paul 	int s;
245395d67482SBill Paul 
245495d67482SBill Paul 	s = splimp();
245595d67482SBill Paul 
245695d67482SBill Paul 	sc = device_get_softc(dev);
245795d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
245895d67482SBill Paul 
2459673d9191SSam Leffler 	ether_ifdetach(ifp);
246095d67482SBill Paul 	bge_stop(sc);
246195d67482SBill Paul 	bge_reset(sc);
246295d67482SBill Paul 
246395d67482SBill Paul 	if (sc->bge_tbi) {
246495d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
246595d67482SBill Paul 	} else {
246695d67482SBill Paul 		bus_generic_detach(dev);
246795d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
246895d67482SBill Paul 	}
246995d67482SBill Paul 
247095d67482SBill Paul 	bge_release_resources(sc);
24710434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
247295d67482SBill Paul 		bge_free_jumbo_mem(sc);
247395d67482SBill Paul 
247495d67482SBill Paul 	splx(s);
247595d67482SBill Paul 
247695d67482SBill Paul 	return(0);
247795d67482SBill Paul }
247895d67482SBill Paul 
247995d67482SBill Paul static void
248095d67482SBill Paul bge_release_resources(sc)
248195d67482SBill Paul 	struct bge_softc *sc;
248295d67482SBill Paul {
248395d67482SBill Paul         device_t dev;
248495d67482SBill Paul 
248595d67482SBill Paul         dev = sc->bge_dev;
248695d67482SBill Paul 
248795d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
248895d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
248995d67482SBill Paul 
249095d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
249195d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
249295d67482SBill Paul 
249395d67482SBill Paul         if (sc->bge_intrhand != NULL)
249495d67482SBill Paul                 bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
249595d67482SBill Paul 
249695d67482SBill Paul         if (sc->bge_irq != NULL)
249795d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
249895d67482SBill Paul 
249995d67482SBill Paul         if (sc->bge_res != NULL)
250095d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
250195d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
250295d67482SBill Paul 
2503f41ac2beSBill Paul 	bge_dma_free(sc);
250495d67482SBill Paul 
250595d67482SBill Paul         return;
250695d67482SBill Paul }
250795d67482SBill Paul 
250895d67482SBill Paul static void
250995d67482SBill Paul bge_reset(sc)
251095d67482SBill Paul 	struct bge_softc *sc;
251195d67482SBill Paul {
251295d67482SBill Paul 	device_t dev;
251395d67482SBill Paul 	u_int32_t cachesize, command, pcistate;
251495d67482SBill Paul 	int i, val = 0;
251595d67482SBill Paul 
251695d67482SBill Paul 	dev = sc->bge_dev;
251795d67482SBill Paul 
251895d67482SBill Paul 	/* Save some important PCI state. */
251995d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
252095d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
252195d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
252295d67482SBill Paul 
252395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
252495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
252595d67482SBill Paul 	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
252695d67482SBill Paul 
252795d67482SBill Paul 	/* Issue global reset */
252895d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG,
252995d67482SBill Paul 	    BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1));
253095d67482SBill Paul 
253195d67482SBill Paul 	DELAY(1000);
253295d67482SBill Paul 
253395d67482SBill Paul 	/* Reset some of the PCI state that got zapped by reset */
253495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
253595d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
253695d67482SBill Paul 	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
253795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
253895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
253995d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
254095d67482SBill Paul 
254195d67482SBill Paul 	/*
254295d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
254395d67482SBill Paul 	 * general communications memory at 0xB50.
254495d67482SBill Paul 	 */
254595d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
254695d67482SBill Paul 	/*
254795d67482SBill Paul 	 * Poll the value location we just wrote until
254895d67482SBill Paul 	 * we see the 1's complement of the magic number.
254995d67482SBill Paul 	 * This indicates that the firmware initialization
255095d67482SBill Paul 	 * is complete.
255195d67482SBill Paul 	 */
255295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
255395d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
255495d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
255595d67482SBill Paul 			break;
255695d67482SBill Paul 		DELAY(10);
255795d67482SBill Paul 	}
255895d67482SBill Paul 
255995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
256095d67482SBill Paul 		printf("bge%d: firmware handshake timed out\n", sc->bge_unit);
256195d67482SBill Paul 		return;
256295d67482SBill Paul 	}
256395d67482SBill Paul 
256495d67482SBill Paul 	/*
256595d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
256695d67482SBill Paul 	 * return to its original pre-reset state. This is a
256795d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
256895d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
256995d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
257095d67482SBill Paul 	 * results.
257195d67482SBill Paul 	 */
257295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
257395d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
257495d67482SBill Paul 			break;
257595d67482SBill Paul 		DELAY(10);
257695d67482SBill Paul 	}
257795d67482SBill Paul 
257895d67482SBill Paul 	/* Enable memory arbiter. */
25790434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
258095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
258195d67482SBill Paul 
258295d67482SBill Paul 	/* Fix up byte swapping */
258395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_BYTESWAP_NONFRAME|
258495d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
258595d67482SBill Paul 
258695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
258795d67482SBill Paul 
258895d67482SBill Paul 	DELAY(10000);
258995d67482SBill Paul 
259095d67482SBill Paul 	return;
259195d67482SBill Paul }
259295d67482SBill Paul 
259395d67482SBill Paul /*
259495d67482SBill Paul  * Frame reception handling. This is called if there's a frame
259595d67482SBill Paul  * on the receive return list.
259695d67482SBill Paul  *
259795d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
259895d67482SBill Paul  * 1) the frame is from the jumbo recieve ring
259995d67482SBill Paul  * 2) the frame is from the standard receive ring
260095d67482SBill Paul  */
260195d67482SBill Paul 
260295d67482SBill Paul static void
260395d67482SBill Paul bge_rxeof(sc)
260495d67482SBill Paul 	struct bge_softc *sc;
260595d67482SBill Paul {
260695d67482SBill Paul 	struct ifnet *ifp;
260795d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
260895d67482SBill Paul 
260995d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
261095d67482SBill Paul 
2611f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2612f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTWRITE);
2613f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2614f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
2615f41ac2beSBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
2616f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2617f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2618f41ac2beSBill Paul 		    BUS_DMASYNC_POSTREAD);
2619f41ac2beSBill Paul 	}
2620f41ac2beSBill Paul 
262195d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2622f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
262395d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
262495d67482SBill Paul 		u_int32_t		rxidx;
262595d67482SBill Paul 		struct ether_header	*eh;
262695d67482SBill Paul 		struct mbuf		*m = NULL;
262795d67482SBill Paul 		u_int16_t		vlan_tag = 0;
262895d67482SBill Paul 		int			have_tag = 0;
262995d67482SBill Paul 
263095d67482SBill Paul 		cur_rx =
2631f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
263295d67482SBill Paul 
263395d67482SBill Paul 		rxidx = cur_rx->bge_idx;
26340434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
263595d67482SBill Paul 
263695d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
263795d67482SBill Paul 			have_tag = 1;
263895d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
263995d67482SBill Paul 		}
264095d67482SBill Paul 
264195d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
264295d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2643f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2644f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2645f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2646f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2647f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
264895d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
264995d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
265095d67482SBill Paul 			jumbocnt++;
265195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
265295d67482SBill Paul 				ifp->if_ierrors++;
265395d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
265495d67482SBill Paul 				continue;
265595d67482SBill Paul 			}
265695d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
265795d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
265895d67482SBill Paul 				ifp->if_ierrors++;
265995d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
266095d67482SBill Paul 				continue;
266195d67482SBill Paul 			}
266295d67482SBill Paul 		} else {
266395d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2664f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2665f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2666f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2667f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2668f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
266995d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
267095d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
267195d67482SBill Paul 			stdcnt++;
267295d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
267395d67482SBill Paul 				ifp->if_ierrors++;
267495d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
267595d67482SBill Paul 				continue;
267695d67482SBill Paul 			}
267795d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
267895d67482SBill Paul 			    NULL) == ENOBUFS) {
267995d67482SBill Paul 				ifp->if_ierrors++;
268095d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
268195d67482SBill Paul 				continue;
268295d67482SBill Paul 			}
268395d67482SBill Paul 		}
268495d67482SBill Paul 
268595d67482SBill Paul 		ifp->if_ipackets++;
2686e255b776SJohn Polstra #ifndef __i386__
2687e255b776SJohn Polstra 		/*
2688e255b776SJohn Polstra 		 * The i386 allows unaligned accesses, but for other
2689e255b776SJohn Polstra 		 * platforms we must make sure the payload is aligned.
2690e255b776SJohn Polstra 		 */
2691e255b776SJohn Polstra 		if (sc->bge_rx_alignment_bug) {
2692e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2693e255b776SJohn Polstra 			    cur_rx->bge_len);
2694e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2695e255b776SJohn Polstra 		}
2696e255b776SJohn Polstra #endif
269795d67482SBill Paul 		eh = mtod(m, struct ether_header *);
269895d67482SBill Paul 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len;
269995d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
270095d67482SBill Paul 
2701eb48892eSDavid Greenman #if 0 /* currently broken for some packets, possibly related to TCP options */
270295d67482SBill Paul 		if (ifp->if_hwassist) {
270395d67482SBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
270495d67482SBill Paul 			if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
270595d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
270695d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
270795d67482SBill Paul 				m->m_pkthdr.csum_data =
270895d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
27090189c944SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
271095d67482SBill Paul 			}
271195d67482SBill Paul 		}
2712eb48892eSDavid Greenman #endif
271395d67482SBill Paul 
271495d67482SBill Paul 		/*
2715673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2716673d9191SSam Leffler 		 * attach that information to the packet.
271795d67482SBill Paul 		 */
2718673d9191SSam Leffler 		if (have_tag)
2719673d9191SSam Leffler 			VLAN_INPUT_TAG(ifp, m, vlan_tag, continue);
272095d67482SBill Paul 
2721673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
272295d67482SBill Paul 	}
272395d67482SBill Paul 
2724f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2725f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREWRITE);
2726f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2727f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
2728f41ac2beSBill Paul 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_PREWRITE);
2729f41ac2beSBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
2730f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2731f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2732f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2733f41ac2beSBill Paul 	}
2734f41ac2beSBill Paul 
273595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
273695d67482SBill Paul 	if (stdcnt)
273795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
273895d67482SBill Paul 	if (jumbocnt)
273995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
274095d67482SBill Paul 
274195d67482SBill Paul 	return;
274295d67482SBill Paul }
274395d67482SBill Paul 
274495d67482SBill Paul static void
274595d67482SBill Paul bge_txeof(sc)
274695d67482SBill Paul 	struct bge_softc *sc;
274795d67482SBill Paul {
274895d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
274995d67482SBill Paul 	struct ifnet *ifp;
275095d67482SBill Paul 
275195d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
275295d67482SBill Paul 
275395d67482SBill Paul 	/*
275495d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
275595d67482SBill Paul 	 * frames that have been sent.
275695d67482SBill Paul 	 */
275795d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2758f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
275995d67482SBill Paul 		u_int32_t		idx = 0;
276095d67482SBill Paul 
276195d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2762f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
276395d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
276495d67482SBill Paul 			ifp->if_opackets++;
276595d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
276695d67482SBill Paul 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
276795d67482SBill Paul 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
2768f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2769f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
277095d67482SBill Paul 		}
277195d67482SBill Paul 		sc->bge_txcnt--;
277295d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
277395d67482SBill Paul 		ifp->if_timer = 0;
277495d67482SBill Paul 	}
277595d67482SBill Paul 
277695d67482SBill Paul 	if (cur_tx != NULL)
277795d67482SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
277895d67482SBill Paul 
277995d67482SBill Paul 	return;
278095d67482SBill Paul }
278195d67482SBill Paul 
278295d67482SBill Paul static void
278395d67482SBill Paul bge_intr(xsc)
278495d67482SBill Paul 	void *xsc;
278595d67482SBill Paul {
278695d67482SBill Paul 	struct bge_softc *sc;
278795d67482SBill Paul 	struct ifnet *ifp;
2788487a8c7eSPaul Saab 	u_int32_t statusword;
278922606b20SBill Paul 	u_int32_t status;
279095d67482SBill Paul 
279195d67482SBill Paul 	sc = xsc;
279295d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
2793f41ac2beSBill Paul 
2794f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2795f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTWRITE);
2796f41ac2beSBill Paul 
2797487a8c7eSPaul Saab 	statusword =
2798f41ac2beSBill Paul 	    atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
279995d67482SBill Paul 
280095d67482SBill Paul #ifdef notdef
280195d67482SBill Paul 	/* Avoid this for now -- checking this register is expensive. */
280295d67482SBill Paul 	/* Make sure this is really our interrupt. */
280395d67482SBill Paul 	if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
280495d67482SBill Paul 		return;
280595d67482SBill Paul #endif
280695d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
280795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
280895d67482SBill Paul 
2809a1d52896SBill Paul 	/*
2810a1d52896SBill Paul 	 * Process link state changes.
2811a1d52896SBill Paul 	 * Grrr. The link status word in the status block does
2812a1d52896SBill Paul 	 * not work correctly on the BCM5700 rev AX and BX chips,
2813a1d52896SBill Paul 	 * according to all avaibable information. Hence, we have
2814a1d52896SBill Paul 	 * to enable MII interrupts in order to properly obtain
2815a1d52896SBill Paul 	 * async link changes. Unfortunately, this also means that
2816a1d52896SBill Paul 	 * we have to read the MAC status register to detect link
2817a1d52896SBill Paul 	 * changes, thereby adding an additional register access to
2818a1d52896SBill Paul 	 * the interrupt handler.
2819a1d52896SBill Paul 	 */
2820a1d52896SBill Paul 
2821e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700) {
2822a1d52896SBill Paul 
2823a1d52896SBill Paul 		status = CSR_READ_4(sc, BGE_MAC_STS);
2824a1d52896SBill Paul 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
282595d67482SBill Paul 			sc->bge_link = 0;
282695d67482SBill Paul 			untimeout(bge_tick, sc, sc->bge_stat_ch);
282795d67482SBill Paul 			bge_tick(sc);
2828a1d52896SBill Paul 			/* Clear the interrupt */
2829a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2830a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2831a1d52896SBill Paul 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
2832a1d52896SBill Paul 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
2833a1d52896SBill Paul 			    BRGPHY_INTRS);
283498b28ee5SBill Paul 		}
2835a1d52896SBill Paul 	} else {
2836487a8c7eSPaul Saab 		if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED) {
283722606b20SBill Paul 			/*
283822606b20SBill Paul 			 * Sometimes PCS encoding errors are detected in
283922606b20SBill Paul 			 * TBI mode (on fiber NICs), and for some reason
284022606b20SBill Paul 			 * the chip will signal them as link changes.
284122606b20SBill Paul 			 * If we get a link change event, but the 'PCS
284222606b20SBill Paul 			 * encoding error' bit in the MAC status register
284322606b20SBill Paul 			 * is set, don't bother doing a link check.
284422606b20SBill Paul 			 * This avoids spurious "gigabit link up" messages
284522606b20SBill Paul 			 * that sometimes appear on fiber NICs during
284622606b20SBill Paul 			 * periods of heavy traffic. (There should be no
284722606b20SBill Paul 			 * effect on copper NICs.)
284822606b20SBill Paul 			 */
284922606b20SBill Paul 			status = CSR_READ_4(sc, BGE_MAC_STS);
285022606b20SBill Paul 			if (!(status & BGE_MACSTAT_PORT_DECODE_ERROR)) {
2851a1d52896SBill Paul 				sc->bge_link = 0;
2852a1d52896SBill Paul 				untimeout(bge_tick, sc, sc->bge_stat_ch);
2853a1d52896SBill Paul 				bge_tick(sc);
285422606b20SBill Paul 			}
2855a1d52896SBill Paul 			/* Clear the interrupt */
285695d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
28570434d1b8SBill Paul 			    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
28580434d1b8SBill Paul 			    BGE_MACSTAT_LINK_CHANGED);
285937ceeb4dSPaul Saab 
286037ceeb4dSPaul Saab 			/* Force flush the status block cached by PCI bridge */
286137ceeb4dSPaul Saab 			CSR_READ_4(sc, BGE_MBX_IRQ0_LO);
2862a1d52896SBill Paul 		}
286395d67482SBill Paul 	}
286495d67482SBill Paul 
286595d67482SBill Paul 	if (ifp->if_flags & IFF_RUNNING) {
286695d67482SBill Paul 		/* Check RX return ring producer/consumer */
286795d67482SBill Paul 		bge_rxeof(sc);
286895d67482SBill Paul 
286995d67482SBill Paul 		/* Check TX ring producer/consumer */
287095d67482SBill Paul 		bge_txeof(sc);
287195d67482SBill Paul 	}
287295d67482SBill Paul 
2873f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2874f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREWRITE);
2875f41ac2beSBill Paul 
287695d67482SBill Paul 	bge_handle_events(sc);
287795d67482SBill Paul 
287895d67482SBill Paul 	/* Re-enable interrupts. */
287995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
288095d67482SBill Paul 
288195d67482SBill Paul 	if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
288295d67482SBill Paul 		bge_start(ifp);
288395d67482SBill Paul 
288495d67482SBill Paul 	return;
288595d67482SBill Paul }
288695d67482SBill Paul 
288795d67482SBill Paul static void
288895d67482SBill Paul bge_tick(xsc)
288995d67482SBill Paul 	void *xsc;
289095d67482SBill Paul {
289195d67482SBill Paul 	struct bge_softc *sc;
289295d67482SBill Paul 	struct mii_data *mii = NULL;
289395d67482SBill Paul 	struct ifmedia *ifm = NULL;
289495d67482SBill Paul 	struct ifnet *ifp;
289595d67482SBill Paul 	int s;
289695d67482SBill Paul 
289795d67482SBill Paul 	sc = xsc;
289895d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
289995d67482SBill Paul 
290095d67482SBill Paul 	s = splimp();
290195d67482SBill Paul 
29020434d1b8SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705)
29030434d1b8SBill Paul 		bge_stats_update_regs(sc);
29040434d1b8SBill Paul 	else
290595d67482SBill Paul 		bge_stats_update(sc);
290695d67482SBill Paul 	sc->bge_stat_ch = timeout(bge_tick, sc, hz);
29071c33cc4bSJohn Polstra 	if (sc->bge_link) {
29081c33cc4bSJohn Polstra 		splx(s);
290995d67482SBill Paul 		return;
29101c33cc4bSJohn Polstra 	}
291195d67482SBill Paul 
291295d67482SBill Paul 	if (sc->bge_tbi) {
291395d67482SBill Paul 		ifm = &sc->bge_ifmedia;
291495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
291595d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED) {
291695d67482SBill Paul 			sc->bge_link++;
291795d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
291895d67482SBill Paul 			printf("bge%d: gigabit link up\n", sc->bge_unit);
291995d67482SBill Paul 			if (ifp->if_snd.ifq_head != NULL)
292095d67482SBill Paul 				bge_start(ifp);
292195d67482SBill Paul 		}
29221c33cc4bSJohn Polstra 		splx(s);
292395d67482SBill Paul 		return;
292495d67482SBill Paul 	}
292595d67482SBill Paul 
292695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
292795d67482SBill Paul 	mii_tick(mii);
292895d67482SBill Paul 
2929b2561871SJonathan Lemon 	if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE &&
293095d67482SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
293195d67482SBill Paul 		sc->bge_link++;
2932b418ad5cSPoul-Henning Kamp 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
293395d67482SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
293495d67482SBill Paul 			printf("bge%d: gigabit link up\n",
293595d67482SBill Paul 			   sc->bge_unit);
293695d67482SBill Paul 		if (ifp->if_snd.ifq_head != NULL)
293795d67482SBill Paul 			bge_start(ifp);
293895d67482SBill Paul 	}
293995d67482SBill Paul 
294095d67482SBill Paul 	splx(s);
294195d67482SBill Paul 
294295d67482SBill Paul 	return;
294395d67482SBill Paul }
294495d67482SBill Paul 
294595d67482SBill Paul static void
29460434d1b8SBill Paul bge_stats_update_regs(sc)
29470434d1b8SBill Paul 	struct bge_softc *sc;
29480434d1b8SBill Paul {
29490434d1b8SBill Paul 	struct ifnet *ifp;
29500434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
29510434d1b8SBill Paul 	u_int32_t *s;
29520434d1b8SBill Paul 	int i;
29530434d1b8SBill Paul 
29540434d1b8SBill Paul 	ifp = &sc->arpcom.ac_if;
29550434d1b8SBill Paul 
29560434d1b8SBill Paul 	s = (u_int32_t *)&stats;
29570434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
29580434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
29590434d1b8SBill Paul 		s++;
29600434d1b8SBill Paul 	}
29610434d1b8SBill Paul 
29620434d1b8SBill Paul 	ifp->if_collisions +=
29630434d1b8SBill Paul 	   (stats.dot3StatsSingleCollisionFrames +
29640434d1b8SBill Paul 	   stats.dot3StatsMultipleCollisionFrames +
29650434d1b8SBill Paul 	   stats.dot3StatsExcessiveCollisions +
29660434d1b8SBill Paul 	   stats.dot3StatsLateCollisions) -
29670434d1b8SBill Paul 	   ifp->if_collisions;
29680434d1b8SBill Paul 
29690434d1b8SBill Paul 	return;
29700434d1b8SBill Paul }
29710434d1b8SBill Paul 
29720434d1b8SBill Paul static void
297395d67482SBill Paul bge_stats_update(sc)
297495d67482SBill Paul 	struct bge_softc *sc;
297595d67482SBill Paul {
297695d67482SBill Paul 	struct ifnet *ifp;
297795d67482SBill Paul 	struct bge_stats *stats;
297895d67482SBill Paul 
297995d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
298095d67482SBill Paul 
298195d67482SBill Paul 	stats = (struct bge_stats *)(sc->bge_vhandle +
298295d67482SBill Paul 	    BGE_MEMWIN_START + BGE_STATS_BLOCK);
298395d67482SBill Paul 
298495d67482SBill Paul 	ifp->if_collisions +=
29850434d1b8SBill Paul 	   (stats->txstats.dot3StatsSingleCollisionFrames.bge_addr_lo +
29860434d1b8SBill Paul 	   stats->txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo +
29870434d1b8SBill Paul 	   stats->txstats.dot3StatsExcessiveCollisions.bge_addr_lo +
29880434d1b8SBill Paul 	   stats->txstats.dot3StatsLateCollisions.bge_addr_lo) -
298995d67482SBill Paul 	   ifp->if_collisions;
299095d67482SBill Paul 
299195d67482SBill Paul #ifdef notdef
299295d67482SBill Paul 	ifp->if_collisions +=
299395d67482SBill Paul 	   (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
299495d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
299595d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
299695d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
299795d67482SBill Paul 	   ifp->if_collisions;
299895d67482SBill Paul #endif
299995d67482SBill Paul 
300095d67482SBill Paul 	return;
300195d67482SBill Paul }
300295d67482SBill Paul 
300395d67482SBill Paul /*
300495d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
300595d67482SBill Paul  * pointers to descriptors.
300695d67482SBill Paul  */
300795d67482SBill Paul static int
300895d67482SBill Paul bge_encap(sc, m_head, txidx)
300995d67482SBill Paul 	struct bge_softc *sc;
301095d67482SBill Paul 	struct mbuf *m_head;
301195d67482SBill Paul 	u_int32_t *txidx;
301295d67482SBill Paul {
301395d67482SBill Paul 	struct bge_tx_bd	*f = NULL;
301495d67482SBill Paul 	u_int16_t		csum_flags = 0;
3015673d9191SSam Leffler 	struct m_tag		*mtag;
3016f41ac2beSBill Paul 	struct bge_dmamap_arg	ctx;
3017f41ac2beSBill Paul 	bus_dmamap_t		map;
3018f41ac2beSBill Paul 	int			error;
301995d67482SBill Paul 
302095d67482SBill Paul 
302195d67482SBill Paul 	if (m_head->m_pkthdr.csum_flags) {
302295d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
302395d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
302495d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
302595d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
302695d67482SBill Paul 		if (m_head->m_flags & M_LASTFRAG)
302795d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
302895d67482SBill Paul 		else if (m_head->m_flags & M_FRAG)
302995d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
303095d67482SBill Paul 	}
303195d67482SBill Paul 
3032f41ac2beSBill Paul 	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m_head);
3033673d9191SSam Leffler 
3034f41ac2beSBill Paul 	ctx.sc = sc;
3035f41ac2beSBill Paul 	ctx.bge_idx = *txidx;
3036f41ac2beSBill Paul 	ctx.bge_ring = sc->bge_ldata.bge_tx_ring;
3037f41ac2beSBill Paul 	ctx.bge_flags = csum_flags;
303895d67482SBill Paul 	/*
303995d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
304095d67482SBill Paul 	 * of the end of the ring.
304195d67482SBill Paul 	 */
3042f41ac2beSBill Paul 	ctx.bge_maxsegs = (BGE_TX_RING_CNT - sc->bge_txcnt) - 16;
3043f41ac2beSBill Paul 
3044f41ac2beSBill Paul 	map = sc->bge_cdata.bge_tx_dmamap[*txidx];
3045f41ac2beSBill Paul 	error = bus_dmamap_load_mbuf(sc->bge_cdata.bge_mtag, map,
3046f41ac2beSBill Paul 	    m_head, bge_dma_map_tx_desc, &ctx, BUS_DMA_NOWAIT);
3047f41ac2beSBill Paul 
3048f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0 /*||
3049f41ac2beSBill Paul 	    ctx.bge_idx == sc->bge_tx_saved_considx*/)
305095d67482SBill Paul 		return (ENOBUFS);
3051f41ac2beSBill Paul 
3052f41ac2beSBill Paul 	/*
3053f41ac2beSBill Paul 	 * Insure that the map for this transmission
3054f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3055f41ac2beSBill Paul 	 * in this chain.
3056f41ac2beSBill Paul 	 */
3057f41ac2beSBill Paul 	sc->bge_cdata.bge_tx_dmamap[*txidx] =
3058f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_dmamap[ctx.bge_idx];
3059f41ac2beSBill Paul 	sc->bge_cdata.bge_tx_dmamap[ctx.bge_idx] = map;
3060f41ac2beSBill Paul 	sc->bge_cdata.bge_tx_chain[ctx.bge_idx] = m_head;
3061f41ac2beSBill Paul 	sc->bge_txcnt += ctx.bge_maxsegs;
3062f41ac2beSBill Paul 	f = &sc->bge_ldata.bge_tx_ring[*txidx];
3063f41ac2beSBill Paul 	if (mtag != NULL) {
3064f41ac2beSBill Paul 		f->bge_flags |= htole16(BGE_TXBDFLAG_VLAN_TAG);
3065f41ac2beSBill Paul 		f->bge_vlan_tag = htole16(VLAN_TAG_VALUE(mtag));
3066f41ac2beSBill Paul 	} else {
3067f41ac2beSBill Paul 		f->bge_vlan_tag = 0;
306895d67482SBill Paul 	}
306995d67482SBill Paul 
3070f41ac2beSBill Paul 	BGE_INC(ctx.bge_idx, BGE_TX_RING_CNT);
3071f41ac2beSBill Paul 	*txidx = ctx.bge_idx;
307295d67482SBill Paul 
307395d67482SBill Paul 	return(0);
307495d67482SBill Paul }
307595d67482SBill Paul 
307695d67482SBill Paul /*
307795d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
307895d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
307995d67482SBill Paul  */
308095d67482SBill Paul static void
308195d67482SBill Paul bge_start(ifp)
308295d67482SBill Paul 	struct ifnet *ifp;
308395d67482SBill Paul {
308495d67482SBill Paul 	struct bge_softc *sc;
308595d67482SBill Paul 	struct mbuf *m_head = NULL;
308695d67482SBill Paul 	u_int32_t prodidx = 0;
308795d67482SBill Paul 
308895d67482SBill Paul 	sc = ifp->if_softc;
308995d67482SBill Paul 
309095d67482SBill Paul 	if (!sc->bge_link && ifp->if_snd.ifq_len < 10)
309195d67482SBill Paul 		return;
309295d67482SBill Paul 
309395d67482SBill Paul 	prodidx = CSR_READ_4(sc, BGE_MBX_TX_HOST_PROD0_LO);
309495d67482SBill Paul 
309595d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
309695d67482SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
309795d67482SBill Paul 		if (m_head == NULL)
309895d67482SBill Paul 			break;
309995d67482SBill Paul 
310095d67482SBill Paul 		/*
310195d67482SBill Paul 		 * XXX
310295d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
310395d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
310495d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
310595d67482SBill Paul 		 * chain at once.
310695d67482SBill Paul 		 * (paranoia -- may not actually be needed)
310795d67482SBill Paul 		 */
310895d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
310995d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
311095d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
311195d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
311295d67482SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
311395d67482SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
311495d67482SBill Paul 				break;
311595d67482SBill Paul 			}
311695d67482SBill Paul 		}
311795d67482SBill Paul 
311895d67482SBill Paul 		/*
311995d67482SBill Paul 		 * Pack the data into the transmit ring. If we
312095d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
312195d67482SBill Paul 		 * for the NIC to drain the ring.
312295d67482SBill Paul 		 */
312395d67482SBill Paul 		if (bge_encap(sc, m_head, &prodidx)) {
312495d67482SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
312595d67482SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
312695d67482SBill Paul 			break;
312795d67482SBill Paul 		}
312895d67482SBill Paul 
312995d67482SBill Paul 		/*
313095d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
313195d67482SBill Paul 		 * to him.
313295d67482SBill Paul 		 */
3133673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
313495d67482SBill Paul 	}
313595d67482SBill Paul 
313695d67482SBill Paul 	/* Transmit */
313795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
31383927098fSPaul Saab 	/* 5700 b2 errata */
3139e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
31403927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
314195d67482SBill Paul 
314295d67482SBill Paul 	/*
314395d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
314495d67482SBill Paul 	 */
314595d67482SBill Paul 	ifp->if_timer = 5;
314695d67482SBill Paul 
314795d67482SBill Paul 	return;
314895d67482SBill Paul }
314995d67482SBill Paul 
315095d67482SBill Paul static void
315195d67482SBill Paul bge_init(xsc)
315295d67482SBill Paul 	void *xsc;
315395d67482SBill Paul {
315495d67482SBill Paul 	struct bge_softc *sc = xsc;
315595d67482SBill Paul 	struct ifnet *ifp;
315695d67482SBill Paul 	u_int16_t *m;
315795d67482SBill Paul         int s;
315895d67482SBill Paul 
315995d67482SBill Paul 	s = splimp();
316095d67482SBill Paul 
316195d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
316295d67482SBill Paul 
316344081f9bSMark Peek 	if (ifp->if_flags & IFF_RUNNING) {
316444081f9bSMark Peek 		splx(s);
316595d67482SBill Paul 		return;
316644081f9bSMark Peek 	}
316795d67482SBill Paul 
316895d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
316995d67482SBill Paul 	bge_stop(sc);
317095d67482SBill Paul 	bge_reset(sc);
317195d67482SBill Paul 	bge_chipinit(sc);
317295d67482SBill Paul 
317395d67482SBill Paul 	/*
317495d67482SBill Paul 	 * Init the various state machines, ring
317595d67482SBill Paul 	 * control blocks and firmware.
317695d67482SBill Paul 	 */
317795d67482SBill Paul 	if (bge_blockinit(sc)) {
317895d67482SBill Paul 		printf("bge%d: initialization failure\n", sc->bge_unit);
317995d67482SBill Paul 		splx(s);
318095d67482SBill Paul 		return;
318195d67482SBill Paul 	}
318295d67482SBill Paul 
318395d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
318495d67482SBill Paul 
318595d67482SBill Paul 	/* Specify MTU. */
318695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
318795d67482SBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN);
318895d67482SBill Paul 
318995d67482SBill Paul 	/* Load our MAC address. */
319095d67482SBill Paul 	m = (u_int16_t *)&sc->arpcom.ac_enaddr[0];
319195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
319295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
319395d67482SBill Paul 
319495d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
319595d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
319695d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
319795d67482SBill Paul 	} else {
319895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
319995d67482SBill Paul 	}
320095d67482SBill Paul 
320195d67482SBill Paul 	/* Program multicast filter. */
320295d67482SBill Paul 	bge_setmulti(sc);
320395d67482SBill Paul 
320495d67482SBill Paul 	/* Init RX ring. */
320595d67482SBill Paul 	bge_init_rx_ring_std(sc);
320695d67482SBill Paul 
32070434d1b8SBill Paul 	/*
32080434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
32090434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
32100434d1b8SBill Paul 	 * entry of the ring.
32110434d1b8SBill Paul 	 */
32120434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
32130434d1b8SBill Paul 		u_int32_t		v, i;
32140434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
32150434d1b8SBill Paul 			DELAY(20);
32160434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
32170434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
32180434d1b8SBill Paul 				break;
32190434d1b8SBill Paul 		}
32200434d1b8SBill Paul 		if (i == 10)
32210434d1b8SBill Paul 			printf ("bge%d: 5705 A0 chip failed to load RX ring\n",
32220434d1b8SBill Paul 			    sc->bge_unit);
32230434d1b8SBill Paul 	}
32240434d1b8SBill Paul 
322595d67482SBill Paul 	/* Init jumbo RX ring. */
322695d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
322795d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
322895d67482SBill Paul 
322995d67482SBill Paul 	/* Init our RX return ring index */
323095d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
323195d67482SBill Paul 
323295d67482SBill Paul 	/* Init TX ring. */
323395d67482SBill Paul 	bge_init_tx_ring(sc);
323495d67482SBill Paul 
323595d67482SBill Paul 	/* Turn on transmitter */
323695d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
323795d67482SBill Paul 
323895d67482SBill Paul 	/* Turn on receiver */
323995d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
324095d67482SBill Paul 
324195d67482SBill Paul 	/* Tell firmware we're alive. */
324295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
324395d67482SBill Paul 
324495d67482SBill Paul 	/* Enable host interrupts. */
324595d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
324695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
324795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
324895d67482SBill Paul 
324995d67482SBill Paul 	bge_ifmedia_upd(ifp);
325095d67482SBill Paul 
325195d67482SBill Paul 	ifp->if_flags |= IFF_RUNNING;
325295d67482SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
325395d67482SBill Paul 
325495d67482SBill Paul 	splx(s);
325595d67482SBill Paul 
325695d67482SBill Paul 	sc->bge_stat_ch = timeout(bge_tick, sc, hz);
325795d67482SBill Paul 
325895d67482SBill Paul 	return;
325995d67482SBill Paul }
326095d67482SBill Paul 
326195d67482SBill Paul /*
326295d67482SBill Paul  * Set media options.
326395d67482SBill Paul  */
326495d67482SBill Paul static int
326595d67482SBill Paul bge_ifmedia_upd(ifp)
326695d67482SBill Paul 	struct ifnet *ifp;
326795d67482SBill Paul {
326895d67482SBill Paul 	struct bge_softc *sc;
326995d67482SBill Paul 	struct mii_data *mii;
327095d67482SBill Paul 	struct ifmedia *ifm;
327195d67482SBill Paul 
327295d67482SBill Paul 	sc = ifp->if_softc;
327395d67482SBill Paul 	ifm = &sc->bge_ifmedia;
327495d67482SBill Paul 
327595d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
327695d67482SBill Paul 	if (sc->bge_tbi) {
327795d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
327895d67482SBill Paul 			return(EINVAL);
327995d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
328095d67482SBill Paul 		case IFM_AUTO:
328195d67482SBill Paul 			break;
328295d67482SBill Paul 		case IFM_1000_SX:
328395d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
328495d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
328595d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
328695d67482SBill Paul 			} else {
328795d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
328895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
328995d67482SBill Paul 			}
329095d67482SBill Paul 			break;
329195d67482SBill Paul 		default:
329295d67482SBill Paul 			return(EINVAL);
329395d67482SBill Paul 		}
329495d67482SBill Paul 		return(0);
329595d67482SBill Paul 	}
329695d67482SBill Paul 
329795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
329895d67482SBill Paul 	sc->bge_link = 0;
329995d67482SBill Paul 	if (mii->mii_instance) {
330095d67482SBill Paul 		struct mii_softc *miisc;
330195d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
330295d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
330395d67482SBill Paul 			mii_phy_reset(miisc);
330495d67482SBill Paul 	}
330595d67482SBill Paul 	mii_mediachg(mii);
330695d67482SBill Paul 
330795d67482SBill Paul 	return(0);
330895d67482SBill Paul }
330995d67482SBill Paul 
331095d67482SBill Paul /*
331195d67482SBill Paul  * Report current media status.
331295d67482SBill Paul  */
331395d67482SBill Paul static void
331495d67482SBill Paul bge_ifmedia_sts(ifp, ifmr)
331595d67482SBill Paul 	struct ifnet *ifp;
331695d67482SBill Paul 	struct ifmediareq *ifmr;
331795d67482SBill Paul {
331895d67482SBill Paul 	struct bge_softc *sc;
331995d67482SBill Paul 	struct mii_data *mii;
332095d67482SBill Paul 
332195d67482SBill Paul 	sc = ifp->if_softc;
332295d67482SBill Paul 
332395d67482SBill Paul 	if (sc->bge_tbi) {
332495d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
332595d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
332695d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
332795d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
332895d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
332995d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
333095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
333195d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
333295d67482SBill Paul 		else
333395d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
333495d67482SBill Paul 		return;
333595d67482SBill Paul 	}
333695d67482SBill Paul 
333795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
333895d67482SBill Paul 	mii_pollstat(mii);
333995d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
334095d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
334195d67482SBill Paul 
334295d67482SBill Paul 	return;
334395d67482SBill Paul }
334495d67482SBill Paul 
334595d67482SBill Paul static int
334695d67482SBill Paul bge_ioctl(ifp, command, data)
334795d67482SBill Paul 	struct ifnet *ifp;
334895d67482SBill Paul 	u_long command;
334995d67482SBill Paul 	caddr_t data;
335095d67482SBill Paul {
335195d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
335295d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
335395d67482SBill Paul 	int s, mask, error = 0;
335495d67482SBill Paul 	struct mii_data *mii;
335595d67482SBill Paul 
335695d67482SBill Paul 	s = splimp();
335795d67482SBill Paul 
335895d67482SBill Paul 	switch(command) {
335995d67482SBill Paul 	case SIOCSIFMTU:
33600434d1b8SBill Paul 		/* Disallow jumbo frames on 5705. */
33610434d1b8SBill Paul 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
33620434d1b8SBill Paul 		    ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU)
336395d67482SBill Paul 			error = EINVAL;
336495d67482SBill Paul 		else {
336595d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
336695d67482SBill Paul 			ifp->if_flags &= ~IFF_RUNNING;
336795d67482SBill Paul 			bge_init(sc);
336895d67482SBill Paul 		}
336995d67482SBill Paul 		break;
337095d67482SBill Paul 	case SIOCSIFFLAGS:
337195d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
337295d67482SBill Paul 			/*
337395d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
337495d67482SBill Paul 			 * then just use the 'set promisc mode' command
337595d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
337695d67482SBill Paul 			 * a full re-init means reloading the firmware and
337795d67482SBill Paul 			 * waiting for it to start up, which may take a
337895d67482SBill Paul 			 * second or two.
337995d67482SBill Paul 			 */
338095d67482SBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
338195d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
338295d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
338395d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
338495d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
338595d67482SBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
338695d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
338795d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
338895d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
338995d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
339095d67482SBill Paul 			} else
339195d67482SBill Paul 				bge_init(sc);
339295d67482SBill Paul 		} else {
339395d67482SBill Paul 			if (ifp->if_flags & IFF_RUNNING) {
339495d67482SBill Paul 				bge_stop(sc);
339595d67482SBill Paul 			}
339695d67482SBill Paul 		}
339795d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
339895d67482SBill Paul 		error = 0;
339995d67482SBill Paul 		break;
340095d67482SBill Paul 	case SIOCADDMULTI:
340195d67482SBill Paul 	case SIOCDELMULTI:
340295d67482SBill Paul 		if (ifp->if_flags & IFF_RUNNING) {
340395d67482SBill Paul 			bge_setmulti(sc);
340495d67482SBill Paul 			error = 0;
340595d67482SBill Paul 		}
340695d67482SBill Paul 		break;
340795d67482SBill Paul 	case SIOCSIFMEDIA:
340895d67482SBill Paul 	case SIOCGIFMEDIA:
340995d67482SBill Paul 		if (sc->bge_tbi) {
341095d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
341195d67482SBill Paul 			    &sc->bge_ifmedia, command);
341295d67482SBill Paul 		} else {
341395d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
341495d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
341595d67482SBill Paul 			    &mii->mii_media, command);
341695d67482SBill Paul 		}
341795d67482SBill Paul 		break;
341895d67482SBill Paul         case SIOCSIFCAP:
341995d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
342095d67482SBill Paul 		if (mask & IFCAP_HWCSUM) {
342195d67482SBill Paul 			if (IFCAP_HWCSUM & ifp->if_capenable)
342295d67482SBill Paul 				ifp->if_capenable &= ~IFCAP_HWCSUM;
342395d67482SBill Paul 			else
342495d67482SBill Paul 				ifp->if_capenable |= IFCAP_HWCSUM;
342595d67482SBill Paul 		}
342695d67482SBill Paul 		error = 0;
342795d67482SBill Paul 		break;
342895d67482SBill Paul 	default:
3429673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
343095d67482SBill Paul 		break;
343195d67482SBill Paul 	}
343295d67482SBill Paul 
343395d67482SBill Paul 	(void)splx(s);
343495d67482SBill Paul 
343595d67482SBill Paul 	return(error);
343695d67482SBill Paul }
343795d67482SBill Paul 
343895d67482SBill Paul static void
343995d67482SBill Paul bge_watchdog(ifp)
344095d67482SBill Paul 	struct ifnet *ifp;
344195d67482SBill Paul {
344295d67482SBill Paul 	struct bge_softc *sc;
344395d67482SBill Paul 
344495d67482SBill Paul 	sc = ifp->if_softc;
344595d67482SBill Paul 
344695d67482SBill Paul 	printf("bge%d: watchdog timeout -- resetting\n", sc->bge_unit);
344795d67482SBill Paul 
344895d67482SBill Paul 	ifp->if_flags &= ~IFF_RUNNING;
344995d67482SBill Paul 	bge_init(sc);
345095d67482SBill Paul 
345195d67482SBill Paul 	ifp->if_oerrors++;
345295d67482SBill Paul 
345395d67482SBill Paul 	return;
345495d67482SBill Paul }
345595d67482SBill Paul 
345695d67482SBill Paul /*
345795d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
345895d67482SBill Paul  * RX and TX lists.
345995d67482SBill Paul  */
346095d67482SBill Paul static void
346195d67482SBill Paul bge_stop(sc)
346295d67482SBill Paul 	struct bge_softc *sc;
346395d67482SBill Paul {
346495d67482SBill Paul 	struct ifnet *ifp;
346595d67482SBill Paul 	struct ifmedia_entry *ifm;
346695d67482SBill Paul 	struct mii_data *mii = NULL;
346795d67482SBill Paul 	int mtmp, itmp;
346895d67482SBill Paul 
346995d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
347095d67482SBill Paul 
347195d67482SBill Paul 	if (!sc->bge_tbi)
347295d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
347395d67482SBill Paul 
347495d67482SBill Paul 	untimeout(bge_tick, sc, sc->bge_stat_ch);
347595d67482SBill Paul 
347695d67482SBill Paul 	/*
347795d67482SBill Paul 	 * Disable all of the receiver blocks
347895d67482SBill Paul 	 */
347995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
348095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
348195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
34820434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
348395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
348495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
348595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
348695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
348795d67482SBill Paul 
348895d67482SBill Paul 	/*
348995d67482SBill Paul 	 * Disable all of the transmit blocks
349095d67482SBill Paul 	 */
349195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
349295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
349395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
349495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
349595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
34960434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
349795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
349895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
349995d67482SBill Paul 
350095d67482SBill Paul 	/*
350195d67482SBill Paul 	 * Shut down all of the memory managers and related
350295d67482SBill Paul 	 * state machines.
350395d67482SBill Paul 	 */
350495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
350595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
35060434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
350795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
350895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
350995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
35100434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
351195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
351295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
35130434d1b8SBill Paul 	}
351495d67482SBill Paul 
351595d67482SBill Paul 	/* Disable host interrupts. */
351695d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
351795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
351895d67482SBill Paul 
351995d67482SBill Paul 	/*
352095d67482SBill Paul 	 * Tell firmware we're shutting down.
352195d67482SBill Paul 	 */
352295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
352395d67482SBill Paul 
352495d67482SBill Paul 	/* Free the RX lists. */
352595d67482SBill Paul 	bge_free_rx_ring_std(sc);
352695d67482SBill Paul 
352795d67482SBill Paul 	/* Free jumbo RX list. */
35280434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
352995d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
353095d67482SBill Paul 
353195d67482SBill Paul 	/* Free TX buffers. */
353295d67482SBill Paul 	bge_free_tx_ring(sc);
353395d67482SBill Paul 
353495d67482SBill Paul 	/*
353595d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
353695d67482SBill Paul 	 * unchanged so that things will be put back to normal when
353795d67482SBill Paul 	 * we bring the interface back up.
353895d67482SBill Paul 	 */
353995d67482SBill Paul 	if (!sc->bge_tbi) {
354095d67482SBill Paul 		itmp = ifp->if_flags;
354195d67482SBill Paul 		ifp->if_flags |= IFF_UP;
354295d67482SBill Paul 		ifm = mii->mii_media.ifm_cur;
354395d67482SBill Paul 		mtmp = ifm->ifm_media;
354495d67482SBill Paul 		ifm->ifm_media = IFM_ETHER|IFM_NONE;
354595d67482SBill Paul 		mii_mediachg(mii);
354695d67482SBill Paul 		ifm->ifm_media = mtmp;
354795d67482SBill Paul 		ifp->if_flags = itmp;
354895d67482SBill Paul 	}
354995d67482SBill Paul 
355095d67482SBill Paul 	sc->bge_link = 0;
355195d67482SBill Paul 
355295d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
355395d67482SBill Paul 
355495d67482SBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
355595d67482SBill Paul 
355695d67482SBill Paul 	return;
355795d67482SBill Paul }
355895d67482SBill Paul 
355995d67482SBill Paul /*
356095d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
356195d67482SBill Paul  * get confused by errant DMAs when rebooting.
356295d67482SBill Paul  */
356395d67482SBill Paul static void
356495d67482SBill Paul bge_shutdown(dev)
356595d67482SBill Paul 	device_t dev;
356695d67482SBill Paul {
356795d67482SBill Paul 	struct bge_softc *sc;
356895d67482SBill Paul 
356995d67482SBill Paul 	sc = device_get_softc(dev);
357095d67482SBill Paul 
357195d67482SBill Paul 	bge_stop(sc);
357295d67482SBill Paul 	bge_reset(sc);
357395d67482SBill Paul 
357495d67482SBill Paul 	return;
357595d67482SBill Paul }
3576