xref: /freebsd/sys/dev/bge/if_bge.c (revision 5f96beb9e08be2b08309a6b98967246b64272ade)
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 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
3895d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4195d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4295d67482SBill Paul  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
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 <machine/clock.h>      /* for DELAY */
9595d67482SBill Paul #include <machine/bus_memio.h>
9695d67482SBill Paul #include <machine/bus.h>
9795d67482SBill Paul #include <machine/resource.h>
9895d67482SBill Paul #include <sys/bus.h>
9995d67482SBill Paul #include <sys/rman.h>
10095d67482SBill Paul 
10195d67482SBill Paul #include <dev/mii/mii.h>
10295d67482SBill Paul #include <dev/mii/miivar.h>
1032d3ce713SDavid E. O'Brien #include "miidevs.h"
10495d67482SBill Paul #include <dev/mii/brgphyreg.h>
10595d67482SBill Paul 
1064fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1074fbd232cSWarner Losh #include <dev/pci/pcivar.h>
10895d67482SBill Paul 
10995d67482SBill Paul #include <dev/bge/if_bgereg.h>
11095d67482SBill Paul 
1115ddc5794SJohn Polstra #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
11295d67482SBill Paul 
113f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
114f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
11595d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
11695d67482SBill Paul 
11795d67482SBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
11895d67482SBill Paul #include "miibus_if.h"
11995d67482SBill Paul 
12095d67482SBill Paul /*
12195d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
12295d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
12395d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
12495d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
12595d67482SBill Paul  */
126029e2ee3SJohn Polstra #define BGE_DEVDESC_MAX		64	/* Maximum device description length */
12795d67482SBill Paul 
12895d67482SBill Paul static struct bge_type bge_devs[] = {
12995d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5700,
13095d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
13195d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5701,
13295d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
13395d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5700,
13495d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
13595d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5701,
13695d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
1370434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702,
1380434d1b8SBill Paul 		"Broadcom BCM5702 Gigabit Ethernet" },
13901598b8dSMitsuru IWASAKI 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702X,
14001598b8dSMitsuru IWASAKI 		"Broadcom BCM5702X Gigabit Ethernet" },
1410434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703,
1420434d1b8SBill Paul 		"Broadcom BCM5703 Gigabit Ethernet" },
143b1265c1aSJohn Polstra 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703X,
144b1265c1aSJohn Polstra 		"Broadcom BCM5703X Gigabit Ethernet" },
1456ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704C,
1466ac6d2c8SPaul Saab 		"Broadcom BCM5704C Dual Gigabit Ethernet" },
1476ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704S,
1486ac6d2c8SPaul Saab 		"Broadcom BCM5704S Dual Gigabit Ethernet" },
1490434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705,
1500434d1b8SBill Paul 		"Broadcom BCM5705 Gigabit Ethernet" },
1510434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M,
1520434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
1530434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M_ALT,
1540434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
1550434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5782,
1560434d1b8SBill Paul 		"Broadcom BCM5782 Gigabit Ethernet" },
1579f71a4c2SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5788,
1589f71a4c2SBill Paul 		"Broadcom BCM5788 Gigabit Ethernet" },
1595d99c641SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901,
1605d99c641SBill Paul 		"Broadcom BCM5901 Fast Ethernet" },
1615d99c641SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901A2,
1625d99c641SBill Paul 		"Broadcom BCM5901A2 Fast Ethernet" },
16395d67482SBill Paul 	{ SK_VENDORID, SK_DEVICEID_ALTIMA,
16495d67482SBill Paul 		"SysKonnect Gigabit Ethernet" },
165586d7c2eSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000,
166586d7c2eSJohn Polstra 		"Altima AC1000 Gigabit Ethernet" },
1672aae6624SBill Paul 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1002,
1682aae6624SBill Paul 		"Altima AC1002 Gigabit Ethernet" },
169470bd96aSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100,
170470bd96aSJohn Polstra 		"Altima AC9100 Gigabit Ethernet" },
17195d67482SBill Paul 	{ 0, 0, NULL }
17295d67482SBill Paul };
17395d67482SBill Paul 
174e51a25f8SAlfred Perlstein static int bge_probe		(device_t);
175e51a25f8SAlfred Perlstein static int bge_attach		(device_t);
176e51a25f8SAlfred Perlstein static int bge_detach		(device_t);
17795d67482SBill Paul static void bge_release_resources
178e51a25f8SAlfred Perlstein 				(struct bge_softc *);
179f41ac2beSBill Paul static void bge_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
180f41ac2beSBill Paul static void bge_dma_map_tx_desc	(void *, bus_dma_segment_t *, int,
181f41ac2beSBill Paul 				    bus_size_t, int);
182f41ac2beSBill Paul static int bge_dma_alloc	(device_t);
183f41ac2beSBill Paul static void bge_dma_free	(struct bge_softc *);
184f41ac2beSBill Paul 
185e51a25f8SAlfred Perlstein static void bge_txeof		(struct bge_softc *);
186e51a25f8SAlfred Perlstein static void bge_rxeof		(struct bge_softc *);
18795d67482SBill Paul 
1880f9bd73bSSam Leffler static void bge_tick_locked	(struct bge_softc *);
189e51a25f8SAlfred Perlstein static void bge_tick		(void *);
190e51a25f8SAlfred Perlstein static void bge_stats_update	(struct bge_softc *);
1910434d1b8SBill Paul static void bge_stats_update_regs
1920434d1b8SBill Paul 				(struct bge_softc *);
193e51a25f8SAlfred Perlstein static int bge_encap		(struct bge_softc *, struct mbuf *,
194e51a25f8SAlfred Perlstein 					u_int32_t *);
19595d67482SBill Paul 
196e51a25f8SAlfred Perlstein static void bge_intr		(void *);
1970f9bd73bSSam Leffler static void bge_start_locked	(struct ifnet *);
198e51a25f8SAlfred Perlstein static void bge_start		(struct ifnet *);
199e51a25f8SAlfred Perlstein static int bge_ioctl		(struct ifnet *, u_long, caddr_t);
2000f9bd73bSSam Leffler static void bge_init_locked	(struct bge_softc *);
201e51a25f8SAlfred Perlstein static void bge_init		(void *);
202e51a25f8SAlfred Perlstein static void bge_stop		(struct bge_softc *);
203e51a25f8SAlfred Perlstein static void bge_watchdog		(struct ifnet *);
204e51a25f8SAlfred Perlstein static void bge_shutdown		(device_t);
205e51a25f8SAlfred Perlstein static int bge_ifmedia_upd	(struct ifnet *);
206e51a25f8SAlfred Perlstein static void bge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
20795d67482SBill Paul 
208e51a25f8SAlfred Perlstein static u_int8_t	bge_eeprom_getbyte	(struct bge_softc *, int, u_int8_t *);
209e51a25f8SAlfred Perlstein static int bge_read_eeprom	(struct bge_softc *, caddr_t, int, int);
21095d67482SBill Paul 
211a55a017fSDavid E. O'Brien static uint32_t bge_mchash	(const uint8_t *);
212e51a25f8SAlfred Perlstein static void bge_setmulti	(struct bge_softc *);
21395d67482SBill Paul 
214e51a25f8SAlfred Perlstein static void bge_handle_events	(struct bge_softc *);
215e51a25f8SAlfred Perlstein static int bge_alloc_jumbo_mem	(struct bge_softc *);
216e51a25f8SAlfred Perlstein static void bge_free_jumbo_mem	(struct bge_softc *);
217e51a25f8SAlfred Perlstein static void *bge_jalloc		(struct bge_softc *);
218914596abSAlfred Perlstein static void bge_jfree		(void *, void *);
219e51a25f8SAlfred Perlstein static int bge_newbuf_std	(struct bge_softc *, int, struct mbuf *);
220e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo	(struct bge_softc *, int, struct mbuf *);
221e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std	(struct bge_softc *);
222e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std	(struct bge_softc *);
223e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo	(struct bge_softc *);
224e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo	(struct bge_softc *);
225e51a25f8SAlfred Perlstein static void bge_free_tx_ring	(struct bge_softc *);
226e51a25f8SAlfred Perlstein static int bge_init_tx_ring	(struct bge_softc *);
22795d67482SBill Paul 
228e51a25f8SAlfred Perlstein static int bge_chipinit		(struct bge_softc *);
229e51a25f8SAlfred Perlstein static int bge_blockinit	(struct bge_softc *);
23095d67482SBill Paul 
2311b4a3b2fSPeter Wemm #ifdef notdef
232e51a25f8SAlfred Perlstein static u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
233e51a25f8SAlfred Perlstein static void bge_vpd_read_res	(struct bge_softc *, struct vpd_res *, int);
234e51a25f8SAlfred Perlstein static void bge_vpd_read	(struct bge_softc *);
2351b4a3b2fSPeter Wemm #endif
23695d67482SBill Paul 
23795d67482SBill Paul static u_int32_t bge_readmem_ind
238e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
239e51a25f8SAlfred Perlstein static void bge_writemem_ind	(struct bge_softc *, int, int);
24095d67482SBill Paul #ifdef notdef
24195d67482SBill Paul static u_int32_t bge_readreg_ind
242e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
24395d67482SBill Paul #endif
244e51a25f8SAlfred Perlstein static void bge_writereg_ind	(struct bge_softc *, int, int);
24595d67482SBill Paul 
246e51a25f8SAlfred Perlstein static int bge_miibus_readreg	(device_t, int, int);
247e51a25f8SAlfred Perlstein static int bge_miibus_writereg	(device_t, int, int, int);
248e51a25f8SAlfred Perlstein static void bge_miibus_statchg	(device_t);
24995d67482SBill Paul 
250e51a25f8SAlfred Perlstein static void bge_reset		(struct bge_softc *);
25195d67482SBill Paul 
25295d67482SBill Paul static device_method_t bge_methods[] = {
25395d67482SBill Paul 	/* Device interface */
25495d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
25595d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
25695d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
25795d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
25895d67482SBill Paul 
25995d67482SBill Paul 	/* bus interface */
26095d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
26195d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
26295d67482SBill Paul 
26395d67482SBill Paul 	/* MII interface */
26495d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
26595d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
26695d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
26795d67482SBill Paul 
26895d67482SBill Paul 	{ 0, 0 }
26995d67482SBill Paul };
27095d67482SBill Paul 
27195d67482SBill Paul static driver_t bge_driver = {
27295d67482SBill Paul 	"bge",
27395d67482SBill Paul 	bge_methods,
27495d67482SBill Paul 	sizeof(struct bge_softc)
27595d67482SBill Paul };
27695d67482SBill Paul 
27795d67482SBill Paul static devclass_t bge_devclass;
27895d67482SBill Paul 
279f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
28095d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
28195d67482SBill Paul 
28295d67482SBill Paul static u_int32_t
28395d67482SBill Paul bge_readmem_ind(sc, off)
28495d67482SBill Paul 	struct bge_softc *sc;
28595d67482SBill Paul 	int off;
28695d67482SBill Paul {
28795d67482SBill Paul 	device_t dev;
28895d67482SBill Paul 
28995d67482SBill Paul 	dev = sc->bge_dev;
29095d67482SBill Paul 
29195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
29295d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
29395d67482SBill Paul }
29495d67482SBill Paul 
29595d67482SBill Paul static void
29695d67482SBill Paul bge_writemem_ind(sc, off, val)
29795d67482SBill Paul 	struct bge_softc *sc;
29895d67482SBill Paul 	int off, val;
29995d67482SBill Paul {
30095d67482SBill Paul 	device_t dev;
30195d67482SBill Paul 
30295d67482SBill Paul 	dev = sc->bge_dev;
30395d67482SBill Paul 
30495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
30595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
30695d67482SBill Paul 
30795d67482SBill Paul 	return;
30895d67482SBill Paul }
30995d67482SBill Paul 
31095d67482SBill Paul #ifdef notdef
31195d67482SBill Paul static u_int32_t
31295d67482SBill Paul bge_readreg_ind(sc, off)
31395d67482SBill Paul 	struct bge_softc *sc;
31495d67482SBill Paul 	int off;
31595d67482SBill Paul {
31695d67482SBill Paul 	device_t dev;
31795d67482SBill Paul 
31895d67482SBill Paul 	dev = sc->bge_dev;
31995d67482SBill Paul 
32095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
32195d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
32295d67482SBill Paul }
32395d67482SBill Paul #endif
32495d67482SBill Paul 
32595d67482SBill Paul static void
32695d67482SBill Paul bge_writereg_ind(sc, off, val)
32795d67482SBill Paul 	struct bge_softc *sc;
32895d67482SBill Paul 	int off, val;
32995d67482SBill Paul {
33095d67482SBill Paul 	device_t dev;
33195d67482SBill Paul 
33295d67482SBill Paul 	dev = sc->bge_dev;
33395d67482SBill Paul 
33495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
33595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
33695d67482SBill Paul 
33795d67482SBill Paul 	return;
33895d67482SBill Paul }
33995d67482SBill Paul 
340f41ac2beSBill Paul /*
341f41ac2beSBill Paul  * Map a single buffer address.
342f41ac2beSBill Paul  */
343f41ac2beSBill Paul 
344f41ac2beSBill Paul static void
345f41ac2beSBill Paul bge_dma_map_addr(arg, segs, nseg, error)
346f41ac2beSBill Paul 	void *arg;
347f41ac2beSBill Paul 	bus_dma_segment_t *segs;
348f41ac2beSBill Paul 	int nseg;
349f41ac2beSBill Paul 	int error;
350f41ac2beSBill Paul {
351f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
352f41ac2beSBill Paul 
353f41ac2beSBill Paul 	if (error)
354f41ac2beSBill Paul 		return;
355f41ac2beSBill Paul 
356f41ac2beSBill Paul 	ctx = arg;
357f41ac2beSBill Paul 
358f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
359f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
360f41ac2beSBill Paul 		return;
361f41ac2beSBill Paul 	}
362f41ac2beSBill Paul 
363f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
364f41ac2beSBill Paul 
365f41ac2beSBill Paul 	return;
366f41ac2beSBill Paul }
367f41ac2beSBill Paul 
368f41ac2beSBill Paul /*
369f41ac2beSBill Paul  * Map an mbuf chain into an TX ring.
370f41ac2beSBill Paul  */
371f41ac2beSBill Paul 
372f41ac2beSBill Paul static void
373f41ac2beSBill Paul bge_dma_map_tx_desc(arg, segs, nseg, mapsize, error)
374f41ac2beSBill Paul 	void *arg;
375f41ac2beSBill Paul 	bus_dma_segment_t *segs;
376f41ac2beSBill Paul 	int nseg;
377f41ac2beSBill Paul 	bus_size_t mapsize;
378f41ac2beSBill Paul 	int error;
379f41ac2beSBill Paul {
380f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
381f41ac2beSBill Paul 	struct bge_tx_bd *d = NULL;
382f41ac2beSBill Paul 	int i = 0, idx;
383f41ac2beSBill Paul 
384f41ac2beSBill Paul 	if (error)
385f41ac2beSBill Paul 		return;
386f41ac2beSBill Paul 
387f41ac2beSBill Paul 	ctx = arg;
388f41ac2beSBill Paul 
389f41ac2beSBill Paul 	/* Signal error to caller if there's too many segments */
390f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
391f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
392f41ac2beSBill Paul 		return;
393f41ac2beSBill Paul 	}
394f41ac2beSBill Paul 
395f41ac2beSBill Paul 	idx = ctx->bge_idx;
396f41ac2beSBill Paul 	while(1) {
397f41ac2beSBill Paul 		d = &ctx->bge_ring[idx];
398f41ac2beSBill Paul 		d->bge_addr.bge_addr_lo =
399f41ac2beSBill Paul 		    htole32(BGE_ADDR_LO(segs[i].ds_addr));
400f41ac2beSBill Paul 		d->bge_addr.bge_addr_hi =
401f41ac2beSBill Paul 		    htole32(BGE_ADDR_HI(segs[i].ds_addr));
402f41ac2beSBill Paul 		d->bge_len = htole16(segs[i].ds_len);
403f41ac2beSBill Paul 		d->bge_flags = htole16(ctx->bge_flags);
404f41ac2beSBill Paul                 i++;
405f41ac2beSBill Paul 		if (i == nseg)
406f41ac2beSBill Paul 			break;
407f41ac2beSBill Paul 		BGE_INC(idx, BGE_TX_RING_CNT);
408f41ac2beSBill Paul 	}
409f41ac2beSBill Paul 
410f41ac2beSBill Paul 	d->bge_flags |= htole16(BGE_TXBDFLAG_END);
411f41ac2beSBill Paul 	ctx->bge_maxsegs = nseg;
412f41ac2beSBill Paul 	ctx->bge_idx = idx;
413f41ac2beSBill Paul 
414f41ac2beSBill Paul 	return;
415f41ac2beSBill Paul }
416f41ac2beSBill Paul 
417f41ac2beSBill Paul 
4181b4a3b2fSPeter Wemm #ifdef notdef
41995d67482SBill Paul static u_int8_t
42095d67482SBill Paul bge_vpd_readbyte(sc, addr)
42195d67482SBill Paul 	struct bge_softc *sc;
42295d67482SBill Paul 	int addr;
42395d67482SBill Paul {
42495d67482SBill Paul 	int i;
42595d67482SBill Paul 	device_t dev;
42695d67482SBill Paul 	u_int32_t val;
42795d67482SBill Paul 
42895d67482SBill Paul 	dev = sc->bge_dev;
42995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
43095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
43195d67482SBill Paul 		DELAY(10);
43295d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
43395d67482SBill Paul 			break;
43495d67482SBill Paul 	}
43595d67482SBill Paul 
43695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
43795d67482SBill Paul 		printf("bge%d: VPD read timed out\n", sc->bge_unit);
43895d67482SBill Paul 		return(0);
43995d67482SBill Paul 	}
44095d67482SBill Paul 
44195d67482SBill Paul 	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
44295d67482SBill Paul 
44395d67482SBill Paul 	return((val >> ((addr % 4) * 8)) & 0xFF);
44495d67482SBill Paul }
44595d67482SBill Paul 
44695d67482SBill Paul static void
44795d67482SBill Paul bge_vpd_read_res(sc, res, addr)
44895d67482SBill Paul 	struct bge_softc *sc;
44995d67482SBill Paul 	struct vpd_res *res;
45095d67482SBill Paul 	int addr;
45195d67482SBill Paul {
45295d67482SBill Paul 	int i;
45395d67482SBill Paul 	u_int8_t *ptr;
45495d67482SBill Paul 
45595d67482SBill Paul 	ptr = (u_int8_t *)res;
45695d67482SBill Paul 	for (i = 0; i < sizeof(struct vpd_res); i++)
45795d67482SBill Paul 		ptr[i] = bge_vpd_readbyte(sc, i + addr);
45895d67482SBill Paul 
45995d67482SBill Paul 	return;
46095d67482SBill Paul }
46195d67482SBill Paul 
46295d67482SBill Paul static void
46395d67482SBill Paul bge_vpd_read(sc)
46495d67482SBill Paul 	struct bge_softc *sc;
46595d67482SBill Paul {
46695d67482SBill Paul 	int pos = 0, i;
46795d67482SBill Paul 	struct vpd_res res;
46895d67482SBill Paul 
46995d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
47095d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
47195d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
47295d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
47395d67482SBill Paul 	sc->bge_vpd_prodname = NULL;
47495d67482SBill Paul 	sc->bge_vpd_readonly = NULL;
47595d67482SBill Paul 
47695d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
47795d67482SBill Paul 
47895d67482SBill Paul 	if (res.vr_id != VPD_RES_ID) {
47995d67482SBill Paul 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
48095d67482SBill Paul 			sc->bge_unit, VPD_RES_ID, res.vr_id);
48195d67482SBill Paul                 return;
48295d67482SBill Paul         }
48395d67482SBill Paul 
48495d67482SBill Paul 	pos += sizeof(res);
48595d67482SBill Paul 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
48695d67482SBill Paul 	for (i = 0; i < res.vr_len; i++)
48795d67482SBill Paul 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
48895d67482SBill Paul 	sc->bge_vpd_prodname[i] = '\0';
48995d67482SBill Paul 	pos += i;
49095d67482SBill Paul 
49195d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
49295d67482SBill Paul 
49395d67482SBill Paul 	if (res.vr_id != VPD_RES_READ) {
49495d67482SBill Paul 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
49595d67482SBill Paul 		    sc->bge_unit, VPD_RES_READ, res.vr_id);
49695d67482SBill Paul 		return;
49795d67482SBill Paul 	}
49895d67482SBill Paul 
49995d67482SBill Paul 	pos += sizeof(res);
50095d67482SBill Paul 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
50195d67482SBill Paul 	for (i = 0; i < res.vr_len + 1; i++)
50295d67482SBill Paul 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
50395d67482SBill Paul 
50495d67482SBill Paul 	return;
50595d67482SBill Paul }
5061b4a3b2fSPeter Wemm #endif
50795d67482SBill Paul 
50895d67482SBill Paul /*
50995d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
51095d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
51195d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
51295d67482SBill Paul  * access method.
51395d67482SBill Paul  */
51495d67482SBill Paul static u_int8_t
51595d67482SBill Paul bge_eeprom_getbyte(sc, addr, dest)
51695d67482SBill Paul 	struct bge_softc *sc;
51795d67482SBill Paul 	int addr;
51895d67482SBill Paul 	u_int8_t *dest;
51995d67482SBill Paul {
52095d67482SBill Paul 	int i;
52195d67482SBill Paul 	u_int32_t byte = 0;
52295d67482SBill Paul 
52395d67482SBill Paul 	/*
52495d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
52595d67482SBill Paul 	 * having to use the bitbang method.
52695d67482SBill Paul 	 */
52795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
52895d67482SBill Paul 
52995d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
53095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
53195d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
53295d67482SBill Paul 	DELAY(20);
53395d67482SBill Paul 
53495d67482SBill Paul 	/* Issue the read EEPROM command. */
53595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
53695d67482SBill Paul 
53795d67482SBill Paul 	/* Wait for completion */
53895d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
53995d67482SBill Paul 		DELAY(10);
54095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
54195d67482SBill Paul 			break;
54295d67482SBill Paul 	}
54395d67482SBill Paul 
54495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
54595d67482SBill Paul 		printf("bge%d: eeprom read timed out\n", sc->bge_unit);
54695d67482SBill Paul 		return(0);
54795d67482SBill Paul 	}
54895d67482SBill Paul 
54995d67482SBill Paul 	/* Get result. */
55095d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
55195d67482SBill Paul 
55295d67482SBill Paul         *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
55395d67482SBill Paul 
55495d67482SBill Paul 	return(0);
55595d67482SBill Paul }
55695d67482SBill Paul 
55795d67482SBill Paul /*
55895d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
55995d67482SBill Paul  */
56095d67482SBill Paul static int
56195d67482SBill Paul bge_read_eeprom(sc, dest, off, cnt)
56295d67482SBill Paul 	struct bge_softc *sc;
56395d67482SBill Paul 	caddr_t dest;
56495d67482SBill Paul 	int off;
56595d67482SBill Paul 	int cnt;
56695d67482SBill Paul {
56795d67482SBill Paul 	int err = 0, i;
56895d67482SBill Paul 	u_int8_t byte = 0;
56995d67482SBill Paul 
57095d67482SBill Paul 	for (i = 0; i < cnt; i++) {
57195d67482SBill Paul 		err = bge_eeprom_getbyte(sc, off + i, &byte);
57295d67482SBill Paul 		if (err)
57395d67482SBill Paul 			break;
57495d67482SBill Paul 		*(dest + i) = byte;
57595d67482SBill Paul 	}
57695d67482SBill Paul 
57795d67482SBill Paul 	return(err ? 1 : 0);
57895d67482SBill Paul }
57995d67482SBill Paul 
58095d67482SBill Paul static int
58195d67482SBill Paul bge_miibus_readreg(dev, phy, reg)
58295d67482SBill Paul 	device_t dev;
58395d67482SBill Paul 	int phy, reg;
58495d67482SBill Paul {
58595d67482SBill Paul 	struct bge_softc *sc;
58637ceeb4dSPaul Saab 	u_int32_t val, autopoll;
58795d67482SBill Paul 	int i;
58895d67482SBill Paul 
58995d67482SBill Paul 	sc = device_get_softc(dev);
59095d67482SBill Paul 
5910434d1b8SBill Paul 	/*
5920434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5930434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5940434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5950434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5960434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5970434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5980434d1b8SBill Paul 	 * special-cased.
5990434d1b8SBill Paul 	 */
600b1265c1aSJohn Polstra 	if (phy != 1)
60198b28ee5SBill Paul 		return(0);
60298b28ee5SBill Paul 
60337ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
60437ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
60537ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
60637ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
60737ceeb4dSPaul Saab 		DELAY(40);
60837ceeb4dSPaul Saab 	}
60937ceeb4dSPaul Saab 
61095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
61195d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
61295d67482SBill Paul 
61395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
61495d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
61595d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
61695d67482SBill Paul 			break;
61795d67482SBill Paul 	}
61895d67482SBill Paul 
61995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
62095d67482SBill Paul 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
62137ceeb4dSPaul Saab 		val = 0;
62237ceeb4dSPaul Saab 		goto done;
62395d67482SBill Paul 	}
62495d67482SBill Paul 
62595d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
62695d67482SBill Paul 
62737ceeb4dSPaul Saab done:
62837ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
62937ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
63037ceeb4dSPaul Saab 		DELAY(40);
63137ceeb4dSPaul Saab 	}
63237ceeb4dSPaul Saab 
63395d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
63495d67482SBill Paul 		return(0);
63595d67482SBill Paul 
63695d67482SBill Paul 	return(val & 0xFFFF);
63795d67482SBill Paul }
63895d67482SBill Paul 
63995d67482SBill Paul static int
64095d67482SBill Paul bge_miibus_writereg(dev, phy, reg, val)
64195d67482SBill Paul 	device_t dev;
64295d67482SBill Paul 	int phy, reg, val;
64395d67482SBill Paul {
64495d67482SBill Paul 	struct bge_softc *sc;
64537ceeb4dSPaul Saab 	u_int32_t autopoll;
64695d67482SBill Paul 	int i;
64795d67482SBill Paul 
64895d67482SBill Paul 	sc = device_get_softc(dev);
64995d67482SBill Paul 
65037ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
65137ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
65237ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
65337ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
65437ceeb4dSPaul Saab 		DELAY(40);
65537ceeb4dSPaul Saab 	}
65637ceeb4dSPaul Saab 
65795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
65895d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
65995d67482SBill Paul 
66095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
66195d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
66295d67482SBill Paul 			break;
66395d67482SBill Paul 	}
66495d67482SBill Paul 
66537ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
66637ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
66737ceeb4dSPaul Saab 		DELAY(40);
66837ceeb4dSPaul Saab 	}
66937ceeb4dSPaul Saab 
67095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
67195d67482SBill Paul 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
67295d67482SBill Paul 		return(0);
67395d67482SBill Paul 	}
67495d67482SBill Paul 
67595d67482SBill Paul 	return(0);
67695d67482SBill Paul }
67795d67482SBill Paul 
67895d67482SBill Paul static void
67995d67482SBill Paul bge_miibus_statchg(dev)
68095d67482SBill Paul 	device_t dev;
68195d67482SBill Paul {
68295d67482SBill Paul 	struct bge_softc *sc;
68395d67482SBill Paul 	struct mii_data *mii;
68495d67482SBill Paul 
68595d67482SBill Paul 	sc = device_get_softc(dev);
68695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
68795d67482SBill Paul 
68895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
689b418ad5cSPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
69095d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
69195d67482SBill Paul 	} else {
69295d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
69395d67482SBill Paul 	}
69495d67482SBill Paul 
69595d67482SBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
69695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
69795d67482SBill Paul 	} else {
69895d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
69995d67482SBill Paul 	}
70095d67482SBill Paul 
70195d67482SBill Paul 	return;
70295d67482SBill Paul }
70395d67482SBill Paul 
70495d67482SBill Paul /*
70595d67482SBill Paul  * Handle events that have triggered interrupts.
70695d67482SBill Paul  */
70795d67482SBill Paul static void
70895d67482SBill Paul bge_handle_events(sc)
70995d67482SBill Paul 	struct bge_softc		*sc;
71095d67482SBill Paul {
71195d67482SBill Paul 
71295d67482SBill Paul 	return;
71395d67482SBill Paul }
71495d67482SBill Paul 
71595d67482SBill Paul /*
71695d67482SBill Paul  * Memory management for jumbo frames.
71795d67482SBill Paul  */
71895d67482SBill Paul 
71995d67482SBill Paul static int
72095d67482SBill Paul bge_alloc_jumbo_mem(sc)
72195d67482SBill Paul 	struct bge_softc		*sc;
72295d67482SBill Paul {
72395d67482SBill Paul 	caddr_t			ptr;
724f41ac2beSBill Paul 	register int		i, error;
72595d67482SBill Paul 	struct bge_jpool_entry   *entry;
72695d67482SBill Paul 
727f41ac2beSBill Paul 	/* Create tag for jumbo buffer block */
72895d67482SBill Paul 
729f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
730f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
731f41ac2beSBill Paul 	    NULL, BGE_JMEM, 1, BGE_JMEM, 0, NULL, NULL,
732f41ac2beSBill Paul 	    &sc->bge_cdata.bge_jumbo_tag);
733f41ac2beSBill Paul 
734f41ac2beSBill Paul 	if (error) {
735f41ac2beSBill Paul 		printf("bge%d: could not allocate jumbo dma tag\n",
736f41ac2beSBill Paul 		    sc->bge_unit);
737f41ac2beSBill Paul 		return (ENOMEM);
73895d67482SBill Paul 	}
73995d67482SBill Paul 
740f41ac2beSBill Paul 	/* Allocate DMA'able memory for jumbo buffer block */
741f41ac2beSBill Paul 
742f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_jumbo_tag,
743f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_jumbo_buf, BUS_DMA_NOWAIT,
744f41ac2beSBill Paul 	    &sc->bge_cdata.bge_jumbo_map);
745f41ac2beSBill Paul 
746f41ac2beSBill Paul 	if (error)
747f41ac2beSBill Paul                 return (ENOMEM);
748f41ac2beSBill Paul 
74995d67482SBill Paul 	SLIST_INIT(&sc->bge_jfree_listhead);
75095d67482SBill Paul 	SLIST_INIT(&sc->bge_jinuse_listhead);
75195d67482SBill Paul 
75295d67482SBill Paul 	/*
75395d67482SBill Paul 	 * Now divide it up into 9K pieces and save the addresses
75495d67482SBill Paul 	 * in an array.
75595d67482SBill Paul 	 */
756f41ac2beSBill Paul 	ptr = sc->bge_ldata.bge_jumbo_buf;
75795d67482SBill Paul 	for (i = 0; i < BGE_JSLOTS; i++) {
75895d67482SBill Paul 		sc->bge_cdata.bge_jslots[i] = ptr;
75995d67482SBill Paul 		ptr += BGE_JLEN;
76095d67482SBill Paul 		entry = malloc(sizeof(struct bge_jpool_entry),
76195d67482SBill Paul 		    M_DEVBUF, M_NOWAIT);
76295d67482SBill Paul 		if (entry == NULL) {
763f41ac2beSBill Paul 			bge_free_jumbo_mem(sc);
764f41ac2beSBill Paul 			sc->bge_ldata.bge_jumbo_buf = NULL;
76595d67482SBill Paul 			printf("bge%d: no memory for jumbo "
76695d67482SBill Paul 			    "buffer queue!\n", sc->bge_unit);
76795d67482SBill Paul 			return(ENOBUFS);
76895d67482SBill Paul 		}
76995d67482SBill Paul 		entry->slot = i;
77095d67482SBill Paul 		SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
77195d67482SBill Paul 		    entry, jpool_entries);
77295d67482SBill Paul 	}
77395d67482SBill Paul 
77495d67482SBill Paul 	return(0);
77595d67482SBill Paul }
77695d67482SBill Paul 
77795d67482SBill Paul static void
77895d67482SBill Paul bge_free_jumbo_mem(sc)
77995d67482SBill Paul         struct bge_softc *sc;
78095d67482SBill Paul {
78195d67482SBill Paul         int i;
78295d67482SBill Paul         struct bge_jpool_entry *entry;
78395d67482SBill Paul 
78495d67482SBill Paul 	for (i = 0; i < BGE_JSLOTS; i++) {
78595d67482SBill Paul 		entry = SLIST_FIRST(&sc->bge_jfree_listhead);
78695d67482SBill Paul 		SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
78795d67482SBill Paul 		free(entry, M_DEVBUF);
78895d67482SBill Paul 	}
78995d67482SBill Paul 
790f41ac2beSBill Paul 	/* Destroy jumbo buffer block */
791f41ac2beSBill Paul 
792f41ac2beSBill Paul 	if (sc->bge_ldata.bge_rx_jumbo_ring)
793f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_jumbo_tag,
794f41ac2beSBill Paul 		    sc->bge_ldata.bge_jumbo_buf,
795f41ac2beSBill Paul 		    sc->bge_cdata.bge_jumbo_map);
796f41ac2beSBill Paul 
797f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
798f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_jumbo_tag,
799f41ac2beSBill Paul 		    sc->bge_cdata.bge_jumbo_map);
800f41ac2beSBill Paul 
801f41ac2beSBill Paul 	if (sc->bge_cdata.bge_jumbo_tag)
802f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_jumbo_tag);
80395d67482SBill Paul 
80495d67482SBill Paul         return;
80595d67482SBill Paul }
80695d67482SBill Paul 
80795d67482SBill Paul /*
80895d67482SBill Paul  * Allocate a jumbo buffer.
80995d67482SBill Paul  */
81095d67482SBill Paul static void *
81195d67482SBill Paul bge_jalloc(sc)
81295d67482SBill Paul 	struct bge_softc		*sc;
81395d67482SBill Paul {
81495d67482SBill Paul 	struct bge_jpool_entry   *entry;
81595d67482SBill Paul 
81695d67482SBill Paul 	entry = SLIST_FIRST(&sc->bge_jfree_listhead);
81795d67482SBill Paul 
81895d67482SBill Paul 	if (entry == NULL) {
81995d67482SBill Paul 		printf("bge%d: no free jumbo buffers\n", sc->bge_unit);
82095d67482SBill Paul 		return(NULL);
82195d67482SBill Paul 	}
82295d67482SBill Paul 
82395d67482SBill Paul 	SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
82495d67482SBill Paul 	SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
82595d67482SBill Paul 	return(sc->bge_cdata.bge_jslots[entry->slot]);
82695d67482SBill Paul }
82795d67482SBill Paul 
82895d67482SBill Paul /*
82995d67482SBill Paul  * Release a jumbo buffer.
83095d67482SBill Paul  */
83195d67482SBill Paul static void
83295d67482SBill Paul bge_jfree(buf, args)
833914596abSAlfred Perlstein 	void *buf;
83495d67482SBill Paul 	void *args;
83595d67482SBill Paul {
83695d67482SBill Paul 	struct bge_jpool_entry *entry;
83795d67482SBill Paul 	struct bge_softc *sc;
83895d67482SBill Paul 	int i;
83995d67482SBill Paul 
84095d67482SBill Paul 	/* Extract the softc struct pointer. */
84195d67482SBill Paul 	sc = (struct bge_softc *)args;
84295d67482SBill Paul 
84395d67482SBill Paul 	if (sc == NULL)
84495d67482SBill Paul 		panic("bge_jfree: can't find softc pointer!");
84595d67482SBill Paul 
84695d67482SBill Paul 	/* calculate the slot this buffer belongs to */
84795d67482SBill Paul 
84895d67482SBill Paul 	i = ((vm_offset_t)buf
849f41ac2beSBill Paul 	     - (vm_offset_t)sc->bge_ldata.bge_jumbo_buf) / BGE_JLEN;
85095d67482SBill Paul 
85195d67482SBill Paul 	if ((i < 0) || (i >= BGE_JSLOTS))
85295d67482SBill Paul 		panic("bge_jfree: asked to free buffer that we don't manage!");
85395d67482SBill Paul 
85495d67482SBill Paul 	entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
85595d67482SBill Paul 	if (entry == NULL)
85695d67482SBill Paul 		panic("bge_jfree: buffer not in use!");
85795d67482SBill Paul 	entry->slot = i;
85895d67482SBill Paul 	SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
85995d67482SBill Paul 	SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
86095d67482SBill Paul 
86195d67482SBill Paul 	return;
86295d67482SBill Paul }
86395d67482SBill Paul 
86495d67482SBill Paul 
86595d67482SBill Paul /*
86695d67482SBill Paul  * Intialize a standard receive ring descriptor.
86795d67482SBill Paul  */
86895d67482SBill Paul static int
86995d67482SBill Paul bge_newbuf_std(sc, i, m)
87095d67482SBill Paul 	struct bge_softc	*sc;
87195d67482SBill Paul 	int			i;
87295d67482SBill Paul 	struct mbuf		*m;
87395d67482SBill Paul {
87495d67482SBill Paul 	struct mbuf		*m_new = NULL;
87595d67482SBill Paul 	struct bge_rx_bd	*r;
876f41ac2beSBill Paul 	struct bge_dmamap_arg	ctx;
877f41ac2beSBill Paul 	int			error;
87895d67482SBill Paul 
87995d67482SBill Paul 	if (m == NULL) {
880a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
88195d67482SBill Paul 		if (m_new == NULL) {
88295d67482SBill Paul 			return(ENOBUFS);
88395d67482SBill Paul 		}
88495d67482SBill Paul 
885a163d034SWarner Losh 		MCLGET(m_new, M_DONTWAIT);
88695d67482SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
88795d67482SBill Paul 			m_freem(m_new);
88895d67482SBill Paul 			return(ENOBUFS);
88995d67482SBill Paul 		}
89095d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
89195d67482SBill Paul 	} else {
89295d67482SBill Paul 		m_new = m;
89395d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
89495d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
89595d67482SBill Paul 	}
89695d67482SBill Paul 
897e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
89895d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
89995d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
900f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
901f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
902f41ac2beSBill Paul 	ctx.sc = sc;
903f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
904f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
905f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
906f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
907f41ac2beSBill Paul 		if (m == NULL)
908f41ac2beSBill Paul 			m_freem(m_new);
909f41ac2beSBill Paul 		return(ENOMEM);
910f41ac2beSBill Paul 	}
911f41ac2beSBill Paul 	r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr));
912f41ac2beSBill Paul 	r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr));
913f41ac2beSBill Paul 	r->bge_flags = htole16(BGE_RXBDFLAG_END);
914f41ac2beSBill Paul 	r->bge_len = htole16(m_new->m_len);
915f41ac2beSBill Paul 	r->bge_idx = htole16(i);
916f41ac2beSBill Paul 
917f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
918f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
919f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
92095d67482SBill Paul 
92195d67482SBill Paul 	return(0);
92295d67482SBill Paul }
92395d67482SBill Paul 
92495d67482SBill Paul /*
92595d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
92695d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
92795d67482SBill Paul  */
92895d67482SBill Paul static int
92995d67482SBill Paul bge_newbuf_jumbo(sc, i, m)
93095d67482SBill Paul 	struct bge_softc *sc;
93195d67482SBill Paul 	int i;
93295d67482SBill Paul 	struct mbuf *m;
93395d67482SBill Paul {
93495d67482SBill Paul 	struct mbuf *m_new = NULL;
93595d67482SBill Paul 	struct bge_rx_bd *r;
936f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
937f41ac2beSBill Paul 	int error;
93895d67482SBill Paul 
93995d67482SBill Paul 	if (m == NULL) {
94095d67482SBill Paul 		caddr_t			*buf = NULL;
94195d67482SBill Paul 
94295d67482SBill Paul 		/* Allocate the mbuf. */
943a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
94495d67482SBill Paul 		if (m_new == NULL) {
94595d67482SBill Paul 			return(ENOBUFS);
94695d67482SBill Paul 		}
94795d67482SBill Paul 
94895d67482SBill Paul 		/* Allocate the jumbo buffer */
94995d67482SBill Paul 		buf = bge_jalloc(sc);
95095d67482SBill Paul 		if (buf == NULL) {
95195d67482SBill Paul 			m_freem(m_new);
95295d67482SBill Paul 			printf("bge%d: jumbo allocation failed "
95395d67482SBill Paul 			    "-- packet dropped!\n", sc->bge_unit);
95495d67482SBill Paul 			return(ENOBUFS);
95595d67482SBill Paul 		}
95695d67482SBill Paul 
95795d67482SBill Paul 		/* Attach the buffer to the mbuf. */
95895d67482SBill Paul 		m_new->m_data = (void *) buf;
95995d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
96095d67482SBill Paul 		MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, bge_jfree,
96195d67482SBill Paul 		    (struct bge_softc *)sc, 0, EXT_NET_DRV);
96295d67482SBill Paul 	} else {
96395d67482SBill Paul 		m_new = m;
96495d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
96595d67482SBill Paul 		m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
96695d67482SBill Paul 	}
96795d67482SBill Paul 
968e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
96995d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
97095d67482SBill Paul 	/* Set up the descriptor. */
97195d67482SBill Paul 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
972f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
973f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
974f41ac2beSBill Paul 	ctx.sc = sc;
975f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag_jumbo,
976f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], mtod(m_new, void *),
977f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
978f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
979f41ac2beSBill Paul 		if (m == NULL)
980f41ac2beSBill Paul 			m_freem(m_new);
981f41ac2beSBill Paul 		return(ENOMEM);
982f41ac2beSBill Paul 	}
983f41ac2beSBill Paul 	r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr));
984f41ac2beSBill Paul 	r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr));
985f41ac2beSBill Paul 	r->bge_flags = htole16(BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING);
986f41ac2beSBill Paul 	r->bge_len = htole16(m_new->m_len);
987f41ac2beSBill Paul 	r->bge_idx = htole16(i);
988f41ac2beSBill Paul 
989f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
990f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
991f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
99295d67482SBill Paul 
99395d67482SBill Paul 	return(0);
99495d67482SBill Paul }
99595d67482SBill Paul 
99695d67482SBill Paul /*
99795d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
99895d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
99995d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
100095d67482SBill Paul  * the NIC.
100195d67482SBill Paul  */
100295d67482SBill Paul static int
100395d67482SBill Paul bge_init_rx_ring_std(sc)
100495d67482SBill Paul 	struct bge_softc *sc;
100595d67482SBill Paul {
100695d67482SBill Paul 	int i;
100795d67482SBill Paul 
100895d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
100995d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
101095d67482SBill Paul 			return(ENOBUFS);
101195d67482SBill Paul 	};
101295d67482SBill Paul 
1013f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1014f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
1015f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1016f41ac2beSBill Paul 
101795d67482SBill Paul 	sc->bge_std = i - 1;
101895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
101995d67482SBill Paul 
102095d67482SBill Paul 	return(0);
102195d67482SBill Paul }
102295d67482SBill Paul 
102395d67482SBill Paul static void
102495d67482SBill Paul bge_free_rx_ring_std(sc)
102595d67482SBill Paul 	struct bge_softc *sc;
102695d67482SBill Paul {
102795d67482SBill Paul 	int i;
102895d67482SBill Paul 
102995d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
103095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
103195d67482SBill Paul 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
103295d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
1033f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1034f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
103595d67482SBill Paul 		}
1036f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
103795d67482SBill Paul 		    sizeof(struct bge_rx_bd));
103895d67482SBill Paul 	}
103995d67482SBill Paul 
104095d67482SBill Paul 	return;
104195d67482SBill Paul }
104295d67482SBill Paul 
104395d67482SBill Paul static int
104495d67482SBill Paul bge_init_rx_ring_jumbo(sc)
104595d67482SBill Paul 	struct bge_softc *sc;
104695d67482SBill Paul {
104795d67482SBill Paul 	int i;
104895d67482SBill Paul 	struct bge_rcb *rcb;
104995d67482SBill Paul 
105095d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
105195d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
105295d67482SBill Paul 			return(ENOBUFS);
105395d67482SBill Paul 	};
105495d67482SBill Paul 
1055f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1056f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
1057f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1058f41ac2beSBill Paul 
105995d67482SBill Paul 	sc->bge_jumbo = i - 1;
106095d67482SBill Paul 
1061f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
106267111612SJohn Polstra 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
106367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
106495d67482SBill Paul 
106595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
106695d67482SBill Paul 
106795d67482SBill Paul 	return(0);
106895d67482SBill Paul }
106995d67482SBill Paul 
107095d67482SBill Paul static void
107195d67482SBill Paul bge_free_rx_ring_jumbo(sc)
107295d67482SBill Paul 	struct bge_softc *sc;
107395d67482SBill Paul {
107495d67482SBill Paul 	int i;
107595d67482SBill Paul 
107695d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
107795d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
107895d67482SBill Paul 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
107995d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1080f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1081f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
108295d67482SBill Paul 		}
1083f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
108495d67482SBill Paul 		    sizeof(struct bge_rx_bd));
108595d67482SBill Paul 	}
108695d67482SBill Paul 
108795d67482SBill Paul 	return;
108895d67482SBill Paul }
108995d67482SBill Paul 
109095d67482SBill Paul static void
109195d67482SBill Paul bge_free_tx_ring(sc)
109295d67482SBill Paul 	struct bge_softc *sc;
109395d67482SBill Paul {
109495d67482SBill Paul 	int i;
109595d67482SBill Paul 
1096f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
109795d67482SBill Paul 		return;
109895d67482SBill Paul 
109995d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
110095d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
110195d67482SBill Paul 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
110295d67482SBill Paul 			sc->bge_cdata.bge_tx_chain[i] = NULL;
1103f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1104f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
110595d67482SBill Paul 		}
1106f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
110795d67482SBill Paul 		    sizeof(struct bge_tx_bd));
110895d67482SBill Paul 	}
110995d67482SBill Paul 
111095d67482SBill Paul 	return;
111195d67482SBill Paul }
111295d67482SBill Paul 
111395d67482SBill Paul static int
111495d67482SBill Paul bge_init_tx_ring(sc)
111595d67482SBill Paul 	struct bge_softc *sc;
111695d67482SBill Paul {
111795d67482SBill Paul 	sc->bge_txcnt = 0;
111895d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
11193927098fSPaul Saab 
112095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
11213927098fSPaul Saab 	/* 5700 b2 errata */
1122e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
11233927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
11243927098fSPaul Saab 
11253927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
11263927098fSPaul Saab 	/* 5700 b2 errata */
1127e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
112895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
112995d67482SBill Paul 
113095d67482SBill Paul 	return(0);
113195d67482SBill Paul }
113295d67482SBill Paul 
113395d67482SBill Paul #define BGE_POLY	0xEDB88320
113495d67482SBill Paul 
1135a55a017fSDavid E. O'Brien static uint32_t
1136aa825502SDavid E. O'Brien bge_mchash(addr)
1137a55a017fSDavid E. O'Brien 	const uint8_t *addr;
113895d67482SBill Paul {
1139a55a017fSDavid E. O'Brien 	uint32_t crc;
1140aa825502SDavid E. O'Brien 	int idx, bit;
1141a55a017fSDavid E. O'Brien 	uint8_t data;
114295d67482SBill Paul 
114395d67482SBill Paul 	/* Compute CRC for the address value. */
114495d67482SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
114595d67482SBill Paul 
114695d67482SBill Paul 	for (idx = 0; idx < 6; idx++) {
114795d67482SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
114895d67482SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? BGE_POLY : 0);
114995d67482SBill Paul 	}
115095d67482SBill Paul 
115195d67482SBill Paul 	return(crc & 0x7F);
115295d67482SBill Paul }
115395d67482SBill Paul 
115495d67482SBill Paul static void
115595d67482SBill Paul bge_setmulti(sc)
115695d67482SBill Paul 	struct bge_softc *sc;
115795d67482SBill Paul {
115895d67482SBill Paul 	struct ifnet *ifp;
115995d67482SBill Paul 	struct ifmultiaddr *ifma;
116095d67482SBill Paul 	u_int32_t hashes[4] = { 0, 0, 0, 0 };
116195d67482SBill Paul 	int h, i;
116295d67482SBill Paul 
11630f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
11640f9bd73bSSam Leffler 
116595d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
116695d67482SBill Paul 
116795d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
116895d67482SBill Paul 		for (i = 0; i < 4; i++)
116995d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
117095d67482SBill Paul 		return;
117195d67482SBill Paul 	}
117295d67482SBill Paul 
117395d67482SBill Paul 	/* First, zot all the existing filters. */
117495d67482SBill Paul 	for (i = 0; i < 4; i++)
117595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
117695d67482SBill Paul 
117795d67482SBill Paul 	/* Now program new ones. */
117895d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
117995d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
118095d67482SBill Paul 			continue;
1181aa825502SDavid E. O'Brien 		h = bge_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
118295d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
118395d67482SBill Paul 	}
118495d67482SBill Paul 
118595d67482SBill Paul 	for (i = 0; i < 4; i++)
118695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
118795d67482SBill Paul 
118895d67482SBill Paul 	return;
118995d67482SBill Paul }
119095d67482SBill Paul 
119195d67482SBill Paul /*
119295d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
119395d67482SBill Paul  * self-test results.
119495d67482SBill Paul  */
119595d67482SBill Paul static int
119695d67482SBill Paul bge_chipinit(sc)
119795d67482SBill Paul 	struct bge_softc *sc;
119895d67482SBill Paul {
119995d67482SBill Paul 	int			i;
12005cba12d3SPaul Saab 	u_int32_t		dma_rw_ctl;
120195d67482SBill Paul 
120295d67482SBill Paul 	/* Set endianness before we access any non-PCI registers. */
120395d67482SBill Paul #if BYTE_ORDER == BIG_ENDIAN
120495d67482SBill Paul 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
120595d67482SBill Paul 	    BGE_BIGENDIAN_INIT, 4);
120695d67482SBill Paul #else
120795d67482SBill Paul 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
120895d67482SBill Paul 	    BGE_LITTLEENDIAN_INIT, 4);
120995d67482SBill Paul #endif
121095d67482SBill Paul 
121195d67482SBill Paul 	/*
121295d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
121395d67482SBill Paul 	 * self-tests passed.
121495d67482SBill Paul 	 */
121595d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
121695d67482SBill Paul 		printf("bge%d: RX CPU self-diagnostics failed!\n",
121795d67482SBill Paul 		    sc->bge_unit);
121895d67482SBill Paul 		return(ENODEV);
121995d67482SBill Paul 	}
122095d67482SBill Paul 
122195d67482SBill Paul 	/* Clear the MAC control register */
122295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
122395d67482SBill Paul 
122495d67482SBill Paul 	/*
122595d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
122695d67482SBill Paul 	 * internal memory.
122795d67482SBill Paul 	 */
122895d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
122995d67482SBill Paul 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
123095d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
123195d67482SBill Paul 
123295d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
123395d67482SBill Paul 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
123495d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
123595d67482SBill Paul 
123695d67482SBill Paul 	/* Set up the PCI DMA control register. */
12378287860eSJohn Polstra 	if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
12388287860eSJohn Polstra 	    BGE_PCISTATE_PCI_BUSMODE) {
12398287860eSJohn Polstra 		/* Conventional PCI bus */
12405cba12d3SPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
12415cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
12425cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
12435cba12d3SPaul Saab 		    (0x0F);
12448287860eSJohn Polstra 	} else {
12458287860eSJohn Polstra 		/* PCI-X bus */
12465cba12d3SPaul Saab 		/*
12475cba12d3SPaul Saab 		 * The 5704 uses a different encoding of read/write
12485cba12d3SPaul Saab 		 * watermarks.
12495cba12d3SPaul Saab 		 */
1250e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
12515cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
12525cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
12535cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
12545cba12d3SPaul Saab 		else
12555cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
12565cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
12575cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
12585cba12d3SPaul Saab 			    (0x0F);
12595cba12d3SPaul Saab 
12605cba12d3SPaul Saab 		/*
12615cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
12625cba12d3SPaul Saab 		 * for hardware bugs.
12635cba12d3SPaul Saab 		 */
1264e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1265e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
12665cba12d3SPaul Saab 			u_int32_t tmp;
12675cba12d3SPaul Saab 
12685cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
12695cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
12705cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
12718287860eSJohn Polstra 		}
12725cba12d3SPaul Saab 	}
12735cba12d3SPaul Saab 
1274e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
12750434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
12760434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5705)
12775cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
12785cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
127995d67482SBill Paul 
128095d67482SBill Paul 	/*
128195d67482SBill Paul 	 * Set up general mode register.
128295d67482SBill Paul 	 */
128395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_WORDSWAP_NONFRAME|
128495d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
128595d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1286e446dc86SPaul Saab 	    BGE_MODECTL_TX_NO_PHDR_CSUM|BGE_MODECTL_RX_NO_PHDR_CSUM);
128795d67482SBill Paul 
128895d67482SBill Paul 	/*
1289ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1290ea13bdd5SJohn Polstra 	 * properly by these devices.
129195d67482SBill Paul 	 */
1292ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
129395d67482SBill Paul 
129495d67482SBill Paul #ifdef __brokenalpha__
129595d67482SBill Paul 	/*
129695d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
129795d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
129895d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
129995d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
130095d67482SBill Paul 	 */
130162f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
130262f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
130395d67482SBill Paul #endif
130495d67482SBill Paul 
130595d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
130695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
130795d67482SBill Paul 
130895d67482SBill Paul 	return(0);
130995d67482SBill Paul }
131095d67482SBill Paul 
131195d67482SBill Paul static int
131295d67482SBill Paul bge_blockinit(sc)
131395d67482SBill Paul 	struct bge_softc *sc;
131495d67482SBill Paul {
131595d67482SBill Paul 	struct bge_rcb *rcb;
131667111612SJohn Polstra 	volatile struct bge_rcb *vrcb;
131795d67482SBill Paul 	int i;
131895d67482SBill Paul 
131995d67482SBill Paul 	/*
132095d67482SBill Paul 	 * Initialize the memory window pointer register so that
132195d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
132295d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
132395d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
132495d67482SBill Paul 	 */
132595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
132695d67482SBill Paul 
1327822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1328822f63fcSBill Paul 
13290434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
133095d67482SBill Paul 		/* Configure mbuf memory pool */
133195d67482SBill Paul 		if (sc->bge_extram) {
13320434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
13330434d1b8SBill Paul 			    BGE_EXT_SSRAM);
1334822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1335822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1336822f63fcSBill Paul 			else
133795d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
133895d67482SBill Paul 		} else {
13390434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
13400434d1b8SBill Paul 			    BGE_BUFFPOOL_1);
1341822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1342822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1343822f63fcSBill Paul 			else
134495d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
134595d67482SBill Paul 		}
134695d67482SBill Paul 
134795d67482SBill Paul 		/* Configure DMA resource pool */
13480434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
13490434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
135095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
13510434d1b8SBill Paul 	}
135295d67482SBill Paul 
135395d67482SBill Paul 	/* Configure mbuf pool watermarks */
13540434d1b8SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
13550434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
13560434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
13570434d1b8SBill Paul 	} else {
1358fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1359fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
13600434d1b8SBill Paul 	}
1361fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
136295d67482SBill Paul 
136395d67482SBill Paul 	/* Configure DMA resource watermarks */
136495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
136595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
136695d67482SBill Paul 
136795d67482SBill Paul 	/* Enable buffer manager */
13680434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
136995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
137095d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
137195d67482SBill Paul 
137295d67482SBill Paul 		/* Poll for buffer manager start indication */
137395d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
137495d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
137595d67482SBill Paul 				break;
137695d67482SBill Paul 			DELAY(10);
137795d67482SBill Paul 		}
137895d67482SBill Paul 
137995d67482SBill Paul 		if (i == BGE_TIMEOUT) {
138095d67482SBill Paul 			printf("bge%d: buffer manager failed to start\n",
138195d67482SBill Paul 			    sc->bge_unit);
138295d67482SBill Paul 			return(ENXIO);
138395d67482SBill Paul 		}
13840434d1b8SBill Paul 	}
138595d67482SBill Paul 
138695d67482SBill Paul 	/* Enable flow-through queues */
138795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
138895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
138995d67482SBill Paul 
139095d67482SBill Paul 	/* Wait until queue initialization is complete */
139195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
139295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
139395d67482SBill Paul 			break;
139495d67482SBill Paul 		DELAY(10);
139595d67482SBill Paul 	}
139695d67482SBill Paul 
139795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
139895d67482SBill Paul 		printf("bge%d: flow-through queue init failed\n",
139995d67482SBill Paul 		    sc->bge_unit);
140095d67482SBill Paul 		return(ENXIO);
140195d67482SBill Paul 	}
140295d67482SBill Paul 
140395d67482SBill Paul 	/* Initialize the standard RX ring control block */
1404f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1405f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1406f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1407f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1408f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1409f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1410f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
14110434d1b8SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705)
14120434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
14130434d1b8SBill Paul 	else
14140434d1b8SBill Paul 		rcb->bge_maxlen_flags =
14150434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
141695d67482SBill Paul 	if (sc->bge_extram)
141795d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
141895d67482SBill Paul 	else
141995d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
142067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
142167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1422f41ac2beSBill Paul 
142367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
142467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
142595d67482SBill Paul 
142695d67482SBill Paul 	/*
142795d67482SBill Paul 	 * Initialize the jumbo RX ring control block
142895d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
142995d67482SBill Paul 	 * field until we're actually ready to start
143095d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
143195d67482SBill Paul 	 * high enough to require it).
143295d67482SBill Paul 	 */
14330434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
1434f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1435f41ac2beSBill Paul 
1436f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1437f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1438f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1439f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1440f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1441f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1442f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
144367111612SJohn Polstra 		rcb->bge_maxlen_flags =
14440434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN,
14450434d1b8SBill Paul 		    BGE_RCB_FLAG_RING_DISABLED);
144695d67482SBill Paul 		if (sc->bge_extram)
144795d67482SBill Paul 			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
144895d67482SBill Paul 		else
144995d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
145067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
145167111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
145267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
145367111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1454f41ac2beSBill Paul 
14550434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
14560434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
145767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
145895d67482SBill Paul 
145995d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1460f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
146167111612SJohn Polstra 		rcb->bge_maxlen_flags =
146267111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
14630434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
14640434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
14650434d1b8SBill Paul 	}
146695d67482SBill Paul 
146795d67482SBill Paul 	/*
146895d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
146995d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
147095d67482SBill Paul 	 * each ring.
147195d67482SBill Paul 	 */
147295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
147395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
147495d67482SBill Paul 
147595d67482SBill Paul 	/*
147695d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
147795d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
147895d67482SBill Paul 	 * These are located in NIC memory.
147995d67482SBill Paul 	 */
148067111612SJohn Polstra 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
148195d67482SBill Paul 	    BGE_SEND_RING_RCB);
148295d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
148367111612SJohn Polstra 		vrcb->bge_maxlen_flags =
148467111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
148567111612SJohn Polstra 		vrcb->bge_nicaddr = 0;
148667111612SJohn Polstra 		vrcb++;
148795d67482SBill Paul 	}
148895d67482SBill Paul 
148995d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
149067111612SJohn Polstra 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
149195d67482SBill Paul 	    BGE_SEND_RING_RCB);
1492f41ac2beSBill Paul 	vrcb->bge_hostaddr.bge_addr_lo =
1493f41ac2beSBill Paul 	    htole32(BGE_ADDR_LO(sc->bge_ldata.bge_tx_ring_paddr));
1494f41ac2beSBill Paul 	vrcb->bge_hostaddr.bge_addr_hi =
1495f41ac2beSBill Paul 	    htole32(BGE_ADDR_HI(sc->bge_ldata.bge_tx_ring_paddr));
149667111612SJohn Polstra 	vrcb->bge_nicaddr = BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT);
14970434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
14980434d1b8SBill Paul 		vrcb->bge_maxlen_flags =
14990434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0);
150095d67482SBill Paul 
150195d67482SBill Paul 	/* Disable all unused RX return rings */
150267111612SJohn Polstra 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
150395d67482SBill Paul 	    BGE_RX_RETURN_RING_RCB);
150495d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
150567111612SJohn Polstra 		vrcb->bge_hostaddr.bge_addr_hi = 0;
150667111612SJohn Polstra 		vrcb->bge_hostaddr.bge_addr_lo = 0;
150767111612SJohn Polstra 		vrcb->bge_maxlen_flags =
15080434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
150967111612SJohn Polstra 		    BGE_RCB_FLAG_RING_DISABLED);
151067111612SJohn Polstra 		vrcb->bge_nicaddr = 0;
151195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
151295d67482SBill Paul 		    (i * (sizeof(u_int64_t))), 0);
151367111612SJohn Polstra 		vrcb++;
151495d67482SBill Paul 	}
151595d67482SBill Paul 
151695d67482SBill Paul 	/* Initialize RX ring indexes */
151795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
151895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
151995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
152095d67482SBill Paul 
152195d67482SBill Paul 	/*
152295d67482SBill Paul 	 * Set up RX return ring 0
152395d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
152495d67482SBill Paul 	 * The return rings live entirely within the host, so the
152595d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
152695d67482SBill Paul 	 */
152767111612SJohn Polstra 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
152895d67482SBill Paul 	    BGE_RX_RETURN_RING_RCB);
1529f41ac2beSBill Paul 	vrcb->bge_hostaddr.bge_addr_lo =
1530f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_return_ring_paddr);
1531f41ac2beSBill Paul 	vrcb->bge_hostaddr.bge_addr_hi =
1532f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_return_ring_paddr);
1533f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
1534f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREWRITE);
153567111612SJohn Polstra 	vrcb->bge_nicaddr = 0x00000000;
15360434d1b8SBill Paul 	vrcb->bge_maxlen_flags =
15370434d1b8SBill Paul 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0);
153895d67482SBill Paul 
153995d67482SBill Paul 	/* Set random backoff seed for TX */
154095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
154195d67482SBill Paul 	    sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
154295d67482SBill Paul 	    sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
154395d67482SBill Paul 	    sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] +
154495d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
154595d67482SBill Paul 
154695d67482SBill Paul 	/* Set inter-packet gap */
154795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
154895d67482SBill Paul 
154995d67482SBill Paul 	/*
155095d67482SBill Paul 	 * Specify which ring to use for packets that don't match
155195d67482SBill Paul 	 * any RX rules.
155295d67482SBill Paul 	 */
155395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
155495d67482SBill Paul 
155595d67482SBill Paul 	/*
155695d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
155795d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
155895d67482SBill Paul 	 */
155995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
156095d67482SBill Paul 
156195d67482SBill Paul 	/* Inialize RX list placement stats mask. */
156295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
156395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
156495d67482SBill Paul 
156595d67482SBill Paul 	/* Disable host coalescing until we get it set up */
156695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
156795d67482SBill Paul 
156895d67482SBill Paul 	/* Poll to make sure it's shut down. */
156995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
157095d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
157195d67482SBill Paul 			break;
157295d67482SBill Paul 		DELAY(10);
157395d67482SBill Paul 	}
157495d67482SBill Paul 
157595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
157695d67482SBill Paul 		printf("bge%d: host coalescing engine failed to idle\n",
157795d67482SBill Paul 		    sc->bge_unit);
157895d67482SBill Paul 		return(ENXIO);
157995d67482SBill Paul 	}
158095d67482SBill Paul 
158195d67482SBill Paul 	/* Set up host coalescing defaults */
158295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
158395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
158495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
158595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
15860434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
158795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
158895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
15890434d1b8SBill Paul 	}
159095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
159195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
159295d67482SBill Paul 
159395d67482SBill Paul 	/* Set up address of statistics block */
15940434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
1595f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1596f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
159795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1598f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
15990434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
160095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
16010434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
16020434d1b8SBill Paul 	}
16030434d1b8SBill Paul 
16040434d1b8SBill Paul 	/* Set up address of status block */
1605f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1606f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
160795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1608f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1609f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
1610f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREWRITE);
1611f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1612f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
161395d67482SBill Paul 
161495d67482SBill Paul 	/* Turn on host coalescing state machine */
161595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
161695d67482SBill Paul 
161795d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
161895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
161995d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
162095d67482SBill Paul 
162195d67482SBill Paul 	/* Turn on RX list placement state machine */
162295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
162395d67482SBill Paul 
162495d67482SBill Paul 	/* Turn on RX list selector state machine. */
16250434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
162695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
162795d67482SBill Paul 
162895d67482SBill Paul 	/* Turn on DMA, clear stats */
162995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
163095d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
163195d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
163295d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
163395d67482SBill Paul 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
163495d67482SBill Paul 
163595d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
163695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
163795d67482SBill Paul 
163895d67482SBill Paul #ifdef notdef
163995d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
164095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
164195d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
164295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
164395d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
164495d67482SBill Paul #endif
164595d67482SBill Paul 
164695d67482SBill Paul 	/* Turn on DMA completion state machine */
16470434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
164895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
164995d67482SBill Paul 
165095d67482SBill Paul 	/* Turn on write DMA state machine */
165195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
165295d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
165395d67482SBill Paul 
165495d67482SBill Paul 	/* Turn on read DMA state machine */
165595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
165695d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
165795d67482SBill Paul 
165895d67482SBill Paul 	/* Turn on RX data completion state machine */
165995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
166095d67482SBill Paul 
166195d67482SBill Paul 	/* Turn on RX BD initiator state machine */
166295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
166395d67482SBill Paul 
166495d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
166595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
166695d67482SBill Paul 
166795d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
16680434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
166995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
167095d67482SBill Paul 
167195d67482SBill Paul 	/* Turn on send BD completion state machine */
167295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
167395d67482SBill Paul 
167495d67482SBill Paul 	/* Turn on send data completion state machine */
167595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
167695d67482SBill Paul 
167795d67482SBill Paul 	/* Turn on send data initiator state machine */
167895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
167995d67482SBill Paul 
168095d67482SBill Paul 	/* Turn on send BD initiator state machine */
168195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
168295d67482SBill Paul 
168395d67482SBill Paul 	/* Turn on send BD selector state machine */
168495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
168595d67482SBill Paul 
168695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
168795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
168895d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
168995d67482SBill Paul 
169095d67482SBill Paul 	/* ack/clear link change events */
169195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
16920434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
16930434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1694f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
169595d67482SBill Paul 
169695d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
169795d67482SBill Paul 	if (sc->bge_tbi) {
169895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1699a1d52896SBill Paul  	} else {
170095d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
1701e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700)
1702a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1703a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1704a1d52896SBill Paul 	}
170595d67482SBill Paul 
170695d67482SBill Paul 	/* Enable link state change attentions. */
170795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
170895d67482SBill Paul 
170995d67482SBill Paul 	return(0);
171095d67482SBill Paul }
171195d67482SBill Paul 
171295d67482SBill Paul /*
171395d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
171495d67482SBill Paul  * against our list and return its name if we find a match. Note
171595d67482SBill Paul  * that since the Broadcom controller contains VPD support, we
171695d67482SBill Paul  * can get the device name string from the controller itself instead
171795d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
171895d67482SBill Paul  * we'll always announce the right product name.
171995d67482SBill Paul  */
172095d67482SBill Paul static int
172195d67482SBill Paul bge_probe(dev)
172295d67482SBill Paul 	device_t dev;
172395d67482SBill Paul {
172495d67482SBill Paul 	struct bge_type *t;
172595d67482SBill Paul 	struct bge_softc *sc;
1726029e2ee3SJohn Polstra 	char *descbuf;
172795d67482SBill Paul 
172895d67482SBill Paul 	t = bge_devs;
172995d67482SBill Paul 
173095d67482SBill Paul 	sc = device_get_softc(dev);
173195d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
173295d67482SBill Paul 	sc->bge_unit = device_get_unit(dev);
173395d67482SBill Paul 	sc->bge_dev = dev;
173495d67482SBill Paul 
173595d67482SBill Paul 	while(t->bge_name != NULL) {
173695d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
173795d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
173895d67482SBill Paul #ifdef notdef
173995d67482SBill Paul 			bge_vpd_read(sc);
174095d67482SBill Paul 			device_set_desc(dev, sc->bge_vpd_prodname);
174195d67482SBill Paul #endif
1742029e2ee3SJohn Polstra 			descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
1743029e2ee3SJohn Polstra 			if (descbuf == NULL)
1744029e2ee3SJohn Polstra 				return(ENOMEM);
1745029e2ee3SJohn Polstra 			snprintf(descbuf, BGE_DEVDESC_MAX,
1746029e2ee3SJohn Polstra 			    "%s, ASIC rev. %#04x", t->bge_name,
1747029e2ee3SJohn Polstra 			    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16);
1748029e2ee3SJohn Polstra 			device_set_desc_copy(dev, descbuf);
17496d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
17506d2a9bd6SDoug Ambrisko 				sc->bge_no_3_led = 1;
1751029e2ee3SJohn Polstra 			free(descbuf, M_TEMP);
175295d67482SBill Paul 			return(0);
175395d67482SBill Paul 		}
175495d67482SBill Paul 		t++;
175595d67482SBill Paul 	}
175695d67482SBill Paul 
175795d67482SBill Paul 	return(ENXIO);
175895d67482SBill Paul }
175995d67482SBill Paul 
1760f41ac2beSBill Paul static void
1761f41ac2beSBill Paul bge_dma_free(sc)
1762f41ac2beSBill Paul 	struct bge_softc *sc;
1763f41ac2beSBill Paul {
1764f41ac2beSBill Paul 	int i;
1765f41ac2beSBill Paul 
1766f41ac2beSBill Paul 
1767f41ac2beSBill Paul 	/* Destroy DMA maps for RX buffers */
1768f41ac2beSBill Paul 
1769f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1770f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1771f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1772f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1773f41ac2beSBill Paul 	}
1774f41ac2beSBill Paul 
1775f41ac2beSBill Paul 	/* Destroy DMA maps for jumbo RX buffers */
1776f41ac2beSBill Paul 
1777f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1778f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1779f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1780f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1781f41ac2beSBill Paul 	}
1782f41ac2beSBill Paul 
1783f41ac2beSBill Paul 	/* Destroy DMA maps for TX buffers */
1784f41ac2beSBill Paul 
1785f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1786f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1787f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1788f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1789f41ac2beSBill Paul 	}
1790f41ac2beSBill Paul 
1791f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1792f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1793f41ac2beSBill Paul 
1794f41ac2beSBill Paul 
1795f41ac2beSBill Paul 	/* Destroy standard RX ring */
1796f41ac2beSBill Paul 
1797f41ac2beSBill Paul 	if (sc->bge_ldata.bge_rx_std_ring)
1798f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1799f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1800f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1801f41ac2beSBill Paul 
1802f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_map) {
1803f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1804f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1805f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_std_ring_tag,
1806f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1807f41ac2beSBill Paul 	}
1808f41ac2beSBill Paul 
1809f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1810f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1811f41ac2beSBill Paul 
1812f41ac2beSBill Paul 	/* Destroy jumbo RX ring */
1813f41ac2beSBill Paul 
1814f41ac2beSBill Paul 	if (sc->bge_ldata.bge_rx_jumbo_ring)
1815f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1816f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1817f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1818f41ac2beSBill Paul 
1819f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_map) {
1820f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1821f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1822f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1823f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1824f41ac2beSBill Paul 	}
1825f41ac2beSBill Paul 
1826f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1827f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1828f41ac2beSBill Paul 
1829f41ac2beSBill Paul 	/* Destroy RX return ring */
1830f41ac2beSBill Paul 
1831f41ac2beSBill Paul 	if (sc->bge_ldata.bge_rx_return_ring)
1832f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1833f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1834f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1835f41ac2beSBill Paul 
1836f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_map) {
1837f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1838f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1839f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_return_ring_tag,
1840f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1841f41ac2beSBill Paul 	}
1842f41ac2beSBill Paul 
1843f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1844f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1845f41ac2beSBill Paul 
1846f41ac2beSBill Paul 	/* Destroy TX ring */
1847f41ac2beSBill Paul 
1848f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring)
1849f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1850f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1851f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1852f41ac2beSBill Paul 
1853f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_map) {
1854f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1855f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1856f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_tx_ring_tag,
1857f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1858f41ac2beSBill Paul 	}
1859f41ac2beSBill Paul 
1860f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1861f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1862f41ac2beSBill Paul 
1863f41ac2beSBill Paul 	/* Destroy status block */
1864f41ac2beSBill Paul 
1865f41ac2beSBill Paul 	if (sc->bge_ldata.bge_status_block)
1866f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1867f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1868f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1869f41ac2beSBill Paul 
1870f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_map) {
1871f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1872f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1873f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_status_tag,
1874f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1875f41ac2beSBill Paul 	}
1876f41ac2beSBill Paul 
1877f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1878f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1879f41ac2beSBill Paul 
1880f41ac2beSBill Paul 	/* Destroy statistics block */
1881f41ac2beSBill Paul 
1882f41ac2beSBill Paul 	if (sc->bge_ldata.bge_stats)
1883f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1884f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1885f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1886f41ac2beSBill Paul 
1887f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_map) {
1888f41ac2beSBill Paul 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1889f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1890f41ac2beSBill Paul 		bus_dmamap_destroy(sc->bge_cdata.bge_stats_tag,
1891f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1892f41ac2beSBill Paul 	}
1893f41ac2beSBill Paul 
1894f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1895f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1896f41ac2beSBill Paul 
1897f41ac2beSBill Paul 	/* Destroy the parent tag */
1898f41ac2beSBill Paul 
1899f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1900f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1901f41ac2beSBill Paul 
1902f41ac2beSBill Paul 	return;
1903f41ac2beSBill Paul }
1904f41ac2beSBill Paul 
1905f41ac2beSBill Paul static int
1906f41ac2beSBill Paul bge_dma_alloc(dev)
1907f41ac2beSBill Paul 	device_t dev;
1908f41ac2beSBill Paul {
1909f41ac2beSBill Paul 	struct bge_softc *sc;
1910f41ac2beSBill Paul 	int nseg, i, error;
1911f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
1912f41ac2beSBill Paul 
1913f41ac2beSBill Paul 	sc = device_get_softc(dev);
1914f41ac2beSBill Paul 
1915f41ac2beSBill Paul 	/*
1916f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1917f41ac2beSBill Paul 	 */
1918f41ac2beSBill Paul #define BGE_NSEG_NEW 32
1919f41ac2beSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1920f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1921f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
1922f41ac2beSBill Paul 			BUS_SPACE_MAXADDR_32BIT,/* highaddr */
1923f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1924f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1925f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1926f41ac2beSBill Paul                         BUS_DMA_ALLOCNOW,	/* flags */
1927f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1928f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1929f41ac2beSBill Paul 
1930f41ac2beSBill Paul 	/*
1931f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1932f41ac2beSBill Paul 	 */
1933f41ac2beSBill Paul 	nseg = 32;
1934f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, ETHER_ALIGN,
1935f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1936f41ac2beSBill Paul 	    NULL, MCLBYTES * nseg, nseg, MCLBYTES, 0, NULL, NULL,
1937f41ac2beSBill Paul 	    &sc->bge_cdata.bge_mtag);
1938f41ac2beSBill Paul 
1939f41ac2beSBill Paul 	if (error) {
1940f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1941f41ac2beSBill Paul 		return (ENOMEM);
1942f41ac2beSBill Paul 	}
1943f41ac2beSBill Paul 
1944f41ac2beSBill Paul 	/* Create DMA maps for RX buffers */
1945f41ac2beSBill Paul 
1946f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1947f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1948f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1949f41ac2beSBill Paul 		if (error) {
1950f41ac2beSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1951f41ac2beSBill Paul 			return(ENOMEM);
1952f41ac2beSBill Paul 		}
1953f41ac2beSBill Paul 	}
1954f41ac2beSBill Paul 
1955f41ac2beSBill Paul 	/* Create DMA maps for TX buffers */
1956f41ac2beSBill Paul 
1957f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1958f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1959f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1960f41ac2beSBill Paul 		if (error) {
1961f41ac2beSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1962f41ac2beSBill Paul 			return(ENOMEM);
1963f41ac2beSBill Paul 		}
1964f41ac2beSBill Paul 	}
1965f41ac2beSBill Paul 
1966f41ac2beSBill Paul 	/* Create tag for standard RX ring */
1967f41ac2beSBill Paul 
1968f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1969f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1970f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1971f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1972f41ac2beSBill Paul 
1973f41ac2beSBill Paul 	if (error) {
1974f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1975f41ac2beSBill Paul 		return (ENOMEM);
1976f41ac2beSBill Paul 	}
1977f41ac2beSBill Paul 
1978f41ac2beSBill Paul 	/* Allocate DMA'able memory for standard RX ring */
1979f41ac2beSBill Paul 
1980f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1981f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1982f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1983f41ac2beSBill Paul         if (error)
1984f41ac2beSBill Paul                 return (ENOMEM);
1985f41ac2beSBill Paul 
1986f41ac2beSBill Paul         bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1987f41ac2beSBill Paul 
1988f41ac2beSBill Paul 	/* Load the address of the standard RX ring */
1989f41ac2beSBill Paul 
1990f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1991f41ac2beSBill Paul 	ctx.sc = sc;
1992f41ac2beSBill Paul 
1993f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1994f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1995f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1996f41ac2beSBill Paul 
1997f41ac2beSBill Paul 	if (error)
1998f41ac2beSBill Paul 		return (ENOMEM);
1999f41ac2beSBill Paul 
2000f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
2001f41ac2beSBill Paul 
2002f41ac2beSBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
2003f41ac2beSBill Paul 
2004f41ac2beSBill Paul 		/*
2005f41ac2beSBill Paul 		 * Create tag for jumbo mbufs.
2006f41ac2beSBill Paul 		 * This is really a bit of a kludge. We allocate a special
2007f41ac2beSBill Paul 		 * jumbo buffer pool which (thanks to the way our DMA
2008f41ac2beSBill Paul 		 * memory allocation works) will consist of contiguous
2009f41ac2beSBill Paul 		 * pages. This means that even though a jumbo buffer might
2010f41ac2beSBill Paul 		 * be larger than a page size, we don't really need to
2011f41ac2beSBill Paul 		 * map it into more than one DMA segment. However, the
2012f41ac2beSBill Paul 		 * default mbuf tag will result in multi-segment mappings,
2013f41ac2beSBill Paul 		 * so we have to create a special jumbo mbuf tag that
2014f41ac2beSBill Paul 		 * lets us get away with mapping the jumbo buffers as
2015f41ac2beSBill Paul 		 * a single segment. I think eventually the driver should
2016f41ac2beSBill Paul 		 * be changed so that it uses ordinary mbufs and cluster
2017f41ac2beSBill Paul 		 * buffers, i.e. jumbo frames can span multiple DMA
2018f41ac2beSBill Paul 		 * descriptors. But that's a project for another day.
2019f41ac2beSBill Paul 		 */
2020f41ac2beSBill Paul 
2021f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2022f41ac2beSBill Paul 		    ETHER_ALIGN, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2023f41ac2beSBill Paul 		    NULL, MCLBYTES * nseg, nseg, BGE_JLEN, 0, NULL, NULL,
2024f41ac2beSBill Paul 		    &sc->bge_cdata.bge_mtag_jumbo);
2025f41ac2beSBill Paul 
2026f41ac2beSBill Paul 		if (error) {
2027f41ac2beSBill Paul 			device_printf(dev, "could not allocate dma tag\n");
2028f41ac2beSBill Paul 			return (ENOMEM);
2029f41ac2beSBill Paul 		}
2030f41ac2beSBill Paul 
2031f41ac2beSBill Paul 		/* Create tag for jumbo RX ring */
2032f41ac2beSBill Paul 
2033f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2034f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2035f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
2036f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
2037f41ac2beSBill Paul 
2038f41ac2beSBill Paul 		if (error) {
2039f41ac2beSBill Paul 			device_printf(dev, "could not allocate dma tag\n");
2040f41ac2beSBill Paul 			return (ENOMEM);
2041f41ac2beSBill Paul 		}
2042f41ac2beSBill Paul 
2043f41ac2beSBill Paul 		/* Allocate DMA'able memory for jumbo RX ring */
2044f41ac2beSBill Paul 
2045f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2046f41ac2beSBill Paul 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring, BUS_DMA_NOWAIT,
2047f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
2048f41ac2beSBill Paul 		if (error)
2049f41ac2beSBill Paul 			return (ENOMEM);
2050f41ac2beSBill Paul 
2051f41ac2beSBill Paul 		bzero((char *)sc->bge_ldata.bge_rx_jumbo_ring,
2052f41ac2beSBill Paul 		    BGE_JUMBO_RX_RING_SZ);
2053f41ac2beSBill Paul 
2054f41ac2beSBill Paul 		/* Load the address of the jumbo RX ring */
2055f41ac2beSBill Paul 
2056f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
2057f41ac2beSBill Paul 		ctx.sc = sc;
2058f41ac2beSBill Paul 
2059f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2060f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2061f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
2062f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2063f41ac2beSBill Paul 
2064f41ac2beSBill Paul 		if (error)
2065f41ac2beSBill Paul 			return (ENOMEM);
2066f41ac2beSBill Paul 
2067f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
2068f41ac2beSBill Paul 
2069f41ac2beSBill Paul 		/* Create DMA maps for jumbo RX buffers */
2070f41ac2beSBill Paul 
2071f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2072f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2073f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2074f41ac2beSBill Paul 			if (error) {
2075f41ac2beSBill Paul 				device_printf(dev,
2076f41ac2beSBill Paul 				    "can't create DMA map for RX\n");
2077f41ac2beSBill Paul 				return(ENOMEM);
2078f41ac2beSBill Paul 			}
2079f41ac2beSBill Paul 		}
2080f41ac2beSBill Paul 
2081f41ac2beSBill Paul 	}
2082f41ac2beSBill Paul 
2083f41ac2beSBill Paul 	/* Create tag for RX return ring */
2084f41ac2beSBill Paul 
2085f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2086f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2087f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
2088f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
2089f41ac2beSBill Paul 
2090f41ac2beSBill Paul 	if (error) {
2091f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
2092f41ac2beSBill Paul 		return (ENOMEM);
2093f41ac2beSBill Paul 	}
2094f41ac2beSBill Paul 
2095f41ac2beSBill Paul 	/* Allocate DMA'able memory for RX return ring */
2096f41ac2beSBill Paul 
2097f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
2098f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
2099f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
2100f41ac2beSBill Paul         if (error)
2101f41ac2beSBill Paul                 return (ENOMEM);
2102f41ac2beSBill Paul 
2103f41ac2beSBill Paul         bzero((char *)sc->bge_ldata.bge_rx_return_ring,
2104f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
2105f41ac2beSBill Paul 
2106f41ac2beSBill Paul 	/* Load the address of the RX return ring */
2107f41ac2beSBill Paul 
2108f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2109f41ac2beSBill Paul 	ctx.sc = sc;
2110f41ac2beSBill Paul 
2111f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
2112f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
2113f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
2114f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2115f41ac2beSBill Paul 
2116f41ac2beSBill Paul 	if (error)
2117f41ac2beSBill Paul 		return (ENOMEM);
2118f41ac2beSBill Paul 
2119f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2120f41ac2beSBill Paul 
2121f41ac2beSBill Paul 	/* Create tag for TX ring */
2122f41ac2beSBill Paul 
2123f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2124f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2125f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2126f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
2127f41ac2beSBill Paul 
2128f41ac2beSBill Paul 	if (error) {
2129f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
2130f41ac2beSBill Paul 		return (ENOMEM);
2131f41ac2beSBill Paul 	}
2132f41ac2beSBill Paul 
2133f41ac2beSBill Paul 	/* Allocate DMA'able memory for TX ring */
2134f41ac2beSBill Paul 
2135f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2136f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2137f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
2138f41ac2beSBill Paul         if (error)
2139f41ac2beSBill Paul                 return (ENOMEM);
2140f41ac2beSBill Paul 
2141f41ac2beSBill Paul         bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2142f41ac2beSBill Paul 
2143f41ac2beSBill Paul 	/* Load the address of the TX ring */
2144f41ac2beSBill Paul 
2145f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2146f41ac2beSBill Paul 	ctx.sc = sc;
2147f41ac2beSBill Paul 
2148f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2149f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2150f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2151f41ac2beSBill Paul 
2152f41ac2beSBill Paul 	if (error)
2153f41ac2beSBill Paul 		return (ENOMEM);
2154f41ac2beSBill Paul 
2155f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2156f41ac2beSBill Paul 
2157f41ac2beSBill Paul 	/* Create tag for status block */
2158f41ac2beSBill Paul 
2159f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2160f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2161f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2162f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2163f41ac2beSBill Paul 
2164f41ac2beSBill Paul 	if (error) {
2165f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
2166f41ac2beSBill Paul 		return (ENOMEM);
2167f41ac2beSBill Paul 	}
2168f41ac2beSBill Paul 
2169f41ac2beSBill Paul 	/* Allocate DMA'able memory for status block */
2170f41ac2beSBill Paul 
2171f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2172f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2173f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
2174f41ac2beSBill Paul         if (error)
2175f41ac2beSBill Paul                 return (ENOMEM);
2176f41ac2beSBill Paul 
2177f41ac2beSBill Paul         bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2178f41ac2beSBill Paul 
2179f41ac2beSBill Paul 	/* Load the address of the status block */
2180f41ac2beSBill Paul 
2181f41ac2beSBill Paul 	ctx.sc = sc;
2182f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2183f41ac2beSBill Paul 
2184f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2185f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2186f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2187f41ac2beSBill Paul 
2188f41ac2beSBill Paul 	if (error)
2189f41ac2beSBill Paul 		return (ENOMEM);
2190f41ac2beSBill Paul 
2191f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2192f41ac2beSBill Paul 
2193f41ac2beSBill Paul 	/* Create tag for statistics block */
2194f41ac2beSBill Paul 
2195f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2196f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2197f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2198f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2199f41ac2beSBill Paul 
2200f41ac2beSBill Paul 	if (error) {
2201f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
2202f41ac2beSBill Paul 		return (ENOMEM);
2203f41ac2beSBill Paul 	}
2204f41ac2beSBill Paul 
2205f41ac2beSBill Paul 	/* Allocate DMA'able memory for statistics block */
2206f41ac2beSBill Paul 
2207f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2208f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2209f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2210f41ac2beSBill Paul         if (error)
2211f41ac2beSBill Paul                 return (ENOMEM);
2212f41ac2beSBill Paul 
2213f41ac2beSBill Paul         bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2214f41ac2beSBill Paul 
2215f41ac2beSBill Paul 	/* Load the address of the statstics block */
2216f41ac2beSBill Paul 
2217f41ac2beSBill Paul 	ctx.sc = sc;
2218f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2219f41ac2beSBill Paul 
2220f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2221f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2222f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2223f41ac2beSBill Paul 
2224f41ac2beSBill Paul 	if (error)
2225f41ac2beSBill Paul 		return (ENOMEM);
2226f41ac2beSBill Paul 
2227f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2228f41ac2beSBill Paul 
2229f41ac2beSBill Paul 	return(0);
2230f41ac2beSBill Paul }
2231f41ac2beSBill Paul 
223295d67482SBill Paul static int
223395d67482SBill Paul bge_attach(dev)
223495d67482SBill Paul 	device_t dev;
223595d67482SBill Paul {
223695d67482SBill Paul 	struct ifnet *ifp;
223795d67482SBill Paul 	struct bge_softc *sc;
2238a1d52896SBill Paul 	u_int32_t hwcfg = 0;
2239b1265c1aSJohn Polstra 	u_int32_t mac_addr = 0;
224095d67482SBill Paul 	int unit, error = 0, rid;
224195d67482SBill Paul 
224295d67482SBill Paul 	sc = device_get_softc(dev);
224395d67482SBill Paul 	unit = device_get_unit(dev);
224495d67482SBill Paul 	sc->bge_dev = dev;
224595d67482SBill Paul 	sc->bge_unit = unit;
224695d67482SBill Paul 
224795d67482SBill Paul 	/*
224895d67482SBill Paul 	 * Map control/status registers.
224995d67482SBill Paul 	 */
225095d67482SBill Paul 	pci_enable_busmaster(dev);
225195d67482SBill Paul 
225295d67482SBill Paul 	rid = BGE_PCI_BAR0;
22535f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
22545f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
225595d67482SBill Paul 
225695d67482SBill Paul 	if (sc->bge_res == NULL) {
225795d67482SBill Paul 		printf ("bge%d: couldn't map memory\n", unit);
225895d67482SBill Paul 		error = ENXIO;
225995d67482SBill Paul 		goto fail;
226095d67482SBill Paul 	}
226195d67482SBill Paul 
226295d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
226395d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
226495d67482SBill Paul 	sc->bge_vhandle = (vm_offset_t)rman_get_virtual(sc->bge_res);
226595d67482SBill Paul 
226695d67482SBill Paul 	/* Allocate interrupt */
226795d67482SBill Paul 	rid = 0;
226895d67482SBill Paul 
22695f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
227095d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
227195d67482SBill Paul 
227295d67482SBill Paul 	if (sc->bge_irq == NULL) {
227395d67482SBill Paul 		printf("bge%d: couldn't map interrupt\n", unit);
227495d67482SBill Paul 		error = ENXIO;
227595d67482SBill Paul 		goto fail;
227695d67482SBill Paul 	}
227795d67482SBill Paul 
227895d67482SBill Paul 	sc->bge_unit = unit;
227995d67482SBill Paul 
22800f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
22810f9bd73bSSam Leffler 
228295d67482SBill Paul 	/* Try to reset the chip. */
228395d67482SBill Paul 	bge_reset(sc);
228495d67482SBill Paul 
228595d67482SBill Paul 	if (bge_chipinit(sc)) {
228695d67482SBill Paul 		printf("bge%d: chip initialization failed\n", sc->bge_unit);
228795d67482SBill Paul 		bge_release_resources(sc);
228895d67482SBill Paul 		error = ENXIO;
228995d67482SBill Paul 		goto fail;
229095d67482SBill Paul 	}
229195d67482SBill Paul 
229295d67482SBill Paul 	/*
229395d67482SBill Paul 	 * Get station address from the EEPROM.
229495d67482SBill Paul 	 */
2295b1265c1aSJohn Polstra 	mac_addr = bge_readmem_ind(sc, 0x0c14);
2296b1265c1aSJohn Polstra 	if ((mac_addr >> 16) == 0x484b) {
2297b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[0] = (u_char)(mac_addr >> 8);
2298b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[1] = (u_char)mac_addr;
2299b1265c1aSJohn Polstra 		mac_addr = bge_readmem_ind(sc, 0x0c18);
2300b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[2] = (u_char)(mac_addr >> 24);
2301b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[3] = (u_char)(mac_addr >> 16);
2302b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[4] = (u_char)(mac_addr >> 8);
2303b1265c1aSJohn Polstra 		sc->arpcom.ac_enaddr[5] = (u_char)mac_addr;
2304b1265c1aSJohn Polstra 	} else if (bge_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
230595d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
230695d67482SBill Paul 		printf("bge%d: failed to read station address\n", unit);
230795d67482SBill Paul 		bge_release_resources(sc);
230895d67482SBill Paul 		error = ENXIO;
230995d67482SBill Paul 		goto fail;
231095d67482SBill Paul 	}
231195d67482SBill Paul 
23120434d1b8SBill Paul 	/* Save ASIC rev. */
23130434d1b8SBill Paul 
23140434d1b8SBill Paul 	sc->bge_chipid =
23150434d1b8SBill Paul 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
23160434d1b8SBill Paul 	    BGE_PCIMISCCTL_ASICREV;
23170434d1b8SBill Paul 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
23180434d1b8SBill Paul 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
23190434d1b8SBill Paul 
2320f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
2321f41ac2beSBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705)
2322f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2323f41ac2beSBill Paul 	else
2324f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2325f41ac2beSBill Paul 
2326f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2327f41ac2beSBill Paul 		printf ("bge%d: failed to allocate DMA resources\n",
2328f41ac2beSBill Paul 		    sc->bge_unit);
2329f41ac2beSBill Paul 		bge_release_resources(sc);
2330f41ac2beSBill Paul 		error = ENXIO;
2331f41ac2beSBill Paul 		goto fail;
2332f41ac2beSBill Paul 	}
2333f41ac2beSBill Paul 
23340434d1b8SBill Paul 	/*
23350434d1b8SBill Paul 	 * Try to allocate memory for jumbo buffers.
23360434d1b8SBill Paul 	 * The 5705 does not appear to support jumbo frames.
23370434d1b8SBill Paul 	 */
23380434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
233995d67482SBill Paul 		if (bge_alloc_jumbo_mem(sc)) {
234095d67482SBill Paul 			printf("bge%d: jumbo buffer allocation "
234195d67482SBill Paul 			    "failed\n", sc->bge_unit);
234295d67482SBill Paul 			bge_release_resources(sc);
234395d67482SBill Paul 			error = ENXIO;
234495d67482SBill Paul 			goto fail;
234595d67482SBill Paul 		}
23460434d1b8SBill Paul 	}
234795d67482SBill Paul 
234895d67482SBill Paul 	/* Set default tuneable values. */
234995d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
235095d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
235195d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
235295d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
235395d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
235495d67482SBill Paul 
235595d67482SBill Paul 	/* Set up ifnet structure */
235695d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
235795d67482SBill Paul 	ifp->if_softc = sc;
23589bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
235995d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
236095d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
236195d67482SBill Paul 	ifp->if_output = ether_output;
236295d67482SBill Paul 	ifp->if_start = bge_start;
236395d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
236495d67482SBill Paul 	ifp->if_init = bge_init;
236595d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
236695d67482SBill Paul 	ifp->if_snd.ifq_maxlen = BGE_TX_RING_CNT - 1;
236795d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
23680434d1b8SBill Paul 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
23690434d1b8SBill Paul 	    IFCAP_VLAN_MTU;
237095d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
237195d67482SBill Paul 
2372a1d52896SBill Paul 	/*
2373a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
237441abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
237541abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
237641abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
237741abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
237841abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
237941abcc1bSPaul Saab 	 * SK-9D41.
2380a1d52896SBill Paul 	 */
238141abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
238241abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
238341abcc1bSPaul Saab 	else {
2384a1d52896SBill Paul 		bge_read_eeprom(sc, (caddr_t)&hwcfg,
2385a1d52896SBill Paul 				BGE_EE_HWCFG_OFFSET, sizeof(hwcfg));
238641abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
238741abcc1bSPaul Saab 	}
238841abcc1bSPaul Saab 
238941abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2390a1d52896SBill Paul 		sc->bge_tbi = 1;
2391a1d52896SBill Paul 
239295d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
239395d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
239495d67482SBill Paul 		sc->bge_tbi = 1;
239595d67482SBill Paul 
239695d67482SBill Paul 	if (sc->bge_tbi) {
239795d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
239895d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
239995d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
240095d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
240195d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
240295d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
240395d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
240495d67482SBill Paul 	} else {
240595d67482SBill Paul 		/*
240695d67482SBill Paul 		 * Do transceiver setup.
240795d67482SBill Paul 		 */
240895d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
240995d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
241095d67482SBill Paul 			printf("bge%d: MII without any PHY!\n", sc->bge_unit);
241195d67482SBill Paul 			bge_release_resources(sc);
241295d67482SBill Paul 			bge_free_jumbo_mem(sc);
241395d67482SBill Paul 			error = ENXIO;
241495d67482SBill Paul 			goto fail;
241595d67482SBill Paul 		}
241695d67482SBill Paul 	}
241795d67482SBill Paul 
241895d67482SBill Paul 	/*
2419e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2420e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2421e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2422e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2423e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2424e255b776SJohn Polstra 	 * payloads by copying the received packets.
2425e255b776SJohn Polstra 	 */
2426e0ced696SPaul Saab 	switch (sc->bge_chipid) {
2427e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_A0:
2428e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B0:
2429e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B2:
2430e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B5:
2431e255b776SJohn Polstra 		/* If in PCI-X mode, work around the alignment bug. */
2432e255b776SJohn Polstra 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
2433e255b776SJohn Polstra 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2434e255b776SJohn Polstra 		    BGE_PCISTATE_PCI_BUSSPEED)
2435e255b776SJohn Polstra 			sc->bge_rx_alignment_bug = 1;
2436e255b776SJohn Polstra 		break;
2437e255b776SJohn Polstra 	}
2438e255b776SJohn Polstra 
2439e255b776SJohn Polstra 	/*
244095d67482SBill Paul 	 * Call MI attach routine.
244195d67482SBill Paul 	 */
2442673d9191SSam Leffler 	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
24430f9bd73bSSam Leffler 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
24440f9bd73bSSam Leffler 
24450f9bd73bSSam Leffler 	/*
24460f9bd73bSSam Leffler 	 * Hookup IRQ last.
24470f9bd73bSSam Leffler 	 */
24480f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
24490f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
24500f9bd73bSSam Leffler 
24510f9bd73bSSam Leffler 	if (error) {
24520f9bd73bSSam Leffler 		bge_release_resources(sc);
24530f9bd73bSSam Leffler 		printf("bge%d: couldn't set up irq\n", unit);
24540f9bd73bSSam Leffler 	}
245595d67482SBill Paul 
245695d67482SBill Paul fail:
245795d67482SBill Paul 	return(error);
245895d67482SBill Paul }
245995d67482SBill Paul 
246095d67482SBill Paul static int
246195d67482SBill Paul bge_detach(dev)
246295d67482SBill Paul 	device_t dev;
246395d67482SBill Paul {
246495d67482SBill Paul 	struct bge_softc *sc;
246595d67482SBill Paul 	struct ifnet *ifp;
246695d67482SBill Paul 
246795d67482SBill Paul 	sc = device_get_softc(dev);
246895d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
246995d67482SBill Paul 
24700f9bd73bSSam Leffler 	BGE_LOCK(sc);
247195d67482SBill Paul 	bge_stop(sc);
247295d67482SBill Paul 	bge_reset(sc);
24730f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
24740f9bd73bSSam Leffler 
24750f9bd73bSSam Leffler 	ether_ifdetach(ifp);
247695d67482SBill Paul 
247795d67482SBill Paul 	if (sc->bge_tbi) {
247895d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
247995d67482SBill Paul 	} else {
248095d67482SBill Paul 		bus_generic_detach(dev);
248195d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
248295d67482SBill Paul 	}
248395d67482SBill Paul 
248495d67482SBill Paul 	bge_release_resources(sc);
24850434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
248695d67482SBill Paul 		bge_free_jumbo_mem(sc);
248795d67482SBill Paul 
248895d67482SBill Paul 	return(0);
248995d67482SBill Paul }
249095d67482SBill Paul 
249195d67482SBill Paul static void
249295d67482SBill Paul bge_release_resources(sc)
249395d67482SBill Paul 	struct bge_softc *sc;
249495d67482SBill Paul {
249595d67482SBill Paul         device_t dev;
249695d67482SBill Paul 
249795d67482SBill Paul         dev = sc->bge_dev;
249895d67482SBill Paul 
249995d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
250095d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
250195d67482SBill Paul 
250295d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
250395d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
250495d67482SBill Paul 
250595d67482SBill Paul         if (sc->bge_intrhand != NULL)
250695d67482SBill Paul                 bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
250795d67482SBill Paul 
250895d67482SBill Paul         if (sc->bge_irq != NULL)
250995d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
251095d67482SBill Paul 
251195d67482SBill Paul         if (sc->bge_res != NULL)
251295d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
251395d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
251495d67482SBill Paul 
2515f41ac2beSBill Paul 	bge_dma_free(sc);
251695d67482SBill Paul 
25170f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
25180f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
25190f9bd73bSSam Leffler 
252095d67482SBill Paul         return;
252195d67482SBill Paul }
252295d67482SBill Paul 
252395d67482SBill Paul static void
252495d67482SBill Paul bge_reset(sc)
252595d67482SBill Paul 	struct bge_softc *sc;
252695d67482SBill Paul {
252795d67482SBill Paul 	device_t dev;
252895d67482SBill Paul 	u_int32_t cachesize, command, pcistate;
252995d67482SBill Paul 	int i, val = 0;
253095d67482SBill Paul 
253195d67482SBill Paul 	dev = sc->bge_dev;
253295d67482SBill Paul 
253395d67482SBill Paul 	/* Save some important PCI state. */
253495d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
253595d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
253695d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
253795d67482SBill Paul 
253895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
253995d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
254095d67482SBill Paul 	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
254195d67482SBill Paul 
254295d67482SBill Paul 	/* Issue global reset */
254395d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG,
254495d67482SBill Paul 	    BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1));
254595d67482SBill Paul 
254695d67482SBill Paul 	DELAY(1000);
254795d67482SBill Paul 
254895d67482SBill Paul 	/* Reset some of the PCI state that got zapped by reset */
254995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
255095d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
255195d67482SBill Paul 	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
255295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
255395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
255495d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
255595d67482SBill Paul 
255695d67482SBill Paul 	/*
255795d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
255895d67482SBill Paul 	 * general communications memory at 0xB50.
255995d67482SBill Paul 	 */
256095d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
256195d67482SBill Paul 	/*
256295d67482SBill Paul 	 * Poll the value location we just wrote until
256395d67482SBill Paul 	 * we see the 1's complement of the magic number.
256495d67482SBill Paul 	 * This indicates that the firmware initialization
256595d67482SBill Paul 	 * is complete.
256695d67482SBill Paul 	 */
256795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
256895d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
256995d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
257095d67482SBill Paul 			break;
257195d67482SBill Paul 		DELAY(10);
257295d67482SBill Paul 	}
257395d67482SBill Paul 
257495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
257595d67482SBill Paul 		printf("bge%d: firmware handshake timed out\n", sc->bge_unit);
257695d67482SBill Paul 		return;
257795d67482SBill Paul 	}
257895d67482SBill Paul 
257995d67482SBill Paul 	/*
258095d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
258195d67482SBill Paul 	 * return to its original pre-reset state. This is a
258295d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
258395d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
258495d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
258595d67482SBill Paul 	 * results.
258695d67482SBill Paul 	 */
258795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
258895d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
258995d67482SBill Paul 			break;
259095d67482SBill Paul 		DELAY(10);
259195d67482SBill Paul 	}
259295d67482SBill Paul 
259395d67482SBill Paul 	/* Enable memory arbiter. */
25940434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
259595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
259695d67482SBill Paul 
259795d67482SBill Paul 	/* Fix up byte swapping */
259895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_BYTESWAP_NONFRAME|
259995d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
260095d67482SBill Paul 
260195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
260295d67482SBill Paul 
260395d67482SBill Paul 	DELAY(10000);
260495d67482SBill Paul 
260595d67482SBill Paul 	return;
260695d67482SBill Paul }
260795d67482SBill Paul 
260895d67482SBill Paul /*
260995d67482SBill Paul  * Frame reception handling. This is called if there's a frame
261095d67482SBill Paul  * on the receive return list.
261195d67482SBill Paul  *
261295d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
261395d67482SBill Paul  * 1) the frame is from the jumbo recieve ring
261495d67482SBill Paul  * 2) the frame is from the standard receive ring
261595d67482SBill Paul  */
261695d67482SBill Paul 
261795d67482SBill Paul static void
261895d67482SBill Paul bge_rxeof(sc)
261995d67482SBill Paul 	struct bge_softc *sc;
262095d67482SBill Paul {
262195d67482SBill Paul 	struct ifnet *ifp;
262295d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
262395d67482SBill Paul 
26240f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
26250f9bd73bSSam Leffler 
262695d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
262795d67482SBill Paul 
2628f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2629f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTWRITE);
2630f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2631f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
2632f41ac2beSBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
2633f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2634f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2635f41ac2beSBill Paul 		    BUS_DMASYNC_POSTREAD);
2636f41ac2beSBill Paul 	}
2637f41ac2beSBill Paul 
263895d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2639f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
264095d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
264195d67482SBill Paul 		u_int32_t		rxidx;
264295d67482SBill Paul 		struct ether_header	*eh;
264395d67482SBill Paul 		struct mbuf		*m = NULL;
264495d67482SBill Paul 		u_int16_t		vlan_tag = 0;
264595d67482SBill Paul 		int			have_tag = 0;
264695d67482SBill Paul 
264795d67482SBill Paul 		cur_rx =
2648f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
264995d67482SBill Paul 
265095d67482SBill Paul 		rxidx = cur_rx->bge_idx;
26510434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
265295d67482SBill Paul 
265395d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
265495d67482SBill Paul 			have_tag = 1;
265595d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
265695d67482SBill Paul 		}
265795d67482SBill Paul 
265895d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
265995d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2660f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2661f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2662f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2663f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2664f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
266595d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
266695d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
266795d67482SBill Paul 			jumbocnt++;
266895d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
266995d67482SBill Paul 				ifp->if_ierrors++;
267095d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
267195d67482SBill Paul 				continue;
267295d67482SBill Paul 			}
267395d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
267495d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
267595d67482SBill Paul 				ifp->if_ierrors++;
267695d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
267795d67482SBill Paul 				continue;
267895d67482SBill Paul 			}
267995d67482SBill Paul 		} else {
268095d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2681f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2682f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2683f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2684f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2685f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
268695d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
268795d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
268895d67482SBill Paul 			stdcnt++;
268995d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
269095d67482SBill Paul 				ifp->if_ierrors++;
269195d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
269295d67482SBill Paul 				continue;
269395d67482SBill Paul 			}
269495d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
269595d67482SBill Paul 			    NULL) == ENOBUFS) {
269695d67482SBill Paul 				ifp->if_ierrors++;
269795d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
269895d67482SBill Paul 				continue;
269995d67482SBill Paul 			}
270095d67482SBill Paul 		}
270195d67482SBill Paul 
270295d67482SBill Paul 		ifp->if_ipackets++;
2703e255b776SJohn Polstra #ifndef __i386__
2704e255b776SJohn Polstra 		/*
2705e255b776SJohn Polstra 		 * The i386 allows unaligned accesses, but for other
2706e255b776SJohn Polstra 		 * platforms we must make sure the payload is aligned.
2707e255b776SJohn Polstra 		 */
2708e255b776SJohn Polstra 		if (sc->bge_rx_alignment_bug) {
2709e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2710e255b776SJohn Polstra 			    cur_rx->bge_len);
2711e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2712e255b776SJohn Polstra 		}
2713e255b776SJohn Polstra #endif
271495d67482SBill Paul 		eh = mtod(m, struct ether_header *);
2715473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
271695d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
271795d67482SBill Paul 
2718eb48892eSDavid Greenman #if 0 /* currently broken for some packets, possibly related to TCP options */
271995d67482SBill Paul 		if (ifp->if_hwassist) {
272095d67482SBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
272195d67482SBill Paul 			if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
272295d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
272395d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
272495d67482SBill Paul 				m->m_pkthdr.csum_data =
272595d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
27260189c944SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
272795d67482SBill Paul 			}
272895d67482SBill Paul 		}
2729eb48892eSDavid Greenman #endif
273095d67482SBill Paul 
273195d67482SBill Paul 		/*
2732673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2733673d9191SSam Leffler 		 * attach that information to the packet.
273495d67482SBill Paul 		 */
2735673d9191SSam Leffler 		if (have_tag)
2736673d9191SSam Leffler 			VLAN_INPUT_TAG(ifp, m, vlan_tag, continue);
273795d67482SBill Paul 
27380f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2739673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
27400f9bd73bSSam Leffler 		BGE_LOCK(sc);
274195d67482SBill Paul 	}
274295d67482SBill Paul 
2743f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2744f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREWRITE);
2745f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2746f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
2747f41ac2beSBill Paul 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_PREWRITE);
2748f41ac2beSBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
2749f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2750f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2751f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2752f41ac2beSBill Paul 	}
2753f41ac2beSBill Paul 
275495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
275595d67482SBill Paul 	if (stdcnt)
275695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
275795d67482SBill Paul 	if (jumbocnt)
275895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
275995d67482SBill Paul 
276095d67482SBill Paul 	return;
276195d67482SBill Paul }
276295d67482SBill Paul 
276395d67482SBill Paul static void
276495d67482SBill Paul bge_txeof(sc)
276595d67482SBill Paul 	struct bge_softc *sc;
276695d67482SBill Paul {
276795d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
276895d67482SBill Paul 	struct ifnet *ifp;
276995d67482SBill Paul 
27700f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
27710f9bd73bSSam Leffler 
277295d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
277395d67482SBill Paul 
277495d67482SBill Paul 	/*
277595d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
277695d67482SBill Paul 	 * frames that have been sent.
277795d67482SBill Paul 	 */
277895d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2779f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
278095d67482SBill Paul 		u_int32_t		idx = 0;
278195d67482SBill Paul 
278295d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2783f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
278495d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
278595d67482SBill Paul 			ifp->if_opackets++;
278695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
278795d67482SBill Paul 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
278895d67482SBill Paul 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
2789f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2790f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
279195d67482SBill Paul 		}
279295d67482SBill Paul 		sc->bge_txcnt--;
279395d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
279495d67482SBill Paul 		ifp->if_timer = 0;
279595d67482SBill Paul 	}
279695d67482SBill Paul 
279795d67482SBill Paul 	if (cur_tx != NULL)
279895d67482SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
279995d67482SBill Paul 
280095d67482SBill Paul 	return;
280195d67482SBill Paul }
280295d67482SBill Paul 
280395d67482SBill Paul static void
280495d67482SBill Paul bge_intr(xsc)
280595d67482SBill Paul 	void *xsc;
280695d67482SBill Paul {
280795d67482SBill Paul 	struct bge_softc *sc;
280895d67482SBill Paul 	struct ifnet *ifp;
2809487a8c7eSPaul Saab 	u_int32_t statusword;
281022606b20SBill Paul 	u_int32_t status;
281195d67482SBill Paul 
281295d67482SBill Paul 	sc = xsc;
281395d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
2814f41ac2beSBill Paul 
28150f9bd73bSSam Leffler 	BGE_LOCK(sc);
28160f9bd73bSSam Leffler 
2817f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2818f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTWRITE);
2819f41ac2beSBill Paul 
2820487a8c7eSPaul Saab 	statusword =
2821f41ac2beSBill Paul 	    atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
282295d67482SBill Paul 
282395d67482SBill Paul #ifdef notdef
282495d67482SBill Paul 	/* Avoid this for now -- checking this register is expensive. */
282595d67482SBill Paul 	/* Make sure this is really our interrupt. */
282695d67482SBill Paul 	if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
282795d67482SBill Paul 		return;
282895d67482SBill Paul #endif
282995d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
283095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
283195d67482SBill Paul 
2832a1d52896SBill Paul 	/*
2833a1d52896SBill Paul 	 * Process link state changes.
2834a1d52896SBill Paul 	 * Grrr. The link status word in the status block does
2835a1d52896SBill Paul 	 * not work correctly on the BCM5700 rev AX and BX chips,
28366034c701SChristian Brueffer 	 * according to all available information. Hence, we have
2837a1d52896SBill Paul 	 * to enable MII interrupts in order to properly obtain
2838a1d52896SBill Paul 	 * async link changes. Unfortunately, this also means that
2839a1d52896SBill Paul 	 * we have to read the MAC status register to detect link
2840a1d52896SBill Paul 	 * changes, thereby adding an additional register access to
2841a1d52896SBill Paul 	 * the interrupt handler.
2842a1d52896SBill Paul 	 */
2843a1d52896SBill Paul 
2844e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700) {
2845a1d52896SBill Paul 
2846a1d52896SBill Paul 		status = CSR_READ_4(sc, BGE_MAC_STS);
2847a1d52896SBill Paul 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
284895d67482SBill Paul 			sc->bge_link = 0;
28490f9bd73bSSam Leffler 			callout_stop(&sc->bge_stat_ch);
28500f9bd73bSSam Leffler 			bge_tick_locked(sc);
2851a1d52896SBill Paul 			/* Clear the interrupt */
2852a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2853a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2854a1d52896SBill Paul 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
2855a1d52896SBill Paul 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
2856a1d52896SBill Paul 			    BRGPHY_INTRS);
285798b28ee5SBill Paul 		}
2858a1d52896SBill Paul 	} else {
2859487a8c7eSPaul Saab 		if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED) {
286022606b20SBill Paul 			/*
286122606b20SBill Paul 			 * Sometimes PCS encoding errors are detected in
286222606b20SBill Paul 			 * TBI mode (on fiber NICs), and for some reason
286322606b20SBill Paul 			 * the chip will signal them as link changes.
286422606b20SBill Paul 			 * If we get a link change event, but the 'PCS
286522606b20SBill Paul 			 * encoding error' bit in the MAC status register
286622606b20SBill Paul 			 * is set, don't bother doing a link check.
286722606b20SBill Paul 			 * This avoids spurious "gigabit link up" messages
286822606b20SBill Paul 			 * that sometimes appear on fiber NICs during
286922606b20SBill Paul 			 * periods of heavy traffic. (There should be no
287022606b20SBill Paul 			 * effect on copper NICs.)
287122606b20SBill Paul 			 */
287222606b20SBill Paul 			status = CSR_READ_4(sc, BGE_MAC_STS);
2873ca3f4fd0SBill Paul 			if (!(status & (BGE_MACSTAT_PORT_DECODE_ERROR|
2874ca3f4fd0SBill Paul 			    BGE_MACSTAT_MI_COMPLETE))) {
2875a1d52896SBill Paul 				sc->bge_link = 0;
28760f9bd73bSSam Leffler 				callout_stop(&sc->bge_stat_ch);
28770f9bd73bSSam Leffler 				bge_tick_locked(sc);
287822606b20SBill Paul 			}
2879a1d52896SBill Paul 			/* Clear the interrupt */
288095d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
28810434d1b8SBill Paul 			    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
28820434d1b8SBill Paul 			    BGE_MACSTAT_LINK_CHANGED);
288337ceeb4dSPaul Saab 
288437ceeb4dSPaul Saab 			/* Force flush the status block cached by PCI bridge */
288537ceeb4dSPaul Saab 			CSR_READ_4(sc, BGE_MBX_IRQ0_LO);
2886a1d52896SBill Paul 		}
288795d67482SBill Paul 	}
288895d67482SBill Paul 
288995d67482SBill Paul 	if (ifp->if_flags & IFF_RUNNING) {
289095d67482SBill Paul 		/* Check RX return ring producer/consumer */
289195d67482SBill Paul 		bge_rxeof(sc);
289295d67482SBill Paul 
289395d67482SBill Paul 		/* Check TX ring producer/consumer */
289495d67482SBill Paul 		bge_txeof(sc);
289595d67482SBill Paul 	}
289695d67482SBill Paul 
2897f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2898f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREWRITE);
2899f41ac2beSBill Paul 
290095d67482SBill Paul 	bge_handle_events(sc);
290195d67482SBill Paul 
290295d67482SBill Paul 	/* Re-enable interrupts. */
290395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
290495d67482SBill Paul 
290595d67482SBill Paul 	if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
29060f9bd73bSSam Leffler 		bge_start_locked(ifp);
29070f9bd73bSSam Leffler 
29080f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
290995d67482SBill Paul 
291095d67482SBill Paul 	return;
291195d67482SBill Paul }
291295d67482SBill Paul 
291395d67482SBill Paul static void
29140f9bd73bSSam Leffler bge_tick_locked(sc)
291595d67482SBill Paul 	struct bge_softc *sc;
29160f9bd73bSSam Leffler {
291795d67482SBill Paul 	struct mii_data *mii = NULL;
291895d67482SBill Paul 	struct ifmedia *ifm = NULL;
291995d67482SBill Paul 	struct ifnet *ifp;
292095d67482SBill Paul 
292195d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
292295d67482SBill Paul 
29230f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
292495d67482SBill Paul 
29250434d1b8SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705)
29260434d1b8SBill Paul 		bge_stats_update_regs(sc);
29270434d1b8SBill Paul 	else
292895d67482SBill Paul 		bge_stats_update(sc);
29290f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
29300f9bd73bSSam Leffler 	if (sc->bge_link)
293195d67482SBill Paul 		return;
293295d67482SBill Paul 
293395d67482SBill Paul 	if (sc->bge_tbi) {
293495d67482SBill Paul 		ifm = &sc->bge_ifmedia;
293595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
293695d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED) {
293795d67482SBill Paul 			sc->bge_link++;
293895d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
293995d67482SBill Paul 			printf("bge%d: gigabit link up\n", sc->bge_unit);
294095d67482SBill Paul 			if (ifp->if_snd.ifq_head != NULL)
29410f9bd73bSSam Leffler 				bge_start_locked(ifp);
294295d67482SBill Paul 		}
294395d67482SBill Paul 		return;
294495d67482SBill Paul 	}
294595d67482SBill Paul 
294695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
294795d67482SBill Paul 	mii_tick(mii);
294895d67482SBill Paul 
2949b2561871SJonathan Lemon 	if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE &&
295095d67482SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
295195d67482SBill Paul 		sc->bge_link++;
2952b418ad5cSPoul-Henning Kamp 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
295395d67482SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
295495d67482SBill Paul 			printf("bge%d: gigabit link up\n",
295595d67482SBill Paul 			   sc->bge_unit);
295695d67482SBill Paul 		if (ifp->if_snd.ifq_head != NULL)
29570f9bd73bSSam Leffler 			bge_start_locked(ifp);
295895d67482SBill Paul 	}
295995d67482SBill Paul 
296095d67482SBill Paul 	return;
296195d67482SBill Paul }
296295d67482SBill Paul 
296395d67482SBill Paul static void
29640f9bd73bSSam Leffler bge_tick(xsc)
29650f9bd73bSSam Leffler 	void *xsc;
29660f9bd73bSSam Leffler {
29670f9bd73bSSam Leffler 	struct bge_softc *sc;
29680f9bd73bSSam Leffler 
29690f9bd73bSSam Leffler 	sc = xsc;
29700f9bd73bSSam Leffler 
29710f9bd73bSSam Leffler 	BGE_LOCK(sc);
29720f9bd73bSSam Leffler 	bge_tick_locked(sc);
29730f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
29740f9bd73bSSam Leffler }
29750f9bd73bSSam Leffler 
29760f9bd73bSSam Leffler static void
29770434d1b8SBill Paul bge_stats_update_regs(sc)
29780434d1b8SBill Paul 	struct bge_softc *sc;
29790434d1b8SBill Paul {
29800434d1b8SBill Paul 	struct ifnet *ifp;
29810434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
29820434d1b8SBill Paul 	u_int32_t *s;
29830434d1b8SBill Paul 	int i;
29840434d1b8SBill Paul 
29850434d1b8SBill Paul 	ifp = &sc->arpcom.ac_if;
29860434d1b8SBill Paul 
29870434d1b8SBill Paul 	s = (u_int32_t *)&stats;
29880434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
29890434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
29900434d1b8SBill Paul 		s++;
29910434d1b8SBill Paul 	}
29920434d1b8SBill Paul 
29930434d1b8SBill Paul 	ifp->if_collisions +=
29940434d1b8SBill Paul 	   (stats.dot3StatsSingleCollisionFrames +
29950434d1b8SBill Paul 	   stats.dot3StatsMultipleCollisionFrames +
29960434d1b8SBill Paul 	   stats.dot3StatsExcessiveCollisions +
29970434d1b8SBill Paul 	   stats.dot3StatsLateCollisions) -
29980434d1b8SBill Paul 	   ifp->if_collisions;
29990434d1b8SBill Paul 
30000434d1b8SBill Paul 	return;
30010434d1b8SBill Paul }
30020434d1b8SBill Paul 
30030434d1b8SBill Paul static void
300495d67482SBill Paul bge_stats_update(sc)
300595d67482SBill Paul 	struct bge_softc *sc;
300695d67482SBill Paul {
300795d67482SBill Paul 	struct ifnet *ifp;
300895d67482SBill Paul 	struct bge_stats *stats;
300995d67482SBill Paul 
301095d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
301195d67482SBill Paul 
301295d67482SBill Paul 	stats = (struct bge_stats *)(sc->bge_vhandle +
301395d67482SBill Paul 	    BGE_MEMWIN_START + BGE_STATS_BLOCK);
301495d67482SBill Paul 
301595d67482SBill Paul 	ifp->if_collisions +=
30160434d1b8SBill Paul 	   (stats->txstats.dot3StatsSingleCollisionFrames.bge_addr_lo +
30170434d1b8SBill Paul 	   stats->txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo +
30180434d1b8SBill Paul 	   stats->txstats.dot3StatsExcessiveCollisions.bge_addr_lo +
30190434d1b8SBill Paul 	   stats->txstats.dot3StatsLateCollisions.bge_addr_lo) -
302095d67482SBill Paul 	   ifp->if_collisions;
302195d67482SBill Paul 
302295d67482SBill Paul #ifdef notdef
302395d67482SBill Paul 	ifp->if_collisions +=
302495d67482SBill Paul 	   (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
302595d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
302695d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
302795d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
302895d67482SBill Paul 	   ifp->if_collisions;
302995d67482SBill Paul #endif
303095d67482SBill Paul 
303195d67482SBill Paul 	return;
303295d67482SBill Paul }
303395d67482SBill Paul 
303495d67482SBill Paul /*
303595d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
303695d67482SBill Paul  * pointers to descriptors.
303795d67482SBill Paul  */
303895d67482SBill Paul static int
303995d67482SBill Paul bge_encap(sc, m_head, txidx)
304095d67482SBill Paul 	struct bge_softc *sc;
304195d67482SBill Paul 	struct mbuf *m_head;
304295d67482SBill Paul 	u_int32_t *txidx;
304395d67482SBill Paul {
304495d67482SBill Paul 	struct bge_tx_bd	*f = NULL;
304595d67482SBill Paul 	u_int16_t		csum_flags = 0;
3046673d9191SSam Leffler 	struct m_tag		*mtag;
3047f41ac2beSBill Paul 	struct bge_dmamap_arg	ctx;
3048f41ac2beSBill Paul 	bus_dmamap_t		map;
3049f41ac2beSBill Paul 	int			error;
305095d67482SBill Paul 
305195d67482SBill Paul 
305295d67482SBill Paul 	if (m_head->m_pkthdr.csum_flags) {
305395d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
305495d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
305595d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
305695d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
305795d67482SBill Paul 		if (m_head->m_flags & M_LASTFRAG)
305895d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
305995d67482SBill Paul 		else if (m_head->m_flags & M_FRAG)
306095d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
306195d67482SBill Paul 	}
306295d67482SBill Paul 
3063f41ac2beSBill Paul 	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m_head);
3064673d9191SSam Leffler 
3065f41ac2beSBill Paul 	ctx.sc = sc;
3066f41ac2beSBill Paul 	ctx.bge_idx = *txidx;
3067f41ac2beSBill Paul 	ctx.bge_ring = sc->bge_ldata.bge_tx_ring;
3068f41ac2beSBill Paul 	ctx.bge_flags = csum_flags;
306995d67482SBill Paul 	/*
307095d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
307195d67482SBill Paul 	 * of the end of the ring.
307295d67482SBill Paul 	 */
3073f41ac2beSBill Paul 	ctx.bge_maxsegs = (BGE_TX_RING_CNT - sc->bge_txcnt) - 16;
3074f41ac2beSBill Paul 
3075f41ac2beSBill Paul 	map = sc->bge_cdata.bge_tx_dmamap[*txidx];
3076f41ac2beSBill Paul 	error = bus_dmamap_load_mbuf(sc->bge_cdata.bge_mtag, map,
3077f41ac2beSBill Paul 	    m_head, bge_dma_map_tx_desc, &ctx, BUS_DMA_NOWAIT);
3078f41ac2beSBill Paul 
3079f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0 /*||
3080f41ac2beSBill Paul 	    ctx.bge_idx == sc->bge_tx_saved_considx*/)
308195d67482SBill Paul 		return (ENOBUFS);
3082f41ac2beSBill Paul 
3083f41ac2beSBill Paul 	/*
3084f41ac2beSBill Paul 	 * Insure that the map for this transmission
3085f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3086f41ac2beSBill Paul 	 * in this chain.
3087f41ac2beSBill Paul 	 */
3088f41ac2beSBill Paul 	sc->bge_cdata.bge_tx_dmamap[*txidx] =
3089f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_dmamap[ctx.bge_idx];
3090f41ac2beSBill Paul 	sc->bge_cdata.bge_tx_dmamap[ctx.bge_idx] = map;
3091f41ac2beSBill Paul 	sc->bge_cdata.bge_tx_chain[ctx.bge_idx] = m_head;
3092f41ac2beSBill Paul 	sc->bge_txcnt += ctx.bge_maxsegs;
3093f41ac2beSBill Paul 	f = &sc->bge_ldata.bge_tx_ring[*txidx];
3094f41ac2beSBill Paul 	if (mtag != NULL) {
3095f41ac2beSBill Paul 		f->bge_flags |= htole16(BGE_TXBDFLAG_VLAN_TAG);
3096f41ac2beSBill Paul 		f->bge_vlan_tag = htole16(VLAN_TAG_VALUE(mtag));
3097f41ac2beSBill Paul 	} else {
3098f41ac2beSBill Paul 		f->bge_vlan_tag = 0;
309995d67482SBill Paul 	}
310095d67482SBill Paul 
3101f41ac2beSBill Paul 	BGE_INC(ctx.bge_idx, BGE_TX_RING_CNT);
3102f41ac2beSBill Paul 	*txidx = ctx.bge_idx;
310395d67482SBill Paul 
310495d67482SBill Paul 	return(0);
310595d67482SBill Paul }
310695d67482SBill Paul 
310795d67482SBill Paul /*
310895d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
310995d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
311095d67482SBill Paul  */
311195d67482SBill Paul static void
31120f9bd73bSSam Leffler bge_start_locked(ifp)
311395d67482SBill Paul 	struct ifnet *ifp;
311495d67482SBill Paul {
311595d67482SBill Paul 	struct bge_softc *sc;
311695d67482SBill Paul 	struct mbuf *m_head = NULL;
311795d67482SBill Paul 	u_int32_t prodidx = 0;
311895d67482SBill Paul 
311995d67482SBill Paul 	sc = ifp->if_softc;
312095d67482SBill Paul 
312195d67482SBill Paul 	if (!sc->bge_link && ifp->if_snd.ifq_len < 10)
312295d67482SBill Paul 		return;
312395d67482SBill Paul 
312495d67482SBill Paul 	prodidx = CSR_READ_4(sc, BGE_MBX_TX_HOST_PROD0_LO);
312595d67482SBill Paul 
312695d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
312795d67482SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
312895d67482SBill Paul 		if (m_head == NULL)
312995d67482SBill Paul 			break;
313095d67482SBill Paul 
313195d67482SBill Paul 		/*
313295d67482SBill Paul 		 * XXX
313395d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
313495d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
313595d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
313695d67482SBill Paul 		 * chain at once.
313795d67482SBill Paul 		 * (paranoia -- may not actually be needed)
313895d67482SBill Paul 		 */
313995d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
314095d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
314195d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
314295d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
314395d67482SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
314495d67482SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
314595d67482SBill Paul 				break;
314695d67482SBill Paul 			}
314795d67482SBill Paul 		}
314895d67482SBill Paul 
314995d67482SBill Paul 		/*
315095d67482SBill Paul 		 * Pack the data into the transmit ring. If we
315195d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
315295d67482SBill Paul 		 * for the NIC to drain the ring.
315395d67482SBill Paul 		 */
315495d67482SBill Paul 		if (bge_encap(sc, m_head, &prodidx)) {
315595d67482SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
315695d67482SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
315795d67482SBill Paul 			break;
315895d67482SBill Paul 		}
315995d67482SBill Paul 
316095d67482SBill Paul 		/*
316195d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
316295d67482SBill Paul 		 * to him.
316395d67482SBill Paul 		 */
3164673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
316595d67482SBill Paul 	}
316695d67482SBill Paul 
316795d67482SBill Paul 	/* Transmit */
316895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
31693927098fSPaul Saab 	/* 5700 b2 errata */
3170e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
31713927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
317295d67482SBill Paul 
317395d67482SBill Paul 	/*
317495d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
317595d67482SBill Paul 	 */
317695d67482SBill Paul 	ifp->if_timer = 5;
317795d67482SBill Paul 
317895d67482SBill Paul 	return;
317995d67482SBill Paul }
318095d67482SBill Paul 
31810f9bd73bSSam Leffler /*
31820f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
31830f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
31840f9bd73bSSam Leffler  */
318595d67482SBill Paul static void
31860f9bd73bSSam Leffler bge_start(ifp)
31870f9bd73bSSam Leffler 	struct ifnet *ifp;
318895d67482SBill Paul {
31890f9bd73bSSam Leffler 	struct bge_softc *sc;
31900f9bd73bSSam Leffler 
31910f9bd73bSSam Leffler 	sc = ifp->if_softc;
31920f9bd73bSSam Leffler 	BGE_LOCK(sc);
31930f9bd73bSSam Leffler 	bge_start_locked(ifp);
31940f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
31950f9bd73bSSam Leffler }
31960f9bd73bSSam Leffler 
31970f9bd73bSSam Leffler static void
31980f9bd73bSSam Leffler bge_init_locked(sc)
31990f9bd73bSSam Leffler 	struct bge_softc *sc;
32000f9bd73bSSam Leffler {
320195d67482SBill Paul 	struct ifnet *ifp;
320295d67482SBill Paul 	u_int16_t *m;
320395d67482SBill Paul 
32040f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
320595d67482SBill Paul 
320695d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
320795d67482SBill Paul 
32080f9bd73bSSam Leffler 	if (ifp->if_flags & IFF_RUNNING)
320995d67482SBill Paul 		return;
321095d67482SBill Paul 
321195d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
321295d67482SBill Paul 	bge_stop(sc);
321395d67482SBill Paul 	bge_reset(sc);
321495d67482SBill Paul 	bge_chipinit(sc);
321595d67482SBill Paul 
321695d67482SBill Paul 	/*
321795d67482SBill Paul 	 * Init the various state machines, ring
321895d67482SBill Paul 	 * control blocks and firmware.
321995d67482SBill Paul 	 */
322095d67482SBill Paul 	if (bge_blockinit(sc)) {
322195d67482SBill Paul 		printf("bge%d: initialization failure\n", sc->bge_unit);
322295d67482SBill Paul 		return;
322395d67482SBill Paul 	}
322495d67482SBill Paul 
322595d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
322695d67482SBill Paul 
322795d67482SBill Paul 	/* Specify MTU. */
322895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3229859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
323095d67482SBill Paul 
323195d67482SBill Paul 	/* Load our MAC address. */
323295d67482SBill Paul 	m = (u_int16_t *)&sc->arpcom.ac_enaddr[0];
323395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
323495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
323595d67482SBill Paul 
323695d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
323795d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
323895d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
323995d67482SBill Paul 	} else {
324095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
324195d67482SBill Paul 	}
324295d67482SBill Paul 
324395d67482SBill Paul 	/* Program multicast filter. */
324495d67482SBill Paul 	bge_setmulti(sc);
324595d67482SBill Paul 
324695d67482SBill Paul 	/* Init RX ring. */
324795d67482SBill Paul 	bge_init_rx_ring_std(sc);
324895d67482SBill Paul 
32490434d1b8SBill Paul 	/*
32500434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
32510434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
32520434d1b8SBill Paul 	 * entry of the ring.
32530434d1b8SBill Paul 	 */
32540434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
32550434d1b8SBill Paul 		u_int32_t		v, i;
32560434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
32570434d1b8SBill Paul 			DELAY(20);
32580434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
32590434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
32600434d1b8SBill Paul 				break;
32610434d1b8SBill Paul 		}
32620434d1b8SBill Paul 		if (i == 10)
32630434d1b8SBill Paul 			printf ("bge%d: 5705 A0 chip failed to load RX ring\n",
32640434d1b8SBill Paul 			    sc->bge_unit);
32650434d1b8SBill Paul 	}
32660434d1b8SBill Paul 
326795d67482SBill Paul 	/* Init jumbo RX ring. */
326895d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
326995d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
327095d67482SBill Paul 
327195d67482SBill Paul 	/* Init our RX return ring index */
327295d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
327395d67482SBill Paul 
327495d67482SBill Paul 	/* Init TX ring. */
327595d67482SBill Paul 	bge_init_tx_ring(sc);
327695d67482SBill Paul 
327795d67482SBill Paul 	/* Turn on transmitter */
327895d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
327995d67482SBill Paul 
328095d67482SBill Paul 	/* Turn on receiver */
328195d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
328295d67482SBill Paul 
328395d67482SBill Paul 	/* Tell firmware we're alive. */
328495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
328595d67482SBill Paul 
328695d67482SBill Paul 	/* Enable host interrupts. */
328795d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
328895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
328995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
329095d67482SBill Paul 
329195d67482SBill Paul 	bge_ifmedia_upd(ifp);
329295d67482SBill Paul 
329395d67482SBill Paul 	ifp->if_flags |= IFF_RUNNING;
329495d67482SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
329595d67482SBill Paul 
32960f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
329795d67482SBill Paul 
32980f9bd73bSSam Leffler 	return;
32990f9bd73bSSam Leffler }
33000f9bd73bSSam Leffler 
33010f9bd73bSSam Leffler static void
33020f9bd73bSSam Leffler bge_init(xsc)
33030f9bd73bSSam Leffler 	void *xsc;
33040f9bd73bSSam Leffler {
33050f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
33060f9bd73bSSam Leffler 
33070f9bd73bSSam Leffler 	BGE_LOCK(sc);
33080f9bd73bSSam Leffler 	bge_init_locked(sc);
33090f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
331095d67482SBill Paul 
331195d67482SBill Paul 	return;
331295d67482SBill Paul }
331395d67482SBill Paul 
331495d67482SBill Paul /*
331595d67482SBill Paul  * Set media options.
331695d67482SBill Paul  */
331795d67482SBill Paul static int
331895d67482SBill Paul bge_ifmedia_upd(ifp)
331995d67482SBill Paul 	struct ifnet *ifp;
332095d67482SBill Paul {
332195d67482SBill Paul 	struct bge_softc *sc;
332295d67482SBill Paul 	struct mii_data *mii;
332395d67482SBill Paul 	struct ifmedia *ifm;
332495d67482SBill Paul 
332595d67482SBill Paul 	sc = ifp->if_softc;
332695d67482SBill Paul 	ifm = &sc->bge_ifmedia;
332795d67482SBill Paul 
332895d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
332995d67482SBill Paul 	if (sc->bge_tbi) {
333095d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
333195d67482SBill Paul 			return(EINVAL);
333295d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
333395d67482SBill Paul 		case IFM_AUTO:
333495d67482SBill Paul 			break;
333595d67482SBill Paul 		case IFM_1000_SX:
333695d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
333795d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
333895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
333995d67482SBill Paul 			} else {
334095d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
334195d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
334295d67482SBill Paul 			}
334395d67482SBill Paul 			break;
334495d67482SBill Paul 		default:
334595d67482SBill Paul 			return(EINVAL);
334695d67482SBill Paul 		}
334795d67482SBill Paul 		return(0);
334895d67482SBill Paul 	}
334995d67482SBill Paul 
335095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
335195d67482SBill Paul 	sc->bge_link = 0;
335295d67482SBill Paul 	if (mii->mii_instance) {
335395d67482SBill Paul 		struct mii_softc *miisc;
335495d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
335595d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
335695d67482SBill Paul 			mii_phy_reset(miisc);
335795d67482SBill Paul 	}
335895d67482SBill Paul 	mii_mediachg(mii);
335995d67482SBill Paul 
336095d67482SBill Paul 	return(0);
336195d67482SBill Paul }
336295d67482SBill Paul 
336395d67482SBill Paul /*
336495d67482SBill Paul  * Report current media status.
336595d67482SBill Paul  */
336695d67482SBill Paul static void
336795d67482SBill Paul bge_ifmedia_sts(ifp, ifmr)
336895d67482SBill Paul 	struct ifnet *ifp;
336995d67482SBill Paul 	struct ifmediareq *ifmr;
337095d67482SBill Paul {
337195d67482SBill Paul 	struct bge_softc *sc;
337295d67482SBill Paul 	struct mii_data *mii;
337395d67482SBill Paul 
337495d67482SBill Paul 	sc = ifp->if_softc;
337595d67482SBill Paul 
337695d67482SBill Paul 	if (sc->bge_tbi) {
337795d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
337895d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
337995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
338095d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
338195d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
338295d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
338395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
338495d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
338595d67482SBill Paul 		else
338695d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
338795d67482SBill Paul 		return;
338895d67482SBill Paul 	}
338995d67482SBill Paul 
339095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
339195d67482SBill Paul 	mii_pollstat(mii);
339295d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
339395d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
339495d67482SBill Paul 
339595d67482SBill Paul 	return;
339695d67482SBill Paul }
339795d67482SBill Paul 
339895d67482SBill Paul static int
339995d67482SBill Paul bge_ioctl(ifp, command, data)
340095d67482SBill Paul 	struct ifnet *ifp;
340195d67482SBill Paul 	u_long command;
340295d67482SBill Paul 	caddr_t data;
340395d67482SBill Paul {
340495d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
340595d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
34060f9bd73bSSam Leffler 	int mask, error = 0;
340795d67482SBill Paul 	struct mii_data *mii;
340895d67482SBill Paul 
340995d67482SBill Paul 	switch(command) {
341095d67482SBill Paul 	case SIOCSIFMTU:
34110434d1b8SBill Paul 		/* Disallow jumbo frames on 5705. */
34120434d1b8SBill Paul 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
34130434d1b8SBill Paul 		    ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU)
341495d67482SBill Paul 			error = EINVAL;
341595d67482SBill Paul 		else {
341695d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
341795d67482SBill Paul 			ifp->if_flags &= ~IFF_RUNNING;
341895d67482SBill Paul 			bge_init(sc);
341995d67482SBill Paul 		}
342095d67482SBill Paul 		break;
342195d67482SBill Paul 	case SIOCSIFFLAGS:
34220f9bd73bSSam Leffler 		BGE_LOCK(sc);
342395d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
342495d67482SBill Paul 			/*
342595d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
342695d67482SBill Paul 			 * then just use the 'set promisc mode' command
342795d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
342895d67482SBill Paul 			 * a full re-init means reloading the firmware and
342995d67482SBill Paul 			 * waiting for it to start up, which may take a
343095d67482SBill Paul 			 * second or two.
343195d67482SBill Paul 			 */
343295d67482SBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
343395d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
343495d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
343595d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
343695d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
343795d67482SBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
343895d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
343995d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
344095d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
344195d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
344295d67482SBill Paul 			} else
34430f9bd73bSSam Leffler 				bge_init_locked(sc);
344495d67482SBill Paul 		} else {
344595d67482SBill Paul 			if (ifp->if_flags & IFF_RUNNING) {
344695d67482SBill Paul 				bge_stop(sc);
344795d67482SBill Paul 			}
344895d67482SBill Paul 		}
344995d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
34500f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
345195d67482SBill Paul 		error = 0;
345295d67482SBill Paul 		break;
345395d67482SBill Paul 	case SIOCADDMULTI:
345495d67482SBill Paul 	case SIOCDELMULTI:
345595d67482SBill Paul 		if (ifp->if_flags & IFF_RUNNING) {
34560f9bd73bSSam Leffler 			BGE_LOCK(sc);
345795d67482SBill Paul 			bge_setmulti(sc);
34580f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
345995d67482SBill Paul 			error = 0;
346095d67482SBill Paul 		}
346195d67482SBill Paul 		break;
346295d67482SBill Paul 	case SIOCSIFMEDIA:
346395d67482SBill Paul 	case SIOCGIFMEDIA:
346495d67482SBill Paul 		if (sc->bge_tbi) {
346595d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
346695d67482SBill Paul 			    &sc->bge_ifmedia, command);
346795d67482SBill Paul 		} else {
346895d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
346995d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
347095d67482SBill Paul 			    &mii->mii_media, command);
347195d67482SBill Paul 		}
347295d67482SBill Paul 		break;
347395d67482SBill Paul         case SIOCSIFCAP:
347495d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
347595d67482SBill Paul 		if (mask & IFCAP_HWCSUM) {
347695d67482SBill Paul 			if (IFCAP_HWCSUM & ifp->if_capenable)
347795d67482SBill Paul 				ifp->if_capenable &= ~IFCAP_HWCSUM;
347895d67482SBill Paul 			else
347995d67482SBill Paul 				ifp->if_capenable |= IFCAP_HWCSUM;
348095d67482SBill Paul 		}
348195d67482SBill Paul 		error = 0;
348295d67482SBill Paul 		break;
348395d67482SBill Paul 	default:
3484673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
348595d67482SBill Paul 		break;
348695d67482SBill Paul 	}
348795d67482SBill Paul 
348895d67482SBill Paul 	return(error);
348995d67482SBill Paul }
349095d67482SBill Paul 
349195d67482SBill Paul static void
349295d67482SBill Paul bge_watchdog(ifp)
349395d67482SBill Paul 	struct ifnet *ifp;
349495d67482SBill Paul {
349595d67482SBill Paul 	struct bge_softc *sc;
349695d67482SBill Paul 
349795d67482SBill Paul 	sc = ifp->if_softc;
349895d67482SBill Paul 
349995d67482SBill Paul 	printf("bge%d: watchdog timeout -- resetting\n", sc->bge_unit);
350095d67482SBill Paul 
350195d67482SBill Paul 	ifp->if_flags &= ~IFF_RUNNING;
350295d67482SBill Paul 	bge_init(sc);
350395d67482SBill Paul 
350495d67482SBill Paul 	ifp->if_oerrors++;
350595d67482SBill Paul 
350695d67482SBill Paul 	return;
350795d67482SBill Paul }
350895d67482SBill Paul 
350995d67482SBill Paul /*
351095d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
351195d67482SBill Paul  * RX and TX lists.
351295d67482SBill Paul  */
351395d67482SBill Paul static void
351495d67482SBill Paul bge_stop(sc)
351595d67482SBill Paul 	struct bge_softc *sc;
351695d67482SBill Paul {
351795d67482SBill Paul 	struct ifnet *ifp;
351895d67482SBill Paul 	struct ifmedia_entry *ifm;
351995d67482SBill Paul 	struct mii_data *mii = NULL;
352095d67482SBill Paul 	int mtmp, itmp;
352195d67482SBill Paul 
35220f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
35230f9bd73bSSam Leffler 
352495d67482SBill Paul 	ifp = &sc->arpcom.ac_if;
352595d67482SBill Paul 
352695d67482SBill Paul 	if (!sc->bge_tbi)
352795d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
352895d67482SBill Paul 
35290f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
353095d67482SBill Paul 
353195d67482SBill Paul 	/*
353295d67482SBill Paul 	 * Disable all of the receiver blocks
353395d67482SBill Paul 	 */
353495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
353595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
353695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
35370434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
353895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
353995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
354095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
354195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
354295d67482SBill Paul 
354395d67482SBill Paul 	/*
354495d67482SBill Paul 	 * Disable all of the transmit blocks
354595d67482SBill Paul 	 */
354695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
354795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
354895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
354995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
355095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
35510434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
355295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
355395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
355495d67482SBill Paul 
355595d67482SBill Paul 	/*
355695d67482SBill Paul 	 * Shut down all of the memory managers and related
355795d67482SBill Paul 	 * state machines.
355895d67482SBill Paul 	 */
355995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
356095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
35610434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
356295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
356395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
356495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
35650434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
356695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
356795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
35680434d1b8SBill Paul 	}
356995d67482SBill Paul 
357095d67482SBill Paul 	/* Disable host interrupts. */
357195d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
357295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
357395d67482SBill Paul 
357495d67482SBill Paul 	/*
357595d67482SBill Paul 	 * Tell firmware we're shutting down.
357695d67482SBill Paul 	 */
357795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
357895d67482SBill Paul 
357995d67482SBill Paul 	/* Free the RX lists. */
358095d67482SBill Paul 	bge_free_rx_ring_std(sc);
358195d67482SBill Paul 
358295d67482SBill Paul 	/* Free jumbo RX list. */
35830434d1b8SBill Paul 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
358495d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
358595d67482SBill Paul 
358695d67482SBill Paul 	/* Free TX buffers. */
358795d67482SBill Paul 	bge_free_tx_ring(sc);
358895d67482SBill Paul 
358995d67482SBill Paul 	/*
359095d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
359195d67482SBill Paul 	 * unchanged so that things will be put back to normal when
359295d67482SBill Paul 	 * we bring the interface back up.
359395d67482SBill Paul 	 */
359495d67482SBill Paul 	if (!sc->bge_tbi) {
359595d67482SBill Paul 		itmp = ifp->if_flags;
359695d67482SBill Paul 		ifp->if_flags |= IFF_UP;
359795d67482SBill Paul 		ifm = mii->mii_media.ifm_cur;
359895d67482SBill Paul 		mtmp = ifm->ifm_media;
359995d67482SBill Paul 		ifm->ifm_media = IFM_ETHER|IFM_NONE;
360095d67482SBill Paul 		mii_mediachg(mii);
360195d67482SBill Paul 		ifm->ifm_media = mtmp;
360295d67482SBill Paul 		ifp->if_flags = itmp;
360395d67482SBill Paul 	}
360495d67482SBill Paul 
360595d67482SBill Paul 	sc->bge_link = 0;
360695d67482SBill Paul 
360795d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
360895d67482SBill Paul 
360995d67482SBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
361095d67482SBill Paul 
361195d67482SBill Paul 	return;
361295d67482SBill Paul }
361395d67482SBill Paul 
361495d67482SBill Paul /*
361595d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
361695d67482SBill Paul  * get confused by errant DMAs when rebooting.
361795d67482SBill Paul  */
361895d67482SBill Paul static void
361995d67482SBill Paul bge_shutdown(dev)
362095d67482SBill Paul 	device_t dev;
362195d67482SBill Paul {
362295d67482SBill Paul 	struct bge_softc *sc;
362395d67482SBill Paul 
362495d67482SBill Paul 	sc = device_get_softc(dev);
362595d67482SBill Paul 
36260f9bd73bSSam Leffler 	BGE_LOCK(sc);
362795d67482SBill Paul 	bge_stop(sc);
362895d67482SBill Paul 	bge_reset(sc);
36290f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
363095d67482SBill Paul 
363195d67482SBill Paul 	return;
363295d67482SBill Paul }
3633