xref: /freebsd/sys/dev/bge/if_bge.c (revision f30cbfc63b991172f68e87449eb8116bc8c3f63f)
1098ca2bdSWarner Losh /*-
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 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
8295d67482SBill Paul 
8395d67482SBill Paul #include <net/if.h>
8495d67482SBill Paul #include <net/if_arp.h>
8595d67482SBill Paul #include <net/ethernet.h>
8695d67482SBill Paul #include <net/if_dl.h>
8795d67482SBill Paul #include <net/if_media.h>
8895d67482SBill Paul 
8995d67482SBill Paul #include <net/bpf.h>
9095d67482SBill Paul 
9195d67482SBill Paul #include <net/if_types.h>
9295d67482SBill Paul #include <net/if_vlan_var.h>
9395d67482SBill Paul 
9495d67482SBill Paul #include <netinet/in_systm.h>
9595d67482SBill Paul #include <netinet/in.h>
9695d67482SBill Paul #include <netinet/ip.h>
9795d67482SBill Paul 
9895d67482SBill Paul #include <machine/clock.h>      /* for DELAY */
9995d67482SBill Paul #include <machine/bus.h>
10095d67482SBill Paul #include <machine/resource.h>
10195d67482SBill Paul #include <sys/bus.h>
10295d67482SBill Paul #include <sys/rman.h>
10395d67482SBill Paul 
10495d67482SBill Paul #include <dev/mii/mii.h>
10595d67482SBill Paul #include <dev/mii/miivar.h>
1062d3ce713SDavid E. O'Brien #include "miidevs.h"
10795d67482SBill Paul #include <dev/mii/brgphyreg.h>
10895d67482SBill Paul 
1094fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1104fbd232cSWarner Losh #include <dev/pci/pcivar.h>
11195d67482SBill Paul 
11295d67482SBill Paul #include <dev/bge/if_bgereg.h>
11395d67482SBill Paul 
114ff50922bSDoug White #include "opt_bge.h"
115ff50922bSDoug White 
1165ddc5794SJohn Polstra #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
117d375e524SGleb Smirnoff #define ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
11895d67482SBill Paul 
119f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
120f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12195d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12295d67482SBill Paul 
1237b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12495d67482SBill Paul #include "miibus_if.h"
12595d67482SBill Paul 
12695d67482SBill Paul /*
12795d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
12895d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
12995d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13095d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13195d67482SBill Paul  */
132029e2ee3SJohn Polstra #define BGE_DEVDESC_MAX		64	/* Maximum device description length */
13395d67482SBill Paul 
13495d67482SBill Paul static struct bge_type bge_devs[] = {
13595d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5700,
13695d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
13795d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5701,
13895d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
13995d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5700,
14095d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
14195d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5701,
14295d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
1430434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702,
1440434d1b8SBill Paul 		"Broadcom BCM5702 Gigabit Ethernet" },
14501598b8dSMitsuru IWASAKI 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702X,
14601598b8dSMitsuru IWASAKI 		"Broadcom BCM5702X Gigabit Ethernet" },
1470434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703,
1480434d1b8SBill Paul 		"Broadcom BCM5703 Gigabit Ethernet" },
149b1265c1aSJohn Polstra 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703X,
150b1265c1aSJohn Polstra 		"Broadcom BCM5703X Gigabit Ethernet" },
1516ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704C,
1526ac6d2c8SPaul Saab 		"Broadcom BCM5704C Dual Gigabit Ethernet" },
1536ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704S,
1546ac6d2c8SPaul Saab 		"Broadcom BCM5704S Dual Gigabit Ethernet" },
1550434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705,
1560434d1b8SBill Paul 		"Broadcom BCM5705 Gigabit Ethernet" },
157c001ccf2SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705K,
158c001ccf2SPaul Saab 		"Broadcom BCM5705K Gigabit Ethernet" },
1590434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M,
1600434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
1610434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M_ALT,
1620434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
163419c028bSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5714C,
164419c028bSPaul Saab 		"Broadcom BCM5714C Gigabit Ethernet" },
16535ca8069SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5721,
16635ca8069SPaul Saab 		"Broadcom BCM5721 Gigabit Ethernet" },
167e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5750,
168e53d81eeSPaul Saab 		"Broadcom BCM5750 Gigabit Ethernet" },
169e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5750M,
170e53d81eeSPaul Saab 		"Broadcom BCM5750M Gigabit Ethernet" },
171e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5751,
172e53d81eeSPaul Saab 		"Broadcom BCM5751 Gigabit Ethernet" },
173d2014b30STai-hwa Liang 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5751M,
174d2014b30STai-hwa Liang 		"Broadcom BCM5751M Gigabit Ethernet" },
175560c1670SGleb Smirnoff 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5752,
176560c1670SGleb Smirnoff 		"Broadcom BCM5752 Gigabit Ethernet" },
1770434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5782,
1780434d1b8SBill Paul 		"Broadcom BCM5782 Gigabit Ethernet" },
1799f71a4c2SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5788,
1809f71a4c2SBill Paul 		"Broadcom BCM5788 Gigabit Ethernet" },
181c3615d48SMike Silbersack 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5789,
182c3615d48SMike Silbersack 		"Broadcom BCM5789 Gigabit Ethernet" },
1835d99c641SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901,
1845d99c641SBill Paul 		"Broadcom BCM5901 Fast Ethernet" },
1855d99c641SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901A2,
1865d99c641SBill Paul 		"Broadcom BCM5901A2 Fast Ethernet" },
18795d67482SBill Paul 	{ SK_VENDORID, SK_DEVICEID_ALTIMA,
18895d67482SBill Paul 		"SysKonnect Gigabit Ethernet" },
189586d7c2eSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000,
190586d7c2eSJohn Polstra 		"Altima AC1000 Gigabit Ethernet" },
1912aae6624SBill Paul 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1002,
1922aae6624SBill Paul 		"Altima AC1002 Gigabit Ethernet" },
193470bd96aSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100,
194470bd96aSJohn Polstra 		"Altima AC9100 Gigabit Ethernet" },
19595d67482SBill Paul 	{ 0, 0, NULL }
19695d67482SBill Paul };
19795d67482SBill Paul 
198e51a25f8SAlfred Perlstein static int bge_probe		(device_t);
199e51a25f8SAlfred Perlstein static int bge_attach		(device_t);
200e51a25f8SAlfred Perlstein static int bge_detach		(device_t);
20114afefa3SPawel Jakub Dawidek static int bge_suspend		(device_t);
20214afefa3SPawel Jakub Dawidek static int bge_resume		(device_t);
20395d67482SBill Paul static void bge_release_resources
204e51a25f8SAlfred Perlstein 				(struct bge_softc *);
205f41ac2beSBill Paul static void bge_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
206f41ac2beSBill Paul static int bge_dma_alloc	(device_t);
207f41ac2beSBill Paul static void bge_dma_free	(struct bge_softc *);
208f41ac2beSBill Paul 
209e51a25f8SAlfred Perlstein static void bge_txeof		(struct bge_softc *);
210e51a25f8SAlfred Perlstein static void bge_rxeof		(struct bge_softc *);
21195d67482SBill Paul 
2120f9bd73bSSam Leffler static void bge_tick_locked	(struct bge_softc *);
213e51a25f8SAlfred Perlstein static void bge_tick		(void *);
214e51a25f8SAlfred Perlstein static void bge_stats_update	(struct bge_softc *);
2150434d1b8SBill Paul static void bge_stats_update_regs
2160434d1b8SBill Paul 				(struct bge_softc *);
217e51a25f8SAlfred Perlstein static int bge_encap		(struct bge_softc *, struct mbuf *,
218e51a25f8SAlfred Perlstein 					u_int32_t *);
21995d67482SBill Paul 
220e51a25f8SAlfred Perlstein static void bge_intr		(void *);
2210f9bd73bSSam Leffler static void bge_start_locked	(struct ifnet *);
222e51a25f8SAlfred Perlstein static void bge_start		(struct ifnet *);
223e51a25f8SAlfred Perlstein static int bge_ioctl		(struct ifnet *, u_long, caddr_t);
2240f9bd73bSSam Leffler static void bge_init_locked	(struct bge_softc *);
225e51a25f8SAlfred Perlstein static void bge_init		(void *);
226e51a25f8SAlfred Perlstein static void bge_stop		(struct bge_softc *);
227e51a25f8SAlfred Perlstein static void bge_watchdog		(struct ifnet *);
228e51a25f8SAlfred Perlstein static void bge_shutdown		(device_t);
229e51a25f8SAlfred Perlstein static int bge_ifmedia_upd	(struct ifnet *);
230e51a25f8SAlfred Perlstein static void bge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
23195d67482SBill Paul 
232e51a25f8SAlfred Perlstein static u_int8_t	bge_eeprom_getbyte	(struct bge_softc *, int, u_int8_t *);
233e51a25f8SAlfred Perlstein static int bge_read_eeprom	(struct bge_softc *, caddr_t, int, int);
23495d67482SBill Paul 
235e51a25f8SAlfred Perlstein static void bge_setmulti	(struct bge_softc *);
23695d67482SBill Paul 
237e51a25f8SAlfred Perlstein static int bge_newbuf_std	(struct bge_softc *, int, struct mbuf *);
238e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo	(struct bge_softc *, int, struct mbuf *);
239e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std	(struct bge_softc *);
240e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std	(struct bge_softc *);
241e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo	(struct bge_softc *);
242e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo	(struct bge_softc *);
243e51a25f8SAlfred Perlstein static void bge_free_tx_ring	(struct bge_softc *);
244e51a25f8SAlfred Perlstein static int bge_init_tx_ring	(struct bge_softc *);
24595d67482SBill Paul 
246e51a25f8SAlfred Perlstein static int bge_chipinit		(struct bge_softc *);
247e51a25f8SAlfred Perlstein static int bge_blockinit	(struct bge_softc *);
24895d67482SBill Paul 
2491b4a3b2fSPeter Wemm #ifdef notdef
250e51a25f8SAlfred Perlstein static u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
251e51a25f8SAlfred Perlstein static void bge_vpd_read_res	(struct bge_softc *, struct vpd_res *, int);
252e51a25f8SAlfred Perlstein static void bge_vpd_read	(struct bge_softc *);
2531b4a3b2fSPeter Wemm #endif
25495d67482SBill Paul 
25595d67482SBill Paul static u_int32_t bge_readmem_ind
256e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
257e51a25f8SAlfred Perlstein static void bge_writemem_ind	(struct bge_softc *, int, int);
25895d67482SBill Paul #ifdef notdef
25995d67482SBill Paul static u_int32_t bge_readreg_ind
260e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
26195d67482SBill Paul #endif
262e51a25f8SAlfred Perlstein static void bge_writereg_ind	(struct bge_softc *, int, int);
26395d67482SBill Paul 
264e51a25f8SAlfred Perlstein static int bge_miibus_readreg	(device_t, int, int);
265e51a25f8SAlfred Perlstein static int bge_miibus_writereg	(device_t, int, int, int);
266e51a25f8SAlfred Perlstein static void bge_miibus_statchg	(device_t);
26775719184SGleb Smirnoff #ifdef DEVICE_POLLING
26875719184SGleb Smirnoff static void bge_poll		(struct ifnet *ifp, enum poll_cmd cmd,
26975719184SGleb Smirnoff 				    int count);
27075719184SGleb Smirnoff static void bge_poll_locked	(struct ifnet *ifp, enum poll_cmd cmd,
27175719184SGleb Smirnoff 				    int count);
27275719184SGleb Smirnoff #endif
27395d67482SBill Paul 
274e51a25f8SAlfred Perlstein static void bge_reset		(struct bge_softc *);
275dab5cd05SOleg Bulyzhin static void bge_link_upd	(struct bge_softc *);
27695d67482SBill Paul 
27795d67482SBill Paul static device_method_t bge_methods[] = {
27895d67482SBill Paul 	/* Device interface */
27995d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
28095d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
28195d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
28295d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
28314afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
28414afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
28595d67482SBill Paul 
28695d67482SBill Paul 	/* bus interface */
28795d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
28895d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
28995d67482SBill Paul 
29095d67482SBill Paul 	/* MII interface */
29195d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
29295d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
29395d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
29495d67482SBill Paul 
29595d67482SBill Paul 	{ 0, 0 }
29695d67482SBill Paul };
29795d67482SBill Paul 
29895d67482SBill Paul static driver_t bge_driver = {
29995d67482SBill Paul 	"bge",
30095d67482SBill Paul 	bge_methods,
30195d67482SBill Paul 	sizeof(struct bge_softc)
30295d67482SBill Paul };
30395d67482SBill Paul 
30495d67482SBill Paul static devclass_t bge_devclass;
30595d67482SBill Paul 
306f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
30795d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
30895d67482SBill Paul 
30995d67482SBill Paul static u_int32_t
31095d67482SBill Paul bge_readmem_ind(sc, off)
31195d67482SBill Paul 	struct bge_softc *sc;
31295d67482SBill Paul 	int off;
31395d67482SBill Paul {
31495d67482SBill Paul 	device_t dev;
31595d67482SBill Paul 
31695d67482SBill Paul 	dev = sc->bge_dev;
31795d67482SBill Paul 
31895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
31995d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
32095d67482SBill Paul }
32195d67482SBill Paul 
32295d67482SBill Paul static void
32395d67482SBill Paul bge_writemem_ind(sc, off, val)
32495d67482SBill Paul 	struct bge_softc *sc;
32595d67482SBill Paul 	int off, val;
32695d67482SBill Paul {
32795d67482SBill Paul 	device_t dev;
32895d67482SBill Paul 
32995d67482SBill Paul 	dev = sc->bge_dev;
33095d67482SBill Paul 
33195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
33295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
33395d67482SBill Paul 
33495d67482SBill Paul 	return;
33595d67482SBill Paul }
33695d67482SBill Paul 
33795d67482SBill Paul #ifdef notdef
33895d67482SBill Paul static u_int32_t
33995d67482SBill Paul bge_readreg_ind(sc, off)
34095d67482SBill Paul 	struct bge_softc *sc;
34195d67482SBill Paul 	int off;
34295d67482SBill Paul {
34395d67482SBill Paul 	device_t dev;
34495d67482SBill Paul 
34595d67482SBill Paul 	dev = sc->bge_dev;
34695d67482SBill Paul 
34795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
34895d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
34995d67482SBill Paul }
35095d67482SBill Paul #endif
35195d67482SBill Paul 
35295d67482SBill Paul static void
35395d67482SBill Paul bge_writereg_ind(sc, off, val)
35495d67482SBill Paul 	struct bge_softc *sc;
35595d67482SBill Paul 	int off, val;
35695d67482SBill Paul {
35795d67482SBill Paul 	device_t dev;
35895d67482SBill Paul 
35995d67482SBill Paul 	dev = sc->bge_dev;
36095d67482SBill Paul 
36195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
36295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
36395d67482SBill Paul 
36495d67482SBill Paul 	return;
36595d67482SBill Paul }
36695d67482SBill Paul 
367f41ac2beSBill Paul /*
368f41ac2beSBill Paul  * Map a single buffer address.
369f41ac2beSBill Paul  */
370f41ac2beSBill Paul 
371f41ac2beSBill Paul static void
372f41ac2beSBill Paul bge_dma_map_addr(arg, segs, nseg, error)
373f41ac2beSBill Paul 	void *arg;
374f41ac2beSBill Paul 	bus_dma_segment_t *segs;
375f41ac2beSBill Paul 	int nseg;
376f41ac2beSBill Paul 	int error;
377f41ac2beSBill Paul {
378f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
379f41ac2beSBill Paul 
380f41ac2beSBill Paul 	if (error)
381f41ac2beSBill Paul 		return;
382f41ac2beSBill Paul 
383f41ac2beSBill Paul 	ctx = arg;
384f41ac2beSBill Paul 
385f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
386f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
387f41ac2beSBill Paul 		return;
388f41ac2beSBill Paul 	}
389f41ac2beSBill Paul 
390f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
391f41ac2beSBill Paul 
392f41ac2beSBill Paul 	return;
393f41ac2beSBill Paul }
394f41ac2beSBill Paul 
3951b4a3b2fSPeter Wemm #ifdef notdef
39695d67482SBill Paul static u_int8_t
39795d67482SBill Paul bge_vpd_readbyte(sc, addr)
39895d67482SBill Paul 	struct bge_softc *sc;
39995d67482SBill Paul 	int addr;
40095d67482SBill Paul {
40195d67482SBill Paul 	int i;
40295d67482SBill Paul 	device_t dev;
40395d67482SBill Paul 	u_int32_t val;
40495d67482SBill Paul 
40595d67482SBill Paul 	dev = sc->bge_dev;
40695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
40795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
40895d67482SBill Paul 		DELAY(10);
40995d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
41095d67482SBill Paul 			break;
41195d67482SBill Paul 	}
41295d67482SBill Paul 
41395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
414fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "VPD read timed out\n");
41595d67482SBill Paul 		return(0);
41695d67482SBill Paul 	}
41795d67482SBill Paul 
41895d67482SBill Paul 	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
41995d67482SBill Paul 
42095d67482SBill Paul 	return((val >> ((addr % 4) * 8)) & 0xFF);
42195d67482SBill Paul }
42295d67482SBill Paul 
42395d67482SBill Paul static void
42495d67482SBill Paul bge_vpd_read_res(sc, res, addr)
42595d67482SBill Paul 	struct bge_softc *sc;
42695d67482SBill Paul 	struct vpd_res *res;
42795d67482SBill Paul 	int addr;
42895d67482SBill Paul {
42995d67482SBill Paul 	int i;
43095d67482SBill Paul 	u_int8_t *ptr;
43195d67482SBill Paul 
43295d67482SBill Paul 	ptr = (u_int8_t *)res;
43395d67482SBill Paul 	for (i = 0; i < sizeof(struct vpd_res); i++)
43495d67482SBill Paul 		ptr[i] = bge_vpd_readbyte(sc, i + addr);
43595d67482SBill Paul 
43695d67482SBill Paul 	return;
43795d67482SBill Paul }
43895d67482SBill Paul 
43995d67482SBill Paul static void
44095d67482SBill Paul bge_vpd_read(sc)
44195d67482SBill Paul 	struct bge_softc *sc;
44295d67482SBill Paul {
44395d67482SBill Paul 	int pos = 0, i;
44495d67482SBill Paul 	struct vpd_res res;
44595d67482SBill Paul 
44695d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
44795d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
44895d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
44995d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
45095d67482SBill Paul 	sc->bge_vpd_prodname = NULL;
45195d67482SBill Paul 	sc->bge_vpd_readonly = NULL;
45295d67482SBill Paul 
45395d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
45495d67482SBill Paul 
45595d67482SBill Paul 	if (res.vr_id != VPD_RES_ID) {
456fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
457fe806fdaSPyun YongHyeon 		    "bad VPD resource id: expected %x got %x\n", VPD_RES_ID,
458fe806fdaSPyun YongHyeon 		    res.vr_id);
45995d67482SBill Paul 		return;
46095d67482SBill Paul 	}
46195d67482SBill Paul 
46295d67482SBill Paul 	pos += sizeof(res);
46395d67482SBill Paul 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
46495d67482SBill Paul 	for (i = 0; i < res.vr_len; i++)
46595d67482SBill Paul 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
46695d67482SBill Paul 	sc->bge_vpd_prodname[i] = '\0';
46795d67482SBill Paul 	pos += i;
46895d67482SBill Paul 
46995d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
47095d67482SBill Paul 
47195d67482SBill Paul 	if (res.vr_id != VPD_RES_READ) {
472fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
473fe806fdaSPyun YongHyeon 		    "bad VPD resource id: expected %x got %x\n", VPD_RES_READ,
474fe806fdaSPyun YongHyeon 		    res.vr_id);
47595d67482SBill Paul 		return;
47695d67482SBill Paul 	}
47795d67482SBill Paul 
47895d67482SBill Paul 	pos += sizeof(res);
47995d67482SBill Paul 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
48095d67482SBill Paul 	for (i = 0; i < res.vr_len + 1; i++)
48195d67482SBill Paul 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
48295d67482SBill Paul 
48395d67482SBill Paul 	return;
48495d67482SBill Paul }
4851b4a3b2fSPeter Wemm #endif
48695d67482SBill Paul 
48795d67482SBill Paul /*
48895d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
48995d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
49095d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
49195d67482SBill Paul  * access method.
49295d67482SBill Paul  */
49395d67482SBill Paul static u_int8_t
49495d67482SBill Paul bge_eeprom_getbyte(sc, addr, dest)
49595d67482SBill Paul 	struct bge_softc *sc;
49695d67482SBill Paul 	int addr;
49795d67482SBill Paul 	u_int8_t *dest;
49895d67482SBill Paul {
49995d67482SBill Paul 	int i;
50095d67482SBill Paul 	u_int32_t byte = 0;
50195d67482SBill Paul 
50295d67482SBill Paul 	/*
50395d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
50495d67482SBill Paul 	 * having to use the bitbang method.
50595d67482SBill Paul 	 */
50695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
50795d67482SBill Paul 
50895d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
50995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
51095d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
51195d67482SBill Paul 	DELAY(20);
51295d67482SBill Paul 
51395d67482SBill Paul 	/* Issue the read EEPROM command. */
51495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
51595d67482SBill Paul 
51695d67482SBill Paul 	/* Wait for completion */
51795d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
51895d67482SBill Paul 		DELAY(10);
51995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
52095d67482SBill Paul 			break;
52195d67482SBill Paul 	}
52295d67482SBill Paul 
52395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
524fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
525f6789fbaSPyun YongHyeon 		return(1);
52695d67482SBill Paul 	}
52795d67482SBill Paul 
52895d67482SBill Paul 	/* Get result. */
52995d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
53095d67482SBill Paul 
53195d67482SBill Paul 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
53295d67482SBill Paul 
53395d67482SBill Paul 	return(0);
53495d67482SBill Paul }
53595d67482SBill Paul 
53695d67482SBill Paul /*
53795d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
53895d67482SBill Paul  */
53995d67482SBill Paul static int
54095d67482SBill Paul bge_read_eeprom(sc, dest, off, cnt)
54195d67482SBill Paul 	struct bge_softc *sc;
54295d67482SBill Paul 	caddr_t dest;
54395d67482SBill Paul 	int off;
54495d67482SBill Paul 	int cnt;
54595d67482SBill Paul {
54695d67482SBill Paul 	int err = 0, i;
54795d67482SBill Paul 	u_int8_t byte = 0;
54895d67482SBill Paul 
54995d67482SBill Paul 	for (i = 0; i < cnt; i++) {
55095d67482SBill Paul 		err = bge_eeprom_getbyte(sc, off + i, &byte);
55195d67482SBill Paul 		if (err)
55295d67482SBill Paul 			break;
55395d67482SBill Paul 		*(dest + i) = byte;
55495d67482SBill Paul 	}
55595d67482SBill Paul 
55695d67482SBill Paul 	return(err ? 1 : 0);
55795d67482SBill Paul }
55895d67482SBill Paul 
55995d67482SBill Paul static int
56095d67482SBill Paul bge_miibus_readreg(dev, phy, reg)
56195d67482SBill Paul 	device_t dev;
56295d67482SBill Paul 	int phy, reg;
56395d67482SBill Paul {
56495d67482SBill Paul 	struct bge_softc *sc;
56537ceeb4dSPaul Saab 	u_int32_t val, autopoll;
56695d67482SBill Paul 	int i;
56795d67482SBill Paul 
56895d67482SBill Paul 	sc = device_get_softc(dev);
56995d67482SBill Paul 
5700434d1b8SBill Paul 	/*
5710434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5720434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5730434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5740434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5750434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5760434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5770434d1b8SBill Paul 	 * special-cased.
5780434d1b8SBill Paul 	 */
579b1265c1aSJohn Polstra 	if (phy != 1)
58098b28ee5SBill Paul 		return(0);
58198b28ee5SBill Paul 
58237ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
58337ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
58437ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
58537ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
58637ceeb4dSPaul Saab 		DELAY(40);
58737ceeb4dSPaul Saab 	}
58837ceeb4dSPaul Saab 
58995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
59095d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
59195d67482SBill Paul 
59295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
59395d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
59495d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
59595d67482SBill Paul 			break;
59695d67482SBill Paul 	}
59795d67482SBill Paul 
59895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
599fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
60037ceeb4dSPaul Saab 		val = 0;
60137ceeb4dSPaul Saab 		goto done;
60295d67482SBill Paul 	}
60395d67482SBill Paul 
60495d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
60595d67482SBill Paul 
60637ceeb4dSPaul Saab done:
60737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
60837ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
60937ceeb4dSPaul Saab 		DELAY(40);
61037ceeb4dSPaul Saab 	}
61137ceeb4dSPaul Saab 
61295d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
61395d67482SBill Paul 		return(0);
61495d67482SBill Paul 
61595d67482SBill Paul 	return(val & 0xFFFF);
61695d67482SBill Paul }
61795d67482SBill Paul 
61895d67482SBill Paul static int
61995d67482SBill Paul bge_miibus_writereg(dev, phy, reg, val)
62095d67482SBill Paul 	device_t dev;
62195d67482SBill Paul 	int phy, reg, val;
62295d67482SBill Paul {
62395d67482SBill Paul 	struct bge_softc *sc;
62437ceeb4dSPaul Saab 	u_int32_t autopoll;
62595d67482SBill Paul 	int i;
62695d67482SBill Paul 
62795d67482SBill Paul 	sc = device_get_softc(dev);
62895d67482SBill Paul 
62937ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
63037ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
63137ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
63237ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
63337ceeb4dSPaul Saab 		DELAY(40);
63437ceeb4dSPaul Saab 	}
63537ceeb4dSPaul Saab 
63695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
63795d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
63895d67482SBill Paul 
63995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
64095d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
64195d67482SBill Paul 			break;
64295d67482SBill Paul 	}
64395d67482SBill Paul 
64437ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
64537ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
64637ceeb4dSPaul Saab 		DELAY(40);
64737ceeb4dSPaul Saab 	}
64837ceeb4dSPaul Saab 
64995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
650fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
65195d67482SBill Paul 		return(0);
65295d67482SBill Paul 	}
65395d67482SBill Paul 
65495d67482SBill Paul 	return(0);
65595d67482SBill Paul }
65695d67482SBill Paul 
65795d67482SBill Paul static void
65895d67482SBill Paul bge_miibus_statchg(dev)
65995d67482SBill Paul 	device_t dev;
66095d67482SBill Paul {
66195d67482SBill Paul 	struct bge_softc *sc;
66295d67482SBill Paul 	struct mii_data *mii;
66395d67482SBill Paul 
66495d67482SBill Paul 	sc = device_get_softc(dev);
66595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
66695d67482SBill Paul 
66795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
668b418ad5cSPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
66995d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
67095d67482SBill Paul 	} else {
67195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
67295d67482SBill Paul 	}
67395d67482SBill Paul 
67495d67482SBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
67595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
67695d67482SBill Paul 	} else {
67795d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
67895d67482SBill Paul 	}
67995d67482SBill Paul 
68095d67482SBill Paul 	return;
68195d67482SBill Paul }
68295d67482SBill Paul 
68395d67482SBill Paul /*
68495d67482SBill Paul  * Intialize a standard receive ring descriptor.
68595d67482SBill Paul  */
68695d67482SBill Paul static int
68795d67482SBill Paul bge_newbuf_std(sc, i, m)
68895d67482SBill Paul 	struct bge_softc	*sc;
68995d67482SBill Paul 	int			i;
69095d67482SBill Paul 	struct mbuf		*m;
69195d67482SBill Paul {
69295d67482SBill Paul 	struct mbuf		*m_new = NULL;
69395d67482SBill Paul 	struct bge_rx_bd	*r;
694f41ac2beSBill Paul 	struct bge_dmamap_arg	ctx;
695f41ac2beSBill Paul 	int			error;
69695d67482SBill Paul 
69795d67482SBill Paul 	if (m == NULL) {
698c3a56752SGleb Smirnoff 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
699c3a56752SGleb Smirnoff 		if (m_new == NULL)
70095d67482SBill Paul 			return(ENOBUFS);
70195d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70295d67482SBill Paul 	} else {
70395d67482SBill Paul 		m_new = m;
70495d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70595d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
70695d67482SBill Paul 	}
70795d67482SBill Paul 
708e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
70995d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
71095d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
711f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
712f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
713f41ac2beSBill Paul 	ctx.sc = sc;
714f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
715f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
716f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
717f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
718f7cea149SGleb Smirnoff 		if (m == NULL) {
719f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
720f41ac2beSBill Paul 			m_freem(m_new);
721f7cea149SGleb Smirnoff 		}
722f41ac2beSBill Paul 		return(ENOMEM);
723f41ac2beSBill Paul 	}
724e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
725e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
726e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
727e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
728e907febfSPyun YongHyeon 	r->bge_idx = i;
729f41ac2beSBill Paul 
730f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
731f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
732f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
73395d67482SBill Paul 
73495d67482SBill Paul 	return(0);
73595d67482SBill Paul }
73695d67482SBill Paul 
73795d67482SBill Paul /*
73895d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
73995d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
74095d67482SBill Paul  */
74195d67482SBill Paul static int
74295d67482SBill Paul bge_newbuf_jumbo(sc, i, m)
74395d67482SBill Paul 	struct bge_softc *sc;
74495d67482SBill Paul 	int i;
74595d67482SBill Paul 	struct mbuf *m;
74695d67482SBill Paul {
7471be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
7481be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
74995d67482SBill Paul 	struct mbuf *m_new = NULL;
7501be6acb7SGleb Smirnoff 	int nsegs;
751f41ac2beSBill Paul 	int error;
75295d67482SBill Paul 
75395d67482SBill Paul 	if (m == NULL) {
754a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
7551be6acb7SGleb Smirnoff 		if (m_new == NULL)
75695d67482SBill Paul 			return(ENOBUFS);
75795d67482SBill Paul 
7581be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7591be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
76095d67482SBill Paul 			m_freem(m_new);
76195d67482SBill Paul 			return(ENOBUFS);
76295d67482SBill Paul 		}
7631be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
76495d67482SBill Paul 	} else {
76595d67482SBill Paul 		m_new = m;
7661be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
76795d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
76895d67482SBill Paul 	}
76995d67482SBill Paul 
770e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
77195d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7721be6acb7SGleb Smirnoff 
7731be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7741be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7751be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7761be6acb7SGleb Smirnoff 	if (error) {
7771be6acb7SGleb Smirnoff 		if (m == NULL)
778f41ac2beSBill Paul 			m_freem(m_new);
7791be6acb7SGleb Smirnoff 		return(error);
780f7cea149SGleb Smirnoff 	}
7811be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
7821be6acb7SGleb Smirnoff 
7831be6acb7SGleb Smirnoff 	/*
7841be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
7851be6acb7SGleb Smirnoff 	 */
7861be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
7874e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING|BGE_RXBDFLAG_END;
7884e7ba1abSGleb Smirnoff 	r->bge_idx = i;
7894e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
7904e7ba1abSGleb Smirnoff 	switch (nsegs) {
7914e7ba1abSGleb Smirnoff 	case 4:
7924e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
7934e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
7944e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
7954e7ba1abSGleb Smirnoff 	case 3:
796e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
797e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
798e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
7994e7ba1abSGleb Smirnoff 	case 2:
8004e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
8014e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
8024e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
8034e7ba1abSGleb Smirnoff 	case 1:
8044e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
8054e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
8064e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
8074e7ba1abSGleb Smirnoff 		break;
8084e7ba1abSGleb Smirnoff 	default:
8094e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
8104e7ba1abSGleb Smirnoff 	}
811f41ac2beSBill Paul 
812f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
813f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
814f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
81595d67482SBill Paul 
81695d67482SBill Paul 	return (0);
81795d67482SBill Paul }
81895d67482SBill Paul 
81995d67482SBill Paul /*
82095d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
82195d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
82295d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
82395d67482SBill Paul  * the NIC.
82495d67482SBill Paul  */
82595d67482SBill Paul static int
82695d67482SBill Paul bge_init_rx_ring_std(sc)
82795d67482SBill Paul 	struct bge_softc *sc;
82895d67482SBill Paul {
82995d67482SBill Paul 	int i;
83095d67482SBill Paul 
83195d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
83295d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
83395d67482SBill Paul 			return(ENOBUFS);
83495d67482SBill Paul 	};
83595d67482SBill Paul 
836f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
837f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
838f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
839f41ac2beSBill Paul 
84095d67482SBill Paul 	sc->bge_std = i - 1;
84195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
84295d67482SBill Paul 
84395d67482SBill Paul 	return(0);
84495d67482SBill Paul }
84595d67482SBill Paul 
84695d67482SBill Paul static void
84795d67482SBill Paul bge_free_rx_ring_std(sc)
84895d67482SBill Paul 	struct bge_softc *sc;
84995d67482SBill Paul {
85095d67482SBill Paul 	int i;
85195d67482SBill Paul 
85295d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
85395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
854e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
855e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
856e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
857f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
858f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
859e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
860e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
86195d67482SBill Paul 		}
862f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
86395d67482SBill Paul 		    sizeof(struct bge_rx_bd));
86495d67482SBill Paul 	}
86595d67482SBill Paul 
86695d67482SBill Paul 	return;
86795d67482SBill Paul }
86895d67482SBill Paul 
86995d67482SBill Paul static int
87095d67482SBill Paul bge_init_rx_ring_jumbo(sc)
87195d67482SBill Paul 	struct bge_softc *sc;
87295d67482SBill Paul {
87395d67482SBill Paul 	struct bge_rcb *rcb;
8741be6acb7SGleb Smirnoff 	int i;
87595d67482SBill Paul 
87695d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
87795d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
87895d67482SBill Paul 			return(ENOBUFS);
87995d67482SBill Paul 	};
88095d67482SBill Paul 
881f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
882f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
883f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
884f41ac2beSBill Paul 
88595d67482SBill Paul 	sc->bge_jumbo = i - 1;
88695d67482SBill Paul 
887f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8881be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8891be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
89067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
89195d67482SBill Paul 
89295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
89395d67482SBill Paul 
89495d67482SBill Paul 	return(0);
89595d67482SBill Paul }
89695d67482SBill Paul 
89795d67482SBill Paul static void
89895d67482SBill Paul bge_free_rx_ring_jumbo(sc)
89995d67482SBill Paul 	struct bge_softc *sc;
90095d67482SBill Paul {
90195d67482SBill Paul 	int i;
90295d67482SBill Paul 
90395d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
90495d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
905e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
906e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
907e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
908f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
909f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
910e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
911e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
91295d67482SBill Paul 		}
913f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
9141be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
91595d67482SBill Paul 	}
91695d67482SBill Paul 
91795d67482SBill Paul 	return;
91895d67482SBill Paul }
91995d67482SBill Paul 
92095d67482SBill Paul static void
92195d67482SBill Paul bge_free_tx_ring(sc)
92295d67482SBill Paul 	struct bge_softc *sc;
92395d67482SBill Paul {
92495d67482SBill Paul 	int i;
92595d67482SBill Paul 
926f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
92795d67482SBill Paul 		return;
92895d67482SBill Paul 
92995d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
93095d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
931e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
932e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
933e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
934f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
935f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
936e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
937e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
93895d67482SBill Paul 		}
939f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
94095d67482SBill Paul 		    sizeof(struct bge_tx_bd));
94195d67482SBill Paul 	}
94295d67482SBill Paul 
94395d67482SBill Paul 	return;
94495d67482SBill Paul }
94595d67482SBill Paul 
94695d67482SBill Paul static int
94795d67482SBill Paul bge_init_tx_ring(sc)
94895d67482SBill Paul 	struct bge_softc *sc;
94995d67482SBill Paul {
95095d67482SBill Paul 	sc->bge_txcnt = 0;
95195d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9523927098fSPaul Saab 
95314bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
95414bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
95514bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
95614bbd30fSGleb Smirnoff 
9573927098fSPaul Saab 	/* 5700 b2 errata */
958e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
95914bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
9603927098fSPaul Saab 
96114bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
9623927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
9633927098fSPaul Saab 	/* 5700 b2 errata */
964e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
96595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
96695d67482SBill Paul 
96795d67482SBill Paul 	return(0);
96895d67482SBill Paul }
96995d67482SBill Paul 
97095d67482SBill Paul static void
97195d67482SBill Paul bge_setmulti(sc)
97295d67482SBill Paul 	struct bge_softc *sc;
97395d67482SBill Paul {
97495d67482SBill Paul 	struct ifnet *ifp;
97595d67482SBill Paul 	struct ifmultiaddr *ifma;
97695d67482SBill Paul 	u_int32_t hashes[4] = { 0, 0, 0, 0 };
97795d67482SBill Paul 	int h, i;
97895d67482SBill Paul 
9790f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9800f9bd73bSSam Leffler 
981fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
98295d67482SBill Paul 
98395d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
98495d67482SBill Paul 		for (i = 0; i < 4; i++)
98595d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
98695d67482SBill Paul 		return;
98795d67482SBill Paul 	}
98895d67482SBill Paul 
98995d67482SBill Paul 	/* First, zot all the existing filters. */
99095d67482SBill Paul 	for (i = 0; i < 4; i++)
99195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
99295d67482SBill Paul 
99395d67482SBill Paul 	/* Now program new ones. */
99413b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
99595d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
99695d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
99795d67482SBill Paul 			continue;
9980e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
9990e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
100095d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
100195d67482SBill Paul 	}
100213b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
100395d67482SBill Paul 
100495d67482SBill Paul 	for (i = 0; i < 4; i++)
100595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
100695d67482SBill Paul 
100795d67482SBill Paul 	return;
100895d67482SBill Paul }
100995d67482SBill Paul 
101095d67482SBill Paul /*
101195d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
101295d67482SBill Paul  * self-test results.
101395d67482SBill Paul  */
101495d67482SBill Paul static int
101595d67482SBill Paul bge_chipinit(sc)
101695d67482SBill Paul 	struct bge_softc *sc;
101795d67482SBill Paul {
101895d67482SBill Paul 	int			i;
10195cba12d3SPaul Saab 	u_int32_t		dma_rw_ctl;
102095d67482SBill Paul 
1021e907febfSPyun YongHyeon 	/* Set endian type before we access any non-PCI registers. */
1022e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
102395d67482SBill Paul 
102495d67482SBill Paul 	/*
102595d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
102695d67482SBill Paul 	 * self-tests passed.
102795d67482SBill Paul 	 */
102895d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1029fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
103095d67482SBill Paul 		return(ENODEV);
103195d67482SBill Paul 	}
103295d67482SBill Paul 
103395d67482SBill Paul 	/* Clear the MAC control register */
103495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
103595d67482SBill Paul 
103695d67482SBill Paul 	/*
103795d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
103895d67482SBill Paul 	 * internal memory.
103995d67482SBill Paul 	 */
104095d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
104195d67482SBill Paul 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
104295d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
104395d67482SBill Paul 
104495d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
104595d67482SBill Paul 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
104695d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
104795d67482SBill Paul 
104895d67482SBill Paul 	/* Set up the PCI DMA control register. */
1049e53d81eeSPaul Saab 	if (sc->bge_pcie) {
1050e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1051e53d81eeSPaul Saab 		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1052e53d81eeSPaul Saab 		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1053e53d81eeSPaul Saab 	} else if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
10548287860eSJohn Polstra 	    BGE_PCISTATE_PCI_BUSMODE) {
10558287860eSJohn Polstra 		/* Conventional PCI bus */
10565cba12d3SPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10575cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10585cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10595cba12d3SPaul Saab 		    (0x0F);
10608287860eSJohn Polstra 	} else {
10618287860eSJohn Polstra 		/* PCI-X bus */
10625cba12d3SPaul Saab 		/*
10635cba12d3SPaul Saab 		 * The 5704 uses a different encoding of read/write
10645cba12d3SPaul Saab 		 * watermarks.
10655cba12d3SPaul Saab 		 */
1066e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
10675cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10685cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10695cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
10705cba12d3SPaul Saab 		else
10715cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10725cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10735cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10745cba12d3SPaul Saab 			    (0x0F);
10755cba12d3SPaul Saab 
10765cba12d3SPaul Saab 		/*
10775cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
10785cba12d3SPaul Saab 		 * for hardware bugs.
10795cba12d3SPaul Saab 		 */
1080e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1081e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
10825cba12d3SPaul Saab 			u_int32_t tmp;
10835cba12d3SPaul Saab 
10845cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
10855cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
10865cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
10878287860eSJohn Polstra 		}
10885cba12d3SPaul Saab 	}
10895cba12d3SPaul Saab 
1090e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
10910434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
1092e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1093e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
10945cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
10955cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
109695d67482SBill Paul 
109795d67482SBill Paul 	/*
109895d67482SBill Paul 	 * Set up general mode register.
109995d67482SBill Paul 	 */
1100e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
110195d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1102ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
110395d67482SBill Paul 
110495d67482SBill Paul 	/*
1105ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1106ea13bdd5SJohn Polstra 	 * properly by these devices.
110795d67482SBill Paul 	 */
1108ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
110995d67482SBill Paul 
111095d67482SBill Paul #ifdef __brokenalpha__
111195d67482SBill Paul 	/*
111295d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
111395d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
111495d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
111595d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
111695d67482SBill Paul 	 */
111762f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
111862f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
111995d67482SBill Paul #endif
112095d67482SBill Paul 
112195d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
112295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
112395d67482SBill Paul 
112495d67482SBill Paul 	return(0);
112595d67482SBill Paul }
112695d67482SBill Paul 
112795d67482SBill Paul static int
112895d67482SBill Paul bge_blockinit(sc)
112995d67482SBill Paul 	struct bge_softc *sc;
113095d67482SBill Paul {
113195d67482SBill Paul 	struct bge_rcb *rcb;
1132e907febfSPyun YongHyeon 	bus_size_t vrcb;
1133e907febfSPyun YongHyeon 	bge_hostaddr taddr;
113495d67482SBill Paul 	int i;
113595d67482SBill Paul 
113695d67482SBill Paul 	/*
113795d67482SBill Paul 	 * Initialize the memory window pointer register so that
113895d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
113995d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
114095d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
114195d67482SBill Paul 	 */
114295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
114395d67482SBill Paul 
1144822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1145822f63fcSBill Paul 
11465dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1147e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
114895d67482SBill Paul 		/* Configure mbuf memory pool */
114995d67482SBill Paul 		if (sc->bge_extram) {
11500434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11510434d1b8SBill Paul 			    BGE_EXT_SSRAM);
1152822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1153822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1154822f63fcSBill Paul 			else
115595d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
115695d67482SBill Paul 		} else {
11570434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11580434d1b8SBill Paul 			    BGE_BUFFPOOL_1);
1159822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1160822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1161822f63fcSBill Paul 			else
116295d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
116395d67482SBill Paul 		}
116495d67482SBill Paul 
116595d67482SBill Paul 		/* Configure DMA resource pool */
11660434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
11670434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
116895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
11690434d1b8SBill Paul 	}
117095d67482SBill Paul 
117195d67482SBill Paul 	/* Configure mbuf pool watermarks */
1172e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1173e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750) {
11740434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11750434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
11760434d1b8SBill Paul 	} else {
1177fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1178fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
11790434d1b8SBill Paul 	}
1180fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
118195d67482SBill Paul 
118295d67482SBill Paul 	/* Configure DMA resource watermarks */
118395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
118495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
118595d67482SBill Paul 
118695d67482SBill Paul 	/* Enable buffer manager */
11875dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1188e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
118995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
119095d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
119195d67482SBill Paul 
119295d67482SBill Paul 		/* Poll for buffer manager start indication */
119395d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
119495d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
119595d67482SBill Paul 				break;
119695d67482SBill Paul 			DELAY(10);
119795d67482SBill Paul 		}
119895d67482SBill Paul 
119995d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1200fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1201fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
120295d67482SBill Paul 			return(ENXIO);
120395d67482SBill Paul 		}
12040434d1b8SBill Paul 	}
120595d67482SBill Paul 
120695d67482SBill Paul 	/* Enable flow-through queues */
120795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
120895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
120995d67482SBill Paul 
121095d67482SBill Paul 	/* Wait until queue initialization is complete */
121195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
121295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
121395d67482SBill Paul 			break;
121495d67482SBill Paul 		DELAY(10);
121595d67482SBill Paul 	}
121695d67482SBill Paul 
121795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1218fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
121995d67482SBill Paul 		return(ENXIO);
122095d67482SBill Paul 	}
122195d67482SBill Paul 
122295d67482SBill Paul 	/* Initialize the standard RX ring control block */
1223f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1224f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1225f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1226f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1227f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1228f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1229f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
1230e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1231e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
12320434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
12330434d1b8SBill Paul 	else
12340434d1b8SBill Paul 		rcb->bge_maxlen_flags =
12350434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
123695d67482SBill Paul 	if (sc->bge_extram)
123795d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
123895d67482SBill Paul 	else
123995d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
124067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
124167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1242f41ac2beSBill Paul 
124367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
124467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
124595d67482SBill Paul 
124695d67482SBill Paul 	/*
124795d67482SBill Paul 	 * Initialize the jumbo RX ring control block
124895d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
124995d67482SBill Paul 	 * field until we're actually ready to start
125095d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
125195d67482SBill Paul 	 * high enough to require it).
125295d67482SBill Paul 	 */
12535dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1254e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1255f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1256f41ac2beSBill Paul 
1257f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1258f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1259f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1260f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1261f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1262f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1263f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
12641be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
12651be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD|BGE_RCB_FLAG_RING_DISABLED);
126695d67482SBill Paul 		if (sc->bge_extram)
126795d67482SBill Paul 			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
126895d67482SBill Paul 		else
126995d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
127067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
127167111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
127267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
127367111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1274f41ac2beSBill Paul 
12750434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
12760434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
127767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
127895d67482SBill Paul 
127995d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1280f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
128167111612SJohn Polstra 		rcb->bge_maxlen_flags =
128267111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
12830434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
12840434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
12850434d1b8SBill Paul 	}
128695d67482SBill Paul 
128795d67482SBill Paul 	/*
128895d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
128995d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
129095d67482SBill Paul 	 * each ring.
129195d67482SBill Paul 	 */
129295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
129395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
129495d67482SBill Paul 
129595d67482SBill Paul 	/*
129695d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
129795d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
129895d67482SBill Paul 	 * These are located in NIC memory.
129995d67482SBill Paul 	 */
1300e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
130195d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1302e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1303e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1304e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1305e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
130695d67482SBill Paul 	}
130795d67482SBill Paul 
130895d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1309e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1310e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1311e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1312e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1313e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1314e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
13155dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1316e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1317e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1318e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
131995d67482SBill Paul 
132095d67482SBill Paul 	/* Disable all unused RX return rings */
1321e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
132295d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1323e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1324e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1325e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
13260434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1327e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1328e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
132995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
133095d67482SBill Paul 		    (i * (sizeof(u_int64_t))), 0);
1331e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
133295d67482SBill Paul 	}
133395d67482SBill Paul 
133495d67482SBill Paul 	/* Initialize RX ring indexes */
133595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
133695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
133795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
133895d67482SBill Paul 
133995d67482SBill Paul 	/*
134095d67482SBill Paul 	 * Set up RX return ring 0
134195d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
134295d67482SBill Paul 	 * The return rings live entirely within the host, so the
134395d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
134495d67482SBill Paul 	 */
1345e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1346e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1347e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1348e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1349e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1350e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1351e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
135295d67482SBill Paul 
135395d67482SBill Paul 	/* Set random backoff seed for TX */
135495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
13554a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
13564a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
13574a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
135895d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
135995d67482SBill Paul 
136095d67482SBill Paul 	/* Set inter-packet gap */
136195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
136295d67482SBill Paul 
136395d67482SBill Paul 	/*
136495d67482SBill Paul 	 * Specify which ring to use for packets that don't match
136595d67482SBill Paul 	 * any RX rules.
136695d67482SBill Paul 	 */
136795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
136895d67482SBill Paul 
136995d67482SBill Paul 	/*
137095d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
137195d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
137295d67482SBill Paul 	 */
137395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
137495d67482SBill Paul 
137595d67482SBill Paul 	/* Inialize RX list placement stats mask. */
137695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
137795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
137895d67482SBill Paul 
137995d67482SBill Paul 	/* Disable host coalescing until we get it set up */
138095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
138195d67482SBill Paul 
138295d67482SBill Paul 	/* Poll to make sure it's shut down. */
138395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
138495d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
138595d67482SBill Paul 			break;
138695d67482SBill Paul 		DELAY(10);
138795d67482SBill Paul 	}
138895d67482SBill Paul 
138995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1390fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1391fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
139295d67482SBill Paul 		return(ENXIO);
139395d67482SBill Paul 	}
139495d67482SBill Paul 
139595d67482SBill Paul 	/* Set up host coalescing defaults */
139695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
139795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
139895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
139995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
14005dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1401e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
140295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
140395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
14040434d1b8SBill Paul 	}
140595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
140695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
140795d67482SBill Paul 
140895d67482SBill Paul 	/* Set up address of statistics block */
14095dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1410e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1411f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1412f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
141395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1414f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
14150434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
141695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
14170434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
14180434d1b8SBill Paul 	}
14190434d1b8SBill Paul 
14200434d1b8SBill Paul 	/* Set up address of status block */
1421f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1422f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
142395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1424f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1425f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1426f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
142795d67482SBill Paul 
142895d67482SBill Paul 	/* Turn on host coalescing state machine */
142995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
143095d67482SBill Paul 
143195d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
143295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
143395d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
143495d67482SBill Paul 
143595d67482SBill Paul 	/* Turn on RX list placement state machine */
143695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
143795d67482SBill Paul 
143895d67482SBill Paul 	/* Turn on RX list selector state machine. */
14395dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1440e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
144195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
144295d67482SBill Paul 
144395d67482SBill Paul 	/* Turn on DMA, clear stats */
144495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
144595d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
144695d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
144795d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
144895d67482SBill Paul 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
144995d67482SBill Paul 
145095d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
145195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
145295d67482SBill Paul 
145395d67482SBill Paul #ifdef notdef
145495d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
145595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
145695d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
145795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
145895d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
145995d67482SBill Paul #endif
146095d67482SBill Paul 
146195d67482SBill Paul 	/* Turn on DMA completion state machine */
14625dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1463e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
146495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
146595d67482SBill Paul 
146695d67482SBill Paul 	/* Turn on write DMA state machine */
146795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
146895d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
146995d67482SBill Paul 
147095d67482SBill Paul 	/* Turn on read DMA state machine */
147195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
147295d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
147395d67482SBill Paul 
147495d67482SBill Paul 	/* Turn on RX data completion state machine */
147595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
147695d67482SBill Paul 
147795d67482SBill Paul 	/* Turn on RX BD initiator state machine */
147895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
147995d67482SBill Paul 
148095d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
148195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
148295d67482SBill Paul 
148395d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
14845dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1485e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
148695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
148795d67482SBill Paul 
148895d67482SBill Paul 	/* Turn on send BD completion state machine */
148995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
149095d67482SBill Paul 
149195d67482SBill Paul 	/* Turn on send data completion state machine */
149295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
149395d67482SBill Paul 
149495d67482SBill Paul 	/* Turn on send data initiator state machine */
149595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
149695d67482SBill Paul 
149795d67482SBill Paul 	/* Turn on send BD initiator state machine */
149895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
149995d67482SBill Paul 
150095d67482SBill Paul 	/* Turn on send BD selector state machine */
150195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
150295d67482SBill Paul 
150395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
150495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
150595d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
150695d67482SBill Paul 
150795d67482SBill Paul 	/* ack/clear link change events */
150895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15090434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15100434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1511f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
151295d67482SBill Paul 
151395d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
151495d67482SBill Paul 	if (sc->bge_tbi) {
151595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1516a1d52896SBill Paul 	} else {
151795d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
15181f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
15191f313773SOleg Bulyzhin 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B1)
1520a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1521a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1522a1d52896SBill Paul 	}
152395d67482SBill Paul 
15241f313773SOleg Bulyzhin 	/*
15251f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
15261f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
15271f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
15281f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
15291f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
15301f313773SOleg Bulyzhin 	 */
15311f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15321f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15331f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
15341f313773SOleg Bulyzhin 
153595d67482SBill Paul 	/* Enable link state change attentions. */
153695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
153795d67482SBill Paul 
153895d67482SBill Paul 	return(0);
153995d67482SBill Paul }
154095d67482SBill Paul 
154195d67482SBill Paul /*
154295d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
154395d67482SBill Paul  * against our list and return its name if we find a match. Note
154495d67482SBill Paul  * that since the Broadcom controller contains VPD support, we
154595d67482SBill Paul  * can get the device name string from the controller itself instead
154695d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
154795d67482SBill Paul  * we'll always announce the right product name.
154895d67482SBill Paul  */
154995d67482SBill Paul static int
155095d67482SBill Paul bge_probe(dev)
155195d67482SBill Paul 	device_t dev;
155295d67482SBill Paul {
155395d67482SBill Paul 	struct bge_type *t;
155495d67482SBill Paul 	struct bge_softc *sc;
1555029e2ee3SJohn Polstra 	char *descbuf;
155695d67482SBill Paul 
155795d67482SBill Paul 	t = bge_devs;
155895d67482SBill Paul 
155995d67482SBill Paul 	sc = device_get_softc(dev);
156095d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
156195d67482SBill Paul 	sc->bge_dev = dev;
156295d67482SBill Paul 
156395d67482SBill Paul 	while(t->bge_name != NULL) {
156495d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
156595d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
156695d67482SBill Paul #ifdef notdef
156795d67482SBill Paul 			bge_vpd_read(sc);
156895d67482SBill Paul 			device_set_desc(dev, sc->bge_vpd_prodname);
156995d67482SBill Paul #endif
1570029e2ee3SJohn Polstra 			descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
1571029e2ee3SJohn Polstra 			if (descbuf == NULL)
1572029e2ee3SJohn Polstra 				return(ENOMEM);
1573029e2ee3SJohn Polstra 			snprintf(descbuf, BGE_DEVDESC_MAX,
1574029e2ee3SJohn Polstra 			    "%s, ASIC rev. %#04x", t->bge_name,
1575029e2ee3SJohn Polstra 			    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16);
1576029e2ee3SJohn Polstra 			device_set_desc_copy(dev, descbuf);
15776d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
15786d2a9bd6SDoug Ambrisko 				sc->bge_no_3_led = 1;
1579029e2ee3SJohn Polstra 			free(descbuf, M_TEMP);
158095d67482SBill Paul 			return(0);
158195d67482SBill Paul 		}
158295d67482SBill Paul 		t++;
158395d67482SBill Paul 	}
158495d67482SBill Paul 
158595d67482SBill Paul 	return(ENXIO);
158695d67482SBill Paul }
158795d67482SBill Paul 
1588f41ac2beSBill Paul static void
1589f41ac2beSBill Paul bge_dma_free(sc)
1590f41ac2beSBill Paul 	struct bge_softc *sc;
1591f41ac2beSBill Paul {
1592f41ac2beSBill Paul 	int i;
1593f41ac2beSBill Paul 
1594f41ac2beSBill Paul 
1595f41ac2beSBill Paul 	/* Destroy DMA maps for RX buffers */
1596f41ac2beSBill Paul 
1597f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1598f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1599f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1600f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1601f41ac2beSBill Paul 	}
1602f41ac2beSBill Paul 
1603f41ac2beSBill Paul 	/* Destroy DMA maps for jumbo RX buffers */
1604f41ac2beSBill Paul 
1605f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1606f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1607f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1608f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1609f41ac2beSBill Paul 	}
1610f41ac2beSBill Paul 
1611f41ac2beSBill Paul 	/* Destroy DMA maps for TX buffers */
1612f41ac2beSBill Paul 
1613f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1614f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1615f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1616f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1617f41ac2beSBill Paul 	}
1618f41ac2beSBill Paul 
1619f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1620f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1621f41ac2beSBill Paul 
1622f41ac2beSBill Paul 
1623f41ac2beSBill Paul 	/* Destroy standard RX ring */
1624f41ac2beSBill Paul 
1625e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1626e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1627e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1628e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1629f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1630f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1631f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1632f41ac2beSBill Paul 
1633f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1634f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1635f41ac2beSBill Paul 
1636f41ac2beSBill Paul 	/* Destroy jumbo RX ring */
1637f41ac2beSBill Paul 
1638e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1639e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1640e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1641e65bed95SPyun YongHyeon 
1642e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1643e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1644f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1645f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1646f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1647f41ac2beSBill Paul 
1648f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1649f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1650f41ac2beSBill Paul 
1651f41ac2beSBill Paul 	/* Destroy RX return ring */
1652f41ac2beSBill Paul 
1653e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1654e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1655e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1656e65bed95SPyun YongHyeon 
1657e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1658e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1659f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1660f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1661f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1662f41ac2beSBill Paul 
1663f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1664f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1665f41ac2beSBill Paul 
1666f41ac2beSBill Paul 	/* Destroy TX ring */
1667f41ac2beSBill Paul 
1668e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1669e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1670e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1671e65bed95SPyun YongHyeon 
1672e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1673f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1674f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1675f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1676f41ac2beSBill Paul 
1677f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1678f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1679f41ac2beSBill Paul 
1680f41ac2beSBill Paul 	/* Destroy status block */
1681f41ac2beSBill Paul 
1682e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1683e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1684e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1685e65bed95SPyun YongHyeon 
1686e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1687f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1688f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1689f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1690f41ac2beSBill Paul 
1691f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1692f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1693f41ac2beSBill Paul 
1694f41ac2beSBill Paul 	/* Destroy statistics block */
1695f41ac2beSBill Paul 
1696e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1697e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1698e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1699e65bed95SPyun YongHyeon 
1700e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1701f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1702f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1703f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1704f41ac2beSBill Paul 
1705f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1706f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1707f41ac2beSBill Paul 
1708f41ac2beSBill Paul 	/* Destroy the parent tag */
1709f41ac2beSBill Paul 
1710f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1711f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1712f41ac2beSBill Paul 
1713f41ac2beSBill Paul 	return;
1714f41ac2beSBill Paul }
1715f41ac2beSBill Paul 
1716f41ac2beSBill Paul static int
1717f41ac2beSBill Paul bge_dma_alloc(dev)
1718f41ac2beSBill Paul 	device_t dev;
1719f41ac2beSBill Paul {
1720f41ac2beSBill Paul 	struct bge_softc *sc;
17211be6acb7SGleb Smirnoff 	int i, error;
1722f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
1723f41ac2beSBill Paul 
1724f41ac2beSBill Paul 	sc = device_get_softc(dev);
1725f41ac2beSBill Paul 
1726f41ac2beSBill Paul 	/*
1727f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1728f41ac2beSBill Paul 	 */
1729f41ac2beSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1730f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1731f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
17322f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1733f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1734f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1735f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
17368a40c10eSScott Long 			0,			/* flags */
1737f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1738f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1739f41ac2beSBill Paul 
1740e65bed95SPyun YongHyeon 	if (error != 0) {
1741fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1742fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1743e65bed95SPyun YongHyeon 		return (ENOMEM);
1744e65bed95SPyun YongHyeon 	}
1745e65bed95SPyun YongHyeon 
1746f41ac2beSBill Paul 	/*
1747f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1748f41ac2beSBill Paul 	 */
17498a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1750f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
17511be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
17521be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1753f41ac2beSBill Paul 
1754f41ac2beSBill Paul 	if (error) {
1755fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1756f41ac2beSBill Paul 		return (ENOMEM);
1757f41ac2beSBill Paul 	}
1758f41ac2beSBill Paul 
1759f41ac2beSBill Paul 	/* Create DMA maps for RX buffers */
1760f41ac2beSBill Paul 
1761f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1762f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1763f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1764f41ac2beSBill Paul 		if (error) {
1765fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1766fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1767f41ac2beSBill Paul 			return(ENOMEM);
1768f41ac2beSBill Paul 		}
1769f41ac2beSBill Paul 	}
1770f41ac2beSBill Paul 
1771f41ac2beSBill Paul 	/* Create DMA maps for TX buffers */
1772f41ac2beSBill Paul 
1773f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1774f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1775f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1776f41ac2beSBill Paul 		if (error) {
1777fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1778fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1779f41ac2beSBill Paul 			return(ENOMEM);
1780f41ac2beSBill Paul 		}
1781f41ac2beSBill Paul 	}
1782f41ac2beSBill Paul 
1783f41ac2beSBill Paul 	/* Create tag for standard RX ring */
1784f41ac2beSBill Paul 
1785f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1786f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1787f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1788f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1789f41ac2beSBill Paul 
1790f41ac2beSBill Paul 	if (error) {
1791fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1792f41ac2beSBill Paul 		return (ENOMEM);
1793f41ac2beSBill Paul 	}
1794f41ac2beSBill Paul 
1795f41ac2beSBill Paul 	/* Allocate DMA'able memory for standard RX ring */
1796f41ac2beSBill Paul 
1797f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1798f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1799f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1800f41ac2beSBill Paul 	if (error)
1801f41ac2beSBill Paul 		return (ENOMEM);
1802f41ac2beSBill Paul 
1803f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1804f41ac2beSBill Paul 
1805f41ac2beSBill Paul 	/* Load the address of the standard RX ring */
1806f41ac2beSBill Paul 
1807f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1808f41ac2beSBill Paul 	ctx.sc = sc;
1809f41ac2beSBill Paul 
1810f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1811f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1812f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1813f41ac2beSBill Paul 
1814f41ac2beSBill Paul 	if (error)
1815f41ac2beSBill Paul 		return (ENOMEM);
1816f41ac2beSBill Paul 
1817f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1818f41ac2beSBill Paul 
18195dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1820e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1821f41ac2beSBill Paul 
1822f41ac2beSBill Paul 		/*
1823f41ac2beSBill Paul 		 * Create tag for jumbo mbufs.
1824f41ac2beSBill Paul 		 * This is really a bit of a kludge. We allocate a special
1825f41ac2beSBill Paul 		 * jumbo buffer pool which (thanks to the way our DMA
1826f41ac2beSBill Paul 		 * memory allocation works) will consist of contiguous
1827f41ac2beSBill Paul 		 * pages. This means that even though a jumbo buffer might
1828f41ac2beSBill Paul 		 * be larger than a page size, we don't really need to
1829f41ac2beSBill Paul 		 * map it into more than one DMA segment. However, the
1830f41ac2beSBill Paul 		 * default mbuf tag will result in multi-segment mappings,
1831f41ac2beSBill Paul 		 * so we have to create a special jumbo mbuf tag that
1832f41ac2beSBill Paul 		 * lets us get away with mapping the jumbo buffers as
1833f41ac2beSBill Paul 		 * a single segment. I think eventually the driver should
1834f41ac2beSBill Paul 		 * be changed so that it uses ordinary mbufs and cluster
1835f41ac2beSBill Paul 		 * buffers, i.e. jumbo frames can span multiple DMA
1836f41ac2beSBill Paul 		 * descriptors. But that's a project for another day.
1837f41ac2beSBill Paul 		 */
1838f41ac2beSBill Paul 
1839f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
18408a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
18411be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
18421be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1843f41ac2beSBill Paul 
1844f41ac2beSBill Paul 		if (error) {
1845fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1846fe806fdaSPyun YongHyeon 			    "could not allocate dma tag\n");
1847f41ac2beSBill Paul 			return (ENOMEM);
1848f41ac2beSBill Paul 		}
1849f41ac2beSBill Paul 
1850f41ac2beSBill Paul 		/* Create tag for jumbo RX ring */
1851f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1852f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1853f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1854f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1855f41ac2beSBill Paul 
1856f41ac2beSBill Paul 		if (error) {
1857fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1858fe806fdaSPyun YongHyeon 			    "could not allocate dma tag\n");
1859f41ac2beSBill Paul 			return (ENOMEM);
1860f41ac2beSBill Paul 		}
1861f41ac2beSBill Paul 
1862f41ac2beSBill Paul 		/* Allocate DMA'able memory for jumbo RX ring */
1863f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
18641be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
18651be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1866f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1867f41ac2beSBill Paul 		if (error)
1868f41ac2beSBill Paul 			return (ENOMEM);
1869f41ac2beSBill Paul 
1870f41ac2beSBill Paul 		/* Load the address of the jumbo RX ring */
1871f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1872f41ac2beSBill Paul 		ctx.sc = sc;
1873f41ac2beSBill Paul 
1874f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1875f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1876f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1877f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1878f41ac2beSBill Paul 
1879f41ac2beSBill Paul 		if (error)
1880f41ac2beSBill Paul 			return (ENOMEM);
1881f41ac2beSBill Paul 
1882f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1883f41ac2beSBill Paul 
1884f41ac2beSBill Paul 		/* Create DMA maps for jumbo RX buffers */
1885f41ac2beSBill Paul 
1886f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1887f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1888f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1889f41ac2beSBill Paul 			if (error) {
1890fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
1891f41ac2beSBill Paul 				    "can't create DMA map for RX\n");
1892f41ac2beSBill Paul 				return(ENOMEM);
1893f41ac2beSBill Paul 			}
1894f41ac2beSBill Paul 		}
1895f41ac2beSBill Paul 
1896f41ac2beSBill Paul 	}
1897f41ac2beSBill Paul 
1898f41ac2beSBill Paul 	/* Create tag for RX return ring */
1899f41ac2beSBill Paul 
1900f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1901f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1902f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1903f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1904f41ac2beSBill Paul 
1905f41ac2beSBill Paul 	if (error) {
1906fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1907f41ac2beSBill Paul 		return (ENOMEM);
1908f41ac2beSBill Paul 	}
1909f41ac2beSBill Paul 
1910f41ac2beSBill Paul 	/* Allocate DMA'able memory for RX return ring */
1911f41ac2beSBill Paul 
1912f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1913f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1914f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1915f41ac2beSBill Paul 	if (error)
1916f41ac2beSBill Paul 		return (ENOMEM);
1917f41ac2beSBill Paul 
1918f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1919f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1920f41ac2beSBill Paul 
1921f41ac2beSBill Paul 	/* Load the address of the RX return ring */
1922f41ac2beSBill Paul 
1923f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1924f41ac2beSBill Paul 	ctx.sc = sc;
1925f41ac2beSBill Paul 
1926f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
1927f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
1928f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
1929f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1930f41ac2beSBill Paul 
1931f41ac2beSBill Paul 	if (error)
1932f41ac2beSBill Paul 		return (ENOMEM);
1933f41ac2beSBill Paul 
1934f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
1935f41ac2beSBill Paul 
1936f41ac2beSBill Paul 	/* Create tag for TX ring */
1937f41ac2beSBill Paul 
1938f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1939f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1940f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
1941f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
1942f41ac2beSBill Paul 
1943f41ac2beSBill Paul 	if (error) {
1944fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1945f41ac2beSBill Paul 		return (ENOMEM);
1946f41ac2beSBill Paul 	}
1947f41ac2beSBill Paul 
1948f41ac2beSBill Paul 	/* Allocate DMA'able memory for TX ring */
1949f41ac2beSBill Paul 
1950f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
1951f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
1952f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
1953f41ac2beSBill Paul 	if (error)
1954f41ac2beSBill Paul 		return (ENOMEM);
1955f41ac2beSBill Paul 
1956f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1957f41ac2beSBill Paul 
1958f41ac2beSBill Paul 	/* Load the address of the TX ring */
1959f41ac2beSBill Paul 
1960f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1961f41ac2beSBill Paul 	ctx.sc = sc;
1962f41ac2beSBill Paul 
1963f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
1964f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
1965f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1966f41ac2beSBill Paul 
1967f41ac2beSBill Paul 	if (error)
1968f41ac2beSBill Paul 		return (ENOMEM);
1969f41ac2beSBill Paul 
1970f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
1971f41ac2beSBill Paul 
1972f41ac2beSBill Paul 	/* Create tag for status block */
1973f41ac2beSBill Paul 
1974f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1975f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1976f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
1977f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
1978f41ac2beSBill Paul 
1979f41ac2beSBill Paul 	if (error) {
1980fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1981f41ac2beSBill Paul 		return (ENOMEM);
1982f41ac2beSBill Paul 	}
1983f41ac2beSBill Paul 
1984f41ac2beSBill Paul 	/* Allocate DMA'able memory for status block */
1985f41ac2beSBill Paul 
1986f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
1987f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
1988f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
1989f41ac2beSBill Paul 	if (error)
1990f41ac2beSBill Paul 		return (ENOMEM);
1991f41ac2beSBill Paul 
1992f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1993f41ac2beSBill Paul 
1994f41ac2beSBill Paul 	/* Load the address of the status block */
1995f41ac2beSBill Paul 
1996f41ac2beSBill Paul 	ctx.sc = sc;
1997f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1998f41ac2beSBill Paul 
1999f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2000f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2001f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2002f41ac2beSBill Paul 
2003f41ac2beSBill Paul 	if (error)
2004f41ac2beSBill Paul 		return (ENOMEM);
2005f41ac2beSBill Paul 
2006f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2007f41ac2beSBill Paul 
2008f41ac2beSBill Paul 	/* Create tag for statistics block */
2009f41ac2beSBill Paul 
2010f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2011f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2012f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2013f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2014f41ac2beSBill Paul 
2015f41ac2beSBill Paul 	if (error) {
2016fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2017f41ac2beSBill Paul 		return (ENOMEM);
2018f41ac2beSBill Paul 	}
2019f41ac2beSBill Paul 
2020f41ac2beSBill Paul 	/* Allocate DMA'able memory for statistics block */
2021f41ac2beSBill Paul 
2022f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2023f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2024f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2025f41ac2beSBill Paul 	if (error)
2026f41ac2beSBill Paul 		return (ENOMEM);
2027f41ac2beSBill Paul 
2028f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2029f41ac2beSBill Paul 
2030f41ac2beSBill Paul 	/* Load the address of the statstics block */
2031f41ac2beSBill Paul 
2032f41ac2beSBill Paul 	ctx.sc = sc;
2033f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2034f41ac2beSBill Paul 
2035f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2036f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2037f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2038f41ac2beSBill Paul 
2039f41ac2beSBill Paul 	if (error)
2040f41ac2beSBill Paul 		return (ENOMEM);
2041f41ac2beSBill Paul 
2042f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2043f41ac2beSBill Paul 
2044f41ac2beSBill Paul 	return(0);
2045f41ac2beSBill Paul }
2046f41ac2beSBill Paul 
204795d67482SBill Paul static int
204895d67482SBill Paul bge_attach(dev)
204995d67482SBill Paul 	device_t dev;
205095d67482SBill Paul {
205195d67482SBill Paul 	struct ifnet *ifp;
205295d67482SBill Paul 	struct bge_softc *sc;
2053a1d52896SBill Paul 	u_int32_t hwcfg = 0;
2054fc74a9f9SBrooks Davis 	u_int32_t mac_tmp = 0;
2055fc74a9f9SBrooks Davis 	u_char eaddr[6];
2056fe806fdaSPyun YongHyeon 	int error = 0, rid;
205795d67482SBill Paul 
205895d67482SBill Paul 	sc = device_get_softc(dev);
205995d67482SBill Paul 	sc->bge_dev = dev;
206095d67482SBill Paul 
206195d67482SBill Paul 	/*
206295d67482SBill Paul 	 * Map control/status registers.
206395d67482SBill Paul 	 */
206495d67482SBill Paul 	pci_enable_busmaster(dev);
206595d67482SBill Paul 
206695d67482SBill Paul 	rid = BGE_PCI_BAR0;
20675f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
20685f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
206995d67482SBill Paul 
207095d67482SBill Paul 	if (sc->bge_res == NULL) {
2071fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
207295d67482SBill Paul 		error = ENXIO;
207395d67482SBill Paul 		goto fail;
207495d67482SBill Paul 	}
207595d67482SBill Paul 
207695d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
207795d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
207895d67482SBill Paul 
207995d67482SBill Paul 	/* Allocate interrupt */
208095d67482SBill Paul 	rid = 0;
208195d67482SBill Paul 
20825f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
208395d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
208495d67482SBill Paul 
208595d67482SBill Paul 	if (sc->bge_irq == NULL) {
2086fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
208795d67482SBill Paul 		error = ENXIO;
208895d67482SBill Paul 		goto fail;
208995d67482SBill Paul 	}
209095d67482SBill Paul 
20910f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
20920f9bd73bSSam Leffler 
2093e53d81eeSPaul Saab 	/* Save ASIC rev. */
2094e53d81eeSPaul Saab 
2095e53d81eeSPaul Saab 	sc->bge_chipid =
2096e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2097e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2098e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2099e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2100e53d81eeSPaul Saab 
2101e53d81eeSPaul Saab 	/*
2102560c1670SGleb Smirnoff 	 * Treat the 5714 and the 5752 like the 5750 until we have more info
2103419c028bSPaul Saab 	 * on this chip.
2104419c028bSPaul Saab 	 */
2105560c1670SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5714 ||
2106560c1670SGleb Smirnoff             sc->bge_asicrev == BGE_ASICREV_BCM5752)
2107419c028bSPaul Saab 		sc->bge_asicrev = BGE_ASICREV_BCM5750;
2108419c028bSPaul Saab 
2109419c028bSPaul Saab 	/*
2110e53d81eeSPaul Saab 	 * XXX: Broadcom Linux driver.  Not in specs or eratta.
2111e53d81eeSPaul Saab 	 * PCI-Express?
2112e53d81eeSPaul Saab 	 */
2113e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
2114e53d81eeSPaul Saab 		u_int32_t v;
2115e53d81eeSPaul Saab 
2116e53d81eeSPaul Saab 		v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4);
2117e53d81eeSPaul Saab 		if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) {
2118e53d81eeSPaul Saab 			v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
2119e53d81eeSPaul Saab 			if ((v & 0xff) == BGE_PCIE_CAPID)
2120e53d81eeSPaul Saab 				sc->bge_pcie = 1;
2121e53d81eeSPaul Saab 		}
2122e53d81eeSPaul Saab 	}
2123e53d81eeSPaul Saab 
212495d67482SBill Paul 	/* Try to reset the chip. */
212595d67482SBill Paul 	bge_reset(sc);
212695d67482SBill Paul 
212795d67482SBill Paul 	if (bge_chipinit(sc)) {
2128fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
212995d67482SBill Paul 		bge_release_resources(sc);
213095d67482SBill Paul 		error = ENXIO;
213195d67482SBill Paul 		goto fail;
213295d67482SBill Paul 	}
213395d67482SBill Paul 
213495d67482SBill Paul 	/*
213595d67482SBill Paul 	 * Get station address from the EEPROM.
213695d67482SBill Paul 	 */
2137fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2138fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2139fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2140fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2141fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2142fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2143fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2144fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2145fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2146fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
214795d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2148fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
214995d67482SBill Paul 		bge_release_resources(sc);
215095d67482SBill Paul 		error = ENXIO;
215195d67482SBill Paul 		goto fail;
215295d67482SBill Paul 	}
215395d67482SBill Paul 
2154f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
2155e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2156e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
2157f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2158f41ac2beSBill Paul 	else
2159f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2160f41ac2beSBill Paul 
2161f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2162fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2163fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2164f41ac2beSBill Paul 		bge_release_resources(sc);
2165f41ac2beSBill Paul 		error = ENXIO;
2166f41ac2beSBill Paul 		goto fail;
2167f41ac2beSBill Paul 	}
2168f41ac2beSBill Paul 
216995d67482SBill Paul 	/* Set default tuneable values. */
217095d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
217195d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
217295d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
217395d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
217495d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
217595d67482SBill Paul 
217695d67482SBill Paul 	/* Set up ifnet structure */
2177fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2178fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2179fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2180fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2181fc74a9f9SBrooks Davis 		error = ENXIO;
2182fc74a9f9SBrooks Davis 		goto fail;
2183fc74a9f9SBrooks Davis 	}
218495d67482SBill Paul 	ifp->if_softc = sc;
21859bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
218695d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
218795d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
218895d67482SBill Paul 	ifp->if_start = bge_start;
218995d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
219095d67482SBill Paul 	ifp->if_init = bge_init;
219195d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
21924d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
21934d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
21944d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
219595d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2196d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
2197479b23b7SGleb Smirnoff 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM;
219895d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
219975719184SGleb Smirnoff #ifdef DEVICE_POLLING
220075719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
220175719184SGleb Smirnoff #endif
220295d67482SBill Paul 
2203a1d52896SBill Paul         /*
2204d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2205d375e524SGleb Smirnoff 	 * to hardware bugs.
2206d375e524SGleb Smirnoff 	 */
2207d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2208d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2209d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2210d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2211d375e524SGleb Smirnoff 	}
2212d375e524SGleb Smirnoff 
2213d375e524SGleb Smirnoff 	/*
2214a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
221541abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
221641abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
221741abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
221841abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
221941abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
222041abcc1bSPaul Saab 	 * SK-9D41.
2221a1d52896SBill Paul 	 */
222241abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
222341abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
222441abcc1bSPaul Saab 	else {
2225f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2226f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2227fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2228f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2229f6789fbaSPyun YongHyeon 			error = ENXIO;
2230f6789fbaSPyun YongHyeon 			goto fail;
2231f6789fbaSPyun YongHyeon 		}
223241abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
223341abcc1bSPaul Saab 	}
223441abcc1bSPaul Saab 
223541abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2236a1d52896SBill Paul 		sc->bge_tbi = 1;
2237a1d52896SBill Paul 
223895d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
223995d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
224095d67482SBill Paul 		sc->bge_tbi = 1;
224195d67482SBill Paul 
224295d67482SBill Paul 	if (sc->bge_tbi) {
224395d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
224495d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
224595d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
224695d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
224795d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
224895d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
224995d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2250da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
225195d67482SBill Paul 	} else {
225295d67482SBill Paul 		/*
225395d67482SBill Paul 		 * Do transceiver setup.
225495d67482SBill Paul 		 */
225595d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
225695d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
2257fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
225895d67482SBill Paul 			bge_release_resources(sc);
225995d67482SBill Paul 			error = ENXIO;
226095d67482SBill Paul 			goto fail;
226195d67482SBill Paul 		}
226295d67482SBill Paul 	}
226395d67482SBill Paul 
226495d67482SBill Paul 	/*
2265e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2266e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2267e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2268e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2269e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2270e255b776SJohn Polstra 	 * payloads by copying the received packets.
2271e255b776SJohn Polstra 	 */
2272e0ced696SPaul Saab 	switch (sc->bge_chipid) {
2273e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_A0:
2274e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B0:
2275e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B2:
2276e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B5:
2277e255b776SJohn Polstra 		/* If in PCI-X mode, work around the alignment bug. */
2278e255b776SJohn Polstra 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
2279e255b776SJohn Polstra 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2280e255b776SJohn Polstra 		    BGE_PCISTATE_PCI_BUSSPEED)
2281e255b776SJohn Polstra 			sc->bge_rx_alignment_bug = 1;
2282e255b776SJohn Polstra 		break;
2283e255b776SJohn Polstra 	}
2284e255b776SJohn Polstra 
2285e255b776SJohn Polstra 	/*
228695d67482SBill Paul 	 * Call MI attach routine.
228795d67482SBill Paul 	 */
2288fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
22890f9bd73bSSam Leffler 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
22900f9bd73bSSam Leffler 
22910f9bd73bSSam Leffler 	/*
22920f9bd73bSSam Leffler 	 * Hookup IRQ last.
22930f9bd73bSSam Leffler 	 */
22940f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
22950f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
22960f9bd73bSSam Leffler 
22970f9bd73bSSam Leffler 	if (error) {
2298fc74a9f9SBrooks Davis 		bge_detach(dev);
2299fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
23000f9bd73bSSam Leffler 	}
230195d67482SBill Paul 
230295d67482SBill Paul fail:
230395d67482SBill Paul 	return(error);
230495d67482SBill Paul }
230595d67482SBill Paul 
230695d67482SBill Paul static int
230795d67482SBill Paul bge_detach(dev)
230895d67482SBill Paul 	device_t dev;
230995d67482SBill Paul {
231095d67482SBill Paul 	struct bge_softc *sc;
231195d67482SBill Paul 	struct ifnet *ifp;
231295d67482SBill Paul 
231395d67482SBill Paul 	sc = device_get_softc(dev);
2314fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
231595d67482SBill Paul 
231675719184SGleb Smirnoff #ifdef DEVICE_POLLING
231775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
231875719184SGleb Smirnoff 		ether_poll_deregister(ifp);
231975719184SGleb Smirnoff #endif
232075719184SGleb Smirnoff 
23210f9bd73bSSam Leffler 	BGE_LOCK(sc);
232295d67482SBill Paul 	bge_stop(sc);
232395d67482SBill Paul 	bge_reset(sc);
23240f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
23250f9bd73bSSam Leffler 
23260f9bd73bSSam Leffler 	ether_ifdetach(ifp);
232795d67482SBill Paul 
232895d67482SBill Paul 	if (sc->bge_tbi) {
232995d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
233095d67482SBill Paul 	} else {
233195d67482SBill Paul 		bus_generic_detach(dev);
233295d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
233395d67482SBill Paul 	}
233495d67482SBill Paul 
233595d67482SBill Paul 	bge_release_resources(sc);
233695d67482SBill Paul 
233795d67482SBill Paul 	return(0);
233895d67482SBill Paul }
233995d67482SBill Paul 
234095d67482SBill Paul static void
234195d67482SBill Paul bge_release_resources(sc)
234295d67482SBill Paul 	struct bge_softc *sc;
234395d67482SBill Paul {
234495d67482SBill Paul 	device_t dev;
234595d67482SBill Paul 
234695d67482SBill Paul 	dev = sc->bge_dev;
234795d67482SBill Paul 
234895d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
234995d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
235095d67482SBill Paul 
235195d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
235295d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
235395d67482SBill Paul 
235495d67482SBill Paul 	if (sc->bge_intrhand != NULL)
235595d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
235695d67482SBill Paul 
235795d67482SBill Paul 	if (sc->bge_irq != NULL)
235895d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
235995d67482SBill Paul 
236095d67482SBill Paul 	if (sc->bge_res != NULL)
236195d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
236295d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
236395d67482SBill Paul 
2364ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2365ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2366ad61f896SRuslan Ermilov 
2367f41ac2beSBill Paul 	bge_dma_free(sc);
236895d67482SBill Paul 
23690f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
23700f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
23710f9bd73bSSam Leffler 
237295d67482SBill Paul 	return;
237395d67482SBill Paul }
237495d67482SBill Paul 
237595d67482SBill Paul static void
237695d67482SBill Paul bge_reset(sc)
237795d67482SBill Paul 	struct bge_softc *sc;
237895d67482SBill Paul {
237995d67482SBill Paul 	device_t dev;
2380e53d81eeSPaul Saab 	u_int32_t cachesize, command, pcistate, reset;
238195d67482SBill Paul 	int i, val = 0;
238295d67482SBill Paul 
238395d67482SBill Paul 	dev = sc->bge_dev;
238495d67482SBill Paul 
238595d67482SBill Paul 	/* Save some important PCI state. */
238695d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
238795d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
238895d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
238995d67482SBill Paul 
239095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
239195d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2392e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
239395d67482SBill Paul 
2394e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2395e53d81eeSPaul Saab 
2396e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2397e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2398e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2399e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2400e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2401e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2402e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2403e53d81eeSPaul Saab 			reset |= (1<<29);
2404e53d81eeSPaul Saab 		}
2405e53d81eeSPaul Saab 	}
2406e53d81eeSPaul Saab 
240795d67482SBill Paul 	/* Issue global reset */
2408e53d81eeSPaul Saab 	bge_writereg_ind(sc, BGE_MISC_CFG, reset);
240995d67482SBill Paul 
241095d67482SBill Paul 	DELAY(1000);
241195d67482SBill Paul 
2412e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2413e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2414e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2415e53d81eeSPaul Saab 			uint32_t v;
2416e53d81eeSPaul Saab 
2417e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2418e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2419e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2420e53d81eeSPaul Saab 		}
2421e53d81eeSPaul Saab 		/* Set PCIE max payload size and clear error status. */
2422e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2423e53d81eeSPaul Saab 	}
2424e53d81eeSPaul Saab 
242595d67482SBill Paul 	/* Reset some of the PCI state that got zapped by reset */
242695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
242795d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2428e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
242995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
243095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
243195d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
243295d67482SBill Paul 
2433a7b0c314SPaul Saab 	/* Enable memory arbiter. */
24345dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2435e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
2436a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2437a7b0c314SPaul Saab 
243895d67482SBill Paul 	/*
243995d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
244095d67482SBill Paul 	 * general communications memory at 0xB50.
244195d67482SBill Paul 	 */
244295d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
244395d67482SBill Paul 	/*
244495d67482SBill Paul 	 * Poll the value location we just wrote until
244595d67482SBill Paul 	 * we see the 1's complement of the magic number.
244695d67482SBill Paul 	 * This indicates that the firmware initialization
244795d67482SBill Paul 	 * is complete.
244895d67482SBill Paul 	 */
244995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
245095d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
245195d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
245295d67482SBill Paul 			break;
245395d67482SBill Paul 		DELAY(10);
245495d67482SBill Paul 	}
245595d67482SBill Paul 
245695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2457fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "firmware handshake timed out\n");
245895d67482SBill Paul 		return;
245995d67482SBill Paul 	}
246095d67482SBill Paul 
246195d67482SBill Paul 	/*
246295d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
246395d67482SBill Paul 	 * return to its original pre-reset state. This is a
246495d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
246595d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
246695d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
246795d67482SBill Paul 	 * results.
246895d67482SBill Paul 	 */
246995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
247095d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
247195d67482SBill Paul 			break;
247295d67482SBill Paul 		DELAY(10);
247395d67482SBill Paul 	}
247495d67482SBill Paul 
247595d67482SBill Paul 	/* Fix up byte swapping */
2476e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
247795d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
247895d67482SBill Paul 
247995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
248095d67482SBill Paul 
2481da3003f0SBill Paul 	/*
2482da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2483da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2484da3003f0SBill Paul 	 * to 1.2V.
2485da3003f0SBill Paul 	 */
2486da3003f0SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 && sc->bge_tbi) {
2487da3003f0SBill Paul 		uint32_t serdescfg;
2488da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2489da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2490da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2491da3003f0SBill Paul 	}
2492da3003f0SBill Paul 
2493e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2494e53d81eeSPaul Saab 	if (sc->bge_pcie && sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2495e53d81eeSPaul Saab 		uint32_t v;
2496e53d81eeSPaul Saab 
2497e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2498e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2499e53d81eeSPaul Saab 	}
250095d67482SBill Paul 	DELAY(10000);
250195d67482SBill Paul 
250295d67482SBill Paul 	return;
250395d67482SBill Paul }
250495d67482SBill Paul 
250595d67482SBill Paul /*
250695d67482SBill Paul  * Frame reception handling. This is called if there's a frame
250795d67482SBill Paul  * on the receive return list.
250895d67482SBill Paul  *
250995d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
25101be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
251195d67482SBill Paul  * 2) the frame is from the standard receive ring
251295d67482SBill Paul  */
251395d67482SBill Paul 
251495d67482SBill Paul static void
251595d67482SBill Paul bge_rxeof(sc)
251695d67482SBill Paul 	struct bge_softc *sc;
251795d67482SBill Paul {
251895d67482SBill Paul 	struct ifnet *ifp;
251995d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
252095d67482SBill Paul 
25210f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
25220f9bd73bSSam Leffler 
2523cfcb5025SOleg Bulyzhin 	/* Nothing to do */
2524cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
2525cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
2526cfcb5025SOleg Bulyzhin 		return;
2527cfcb5025SOleg Bulyzhin 
2528fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
252995d67482SBill Paul 
2530f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2531e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2532f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2533f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
25345dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2535e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2536f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2537f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2538f41ac2beSBill Paul 		    BUS_DMASYNC_POSTREAD);
2539f41ac2beSBill Paul 	}
2540f41ac2beSBill Paul 
254195d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2542f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
254395d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
254495d67482SBill Paul 		u_int32_t		rxidx;
254595d67482SBill Paul 		struct mbuf		*m = NULL;
254695d67482SBill Paul 		u_int16_t		vlan_tag = 0;
254795d67482SBill Paul 		int			have_tag = 0;
254895d67482SBill Paul 
254975719184SGleb Smirnoff #ifdef DEVICE_POLLING
255075719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
255175719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
255275719184SGleb Smirnoff 				break;
255375719184SGleb Smirnoff 			sc->rxcycles--;
255475719184SGleb Smirnoff 		}
255575719184SGleb Smirnoff #endif
255675719184SGleb Smirnoff 
255795d67482SBill Paul 		cur_rx =
2558f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
255995d67482SBill Paul 
256095d67482SBill Paul 		rxidx = cur_rx->bge_idx;
25610434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
256295d67482SBill Paul 
256395d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
256495d67482SBill Paul 			have_tag = 1;
256595d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
256695d67482SBill Paul 		}
256795d67482SBill Paul 
256895d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
256995d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2570f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2571f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2572f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2573f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2574f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
257595d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
257695d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
257795d67482SBill Paul 			jumbocnt++;
257895d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
257995d67482SBill Paul 				ifp->if_ierrors++;
258095d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
258195d67482SBill Paul 				continue;
258295d67482SBill Paul 			}
258395d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
258495d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
258595d67482SBill Paul 				ifp->if_ierrors++;
258695d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
258795d67482SBill Paul 				continue;
258895d67482SBill Paul 			}
258995d67482SBill Paul 		} else {
259095d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2591f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2592f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2593f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2594f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2595f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
259695d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
259795d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
259895d67482SBill Paul 			stdcnt++;
259995d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
260095d67482SBill Paul 				ifp->if_ierrors++;
260195d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
260295d67482SBill Paul 				continue;
260395d67482SBill Paul 			}
260495d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
260595d67482SBill Paul 			    NULL) == ENOBUFS) {
260695d67482SBill Paul 				ifp->if_ierrors++;
260795d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
260895d67482SBill Paul 				continue;
260995d67482SBill Paul 			}
261095d67482SBill Paul 		}
261195d67482SBill Paul 
261295d67482SBill Paul 		ifp->if_ipackets++;
2613e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2614e255b776SJohn Polstra 		/*
2615e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2616e65bed95SPyun YongHyeon 		 * the payload is aligned.
2617e255b776SJohn Polstra 		 */
2618e255b776SJohn Polstra 		if (sc->bge_rx_alignment_bug) {
2619e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2620e255b776SJohn Polstra 			    cur_rx->bge_len);
2621e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2622e255b776SJohn Polstra 		}
2623e255b776SJohn Polstra #endif
2624473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
262595d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
262695d67482SBill Paul 
2627b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
262878178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
262995d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
263095d67482SBill Paul 				if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
263195d67482SBill Paul 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
263278178cd1SGleb Smirnoff 			}
2633d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2634d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
263595d67482SBill Paul 				m->m_pkthdr.csum_data =
263695d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
2637ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
2638ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
263995d67482SBill Paul 			}
264095d67482SBill Paul 		}
264195d67482SBill Paul 
264295d67482SBill Paul 		/*
2643673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2644673d9191SSam Leffler 		 * attach that information to the packet.
264595d67482SBill Paul 		 */
2646d147662cSGleb Smirnoff 		if (have_tag) {
2647d147662cSGleb Smirnoff 			VLAN_INPUT_TAG(ifp, m, vlan_tag);
2648d147662cSGleb Smirnoff 			if (m == NULL)
2649d147662cSGleb Smirnoff 				continue;
2650d147662cSGleb Smirnoff 		}
265195d67482SBill Paul 
26520f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2653673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
26540f9bd73bSSam Leffler 		BGE_LOCK(sc);
265595d67482SBill Paul 	}
265695d67482SBill Paul 
2657e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2658f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2659e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
26605dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2661e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2662e65bed95SPyun YongHyeon 		if (jumbocnt > 0)
2663f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2664f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_ring_map,
2665e65bed95SPyun YongHyeon 			    BUS_DMASYNC_PREWRITE);
2666f41ac2beSBill Paul 	}
2667f41ac2beSBill Paul 
266895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
266995d67482SBill Paul 	if (stdcnt)
267095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
267195d67482SBill Paul 	if (jumbocnt)
267295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
267395d67482SBill Paul }
267495d67482SBill Paul 
267595d67482SBill Paul static void
267695d67482SBill Paul bge_txeof(sc)
267795d67482SBill Paul 	struct bge_softc *sc;
267895d67482SBill Paul {
267995d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
268095d67482SBill Paul 	struct ifnet *ifp;
268195d67482SBill Paul 
26820f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
26830f9bd73bSSam Leffler 
2684cfcb5025SOleg Bulyzhin 	/* Nothing to do */
2685cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
2686cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
2687cfcb5025SOleg Bulyzhin 		return;
2688cfcb5025SOleg Bulyzhin 
2689fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
269095d67482SBill Paul 
2691e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2692e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2693e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
269495d67482SBill Paul 	/*
269595d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
269695d67482SBill Paul 	 * frames that have been sent.
269795d67482SBill Paul 	 */
269895d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2699f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
270095d67482SBill Paul 		u_int32_t		idx = 0;
270195d67482SBill Paul 
270295d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2703f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
270495d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
270595d67482SBill Paul 			ifp->if_opackets++;
270695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2707e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2708e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2709e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2710f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2711f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2712e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2713e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
271495d67482SBill Paul 		}
271595d67482SBill Paul 		sc->bge_txcnt--;
271695d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
271795d67482SBill Paul 		ifp->if_timer = 0;
271895d67482SBill Paul 	}
271995d67482SBill Paul 
272095d67482SBill Paul 	if (cur_tx != NULL)
272113f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
272295d67482SBill Paul }
272395d67482SBill Paul 
272475719184SGleb Smirnoff #ifdef DEVICE_POLLING
272575719184SGleb Smirnoff static void
272675719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
272775719184SGleb Smirnoff {
272875719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
272975719184SGleb Smirnoff 
273075719184SGleb Smirnoff 	BGE_LOCK(sc);
273175719184SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
273275719184SGleb Smirnoff 		bge_poll_locked(ifp, cmd, count);
273375719184SGleb Smirnoff 	BGE_UNLOCK(sc);
273475719184SGleb Smirnoff }
273575719184SGleb Smirnoff 
273675719184SGleb Smirnoff static void
273775719184SGleb Smirnoff bge_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
273875719184SGleb Smirnoff {
273975719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
2740366454f2SOleg Bulyzhin 	uint32_t statusword;
274175719184SGleb Smirnoff 
274275719184SGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
274375719184SGleb Smirnoff 
2744dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2745e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2746dab5cd05SOleg Bulyzhin 
2747dab5cd05SOleg Bulyzhin 	statusword = atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
2748dab5cd05SOleg Bulyzhin 
2749dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2750e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2751366454f2SOleg Bulyzhin 
2752366454f2SOleg Bulyzhin 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS cmd */
2753366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2754366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
2755366454f2SOleg Bulyzhin 
2756366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
2757366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2758366454f2SOleg Bulyzhin 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) ||
2759366454f2SOleg Bulyzhin 		    sc->bge_link_evt || sc->bge_tbi)
2760366454f2SOleg Bulyzhin 			bge_link_upd(sc);
2761366454f2SOleg Bulyzhin 
2762366454f2SOleg Bulyzhin 	sc->rxcycles = count;
2763366454f2SOleg Bulyzhin 	bge_rxeof(sc);
2764366454f2SOleg Bulyzhin 	bge_txeof(sc);
2765366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2766366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
276775719184SGleb Smirnoff }
276875719184SGleb Smirnoff #endif /* DEVICE_POLLING */
276975719184SGleb Smirnoff 
277095d67482SBill Paul static void
277195d67482SBill Paul bge_intr(xsc)
277295d67482SBill Paul 	void *xsc;
277395d67482SBill Paul {
277495d67482SBill Paul 	struct bge_softc *sc;
277595d67482SBill Paul 	struct ifnet *ifp;
2776dab5cd05SOleg Bulyzhin 	uint32_t statusword;
277795d67482SBill Paul 
277895d67482SBill Paul 	sc = xsc;
2779f41ac2beSBill Paul 
27800f9bd73bSSam Leffler 	BGE_LOCK(sc);
27810f9bd73bSSam Leffler 
2782dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2783dab5cd05SOleg Bulyzhin 
278475719184SGleb Smirnoff #ifdef DEVICE_POLLING
278575719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
278675719184SGleb Smirnoff 		BGE_UNLOCK(sc);
278775719184SGleb Smirnoff 		return;
278875719184SGleb Smirnoff 	}
278975719184SGleb Smirnoff #endif
279075719184SGleb Smirnoff 
2791f30cbfc6SScott Long 	/*
2792f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
2793f30cbfc6SScott Long 	 */
2794f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
2795f41ac2beSBill Paul 
279695d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
279795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
279895d67482SBill Paul 
2799f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
2800f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2801f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2802f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2803f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2804f30cbfc6SScott Long 
28051f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
28061f313773SOleg Bulyzhin 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) ||
2807f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
2808dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
280995d67482SBill Paul 
281013f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
281195d67482SBill Paul 		/* Check RX return ring producer/consumer */
281295d67482SBill Paul 		bge_rxeof(sc);
281395d67482SBill Paul 
281495d67482SBill Paul 		/* Check TX ring producer/consumer */
281595d67482SBill Paul 		bge_txeof(sc);
281695d67482SBill Paul 	}
281795d67482SBill Paul 
281895d67482SBill Paul 	/* Re-enable interrupts. */
281995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
282095d67482SBill Paul 
282113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
282213f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
28230f9bd73bSSam Leffler 		bge_start_locked(ifp);
28240f9bd73bSSam Leffler 
28250f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
282695d67482SBill Paul 
282795d67482SBill Paul 	return;
282895d67482SBill Paul }
282995d67482SBill Paul 
283095d67482SBill Paul static void
28310f9bd73bSSam Leffler bge_tick_locked(sc)
283295d67482SBill Paul 	struct bge_softc *sc;
28330f9bd73bSSam Leffler {
283495d67482SBill Paul 	struct mii_data *mii = NULL;
283595d67482SBill Paul 
28360f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
283795d67482SBill Paul 
2838e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2839e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
28400434d1b8SBill Paul 		bge_stats_update_regs(sc);
28410434d1b8SBill Paul 	else
284295d67482SBill Paul 		bge_stats_update(sc);
284395d67482SBill Paul 
28441f313773SOleg Bulyzhin 	if (!sc->bge_tbi) {
284595d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
284695d67482SBill Paul 		mii_tick(mii);
28477b97099dSOleg Bulyzhin 	} else {
28487b97099dSOleg Bulyzhin 		/*
28497b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
28507b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
28517b97099dSOleg Bulyzhin 		 * and trigger interrupt.
28527b97099dSOleg Bulyzhin 		 */
28537b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
28547b97099dSOleg Bulyzhin 		/* In polling mode we poll link state in bge_poll_locked() */
28557b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
28567b97099dSOleg Bulyzhin #endif
28577b97099dSOleg Bulyzhin 		{
28587b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
28597b97099dSOleg Bulyzhin 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
28607b97099dSOleg Bulyzhin 		}
2861dab5cd05SOleg Bulyzhin 	}
286295d67482SBill Paul 
2863dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
286495d67482SBill Paul }
286595d67482SBill Paul 
286695d67482SBill Paul static void
28670f9bd73bSSam Leffler bge_tick(xsc)
28680f9bd73bSSam Leffler 	void *xsc;
28690f9bd73bSSam Leffler {
28700f9bd73bSSam Leffler 	struct bge_softc *sc;
28710f9bd73bSSam Leffler 
28720f9bd73bSSam Leffler 	sc = xsc;
28730f9bd73bSSam Leffler 
28740f9bd73bSSam Leffler 	BGE_LOCK(sc);
28750f9bd73bSSam Leffler 	bge_tick_locked(sc);
28760f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
28770f9bd73bSSam Leffler }
28780f9bd73bSSam Leffler 
28790f9bd73bSSam Leffler static void
28800434d1b8SBill Paul bge_stats_update_regs(sc)
28810434d1b8SBill Paul 	struct bge_softc *sc;
28820434d1b8SBill Paul {
28830434d1b8SBill Paul 	struct ifnet *ifp;
28840434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
28850434d1b8SBill Paul 	u_int32_t *s;
28866fb34dd2SOleg Bulyzhin 	u_long cnt;			/* current register value */
28870434d1b8SBill Paul 	int i;
28880434d1b8SBill Paul 
2889fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
28900434d1b8SBill Paul 
28910434d1b8SBill Paul 	s = (u_int32_t *)&stats;
28920434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
28930434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
28940434d1b8SBill Paul 		s++;
28950434d1b8SBill Paul 	}
28960434d1b8SBill Paul 
28976fb34dd2SOleg Bulyzhin 	cnt = stats.dot3StatsSingleCollisionFrames +
28980434d1b8SBill Paul 	    stats.dot3StatsMultipleCollisionFrames +
28990434d1b8SBill Paul 	    stats.dot3StatsExcessiveCollisions +
29006fb34dd2SOleg Bulyzhin 	    stats.dot3StatsLateCollisions;
29016fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
29026fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
29036fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
29040434d1b8SBill Paul }
29050434d1b8SBill Paul 
29060434d1b8SBill Paul static void
290795d67482SBill Paul bge_stats_update(sc)
290895d67482SBill Paul 	struct bge_softc *sc;
290995d67482SBill Paul {
291095d67482SBill Paul 	struct ifnet *ifp;
2911e907febfSPyun YongHyeon 	bus_size_t stats;
29126fb34dd2SOleg Bulyzhin 	u_long cnt;			/* current register value */
291395d67482SBill Paul 
2914fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
291595d67482SBill Paul 
2916e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2917e907febfSPyun YongHyeon 
2918e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
2919e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
292095d67482SBill Paul 
29216fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats,
29226fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsSingleCollisionFrames.bge_addr_lo);
29236fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
29246fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo);
29256fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
29266fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsExcessiveCollisions.bge_addr_lo);
29276fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
29286fb34dd2SOleg Bulyzhin 		txstats.dot3StatsLateCollisions.bge_addr_lo);
29296fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
29306fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
29316fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
29326fb34dd2SOleg Bulyzhin 
29336fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
29346fb34dd2SOleg Bulyzhin 	ifp->if_ierrors += cnt >= sc->bge_rx_discards ?
29356fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_rx_discards : cnt;
29366fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
29376fb34dd2SOleg Bulyzhin 
29386fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
29396fb34dd2SOleg Bulyzhin 	ifp->if_oerrors += cnt >= sc->bge_tx_discards ?
29406fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_discards : cnt;
29416fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
294295d67482SBill Paul 
2943e907febfSPyun YongHyeon #undef READ_STAT
294495d67482SBill Paul }
294595d67482SBill Paul 
294695d67482SBill Paul /*
2947d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
2948d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
2949d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
2950d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
2951d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
2952d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
2953d375e524SGleb Smirnoff  */
2954d375e524SGleb Smirnoff static __inline int
2955d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
2956d375e524SGleb Smirnoff {
2957d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
2958d375e524SGleb Smirnoff 	struct mbuf *last;
2959d375e524SGleb Smirnoff 
2960d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
2961d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
2962d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
2963d375e524SGleb Smirnoff 		last = m;
2964d375e524SGleb Smirnoff 	} else {
2965d375e524SGleb Smirnoff 		/*
2966d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
2967d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
2968d375e524SGleb Smirnoff 		 */
2969d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
2970d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
2971d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
2972d375e524SGleb Smirnoff 			struct mbuf *n;
2973d375e524SGleb Smirnoff 
2974d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
2975d375e524SGleb Smirnoff 			if (n == NULL)
2976d375e524SGleb Smirnoff 				return (ENOBUFS);
2977d375e524SGleb Smirnoff 			n->m_len = 0;
2978d375e524SGleb Smirnoff 			last->m_next = n;
2979d375e524SGleb Smirnoff 			last = n;
2980d375e524SGleb Smirnoff 		}
2981d375e524SGleb Smirnoff 	}
2982d375e524SGleb Smirnoff 
2983d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
2984d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
2985d375e524SGleb Smirnoff 	last->m_len += padlen;
2986d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
2987d375e524SGleb Smirnoff 
2988d375e524SGleb Smirnoff 	return (0);
2989d375e524SGleb Smirnoff }
2990d375e524SGleb Smirnoff 
2991d375e524SGleb Smirnoff /*
299295d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
299395d67482SBill Paul  * pointers to descriptors.
299495d67482SBill Paul  */
299595d67482SBill Paul static int
299695d67482SBill Paul bge_encap(sc, m_head, txidx)
299795d67482SBill Paul 	struct bge_softc *sc;
299895d67482SBill Paul 	struct mbuf *m_head;
29997e27542aSGleb Smirnoff 	uint32_t *txidx;
300095d67482SBill Paul {
30017e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
3002f41ac2beSBill Paul 	bus_dmamap_t		map;
30037e27542aSGleb Smirnoff 	struct bge_tx_bd	*d = NULL;
30047e27542aSGleb Smirnoff 	struct m_tag		*mtag;
30057e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
30067e27542aSGleb Smirnoff 	uint16_t		csum_flags = 0;
30077e27542aSGleb Smirnoff 	int			nsegs, i, error;
300895d67482SBill Paul 
300995d67482SBill Paul 	if (m_head->m_pkthdr.csum_flags) {
301095d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
301195d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3012d375e524SGleb Smirnoff 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
301395d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3014d375e524SGleb Smirnoff 			if (m_head->m_pkthdr.len < ETHER_MIN_NOPAD &&
3015d375e524SGleb Smirnoff 			    bge_cksum_pad(m_head) != 0)
3016d375e524SGleb Smirnoff 				return (ENOBUFS);
3017d375e524SGleb Smirnoff 		}
301895d67482SBill Paul 		if (m_head->m_flags & M_LASTFRAG)
301995d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
302095d67482SBill Paul 		else if (m_head->m_flags & M_FRAG)
302195d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
302295d67482SBill Paul 	}
302395d67482SBill Paul 
3024fc74a9f9SBrooks Davis 	mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m_head);
3025673d9191SSam Leffler 
30267e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
30277e27542aSGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map,
30287e27542aSGleb Smirnoff 	    m_head, segs, &nsegs, BUS_DMA_NOWAIT);
30297e27542aSGleb Smirnoff         if (error) {
30307e27542aSGleb Smirnoff 		if (error == EFBIG) {
30317e27542aSGleb Smirnoff 			struct mbuf *m0;
30327e27542aSGleb Smirnoff 
30337e27542aSGleb Smirnoff 			m0 = m_defrag(m_head, M_DONTWAIT);
30347e27542aSGleb Smirnoff 			if (m0 == NULL)
30357e27542aSGleb Smirnoff 				return (ENOBUFS);
30367e27542aSGleb Smirnoff 			m_head = m0;
30377e27542aSGleb Smirnoff 			error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag,
30387e27542aSGleb Smirnoff 			    map, m_head, segs, &nsegs, BUS_DMA_NOWAIT);
30397e27542aSGleb Smirnoff 		}
30407e27542aSGleb Smirnoff 		if (error)
30417e27542aSGleb Smirnoff 			return (error);
30427e27542aSGleb Smirnoff 	}
30437e27542aSGleb Smirnoff 
304495d67482SBill Paul 	/*
304595d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
304695d67482SBill Paul 	 * of the end of the ring.
304795d67482SBill Paul 	 */
30487e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
30497e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
305095d67482SBill Paul 		return (ENOBUFS);
30517e27542aSGleb Smirnoff 	}
30527e27542aSGleb Smirnoff 
3053e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3054e65bed95SPyun YongHyeon 
30557e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
30567e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
30577e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
30587e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
30597e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
30607e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
30617e27542aSGleb Smirnoff 		if (i == nsegs - 1)
30627e27542aSGleb Smirnoff 			break;
30637e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
30647e27542aSGleb Smirnoff 	}
30657e27542aSGleb Smirnoff 
30667e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
30677e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
30687e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
30697e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
30707e27542aSGleb Smirnoff 	if (mtag != NULL) {
30717e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
30727e27542aSGleb Smirnoff 		d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
30737e27542aSGleb Smirnoff 	} else
30747e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
3075f41ac2beSBill Paul 
3076f41ac2beSBill Paul 	/*
3077f41ac2beSBill Paul 	 * Insure that the map for this transmission
3078f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3079f41ac2beSBill Paul 	 * in this chain.
3080f41ac2beSBill Paul 	 */
30817e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
30827e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
30837e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m_head;
30847e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
308595d67482SBill Paul 
30867e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
30877e27542aSGleb Smirnoff 	*txidx = idx;
308895d67482SBill Paul 
308995d67482SBill Paul 	return (0);
309095d67482SBill Paul }
309195d67482SBill Paul 
309295d67482SBill Paul /*
309395d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
309495d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
309595d67482SBill Paul  */
309695d67482SBill Paul static void
30970f9bd73bSSam Leffler bge_start_locked(ifp)
309895d67482SBill Paul 	struct ifnet *ifp;
309995d67482SBill Paul {
310095d67482SBill Paul 	struct bge_softc *sc;
310195d67482SBill Paul 	struct mbuf *m_head = NULL;
310214bbd30fSGleb Smirnoff 	uint32_t prodidx;
3103303a718cSDag-Erling Smørgrav 	int count = 0;
310495d67482SBill Paul 
310595d67482SBill Paul 	sc = ifp->if_softc;
310695d67482SBill Paul 
3107dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
310895d67482SBill Paul 		return;
310995d67482SBill Paul 
311014bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
311195d67482SBill Paul 
311295d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
31134d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
311495d67482SBill Paul 		if (m_head == NULL)
311595d67482SBill Paul 			break;
311695d67482SBill Paul 
311795d67482SBill Paul 		/*
311895d67482SBill Paul 		 * XXX
3119b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3120b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3121b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3122b874fdd4SYaroslav Tykhiy 		 *
3123b874fdd4SYaroslav Tykhiy 		 * XXX
312495d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
312595d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
312695d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
312795d67482SBill Paul 		 * chain at once.
312895d67482SBill Paul 		 * (paranoia -- may not actually be needed)
312995d67482SBill Paul 		 */
313095d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
313195d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
313295d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
313395d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
31344d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
313513f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
313695d67482SBill Paul 				break;
313795d67482SBill Paul 			}
313895d67482SBill Paul 		}
313995d67482SBill Paul 
314095d67482SBill Paul 		/*
314195d67482SBill Paul 		 * Pack the data into the transmit ring. If we
314295d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
314395d67482SBill Paul 		 * for the NIC to drain the ring.
314495d67482SBill Paul 		 */
314595d67482SBill Paul 		if (bge_encap(sc, m_head, &prodidx)) {
31464d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
314713f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
314895d67482SBill Paul 			break;
314995d67482SBill Paul 		}
3150303a718cSDag-Erling Smørgrav 		++count;
315195d67482SBill Paul 
315295d67482SBill Paul 		/*
315395d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
315495d67482SBill Paul 		 * to him.
315595d67482SBill Paul 		 */
3156673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
315795d67482SBill Paul 	}
315895d67482SBill Paul 
3159303a718cSDag-Erling Smørgrav 	if (count == 0) {
3160303a718cSDag-Erling Smørgrav 		/* no packets were dequeued */
3161303a718cSDag-Erling Smørgrav 		return;
3162303a718cSDag-Erling Smørgrav 	}
3163303a718cSDag-Erling Smørgrav 
316495d67482SBill Paul 	/* Transmit */
316595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
31663927098fSPaul Saab 	/* 5700 b2 errata */
3167e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
31683927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
316995d67482SBill Paul 
317014bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
317114bbd30fSGleb Smirnoff 
317295d67482SBill Paul 	/*
317395d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
317495d67482SBill Paul 	 */
317595d67482SBill Paul 	ifp->if_timer = 5;
317695d67482SBill Paul 
317795d67482SBill Paul 	return;
317895d67482SBill Paul }
317995d67482SBill Paul 
31800f9bd73bSSam Leffler /*
31810f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
31820f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
31830f9bd73bSSam Leffler  */
318495d67482SBill Paul static void
31850f9bd73bSSam Leffler bge_start(ifp)
31860f9bd73bSSam Leffler 	struct ifnet *ifp;
318795d67482SBill Paul {
31880f9bd73bSSam Leffler 	struct bge_softc *sc;
31890f9bd73bSSam Leffler 
31900f9bd73bSSam Leffler 	sc = ifp->if_softc;
31910f9bd73bSSam Leffler 	BGE_LOCK(sc);
31920f9bd73bSSam Leffler 	bge_start_locked(ifp);
31930f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
31940f9bd73bSSam Leffler }
31950f9bd73bSSam Leffler 
31960f9bd73bSSam Leffler static void
31970f9bd73bSSam Leffler bge_init_locked(sc)
31980f9bd73bSSam Leffler 	struct bge_softc *sc;
31990f9bd73bSSam Leffler {
320095d67482SBill Paul 	struct ifnet *ifp;
320195d67482SBill Paul 	u_int16_t *m;
320295d67482SBill Paul 
32030f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
320495d67482SBill Paul 
3205fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
320695d67482SBill Paul 
320713f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
320895d67482SBill Paul 		return;
320995d67482SBill Paul 
321095d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
321195d67482SBill Paul 	bge_stop(sc);
321295d67482SBill Paul 	bge_reset(sc);
321395d67482SBill Paul 	bge_chipinit(sc);
321495d67482SBill Paul 
321595d67482SBill Paul 	/*
321695d67482SBill Paul 	 * Init the various state machines, ring
321795d67482SBill Paul 	 * control blocks and firmware.
321895d67482SBill Paul 	 */
321995d67482SBill Paul 	if (bge_blockinit(sc)) {
3220fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
322195d67482SBill Paul 		return;
322295d67482SBill Paul 	}
322395d67482SBill Paul 
3224fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
322595d67482SBill Paul 
322695d67482SBill Paul 	/* Specify MTU. */
322795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3228859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
322995d67482SBill Paul 
323095d67482SBill Paul 	/* Load our MAC address. */
32314a0d6638SRuslan Ermilov 	m = (u_int16_t *)IF_LLADDR(sc->bge_ifp);
323295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
323395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
323495d67482SBill Paul 
323595d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
323695d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
323795d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
323895d67482SBill Paul 	} else {
323995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
324095d67482SBill Paul 	}
324195d67482SBill Paul 
324295d67482SBill Paul 	/* Program multicast filter. */
324395d67482SBill Paul 	bge_setmulti(sc);
324495d67482SBill Paul 
324595d67482SBill Paul 	/* Init RX ring. */
324695d67482SBill Paul 	bge_init_rx_ring_std(sc);
324795d67482SBill Paul 
32480434d1b8SBill Paul 	/*
32490434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
32500434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
32510434d1b8SBill Paul 	 * entry of the ring.
32520434d1b8SBill Paul 	 */
32530434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
32540434d1b8SBill Paul 		u_int32_t		v, i;
32550434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
32560434d1b8SBill Paul 			DELAY(20);
32570434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
32580434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
32590434d1b8SBill Paul 				break;
32600434d1b8SBill Paul 		}
32610434d1b8SBill Paul 		if (i == 10)
3262fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3263fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
32640434d1b8SBill Paul 	}
32650434d1b8SBill Paul 
326695d67482SBill Paul 	/* Init jumbo RX ring. */
326795d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
326895d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
326995d67482SBill Paul 
327095d67482SBill Paul 	/* Init our RX return ring index */
327195d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
327295d67482SBill Paul 
327395d67482SBill Paul 	/* Init TX ring. */
327495d67482SBill Paul 	bge_init_tx_ring(sc);
327595d67482SBill Paul 
327695d67482SBill Paul 	/* Turn on transmitter */
327795d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
327895d67482SBill Paul 
327995d67482SBill Paul 	/* Turn on receiver */
328095d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
328195d67482SBill Paul 
328295d67482SBill Paul 	/* Tell firmware we're alive. */
328395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
328495d67482SBill Paul 
328575719184SGleb Smirnoff #ifdef DEVICE_POLLING
328675719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
328775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
328875719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
328975719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
329075719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
329175719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
329275719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
329375719184SGleb Smirnoff 	} else
329475719184SGleb Smirnoff #endif
329575719184SGleb Smirnoff 
329695d67482SBill Paul 	/* Enable host interrupts. */
329775719184SGleb Smirnoff 	{
329895d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
329995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
330095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
330175719184SGleb Smirnoff 	}
330295d67482SBill Paul 
330395d67482SBill Paul 	bge_ifmedia_upd(ifp);
330495d67482SBill Paul 
330513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
330613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
330795d67482SBill Paul 
33080f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
33090f9bd73bSSam Leffler }
33100f9bd73bSSam Leffler 
33110f9bd73bSSam Leffler static void
33120f9bd73bSSam Leffler bge_init(xsc)
33130f9bd73bSSam Leffler 	void *xsc;
33140f9bd73bSSam Leffler {
33150f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
33160f9bd73bSSam Leffler 
33170f9bd73bSSam Leffler 	BGE_LOCK(sc);
33180f9bd73bSSam Leffler 	bge_init_locked(sc);
33190f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
332095d67482SBill Paul 
332195d67482SBill Paul 	return;
332295d67482SBill Paul }
332395d67482SBill Paul 
332495d67482SBill Paul /*
332595d67482SBill Paul  * Set media options.
332695d67482SBill Paul  */
332795d67482SBill Paul static int
332895d67482SBill Paul bge_ifmedia_upd(ifp)
332995d67482SBill Paul 	struct ifnet *ifp;
333095d67482SBill Paul {
333195d67482SBill Paul 	struct bge_softc *sc;
333295d67482SBill Paul 	struct mii_data *mii;
333395d67482SBill Paul 	struct ifmedia *ifm;
333495d67482SBill Paul 
333595d67482SBill Paul 	sc = ifp->if_softc;
333695d67482SBill Paul 	ifm = &sc->bge_ifmedia;
333795d67482SBill Paul 
333895d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
333995d67482SBill Paul 	if (sc->bge_tbi) {
334095d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
334195d67482SBill Paul 			return(EINVAL);
334295d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
334395d67482SBill Paul 		case IFM_AUTO:
3344ff50922bSDoug White #ifndef BGE_FAKE_AUTONEG
3345ff50922bSDoug White 			/*
3346ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3347ff50922bSDoug White 			 * mechanism for programming the autoneg
3348ff50922bSDoug White 			 * advertisement registers in TBI mode.
3349ff50922bSDoug White 			 */
3350ff50922bSDoug White 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3351ff50922bSDoug White 				uint32_t sgdig;
3352ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3353ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3354ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3355ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3356ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3357ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3358ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3359ff50922bSDoug White 				DELAY(5);
3360ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3361ff50922bSDoug White 			}
3362ff50922bSDoug White #endif
336395d67482SBill Paul 			break;
336495d67482SBill Paul 		case IFM_1000_SX:
336595d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
336695d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
336795d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
336895d67482SBill Paul 			} else {
336995d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
337095d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
337195d67482SBill Paul 			}
337295d67482SBill Paul 			break;
337395d67482SBill Paul 		default:
337495d67482SBill Paul 			return(EINVAL);
337595d67482SBill Paul 		}
337695d67482SBill Paul 		return(0);
337795d67482SBill Paul 	}
337895d67482SBill Paul 
33791493e883SOleg Bulyzhin 	sc->bge_link_evt++;
338095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
338195d67482SBill Paul 	if (mii->mii_instance) {
338295d67482SBill Paul 		struct mii_softc *miisc;
338395d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
338495d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
338595d67482SBill Paul 			mii_phy_reset(miisc);
338695d67482SBill Paul 	}
338795d67482SBill Paul 	mii_mediachg(mii);
338895d67482SBill Paul 
338995d67482SBill Paul 	return(0);
339095d67482SBill Paul }
339195d67482SBill Paul 
339295d67482SBill Paul /*
339395d67482SBill Paul  * Report current media status.
339495d67482SBill Paul  */
339595d67482SBill Paul static void
339695d67482SBill Paul bge_ifmedia_sts(ifp, ifmr)
339795d67482SBill Paul 	struct ifnet *ifp;
339895d67482SBill Paul 	struct ifmediareq *ifmr;
339995d67482SBill Paul {
340095d67482SBill Paul 	struct bge_softc *sc;
340195d67482SBill Paul 	struct mii_data *mii;
340295d67482SBill Paul 
340395d67482SBill Paul 	sc = ifp->if_softc;
340495d67482SBill Paul 
340595d67482SBill Paul 	if (sc->bge_tbi) {
340695d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
340795d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
340895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
340995d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
341095d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
341195d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
341295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
341395d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
341495d67482SBill Paul 		else
341595d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
341695d67482SBill Paul 		return;
341795d67482SBill Paul 	}
341895d67482SBill Paul 
341995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
342095d67482SBill Paul 	mii_pollstat(mii);
342195d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
342295d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
342395d67482SBill Paul 
342495d67482SBill Paul 	return;
342595d67482SBill Paul }
342695d67482SBill Paul 
342795d67482SBill Paul static int
342895d67482SBill Paul bge_ioctl(ifp, command, data)
342995d67482SBill Paul 	struct ifnet *ifp;
343095d67482SBill Paul 	u_long command;
343195d67482SBill Paul 	caddr_t data;
343295d67482SBill Paul {
343395d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
343495d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
34350f9bd73bSSam Leffler 	int mask, error = 0;
343695d67482SBill Paul 	struct mii_data *mii;
343795d67482SBill Paul 
343895d67482SBill Paul 	switch(command) {
343995d67482SBill Paul 	case SIOCSIFMTU:
34400434d1b8SBill Paul 		/* Disallow jumbo frames on 5705. */
3441e53d81eeSPaul Saab 		if (((sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
3442e53d81eeSPaul Saab 		      sc->bge_asicrev == BGE_ASICREV_BCM5750) &&
34430434d1b8SBill Paul 		    ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU)
344495d67482SBill Paul 			error = EINVAL;
344595d67482SBill Paul 		else {
344695d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
344713f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
344895d67482SBill Paul 			bge_init(sc);
344995d67482SBill Paul 		}
345095d67482SBill Paul 		break;
345195d67482SBill Paul 	case SIOCSIFFLAGS:
34520f9bd73bSSam Leffler 		BGE_LOCK(sc);
345395d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
345495d67482SBill Paul 			/*
345595d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
345695d67482SBill Paul 			 * then just use the 'set promisc mode' command
345795d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
345895d67482SBill Paul 			 * a full re-init means reloading the firmware and
345995d67482SBill Paul 			 * waiting for it to start up, which may take a
3460d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
346195d67482SBill Paul 			 */
346213f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
346395d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
346495d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
346595d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
346695d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
346713f4c340SRobert Watson 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
346895d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
346995d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
347095d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
347195d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
3472d183af7fSRuslan Ermilov 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3473d183af7fSRuslan Ermilov 			    (ifp->if_flags ^ sc->bge_if_flags) & IFF_ALLMULTI) {
3474d183af7fSRuslan Ermilov 				bge_setmulti(sc);
347595d67482SBill Paul 			} else
34760f9bd73bSSam Leffler 				bge_init_locked(sc);
347795d67482SBill Paul 		} else {
347813f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
347995d67482SBill Paul 				bge_stop(sc);
348095d67482SBill Paul 			}
348195d67482SBill Paul 		}
348295d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
34830f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
348495d67482SBill Paul 		error = 0;
348595d67482SBill Paul 		break;
348695d67482SBill Paul 	case SIOCADDMULTI:
348795d67482SBill Paul 	case SIOCDELMULTI:
348813f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
34890f9bd73bSSam Leffler 			BGE_LOCK(sc);
349095d67482SBill Paul 			bge_setmulti(sc);
34910f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
349295d67482SBill Paul 			error = 0;
349395d67482SBill Paul 		}
349495d67482SBill Paul 		break;
349595d67482SBill Paul 	case SIOCSIFMEDIA:
349695d67482SBill Paul 	case SIOCGIFMEDIA:
349795d67482SBill Paul 		if (sc->bge_tbi) {
349895d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
349995d67482SBill Paul 			    &sc->bge_ifmedia, command);
350095d67482SBill Paul 		} else {
350195d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
350295d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
350395d67482SBill Paul 			    &mii->mii_media, command);
350495d67482SBill Paul 		}
350595d67482SBill Paul 		break;
350695d67482SBill Paul 	case SIOCSIFCAP:
350795d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
350875719184SGleb Smirnoff #ifdef DEVICE_POLLING
350975719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
351075719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
351175719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
351275719184SGleb Smirnoff 				if (error)
351375719184SGleb Smirnoff 					return(error);
351475719184SGleb Smirnoff 				BGE_LOCK(sc);
351575719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
351675719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
351775719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
351875719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
351975719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
352075719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
352175719184SGleb Smirnoff 				BGE_UNLOCK(sc);
352275719184SGleb Smirnoff 			} else {
352375719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
352475719184SGleb Smirnoff 				/* Enable interrupt even in error case */
352575719184SGleb Smirnoff 				BGE_LOCK(sc);
352675719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
352775719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
352875719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
352975719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
353075719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
353175719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
353275719184SGleb Smirnoff 				BGE_UNLOCK(sc);
353375719184SGleb Smirnoff 			}
353475719184SGleb Smirnoff 		}
353575719184SGleb Smirnoff #endif
3536d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3537d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3538d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3539d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3540b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
354195d67482SBill Paul 			else
3542b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
3543479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
354495d67482SBill Paul 		}
354595d67482SBill Paul 		break;
354695d67482SBill Paul 	default:
3547673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
354895d67482SBill Paul 		break;
354995d67482SBill Paul 	}
355095d67482SBill Paul 
355195d67482SBill Paul 	return(error);
355295d67482SBill Paul }
355395d67482SBill Paul 
355495d67482SBill Paul static void
355595d67482SBill Paul bge_watchdog(ifp)
355695d67482SBill Paul 	struct ifnet *ifp;
355795d67482SBill Paul {
355895d67482SBill Paul 	struct bge_softc *sc;
355995d67482SBill Paul 
356095d67482SBill Paul 	sc = ifp->if_softc;
356195d67482SBill Paul 
3562fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
356395d67482SBill Paul 
356413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
356595d67482SBill Paul 	bge_init(sc);
356695d67482SBill Paul 
356795d67482SBill Paul 	ifp->if_oerrors++;
356895d67482SBill Paul 
356995d67482SBill Paul 	return;
357095d67482SBill Paul }
357195d67482SBill Paul 
357295d67482SBill Paul /*
357395d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
357495d67482SBill Paul  * RX and TX lists.
357595d67482SBill Paul  */
357695d67482SBill Paul static void
357795d67482SBill Paul bge_stop(sc)
357895d67482SBill Paul 	struct bge_softc *sc;
357995d67482SBill Paul {
358095d67482SBill Paul 	struct ifnet *ifp;
358195d67482SBill Paul 	struct ifmedia_entry *ifm;
358295d67482SBill Paul 	struct mii_data *mii = NULL;
358395d67482SBill Paul 	int mtmp, itmp;
358495d67482SBill Paul 
35850f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
35860f9bd73bSSam Leffler 
3587fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
358895d67482SBill Paul 
358995d67482SBill Paul 	if (!sc->bge_tbi)
359095d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
359195d67482SBill Paul 
35920f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
359395d67482SBill Paul 
359495d67482SBill Paul 	/*
359595d67482SBill Paul 	 * Disable all of the receiver blocks
359695d67482SBill Paul 	 */
359795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
359895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
359995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
36005dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3601e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
360295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
360395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
360495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
360595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
360695d67482SBill Paul 
360795d67482SBill Paul 	/*
360895d67482SBill Paul 	 * Disable all of the transmit blocks
360995d67482SBill Paul 	 */
361095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
361195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
361295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
361395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
361495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
36155dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3616e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
361795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
361895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
361995d67482SBill Paul 
362095d67482SBill Paul 	/*
362195d67482SBill Paul 	 * Shut down all of the memory managers and related
362295d67482SBill Paul 	 * state machines.
362395d67482SBill Paul 	 */
362495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
362595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
36265dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3627e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
362895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
362995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
363095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
36315dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3632e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
363395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
363495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
36350434d1b8SBill Paul 	}
363695d67482SBill Paul 
363795d67482SBill Paul 	/* Disable host interrupts. */
363895d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
363995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
364095d67482SBill Paul 
364195d67482SBill Paul 	/*
364295d67482SBill Paul 	 * Tell firmware we're shutting down.
364395d67482SBill Paul 	 */
364495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
364595d67482SBill Paul 
364695d67482SBill Paul 	/* Free the RX lists. */
364795d67482SBill Paul 	bge_free_rx_ring_std(sc);
364895d67482SBill Paul 
364995d67482SBill Paul 	/* Free jumbo RX list. */
36505dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3651e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
365295d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
365395d67482SBill Paul 
365495d67482SBill Paul 	/* Free TX buffers. */
365595d67482SBill Paul 	bge_free_tx_ring(sc);
365695d67482SBill Paul 
365795d67482SBill Paul 	/*
365895d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
365995d67482SBill Paul 	 * unchanged so that things will be put back to normal when
366095d67482SBill Paul 	 * we bring the interface back up.
366195d67482SBill Paul 	 */
366295d67482SBill Paul 	if (!sc->bge_tbi) {
366395d67482SBill Paul 		itmp = ifp->if_flags;
366495d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3665dcc34049SPawel Jakub Dawidek 		/*
3666dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3667dcc34049SPawel Jakub Dawidek 		 */
3668dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
366995d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
367095d67482SBill Paul 			mtmp = ifm->ifm_media;
367195d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
367295d67482SBill Paul 			mii_mediachg(mii);
367395d67482SBill Paul 			ifm->ifm_media = mtmp;
3674dcc34049SPawel Jakub Dawidek 		}
367595d67482SBill Paul 		ifp->if_flags = itmp;
367695d67482SBill Paul 	}
367795d67482SBill Paul 
367895d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
367995d67482SBill Paul 
36801493e883SOleg Bulyzhin 	/*
36811493e883SOleg Bulyzhin 	 * We can't just call bge_link_upd() cause chip is almost stopped so
36821493e883SOleg Bulyzhin 	 * bge_link_upd -> bge_tick_locked -> bge_stats_update sequence may
36831493e883SOleg Bulyzhin 	 * lead to hardware deadlock. So we just clearing MAC's link state
36841493e883SOleg Bulyzhin 	 * (PHY may still have link UP).
36851493e883SOleg Bulyzhin 	 */
36861493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
36871493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
36881493e883SOleg Bulyzhin 	sc->bge_link = 0;
368995d67482SBill Paul 
36901493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
369195d67482SBill Paul }
369295d67482SBill Paul 
369395d67482SBill Paul /*
369495d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
369595d67482SBill Paul  * get confused by errant DMAs when rebooting.
369695d67482SBill Paul  */
369795d67482SBill Paul static void
369895d67482SBill Paul bge_shutdown(dev)
369995d67482SBill Paul 	device_t dev;
370095d67482SBill Paul {
370195d67482SBill Paul 	struct bge_softc *sc;
370295d67482SBill Paul 
370395d67482SBill Paul 	sc = device_get_softc(dev);
370495d67482SBill Paul 
37050f9bd73bSSam Leffler 	BGE_LOCK(sc);
370695d67482SBill Paul 	bge_stop(sc);
370795d67482SBill Paul 	bge_reset(sc);
37080f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
370995d67482SBill Paul 
371095d67482SBill Paul 	return;
371195d67482SBill Paul }
371214afefa3SPawel Jakub Dawidek 
371314afefa3SPawel Jakub Dawidek static int
371414afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
371514afefa3SPawel Jakub Dawidek {
371614afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
371714afefa3SPawel Jakub Dawidek 
371814afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
371914afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
372014afefa3SPawel Jakub Dawidek 	bge_stop(sc);
372114afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
372214afefa3SPawel Jakub Dawidek 
372314afefa3SPawel Jakub Dawidek 	return (0);
372414afefa3SPawel Jakub Dawidek }
372514afefa3SPawel Jakub Dawidek 
372614afefa3SPawel Jakub Dawidek static int
372714afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
372814afefa3SPawel Jakub Dawidek {
372914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
373014afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
373114afefa3SPawel Jakub Dawidek 
373214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
373314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
373414afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
373514afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
373614afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
373714afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
373814afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
373914afefa3SPawel Jakub Dawidek 	}
374014afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
374114afefa3SPawel Jakub Dawidek 
374214afefa3SPawel Jakub Dawidek 	return (0);
374314afefa3SPawel Jakub Dawidek }
3744dab5cd05SOleg Bulyzhin 
3745dab5cd05SOleg Bulyzhin static void
3746dab5cd05SOleg Bulyzhin bge_link_upd(sc)
3747dab5cd05SOleg Bulyzhin 	struct bge_softc *sc;
3748dab5cd05SOleg Bulyzhin {
37491f313773SOleg Bulyzhin 	struct mii_data *mii;
37501f313773SOleg Bulyzhin 	uint32_t link, status;
3751dab5cd05SOleg Bulyzhin 
3752dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
37531f313773SOleg Bulyzhin 
37547b97099dSOleg Bulyzhin 	/* Clear 'pending link event' flag */
37557b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
37567b97099dSOleg Bulyzhin 
3757dab5cd05SOleg Bulyzhin 	/*
3758dab5cd05SOleg Bulyzhin 	 * Process link state changes.
3759dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
3760dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
3761dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
3762dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
3763dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
3764dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
3765dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
3766dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
37671f313773SOleg Bulyzhin 	 *
37681f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
37691f313773SOleg Bulyzhin 	 * BGE_CHIPID_BCM5700_B1 can be used for others BCM5700 revisions.
3770dab5cd05SOleg Bulyzhin 	 */
3771dab5cd05SOleg Bulyzhin 
37721f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
37731f313773SOleg Bulyzhin 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) {
3774dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
3775dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
3776dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3777dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
37781f313773SOleg Bulyzhin 
37791f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
37801f313773SOleg Bulyzhin 			if (!sc->bge_link &&
37811f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
37821f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
37831f313773SOleg Bulyzhin 				sc->bge_link++;
37841f313773SOleg Bulyzhin 				if (bootverbose)
37851f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
37861f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
37871f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
37881f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
37891f313773SOleg Bulyzhin 				sc->bge_link = 0;
37901f313773SOleg Bulyzhin 				if (bootverbose)
37911f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
37921f313773SOleg Bulyzhin 			}
37931f313773SOleg Bulyzhin 
3794dab5cd05SOleg Bulyzhin 			/* Clear the interrupt */
3795dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
3796dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
3797dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
3798dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
3799dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
3800dab5cd05SOleg Bulyzhin 		}
3801dab5cd05SOleg Bulyzhin 		return;
3802dab5cd05SOleg Bulyzhin 	}
3803dab5cd05SOleg Bulyzhin 
38041f313773SOleg Bulyzhin 	if (sc->bge_tbi) {
38051f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
38067b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
38077b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
38081f313773SOleg Bulyzhin 				sc->bge_link++;
38091f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
38101f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
38111f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
38121f313773SOleg Bulyzhin 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
38131f313773SOleg Bulyzhin 				if (bootverbose)
38141f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
38157b97099dSOleg Bulyzhin 				if_link_state_change(sc->bge_ifp, LINK_STATE_UP);
38167b97099dSOleg Bulyzhin 			}
38171f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
3818dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
38191f313773SOleg Bulyzhin 			if (bootverbose)
38201f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
38217b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
38221f313773SOleg Bulyzhin 		}
38231493e883SOleg Bulyzhin 	/* Discard link events for MII/GMII cards if MI auto-polling disabled */
38241493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
38251f313773SOleg Bulyzhin 		/*
38261f313773SOleg Bulyzhin 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
38271f313773SOleg Bulyzhin 		 * in status word always set. Workaround this bug by reading
38281f313773SOleg Bulyzhin 		 * PHY link status directly.
38291f313773SOleg Bulyzhin 		 */
38301f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
38311f313773SOleg Bulyzhin 
38321f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
38331f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
3834dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3835dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
38361f313773SOleg Bulyzhin 
38371f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
38381f313773SOleg Bulyzhin 			if (!sc->bge_link &&
38391f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
38401f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
38411f313773SOleg Bulyzhin 				sc->bge_link++;
38421f313773SOleg Bulyzhin 				if (bootverbose)
38431f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
38441f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
38451f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
38461f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
38471f313773SOleg Bulyzhin 				sc->bge_link = 0;
38481f313773SOleg Bulyzhin 				if (bootverbose)
38491f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
38501f313773SOleg Bulyzhin 			}
38511f313773SOleg Bulyzhin 		}
3852dab5cd05SOleg Bulyzhin 	}
3853dab5cd05SOleg Bulyzhin 
38541493e883SOleg Bulyzhin 	/* Clear the attention */
3855dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
3856dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
3857dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
3858dab5cd05SOleg Bulyzhin }
3859