xref: /freebsd/sys/dev/bge/if_bge.c (revision c4529f416185f13ab20bda5ac8281f441ad682f9)
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 
309c4529f41SMichael Reifenberger static int bge_fake_autoneg = 0;
310c4529f41SMichael Reifenberger TUNABLE_INT("hw.bge.fake_autoneg", &bge_fake_autoneg);
311c4529f41SMichael Reifenberger 
31295d67482SBill Paul static u_int32_t
31395d67482SBill Paul bge_readmem_ind(sc, off)
31495d67482SBill Paul 	struct bge_softc *sc;
31595d67482SBill Paul 	int off;
31695d67482SBill Paul {
31795d67482SBill Paul 	device_t dev;
31895d67482SBill Paul 
31995d67482SBill Paul 	dev = sc->bge_dev;
32095d67482SBill Paul 
32195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
32295d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
32395d67482SBill Paul }
32495d67482SBill Paul 
32595d67482SBill Paul static void
32695d67482SBill Paul bge_writemem_ind(sc, off, val)
32795d67482SBill Paul 	struct bge_softc *sc;
32895d67482SBill Paul 	int off, val;
32995d67482SBill Paul {
33095d67482SBill Paul 	device_t dev;
33195d67482SBill Paul 
33295d67482SBill Paul 	dev = sc->bge_dev;
33395d67482SBill Paul 
33495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
33595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
33695d67482SBill Paul 
33795d67482SBill Paul 	return;
33895d67482SBill Paul }
33995d67482SBill Paul 
34095d67482SBill Paul #ifdef notdef
34195d67482SBill Paul static u_int32_t
34295d67482SBill Paul bge_readreg_ind(sc, off)
34395d67482SBill Paul 	struct bge_softc *sc;
34495d67482SBill Paul 	int off;
34595d67482SBill Paul {
34695d67482SBill Paul 	device_t dev;
34795d67482SBill Paul 
34895d67482SBill Paul 	dev = sc->bge_dev;
34995d67482SBill Paul 
35095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
35195d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
35295d67482SBill Paul }
35395d67482SBill Paul #endif
35495d67482SBill Paul 
35595d67482SBill Paul static void
35695d67482SBill Paul bge_writereg_ind(sc, off, val)
35795d67482SBill Paul 	struct bge_softc *sc;
35895d67482SBill Paul 	int off, val;
35995d67482SBill Paul {
36095d67482SBill Paul 	device_t dev;
36195d67482SBill Paul 
36295d67482SBill Paul 	dev = sc->bge_dev;
36395d67482SBill Paul 
36495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
36595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
36695d67482SBill Paul 
36795d67482SBill Paul 	return;
36895d67482SBill Paul }
36995d67482SBill Paul 
370f41ac2beSBill Paul /*
371f41ac2beSBill Paul  * Map a single buffer address.
372f41ac2beSBill Paul  */
373f41ac2beSBill Paul 
374f41ac2beSBill Paul static void
375f41ac2beSBill Paul bge_dma_map_addr(arg, segs, nseg, error)
376f41ac2beSBill Paul 	void *arg;
377f41ac2beSBill Paul 	bus_dma_segment_t *segs;
378f41ac2beSBill Paul 	int nseg;
379f41ac2beSBill Paul 	int error;
380f41ac2beSBill Paul {
381f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
382f41ac2beSBill Paul 
383f41ac2beSBill Paul 	if (error)
384f41ac2beSBill Paul 		return;
385f41ac2beSBill Paul 
386f41ac2beSBill Paul 	ctx = arg;
387f41ac2beSBill Paul 
388f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
389f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
390f41ac2beSBill Paul 		return;
391f41ac2beSBill Paul 	}
392f41ac2beSBill Paul 
393f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
394f41ac2beSBill Paul 
395f41ac2beSBill Paul 	return;
396f41ac2beSBill Paul }
397f41ac2beSBill Paul 
3981b4a3b2fSPeter Wemm #ifdef notdef
39995d67482SBill Paul static u_int8_t
40095d67482SBill Paul bge_vpd_readbyte(sc, addr)
40195d67482SBill Paul 	struct bge_softc *sc;
40295d67482SBill Paul 	int addr;
40395d67482SBill Paul {
40495d67482SBill Paul 	int i;
40595d67482SBill Paul 	device_t dev;
40695d67482SBill Paul 	u_int32_t val;
40795d67482SBill Paul 
40895d67482SBill Paul 	dev = sc->bge_dev;
40995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
41095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
41195d67482SBill Paul 		DELAY(10);
41295d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
41395d67482SBill Paul 			break;
41495d67482SBill Paul 	}
41595d67482SBill Paul 
41695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
417fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "VPD read timed out\n");
41895d67482SBill Paul 		return(0);
41995d67482SBill Paul 	}
42095d67482SBill Paul 
42195d67482SBill Paul 	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
42295d67482SBill Paul 
42395d67482SBill Paul 	return((val >> ((addr % 4) * 8)) & 0xFF);
42495d67482SBill Paul }
42595d67482SBill Paul 
42695d67482SBill Paul static void
42795d67482SBill Paul bge_vpd_read_res(sc, res, addr)
42895d67482SBill Paul 	struct bge_softc *sc;
42995d67482SBill Paul 	struct vpd_res *res;
43095d67482SBill Paul 	int addr;
43195d67482SBill Paul {
43295d67482SBill Paul 	int i;
43395d67482SBill Paul 	u_int8_t *ptr;
43495d67482SBill Paul 
43595d67482SBill Paul 	ptr = (u_int8_t *)res;
43695d67482SBill Paul 	for (i = 0; i < sizeof(struct vpd_res); i++)
43795d67482SBill Paul 		ptr[i] = bge_vpd_readbyte(sc, i + addr);
43895d67482SBill Paul 
43995d67482SBill Paul 	return;
44095d67482SBill Paul }
44195d67482SBill Paul 
44295d67482SBill Paul static void
44395d67482SBill Paul bge_vpd_read(sc)
44495d67482SBill Paul 	struct bge_softc *sc;
44595d67482SBill Paul {
44695d67482SBill Paul 	int pos = 0, i;
44795d67482SBill Paul 	struct vpd_res res;
44895d67482SBill Paul 
44995d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
45095d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
45195d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
45295d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
45395d67482SBill Paul 	sc->bge_vpd_prodname = NULL;
45495d67482SBill Paul 	sc->bge_vpd_readonly = NULL;
45595d67482SBill Paul 
45695d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
45795d67482SBill Paul 
45895d67482SBill Paul 	if (res.vr_id != VPD_RES_ID) {
459fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
460fe806fdaSPyun YongHyeon 		    "bad VPD resource id: expected %x got %x\n", VPD_RES_ID,
461fe806fdaSPyun YongHyeon 		    res.vr_id);
46295d67482SBill Paul 		return;
46395d67482SBill Paul 	}
46495d67482SBill Paul 
46595d67482SBill Paul 	pos += sizeof(res);
46695d67482SBill Paul 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
46795d67482SBill Paul 	for (i = 0; i < res.vr_len; i++)
46895d67482SBill Paul 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
46995d67482SBill Paul 	sc->bge_vpd_prodname[i] = '\0';
47095d67482SBill Paul 	pos += i;
47195d67482SBill Paul 
47295d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
47395d67482SBill Paul 
47495d67482SBill Paul 	if (res.vr_id != VPD_RES_READ) {
475fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
476fe806fdaSPyun YongHyeon 		    "bad VPD resource id: expected %x got %x\n", VPD_RES_READ,
477fe806fdaSPyun YongHyeon 		    res.vr_id);
47895d67482SBill Paul 		return;
47995d67482SBill Paul 	}
48095d67482SBill Paul 
48195d67482SBill Paul 	pos += sizeof(res);
48295d67482SBill Paul 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
48395d67482SBill Paul 	for (i = 0; i < res.vr_len + 1; i++)
48495d67482SBill Paul 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
48595d67482SBill Paul 
48695d67482SBill Paul 	return;
48795d67482SBill Paul }
4881b4a3b2fSPeter Wemm #endif
48995d67482SBill Paul 
49095d67482SBill Paul /*
49195d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
49295d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
49395d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
49495d67482SBill Paul  * access method.
49595d67482SBill Paul  */
49695d67482SBill Paul static u_int8_t
49795d67482SBill Paul bge_eeprom_getbyte(sc, addr, dest)
49895d67482SBill Paul 	struct bge_softc *sc;
49995d67482SBill Paul 	int addr;
50095d67482SBill Paul 	u_int8_t *dest;
50195d67482SBill Paul {
50295d67482SBill Paul 	int i;
50395d67482SBill Paul 	u_int32_t byte = 0;
50495d67482SBill Paul 
50595d67482SBill Paul 	/*
50695d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
50795d67482SBill Paul 	 * having to use the bitbang method.
50895d67482SBill Paul 	 */
50995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
51095d67482SBill Paul 
51195d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
51295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
51395d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
51495d67482SBill Paul 	DELAY(20);
51595d67482SBill Paul 
51695d67482SBill Paul 	/* Issue the read EEPROM command. */
51795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
51895d67482SBill Paul 
51995d67482SBill Paul 	/* Wait for completion */
52095d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
52195d67482SBill Paul 		DELAY(10);
52295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
52395d67482SBill Paul 			break;
52495d67482SBill Paul 	}
52595d67482SBill Paul 
52695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
527fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
528f6789fbaSPyun YongHyeon 		return(1);
52995d67482SBill Paul 	}
53095d67482SBill Paul 
53195d67482SBill Paul 	/* Get result. */
53295d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
53395d67482SBill Paul 
53495d67482SBill Paul 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
53595d67482SBill Paul 
53695d67482SBill Paul 	return(0);
53795d67482SBill Paul }
53895d67482SBill Paul 
53995d67482SBill Paul /*
54095d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
54195d67482SBill Paul  */
54295d67482SBill Paul static int
54395d67482SBill Paul bge_read_eeprom(sc, dest, off, cnt)
54495d67482SBill Paul 	struct bge_softc *sc;
54595d67482SBill Paul 	caddr_t dest;
54695d67482SBill Paul 	int off;
54795d67482SBill Paul 	int cnt;
54895d67482SBill Paul {
54995d67482SBill Paul 	int err = 0, i;
55095d67482SBill Paul 	u_int8_t byte = 0;
55195d67482SBill Paul 
55295d67482SBill Paul 	for (i = 0; i < cnt; i++) {
55395d67482SBill Paul 		err = bge_eeprom_getbyte(sc, off + i, &byte);
55495d67482SBill Paul 		if (err)
55595d67482SBill Paul 			break;
55695d67482SBill Paul 		*(dest + i) = byte;
55795d67482SBill Paul 	}
55895d67482SBill Paul 
55995d67482SBill Paul 	return(err ? 1 : 0);
56095d67482SBill Paul }
56195d67482SBill Paul 
56295d67482SBill Paul static int
56395d67482SBill Paul bge_miibus_readreg(dev, phy, reg)
56495d67482SBill Paul 	device_t dev;
56595d67482SBill Paul 	int phy, reg;
56695d67482SBill Paul {
56795d67482SBill Paul 	struct bge_softc *sc;
56837ceeb4dSPaul Saab 	u_int32_t val, autopoll;
56995d67482SBill Paul 	int i;
57095d67482SBill Paul 
57195d67482SBill Paul 	sc = device_get_softc(dev);
57295d67482SBill Paul 
5730434d1b8SBill Paul 	/*
5740434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5750434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5760434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5770434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5780434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5790434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5800434d1b8SBill Paul 	 * special-cased.
5810434d1b8SBill Paul 	 */
582b1265c1aSJohn Polstra 	if (phy != 1)
58398b28ee5SBill Paul 		return(0);
58498b28ee5SBill Paul 
58537ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
58637ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
58737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
58837ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
58937ceeb4dSPaul Saab 		DELAY(40);
59037ceeb4dSPaul Saab 	}
59137ceeb4dSPaul Saab 
59295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
59395d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
59495d67482SBill Paul 
59595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
59695d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
59795d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
59895d67482SBill Paul 			break;
59995d67482SBill Paul 	}
60095d67482SBill Paul 
60195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
602fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
60337ceeb4dSPaul Saab 		val = 0;
60437ceeb4dSPaul Saab 		goto done;
60595d67482SBill Paul 	}
60695d67482SBill Paul 
60795d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
60895d67482SBill Paul 
60937ceeb4dSPaul Saab done:
61037ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
61137ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
61237ceeb4dSPaul Saab 		DELAY(40);
61337ceeb4dSPaul Saab 	}
61437ceeb4dSPaul Saab 
61595d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
61695d67482SBill Paul 		return(0);
61795d67482SBill Paul 
61895d67482SBill Paul 	return(val & 0xFFFF);
61995d67482SBill Paul }
62095d67482SBill Paul 
62195d67482SBill Paul static int
62295d67482SBill Paul bge_miibus_writereg(dev, phy, reg, val)
62395d67482SBill Paul 	device_t dev;
62495d67482SBill Paul 	int phy, reg, val;
62595d67482SBill Paul {
62695d67482SBill Paul 	struct bge_softc *sc;
62737ceeb4dSPaul Saab 	u_int32_t autopoll;
62895d67482SBill Paul 	int i;
62995d67482SBill Paul 
63095d67482SBill Paul 	sc = device_get_softc(dev);
63195d67482SBill Paul 
63237ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
63337ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
63437ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
63537ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
63637ceeb4dSPaul Saab 		DELAY(40);
63737ceeb4dSPaul Saab 	}
63837ceeb4dSPaul Saab 
63995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
64095d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
64195d67482SBill Paul 
64295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
64395d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
64495d67482SBill Paul 			break;
64595d67482SBill Paul 	}
64695d67482SBill Paul 
64737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
64837ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
64937ceeb4dSPaul Saab 		DELAY(40);
65037ceeb4dSPaul Saab 	}
65137ceeb4dSPaul Saab 
65295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
653fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
65495d67482SBill Paul 		return(0);
65595d67482SBill Paul 	}
65695d67482SBill Paul 
65795d67482SBill Paul 	return(0);
65895d67482SBill Paul }
65995d67482SBill Paul 
66095d67482SBill Paul static void
66195d67482SBill Paul bge_miibus_statchg(dev)
66295d67482SBill Paul 	device_t dev;
66395d67482SBill Paul {
66495d67482SBill Paul 	struct bge_softc *sc;
66595d67482SBill Paul 	struct mii_data *mii;
66695d67482SBill Paul 
66795d67482SBill Paul 	sc = device_get_softc(dev);
66895d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
66995d67482SBill Paul 
67095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
671b418ad5cSPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
67295d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
67395d67482SBill Paul 	} else {
67495d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
67595d67482SBill Paul 	}
67695d67482SBill Paul 
67795d67482SBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
67895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
67995d67482SBill Paul 	} else {
68095d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
68195d67482SBill Paul 	}
68295d67482SBill Paul 
68395d67482SBill Paul 	return;
68495d67482SBill Paul }
68595d67482SBill Paul 
68695d67482SBill Paul /*
68795d67482SBill Paul  * Intialize a standard receive ring descriptor.
68895d67482SBill Paul  */
68995d67482SBill Paul static int
69095d67482SBill Paul bge_newbuf_std(sc, i, m)
69195d67482SBill Paul 	struct bge_softc	*sc;
69295d67482SBill Paul 	int			i;
69395d67482SBill Paul 	struct mbuf		*m;
69495d67482SBill Paul {
69595d67482SBill Paul 	struct mbuf		*m_new = NULL;
69695d67482SBill Paul 	struct bge_rx_bd	*r;
697f41ac2beSBill Paul 	struct bge_dmamap_arg	ctx;
698f41ac2beSBill Paul 	int			error;
69995d67482SBill Paul 
70095d67482SBill Paul 	if (m == NULL) {
701c3a56752SGleb Smirnoff 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
702c3a56752SGleb Smirnoff 		if (m_new == NULL)
70395d67482SBill Paul 			return(ENOBUFS);
70495d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70595d67482SBill Paul 	} else {
70695d67482SBill Paul 		m_new = m;
70795d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70895d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
70995d67482SBill Paul 	}
71095d67482SBill Paul 
711e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
71295d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
71395d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
714f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
715f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
716f41ac2beSBill Paul 	ctx.sc = sc;
717f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
718f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
719f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
720f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
721f7cea149SGleb Smirnoff 		if (m == NULL) {
722f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
723f41ac2beSBill Paul 			m_freem(m_new);
724f7cea149SGleb Smirnoff 		}
725f41ac2beSBill Paul 		return(ENOMEM);
726f41ac2beSBill Paul 	}
727e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
728e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
729e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
730e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
731e907febfSPyun YongHyeon 	r->bge_idx = i;
732f41ac2beSBill Paul 
733f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
734f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
735f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
73695d67482SBill Paul 
73795d67482SBill Paul 	return(0);
73895d67482SBill Paul }
73995d67482SBill Paul 
74095d67482SBill Paul /*
74195d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
74295d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
74395d67482SBill Paul  */
74495d67482SBill Paul static int
74595d67482SBill Paul bge_newbuf_jumbo(sc, i, m)
74695d67482SBill Paul 	struct bge_softc *sc;
74795d67482SBill Paul 	int i;
74895d67482SBill Paul 	struct mbuf *m;
74995d67482SBill Paul {
7501be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
7511be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
75295d67482SBill Paul 	struct mbuf *m_new = NULL;
7531be6acb7SGleb Smirnoff 	int nsegs;
754f41ac2beSBill Paul 	int error;
75595d67482SBill Paul 
75695d67482SBill Paul 	if (m == NULL) {
757a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
7581be6acb7SGleb Smirnoff 		if (m_new == NULL)
75995d67482SBill Paul 			return(ENOBUFS);
76095d67482SBill Paul 
7611be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7621be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
76395d67482SBill Paul 			m_freem(m_new);
76495d67482SBill Paul 			return(ENOBUFS);
76595d67482SBill Paul 		}
7661be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
76795d67482SBill Paul 	} else {
76895d67482SBill Paul 		m_new = m;
7691be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
77095d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
77195d67482SBill Paul 	}
77295d67482SBill Paul 
773e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
77495d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7751be6acb7SGleb Smirnoff 
7761be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7771be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7781be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7791be6acb7SGleb Smirnoff 	if (error) {
7801be6acb7SGleb Smirnoff 		if (m == NULL)
781f41ac2beSBill Paul 			m_freem(m_new);
7821be6acb7SGleb Smirnoff 		return(error);
783f7cea149SGleb Smirnoff 	}
7841be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
7851be6acb7SGleb Smirnoff 
7861be6acb7SGleb Smirnoff 	/*
7871be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
7881be6acb7SGleb Smirnoff 	 */
7891be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
7904e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING|BGE_RXBDFLAG_END;
7914e7ba1abSGleb Smirnoff 	r->bge_idx = i;
7924e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
7934e7ba1abSGleb Smirnoff 	switch (nsegs) {
7944e7ba1abSGleb Smirnoff 	case 4:
7954e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
7964e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
7974e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
7984e7ba1abSGleb Smirnoff 	case 3:
799e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
800e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
801e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
8024e7ba1abSGleb Smirnoff 	case 2:
8034e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
8044e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
8054e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
8064e7ba1abSGleb Smirnoff 	case 1:
8074e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
8084e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
8094e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
8104e7ba1abSGleb Smirnoff 		break;
8114e7ba1abSGleb Smirnoff 	default:
8124e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
8134e7ba1abSGleb Smirnoff 	}
814f41ac2beSBill Paul 
815f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
816f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
817f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
81895d67482SBill Paul 
81995d67482SBill Paul 	return (0);
82095d67482SBill Paul }
82195d67482SBill Paul 
82295d67482SBill Paul /*
82395d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
82495d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
82595d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
82695d67482SBill Paul  * the NIC.
82795d67482SBill Paul  */
82895d67482SBill Paul static int
82995d67482SBill Paul bge_init_rx_ring_std(sc)
83095d67482SBill Paul 	struct bge_softc *sc;
83195d67482SBill Paul {
83295d67482SBill Paul 	int i;
83395d67482SBill Paul 
83495d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
83595d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
83695d67482SBill Paul 			return(ENOBUFS);
83795d67482SBill Paul 	};
83895d67482SBill Paul 
839f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
840f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
841f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
842f41ac2beSBill Paul 
84395d67482SBill Paul 	sc->bge_std = i - 1;
84495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
84595d67482SBill Paul 
84695d67482SBill Paul 	return(0);
84795d67482SBill Paul }
84895d67482SBill Paul 
84995d67482SBill Paul static void
85095d67482SBill Paul bge_free_rx_ring_std(sc)
85195d67482SBill Paul 	struct bge_softc *sc;
85295d67482SBill Paul {
85395d67482SBill Paul 	int i;
85495d67482SBill Paul 
85595d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
85695d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
857e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
858e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
859e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
860f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
861f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
862e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
863e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
86495d67482SBill Paul 		}
865f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
86695d67482SBill Paul 		    sizeof(struct bge_rx_bd));
86795d67482SBill Paul 	}
86895d67482SBill Paul 
86995d67482SBill Paul 	return;
87095d67482SBill Paul }
87195d67482SBill Paul 
87295d67482SBill Paul static int
87395d67482SBill Paul bge_init_rx_ring_jumbo(sc)
87495d67482SBill Paul 	struct bge_softc *sc;
87595d67482SBill Paul {
87695d67482SBill Paul 	struct bge_rcb *rcb;
8771be6acb7SGleb Smirnoff 	int i;
87895d67482SBill Paul 
87995d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
88095d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
88195d67482SBill Paul 			return(ENOBUFS);
88295d67482SBill Paul 	};
88395d67482SBill Paul 
884f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
885f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
886f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
887f41ac2beSBill Paul 
88895d67482SBill Paul 	sc->bge_jumbo = i - 1;
88995d67482SBill Paul 
890f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8911be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8921be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
89367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
89495d67482SBill Paul 
89595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
89695d67482SBill Paul 
89795d67482SBill Paul 	return(0);
89895d67482SBill Paul }
89995d67482SBill Paul 
90095d67482SBill Paul static void
90195d67482SBill Paul bge_free_rx_ring_jumbo(sc)
90295d67482SBill Paul 	struct bge_softc *sc;
90395d67482SBill Paul {
90495d67482SBill Paul 	int i;
90595d67482SBill Paul 
90695d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
90795d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
908e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
909e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
910e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
911f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
912f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
913e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
914e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
91595d67482SBill Paul 		}
916f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
9171be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
91895d67482SBill Paul 	}
91995d67482SBill Paul 
92095d67482SBill Paul 	return;
92195d67482SBill Paul }
92295d67482SBill Paul 
92395d67482SBill Paul static void
92495d67482SBill Paul bge_free_tx_ring(sc)
92595d67482SBill Paul 	struct bge_softc *sc;
92695d67482SBill Paul {
92795d67482SBill Paul 	int i;
92895d67482SBill Paul 
929f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
93095d67482SBill Paul 		return;
93195d67482SBill Paul 
93295d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
93395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
934e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
935e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
936e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
937f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
938f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
939e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
940e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
94195d67482SBill Paul 		}
942f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
94395d67482SBill Paul 		    sizeof(struct bge_tx_bd));
94495d67482SBill Paul 	}
94595d67482SBill Paul 
94695d67482SBill Paul 	return;
94795d67482SBill Paul }
94895d67482SBill Paul 
94995d67482SBill Paul static int
95095d67482SBill Paul bge_init_tx_ring(sc)
95195d67482SBill Paul 	struct bge_softc *sc;
95295d67482SBill Paul {
95395d67482SBill Paul 	sc->bge_txcnt = 0;
95495d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9553927098fSPaul Saab 
95614bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
95714bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
95814bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
95914bbd30fSGleb Smirnoff 
9603927098fSPaul Saab 	/* 5700 b2 errata */
961e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
96214bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
9633927098fSPaul Saab 
96414bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
9653927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
9663927098fSPaul Saab 	/* 5700 b2 errata */
967e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
96895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
96995d67482SBill Paul 
97095d67482SBill Paul 	return(0);
97195d67482SBill Paul }
97295d67482SBill Paul 
97395d67482SBill Paul static void
97495d67482SBill Paul bge_setmulti(sc)
97595d67482SBill Paul 	struct bge_softc *sc;
97695d67482SBill Paul {
97795d67482SBill Paul 	struct ifnet *ifp;
97895d67482SBill Paul 	struct ifmultiaddr *ifma;
97995d67482SBill Paul 	u_int32_t hashes[4] = { 0, 0, 0, 0 };
98095d67482SBill Paul 	int h, i;
98195d67482SBill Paul 
9820f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9830f9bd73bSSam Leffler 
984fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
98595d67482SBill Paul 
98695d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
98795d67482SBill Paul 		for (i = 0; i < 4; i++)
98895d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
98995d67482SBill Paul 		return;
99095d67482SBill Paul 	}
99195d67482SBill Paul 
99295d67482SBill Paul 	/* First, zot all the existing filters. */
99395d67482SBill Paul 	for (i = 0; i < 4; i++)
99495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
99595d67482SBill Paul 
99695d67482SBill Paul 	/* Now program new ones. */
99713b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
99895d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
99995d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
100095d67482SBill Paul 			continue;
10010e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
10020e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
100395d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
100495d67482SBill Paul 	}
100513b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
100695d67482SBill Paul 
100795d67482SBill Paul 	for (i = 0; i < 4; i++)
100895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
100995d67482SBill Paul 
101095d67482SBill Paul 	return;
101195d67482SBill Paul }
101295d67482SBill Paul 
101395d67482SBill Paul /*
101495d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
101595d67482SBill Paul  * self-test results.
101695d67482SBill Paul  */
101795d67482SBill Paul static int
101895d67482SBill Paul bge_chipinit(sc)
101995d67482SBill Paul 	struct bge_softc *sc;
102095d67482SBill Paul {
102195d67482SBill Paul 	int			i;
10225cba12d3SPaul Saab 	u_int32_t		dma_rw_ctl;
102395d67482SBill Paul 
1024e907febfSPyun YongHyeon 	/* Set endian type before we access any non-PCI registers. */
1025e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
102695d67482SBill Paul 
102795d67482SBill Paul 	/*
102895d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
102995d67482SBill Paul 	 * self-tests passed.
103095d67482SBill Paul 	 */
103195d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1032fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
103395d67482SBill Paul 		return(ENODEV);
103495d67482SBill Paul 	}
103595d67482SBill Paul 
103695d67482SBill Paul 	/* Clear the MAC control register */
103795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
103895d67482SBill Paul 
103995d67482SBill Paul 	/*
104095d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
104195d67482SBill Paul 	 * internal memory.
104295d67482SBill Paul 	 */
104395d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
104495d67482SBill Paul 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
104595d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
104695d67482SBill Paul 
104795d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
104895d67482SBill Paul 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
104995d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
105095d67482SBill Paul 
105195d67482SBill Paul 	/* Set up the PCI DMA control register. */
1052e53d81eeSPaul Saab 	if (sc->bge_pcie) {
1053e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1054e53d81eeSPaul Saab 		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1055e53d81eeSPaul Saab 		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1056e53d81eeSPaul Saab 	} else if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
10578287860eSJohn Polstra 	    BGE_PCISTATE_PCI_BUSMODE) {
10588287860eSJohn Polstra 		/* Conventional PCI bus */
10595cba12d3SPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10605cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10615cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10625cba12d3SPaul Saab 		    (0x0F);
10638287860eSJohn Polstra 	} else {
10648287860eSJohn Polstra 		/* PCI-X bus */
10655cba12d3SPaul Saab 		/*
10665cba12d3SPaul Saab 		 * The 5704 uses a different encoding of read/write
10675cba12d3SPaul Saab 		 * watermarks.
10685cba12d3SPaul Saab 		 */
1069e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
10705cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10715cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10725cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
10735cba12d3SPaul Saab 		else
10745cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10755cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10765cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10775cba12d3SPaul Saab 			    (0x0F);
10785cba12d3SPaul Saab 
10795cba12d3SPaul Saab 		/*
10805cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
10815cba12d3SPaul Saab 		 * for hardware bugs.
10825cba12d3SPaul Saab 		 */
1083e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1084e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
10855cba12d3SPaul Saab 			u_int32_t tmp;
10865cba12d3SPaul Saab 
10875cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
10885cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
10895cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
10908287860eSJohn Polstra 		}
10915cba12d3SPaul Saab 	}
10925cba12d3SPaul Saab 
1093e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
10940434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
1095e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1096e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
10975cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
10985cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
109995d67482SBill Paul 
110095d67482SBill Paul 	/*
110195d67482SBill Paul 	 * Set up general mode register.
110295d67482SBill Paul 	 */
1103e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
110495d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1105ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
110695d67482SBill Paul 
110795d67482SBill Paul 	/*
1108ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1109ea13bdd5SJohn Polstra 	 * properly by these devices.
111095d67482SBill Paul 	 */
1111ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
111295d67482SBill Paul 
111395d67482SBill Paul #ifdef __brokenalpha__
111495d67482SBill Paul 	/*
111595d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
111695d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
111795d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
111895d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
111995d67482SBill Paul 	 */
112062f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
112162f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
112295d67482SBill Paul #endif
112395d67482SBill Paul 
112495d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
112595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
112695d67482SBill Paul 
112795d67482SBill Paul 	return(0);
112895d67482SBill Paul }
112995d67482SBill Paul 
113095d67482SBill Paul static int
113195d67482SBill Paul bge_blockinit(sc)
113295d67482SBill Paul 	struct bge_softc *sc;
113395d67482SBill Paul {
113495d67482SBill Paul 	struct bge_rcb *rcb;
1135e907febfSPyun YongHyeon 	bus_size_t vrcb;
1136e907febfSPyun YongHyeon 	bge_hostaddr taddr;
113795d67482SBill Paul 	int i;
113895d67482SBill Paul 
113995d67482SBill Paul 	/*
114095d67482SBill Paul 	 * Initialize the memory window pointer register so that
114195d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
114295d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
114395d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
114495d67482SBill Paul 	 */
114595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
114695d67482SBill Paul 
1147822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1148822f63fcSBill Paul 
11495dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1150e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
115195d67482SBill Paul 		/* Configure mbuf memory pool */
115295d67482SBill Paul 		if (sc->bge_extram) {
11530434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11540434d1b8SBill Paul 			    BGE_EXT_SSRAM);
1155822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1156822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1157822f63fcSBill Paul 			else
115895d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
115995d67482SBill Paul 		} else {
11600434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11610434d1b8SBill Paul 			    BGE_BUFFPOOL_1);
1162822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1163822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1164822f63fcSBill Paul 			else
116595d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
116695d67482SBill Paul 		}
116795d67482SBill Paul 
116895d67482SBill Paul 		/* Configure DMA resource pool */
11690434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
11700434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
117195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
11720434d1b8SBill Paul 	}
117395d67482SBill Paul 
117495d67482SBill Paul 	/* Configure mbuf pool watermarks */
1175e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1176e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750) {
11770434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11780434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
11790434d1b8SBill Paul 	} else {
1180fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1181fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
11820434d1b8SBill Paul 	}
1183fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
118495d67482SBill Paul 
118595d67482SBill Paul 	/* Configure DMA resource watermarks */
118695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
118795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
118895d67482SBill Paul 
118995d67482SBill Paul 	/* Enable buffer manager */
11905dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1191e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
119295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
119395d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
119495d67482SBill Paul 
119595d67482SBill Paul 		/* Poll for buffer manager start indication */
119695d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
119795d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
119895d67482SBill Paul 				break;
119995d67482SBill Paul 			DELAY(10);
120095d67482SBill Paul 		}
120195d67482SBill Paul 
120295d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1203fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1204fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
120595d67482SBill Paul 			return(ENXIO);
120695d67482SBill Paul 		}
12070434d1b8SBill Paul 	}
120895d67482SBill Paul 
120995d67482SBill Paul 	/* Enable flow-through queues */
121095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
121195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
121295d67482SBill Paul 
121395d67482SBill Paul 	/* Wait until queue initialization is complete */
121495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
121595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
121695d67482SBill Paul 			break;
121795d67482SBill Paul 		DELAY(10);
121895d67482SBill Paul 	}
121995d67482SBill Paul 
122095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1221fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
122295d67482SBill Paul 		return(ENXIO);
122395d67482SBill Paul 	}
122495d67482SBill Paul 
122595d67482SBill Paul 	/* Initialize the standard RX ring control block */
1226f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1227f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1228f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1229f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1230f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1231f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1232f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
1233e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1234e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
12350434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
12360434d1b8SBill Paul 	else
12370434d1b8SBill Paul 		rcb->bge_maxlen_flags =
12380434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
123995d67482SBill Paul 	if (sc->bge_extram)
124095d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
124195d67482SBill Paul 	else
124295d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
124367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
124467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1245f41ac2beSBill Paul 
124667111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
124767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
124895d67482SBill Paul 
124995d67482SBill Paul 	/*
125095d67482SBill Paul 	 * Initialize the jumbo RX ring control block
125195d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
125295d67482SBill Paul 	 * field until we're actually ready to start
125395d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
125495d67482SBill Paul 	 * high enough to require it).
125595d67482SBill Paul 	 */
12565dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1257e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1258f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1259f41ac2beSBill Paul 
1260f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1261f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1262f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1263f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1264f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1265f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1266f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
12671be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
12681be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD|BGE_RCB_FLAG_RING_DISABLED);
126995d67482SBill Paul 		if (sc->bge_extram)
127095d67482SBill Paul 			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
127195d67482SBill Paul 		else
127295d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
127367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
127467111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
127567111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
127667111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1277f41ac2beSBill Paul 
12780434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
12790434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
128067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
128195d67482SBill Paul 
128295d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1283f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
128467111612SJohn Polstra 		rcb->bge_maxlen_flags =
128567111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
12860434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
12870434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
12880434d1b8SBill Paul 	}
128995d67482SBill Paul 
129095d67482SBill Paul 	/*
129195d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
129295d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
129395d67482SBill Paul 	 * each ring.
129495d67482SBill Paul 	 */
129595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
129695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
129795d67482SBill Paul 
129895d67482SBill Paul 	/*
129995d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
130095d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
130195d67482SBill Paul 	 * These are located in NIC memory.
130295d67482SBill Paul 	 */
1303e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
130495d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1305e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1306e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1307e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1308e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
130995d67482SBill Paul 	}
131095d67482SBill Paul 
131195d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1312e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1313e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1314e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1315e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1316e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1317e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
13185dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1319e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1320e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1321e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
132295d67482SBill Paul 
132395d67482SBill Paul 	/* Disable all unused RX return rings */
1324e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
132595d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1326e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1327e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1328e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
13290434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1330e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1331e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
133295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
133395d67482SBill Paul 		    (i * (sizeof(u_int64_t))), 0);
1334e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
133595d67482SBill Paul 	}
133695d67482SBill Paul 
133795d67482SBill Paul 	/* Initialize RX ring indexes */
133895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
133995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
134095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
134195d67482SBill Paul 
134295d67482SBill Paul 	/*
134395d67482SBill Paul 	 * Set up RX return ring 0
134495d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
134595d67482SBill Paul 	 * The return rings live entirely within the host, so the
134695d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
134795d67482SBill Paul 	 */
1348e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1349e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1350e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1351e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1352e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1353e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1354e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
135595d67482SBill Paul 
135695d67482SBill Paul 	/* Set random backoff seed for TX */
135795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
13584a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
13594a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
13604a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
136195d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
136295d67482SBill Paul 
136395d67482SBill Paul 	/* Set inter-packet gap */
136495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
136595d67482SBill Paul 
136695d67482SBill Paul 	/*
136795d67482SBill Paul 	 * Specify which ring to use for packets that don't match
136895d67482SBill Paul 	 * any RX rules.
136995d67482SBill Paul 	 */
137095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
137195d67482SBill Paul 
137295d67482SBill Paul 	/*
137395d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
137495d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
137595d67482SBill Paul 	 */
137695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
137795d67482SBill Paul 
137895d67482SBill Paul 	/* Inialize RX list placement stats mask. */
137995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
138095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
138195d67482SBill Paul 
138295d67482SBill Paul 	/* Disable host coalescing until we get it set up */
138395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
138495d67482SBill Paul 
138595d67482SBill Paul 	/* Poll to make sure it's shut down. */
138695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
138795d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
138895d67482SBill Paul 			break;
138995d67482SBill Paul 		DELAY(10);
139095d67482SBill Paul 	}
139195d67482SBill Paul 
139295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1393fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1394fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
139595d67482SBill Paul 		return(ENXIO);
139695d67482SBill Paul 	}
139795d67482SBill Paul 
139895d67482SBill Paul 	/* Set up host coalescing defaults */
139995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
140095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
140195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
140295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
14035dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1404e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
140595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
140695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
14070434d1b8SBill Paul 	}
140895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
140995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
141095d67482SBill Paul 
141195d67482SBill Paul 	/* Set up address of statistics block */
14125dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1413e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1414f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1415f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
141695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1417f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
14180434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
141995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
14200434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
14210434d1b8SBill Paul 	}
14220434d1b8SBill Paul 
14230434d1b8SBill Paul 	/* Set up address of status block */
1424f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1425f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
142695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1427f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1428f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1429f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
143095d67482SBill Paul 
143195d67482SBill Paul 	/* Turn on host coalescing state machine */
143295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
143395d67482SBill Paul 
143495d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
143595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
143695d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
143795d67482SBill Paul 
143895d67482SBill Paul 	/* Turn on RX list placement state machine */
143995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
144095d67482SBill Paul 
144195d67482SBill Paul 	/* Turn on RX list selector state machine. */
14425dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1443e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
144495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
144595d67482SBill Paul 
144695d67482SBill Paul 	/* Turn on DMA, clear stats */
144795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
144895d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
144995d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
145095d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
145195d67482SBill Paul 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
145295d67482SBill Paul 
145395d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
145495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
145595d67482SBill Paul 
145695d67482SBill Paul #ifdef notdef
145795d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
145895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
145995d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
146095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
146195d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
146295d67482SBill Paul #endif
146395d67482SBill Paul 
146495d67482SBill Paul 	/* Turn on DMA completion state machine */
14655dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1466e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
146795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
146895d67482SBill Paul 
146995d67482SBill Paul 	/* Turn on write DMA state machine */
147095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
147195d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
147295d67482SBill Paul 
147395d67482SBill Paul 	/* Turn on read DMA state machine */
147495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
147595d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
147695d67482SBill Paul 
147795d67482SBill Paul 	/* Turn on RX data completion state machine */
147895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
147995d67482SBill Paul 
148095d67482SBill Paul 	/* Turn on RX BD initiator state machine */
148195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
148295d67482SBill Paul 
148395d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
148495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
148595d67482SBill Paul 
148695d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
14875dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1488e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
148995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
149095d67482SBill Paul 
149195d67482SBill Paul 	/* Turn on send BD completion state machine */
149295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
149395d67482SBill Paul 
149495d67482SBill Paul 	/* Turn on send data completion state machine */
149595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
149695d67482SBill Paul 
149795d67482SBill Paul 	/* Turn on send data initiator state machine */
149895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
149995d67482SBill Paul 
150095d67482SBill Paul 	/* Turn on send BD initiator state machine */
150195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
150295d67482SBill Paul 
150395d67482SBill Paul 	/* Turn on send BD selector state machine */
150495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
150595d67482SBill Paul 
150695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
150795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
150895d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
150995d67482SBill Paul 
151095d67482SBill Paul 	/* ack/clear link change events */
151195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15120434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15130434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1514f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
151595d67482SBill Paul 
151695d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
151795d67482SBill Paul 	if (sc->bge_tbi) {
151895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1519a1d52896SBill Paul 	} else {
152095d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
15211f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
15221f313773SOleg Bulyzhin 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B1)
1523a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1524a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1525a1d52896SBill Paul 	}
152695d67482SBill Paul 
15271f313773SOleg Bulyzhin 	/*
15281f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
15291f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
15301f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
15311f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
15321f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
15331f313773SOleg Bulyzhin 	 */
15341f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15351f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15361f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
15371f313773SOleg Bulyzhin 
153895d67482SBill Paul 	/* Enable link state change attentions. */
153995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
154095d67482SBill Paul 
154195d67482SBill Paul 	return(0);
154295d67482SBill Paul }
154395d67482SBill Paul 
154495d67482SBill Paul /*
154595d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
154695d67482SBill Paul  * against our list and return its name if we find a match. Note
154795d67482SBill Paul  * that since the Broadcom controller contains VPD support, we
154895d67482SBill Paul  * can get the device name string from the controller itself instead
154995d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
155095d67482SBill Paul  * we'll always announce the right product name.
155195d67482SBill Paul  */
155295d67482SBill Paul static int
155395d67482SBill Paul bge_probe(dev)
155495d67482SBill Paul 	device_t dev;
155595d67482SBill Paul {
155695d67482SBill Paul 	struct bge_type *t;
155795d67482SBill Paul 	struct bge_softc *sc;
1558029e2ee3SJohn Polstra 	char *descbuf;
155995d67482SBill Paul 
156095d67482SBill Paul 	t = bge_devs;
156195d67482SBill Paul 
156295d67482SBill Paul 	sc = device_get_softc(dev);
156395d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
156495d67482SBill Paul 	sc->bge_dev = dev;
156595d67482SBill Paul 
156695d67482SBill Paul 	while(t->bge_name != NULL) {
156795d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
156895d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
156995d67482SBill Paul #ifdef notdef
157095d67482SBill Paul 			bge_vpd_read(sc);
157195d67482SBill Paul 			device_set_desc(dev, sc->bge_vpd_prodname);
157295d67482SBill Paul #endif
1573029e2ee3SJohn Polstra 			descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
1574029e2ee3SJohn Polstra 			if (descbuf == NULL)
1575029e2ee3SJohn Polstra 				return(ENOMEM);
1576029e2ee3SJohn Polstra 			snprintf(descbuf, BGE_DEVDESC_MAX,
1577029e2ee3SJohn Polstra 			    "%s, ASIC rev. %#04x", t->bge_name,
1578029e2ee3SJohn Polstra 			    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16);
1579029e2ee3SJohn Polstra 			device_set_desc_copy(dev, descbuf);
15806d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
15816d2a9bd6SDoug Ambrisko 				sc->bge_no_3_led = 1;
1582029e2ee3SJohn Polstra 			free(descbuf, M_TEMP);
158395d67482SBill Paul 			return(0);
158495d67482SBill Paul 		}
158595d67482SBill Paul 		t++;
158695d67482SBill Paul 	}
158795d67482SBill Paul 
158895d67482SBill Paul 	return(ENXIO);
158995d67482SBill Paul }
159095d67482SBill Paul 
1591f41ac2beSBill Paul static void
1592f41ac2beSBill Paul bge_dma_free(sc)
1593f41ac2beSBill Paul 	struct bge_softc *sc;
1594f41ac2beSBill Paul {
1595f41ac2beSBill Paul 	int i;
1596f41ac2beSBill Paul 
1597f41ac2beSBill Paul 
1598f41ac2beSBill Paul 	/* Destroy DMA maps for RX buffers */
1599f41ac2beSBill Paul 
1600f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1601f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1602f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1603f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1604f41ac2beSBill Paul 	}
1605f41ac2beSBill Paul 
1606f41ac2beSBill Paul 	/* Destroy DMA maps for jumbo RX buffers */
1607f41ac2beSBill Paul 
1608f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1609f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1610f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1611f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1612f41ac2beSBill Paul 	}
1613f41ac2beSBill Paul 
1614f41ac2beSBill Paul 	/* Destroy DMA maps for TX buffers */
1615f41ac2beSBill Paul 
1616f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1617f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1618f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1619f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1620f41ac2beSBill Paul 	}
1621f41ac2beSBill Paul 
1622f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1623f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1624f41ac2beSBill Paul 
1625f41ac2beSBill Paul 
1626f41ac2beSBill Paul 	/* Destroy standard RX ring */
1627f41ac2beSBill Paul 
1628e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1629e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1630e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1631e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1632f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1633f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1634f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1635f41ac2beSBill Paul 
1636f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1637f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1638f41ac2beSBill Paul 
1639f41ac2beSBill Paul 	/* Destroy jumbo RX ring */
1640f41ac2beSBill Paul 
1641e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1642e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1643e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1644e65bed95SPyun YongHyeon 
1645e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1646e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1647f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1648f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1649f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1650f41ac2beSBill Paul 
1651f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1652f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1653f41ac2beSBill Paul 
1654f41ac2beSBill Paul 	/* Destroy RX return ring */
1655f41ac2beSBill Paul 
1656e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1657e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1658e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1659e65bed95SPyun YongHyeon 
1660e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1661e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1662f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1663f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1664f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1665f41ac2beSBill Paul 
1666f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1667f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1668f41ac2beSBill Paul 
1669f41ac2beSBill Paul 	/* Destroy TX ring */
1670f41ac2beSBill Paul 
1671e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1672e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1673e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1674e65bed95SPyun YongHyeon 
1675e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1676f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1677f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1678f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1679f41ac2beSBill Paul 
1680f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1681f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1682f41ac2beSBill Paul 
1683f41ac2beSBill Paul 	/* Destroy status block */
1684f41ac2beSBill Paul 
1685e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1686e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1687e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1688e65bed95SPyun YongHyeon 
1689e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1690f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1691f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1692f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1693f41ac2beSBill Paul 
1694f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1695f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1696f41ac2beSBill Paul 
1697f41ac2beSBill Paul 	/* Destroy statistics block */
1698f41ac2beSBill Paul 
1699e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1700e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1701e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1702e65bed95SPyun YongHyeon 
1703e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1704f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1705f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1706f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1707f41ac2beSBill Paul 
1708f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1709f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1710f41ac2beSBill Paul 
1711f41ac2beSBill Paul 	/* Destroy the parent tag */
1712f41ac2beSBill Paul 
1713f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1714f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1715f41ac2beSBill Paul 
1716f41ac2beSBill Paul 	return;
1717f41ac2beSBill Paul }
1718f41ac2beSBill Paul 
1719f41ac2beSBill Paul static int
1720f41ac2beSBill Paul bge_dma_alloc(dev)
1721f41ac2beSBill Paul 	device_t dev;
1722f41ac2beSBill Paul {
1723f41ac2beSBill Paul 	struct bge_softc *sc;
17241be6acb7SGleb Smirnoff 	int i, error;
1725f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
1726f41ac2beSBill Paul 
1727f41ac2beSBill Paul 	sc = device_get_softc(dev);
1728f41ac2beSBill Paul 
1729f41ac2beSBill Paul 	/*
1730f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1731f41ac2beSBill Paul 	 */
1732f41ac2beSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1733f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1734f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
17352f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1736f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1737f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1738f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
17398a40c10eSScott Long 			0,			/* flags */
1740f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1741f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1742f41ac2beSBill Paul 
1743e65bed95SPyun YongHyeon 	if (error != 0) {
1744fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1745fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1746e65bed95SPyun YongHyeon 		return (ENOMEM);
1747e65bed95SPyun YongHyeon 	}
1748e65bed95SPyun YongHyeon 
1749f41ac2beSBill Paul 	/*
1750f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1751f41ac2beSBill Paul 	 */
17528a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1753f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
17541be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
17551be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1756f41ac2beSBill Paul 
1757f41ac2beSBill Paul 	if (error) {
1758fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1759f41ac2beSBill Paul 		return (ENOMEM);
1760f41ac2beSBill Paul 	}
1761f41ac2beSBill Paul 
1762f41ac2beSBill Paul 	/* Create DMA maps for RX buffers */
1763f41ac2beSBill Paul 
1764f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1765f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1766f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1767f41ac2beSBill Paul 		if (error) {
1768fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1769fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1770f41ac2beSBill Paul 			return(ENOMEM);
1771f41ac2beSBill Paul 		}
1772f41ac2beSBill Paul 	}
1773f41ac2beSBill Paul 
1774f41ac2beSBill Paul 	/* Create DMA maps for TX buffers */
1775f41ac2beSBill Paul 
1776f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1777f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1778f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1779f41ac2beSBill Paul 		if (error) {
1780fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1781fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1782f41ac2beSBill Paul 			return(ENOMEM);
1783f41ac2beSBill Paul 		}
1784f41ac2beSBill Paul 	}
1785f41ac2beSBill Paul 
1786f41ac2beSBill Paul 	/* Create tag for standard RX ring */
1787f41ac2beSBill Paul 
1788f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1789f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1790f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1791f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1792f41ac2beSBill Paul 
1793f41ac2beSBill Paul 	if (error) {
1794fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1795f41ac2beSBill Paul 		return (ENOMEM);
1796f41ac2beSBill Paul 	}
1797f41ac2beSBill Paul 
1798f41ac2beSBill Paul 	/* Allocate DMA'able memory for standard RX ring */
1799f41ac2beSBill Paul 
1800f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1801f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1802f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1803f41ac2beSBill Paul 	if (error)
1804f41ac2beSBill Paul 		return (ENOMEM);
1805f41ac2beSBill Paul 
1806f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1807f41ac2beSBill Paul 
1808f41ac2beSBill Paul 	/* Load the address of the standard RX ring */
1809f41ac2beSBill Paul 
1810f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1811f41ac2beSBill Paul 	ctx.sc = sc;
1812f41ac2beSBill Paul 
1813f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1814f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1815f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1816f41ac2beSBill Paul 
1817f41ac2beSBill Paul 	if (error)
1818f41ac2beSBill Paul 		return (ENOMEM);
1819f41ac2beSBill Paul 
1820f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1821f41ac2beSBill Paul 
18225dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1823e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1824f41ac2beSBill Paul 
1825f41ac2beSBill Paul 		/*
1826f41ac2beSBill Paul 		 * Create tag for jumbo mbufs.
1827f41ac2beSBill Paul 		 * This is really a bit of a kludge. We allocate a special
1828f41ac2beSBill Paul 		 * jumbo buffer pool which (thanks to the way our DMA
1829f41ac2beSBill Paul 		 * memory allocation works) will consist of contiguous
1830f41ac2beSBill Paul 		 * pages. This means that even though a jumbo buffer might
1831f41ac2beSBill Paul 		 * be larger than a page size, we don't really need to
1832f41ac2beSBill Paul 		 * map it into more than one DMA segment. However, the
1833f41ac2beSBill Paul 		 * default mbuf tag will result in multi-segment mappings,
1834f41ac2beSBill Paul 		 * so we have to create a special jumbo mbuf tag that
1835f41ac2beSBill Paul 		 * lets us get away with mapping the jumbo buffers as
1836f41ac2beSBill Paul 		 * a single segment. I think eventually the driver should
1837f41ac2beSBill Paul 		 * be changed so that it uses ordinary mbufs and cluster
1838f41ac2beSBill Paul 		 * buffers, i.e. jumbo frames can span multiple DMA
1839f41ac2beSBill Paul 		 * descriptors. But that's a project for another day.
1840f41ac2beSBill Paul 		 */
1841f41ac2beSBill Paul 
1842f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
18438a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
18441be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
18451be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1846f41ac2beSBill Paul 
1847f41ac2beSBill Paul 		if (error) {
1848fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1849fe806fdaSPyun YongHyeon 			    "could not allocate dma tag\n");
1850f41ac2beSBill Paul 			return (ENOMEM);
1851f41ac2beSBill Paul 		}
1852f41ac2beSBill Paul 
1853f41ac2beSBill Paul 		/* Create tag for jumbo RX ring */
1854f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1855f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1856f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1857f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1858f41ac2beSBill Paul 
1859f41ac2beSBill Paul 		if (error) {
1860fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1861fe806fdaSPyun YongHyeon 			    "could not allocate dma tag\n");
1862f41ac2beSBill Paul 			return (ENOMEM);
1863f41ac2beSBill Paul 		}
1864f41ac2beSBill Paul 
1865f41ac2beSBill Paul 		/* Allocate DMA'able memory for jumbo RX ring */
1866f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
18671be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
18681be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1869f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1870f41ac2beSBill Paul 		if (error)
1871f41ac2beSBill Paul 			return (ENOMEM);
1872f41ac2beSBill Paul 
1873f41ac2beSBill Paul 		/* Load the address of the jumbo RX ring */
1874f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1875f41ac2beSBill Paul 		ctx.sc = sc;
1876f41ac2beSBill Paul 
1877f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1878f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1879f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1880f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1881f41ac2beSBill Paul 
1882f41ac2beSBill Paul 		if (error)
1883f41ac2beSBill Paul 			return (ENOMEM);
1884f41ac2beSBill Paul 
1885f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1886f41ac2beSBill Paul 
1887f41ac2beSBill Paul 		/* Create DMA maps for jumbo RX buffers */
1888f41ac2beSBill Paul 
1889f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1890f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1891f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1892f41ac2beSBill Paul 			if (error) {
1893fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
1894f41ac2beSBill Paul 				    "can't create DMA map for RX\n");
1895f41ac2beSBill Paul 				return(ENOMEM);
1896f41ac2beSBill Paul 			}
1897f41ac2beSBill Paul 		}
1898f41ac2beSBill Paul 
1899f41ac2beSBill Paul 	}
1900f41ac2beSBill Paul 
1901f41ac2beSBill Paul 	/* Create tag for RX return ring */
1902f41ac2beSBill Paul 
1903f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1904f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1905f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1906f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1907f41ac2beSBill Paul 
1908f41ac2beSBill Paul 	if (error) {
1909fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1910f41ac2beSBill Paul 		return (ENOMEM);
1911f41ac2beSBill Paul 	}
1912f41ac2beSBill Paul 
1913f41ac2beSBill Paul 	/* Allocate DMA'able memory for RX return ring */
1914f41ac2beSBill Paul 
1915f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1916f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1917f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1918f41ac2beSBill Paul 	if (error)
1919f41ac2beSBill Paul 		return (ENOMEM);
1920f41ac2beSBill Paul 
1921f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1922f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1923f41ac2beSBill Paul 
1924f41ac2beSBill Paul 	/* Load the address of the RX return ring */
1925f41ac2beSBill Paul 
1926f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1927f41ac2beSBill Paul 	ctx.sc = sc;
1928f41ac2beSBill Paul 
1929f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
1930f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
1931f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
1932f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1933f41ac2beSBill Paul 
1934f41ac2beSBill Paul 	if (error)
1935f41ac2beSBill Paul 		return (ENOMEM);
1936f41ac2beSBill Paul 
1937f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
1938f41ac2beSBill Paul 
1939f41ac2beSBill Paul 	/* Create tag for TX ring */
1940f41ac2beSBill Paul 
1941f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1942f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1943f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
1944f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
1945f41ac2beSBill Paul 
1946f41ac2beSBill Paul 	if (error) {
1947fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1948f41ac2beSBill Paul 		return (ENOMEM);
1949f41ac2beSBill Paul 	}
1950f41ac2beSBill Paul 
1951f41ac2beSBill Paul 	/* Allocate DMA'able memory for TX ring */
1952f41ac2beSBill Paul 
1953f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
1954f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
1955f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
1956f41ac2beSBill Paul 	if (error)
1957f41ac2beSBill Paul 		return (ENOMEM);
1958f41ac2beSBill Paul 
1959f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1960f41ac2beSBill Paul 
1961f41ac2beSBill Paul 	/* Load the address of the TX ring */
1962f41ac2beSBill Paul 
1963f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1964f41ac2beSBill Paul 	ctx.sc = sc;
1965f41ac2beSBill Paul 
1966f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
1967f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
1968f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1969f41ac2beSBill Paul 
1970f41ac2beSBill Paul 	if (error)
1971f41ac2beSBill Paul 		return (ENOMEM);
1972f41ac2beSBill Paul 
1973f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
1974f41ac2beSBill Paul 
1975f41ac2beSBill Paul 	/* Create tag for status block */
1976f41ac2beSBill Paul 
1977f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1978f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1979f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
1980f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
1981f41ac2beSBill Paul 
1982f41ac2beSBill Paul 	if (error) {
1983fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1984f41ac2beSBill Paul 		return (ENOMEM);
1985f41ac2beSBill Paul 	}
1986f41ac2beSBill Paul 
1987f41ac2beSBill Paul 	/* Allocate DMA'able memory for status block */
1988f41ac2beSBill Paul 
1989f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
1990f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
1991f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
1992f41ac2beSBill Paul 	if (error)
1993f41ac2beSBill Paul 		return (ENOMEM);
1994f41ac2beSBill Paul 
1995f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1996f41ac2beSBill Paul 
1997f41ac2beSBill Paul 	/* Load the address of the status block */
1998f41ac2beSBill Paul 
1999f41ac2beSBill Paul 	ctx.sc = sc;
2000f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2001f41ac2beSBill Paul 
2002f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2003f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2004f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2005f41ac2beSBill Paul 
2006f41ac2beSBill Paul 	if (error)
2007f41ac2beSBill Paul 		return (ENOMEM);
2008f41ac2beSBill Paul 
2009f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2010f41ac2beSBill Paul 
2011f41ac2beSBill Paul 	/* Create tag for statistics block */
2012f41ac2beSBill Paul 
2013f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2014f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2015f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2016f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2017f41ac2beSBill Paul 
2018f41ac2beSBill Paul 	if (error) {
2019fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2020f41ac2beSBill Paul 		return (ENOMEM);
2021f41ac2beSBill Paul 	}
2022f41ac2beSBill Paul 
2023f41ac2beSBill Paul 	/* Allocate DMA'able memory for statistics block */
2024f41ac2beSBill Paul 
2025f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2026f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2027f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2028f41ac2beSBill Paul 	if (error)
2029f41ac2beSBill Paul 		return (ENOMEM);
2030f41ac2beSBill Paul 
2031f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2032f41ac2beSBill Paul 
2033f41ac2beSBill Paul 	/* Load the address of the statstics block */
2034f41ac2beSBill Paul 
2035f41ac2beSBill Paul 	ctx.sc = sc;
2036f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2037f41ac2beSBill Paul 
2038f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2039f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2040f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2041f41ac2beSBill Paul 
2042f41ac2beSBill Paul 	if (error)
2043f41ac2beSBill Paul 		return (ENOMEM);
2044f41ac2beSBill Paul 
2045f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2046f41ac2beSBill Paul 
2047f41ac2beSBill Paul 	return(0);
2048f41ac2beSBill Paul }
2049f41ac2beSBill Paul 
205095d67482SBill Paul static int
205195d67482SBill Paul bge_attach(dev)
205295d67482SBill Paul 	device_t dev;
205395d67482SBill Paul {
205495d67482SBill Paul 	struct ifnet *ifp;
205595d67482SBill Paul 	struct bge_softc *sc;
2056a1d52896SBill Paul 	u_int32_t hwcfg = 0;
2057fc74a9f9SBrooks Davis 	u_int32_t mac_tmp = 0;
2058fc74a9f9SBrooks Davis 	u_char eaddr[6];
2059fe806fdaSPyun YongHyeon 	int error = 0, rid;
206095d67482SBill Paul 
206195d67482SBill Paul 	sc = device_get_softc(dev);
206295d67482SBill Paul 	sc->bge_dev = dev;
206395d67482SBill Paul 
206495d67482SBill Paul 	/*
206595d67482SBill Paul 	 * Map control/status registers.
206695d67482SBill Paul 	 */
206795d67482SBill Paul 	pci_enable_busmaster(dev);
206895d67482SBill Paul 
206995d67482SBill Paul 	rid = BGE_PCI_BAR0;
20705f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
20715f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
207295d67482SBill Paul 
207395d67482SBill Paul 	if (sc->bge_res == NULL) {
2074fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
207595d67482SBill Paul 		error = ENXIO;
207695d67482SBill Paul 		goto fail;
207795d67482SBill Paul 	}
207895d67482SBill Paul 
207995d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
208095d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
208195d67482SBill Paul 
208295d67482SBill Paul 	/* Allocate interrupt */
208395d67482SBill Paul 	rid = 0;
208495d67482SBill Paul 
20855f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
208695d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
208795d67482SBill Paul 
208895d67482SBill Paul 	if (sc->bge_irq == NULL) {
2089fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
209095d67482SBill Paul 		error = ENXIO;
209195d67482SBill Paul 		goto fail;
209295d67482SBill Paul 	}
209395d67482SBill Paul 
20940f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
20950f9bd73bSSam Leffler 
2096e53d81eeSPaul Saab 	/* Save ASIC rev. */
2097e53d81eeSPaul Saab 
2098e53d81eeSPaul Saab 	sc->bge_chipid =
2099e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2100e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2101e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2102e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2103e53d81eeSPaul Saab 
2104e53d81eeSPaul Saab 	/*
2105560c1670SGleb Smirnoff 	 * Treat the 5714 and the 5752 like the 5750 until we have more info
2106419c028bSPaul Saab 	 * on this chip.
2107419c028bSPaul Saab 	 */
2108560c1670SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5714 ||
2109560c1670SGleb Smirnoff             sc->bge_asicrev == BGE_ASICREV_BCM5752)
2110419c028bSPaul Saab 		sc->bge_asicrev = BGE_ASICREV_BCM5750;
2111419c028bSPaul Saab 
2112419c028bSPaul Saab 	/*
2113e53d81eeSPaul Saab 	 * XXX: Broadcom Linux driver.  Not in specs or eratta.
2114e53d81eeSPaul Saab 	 * PCI-Express?
2115e53d81eeSPaul Saab 	 */
2116e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
2117e53d81eeSPaul Saab 		u_int32_t v;
2118e53d81eeSPaul Saab 
2119e53d81eeSPaul Saab 		v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4);
2120e53d81eeSPaul Saab 		if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) {
2121e53d81eeSPaul Saab 			v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
2122e53d81eeSPaul Saab 			if ((v & 0xff) == BGE_PCIE_CAPID)
2123e53d81eeSPaul Saab 				sc->bge_pcie = 1;
2124e53d81eeSPaul Saab 		}
2125e53d81eeSPaul Saab 	}
2126e53d81eeSPaul Saab 
212795d67482SBill Paul 	/* Try to reset the chip. */
212895d67482SBill Paul 	bge_reset(sc);
212995d67482SBill Paul 
213095d67482SBill Paul 	if (bge_chipinit(sc)) {
2131fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
213295d67482SBill Paul 		bge_release_resources(sc);
213395d67482SBill Paul 		error = ENXIO;
213495d67482SBill Paul 		goto fail;
213595d67482SBill Paul 	}
213695d67482SBill Paul 
213795d67482SBill Paul 	/*
213895d67482SBill Paul 	 * Get station address from the EEPROM.
213995d67482SBill Paul 	 */
2140fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2141fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2142fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2143fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2144fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2145fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2146fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2147fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2148fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2149fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
215095d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2151fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
215295d67482SBill Paul 		bge_release_resources(sc);
215395d67482SBill Paul 		error = ENXIO;
215495d67482SBill Paul 		goto fail;
215595d67482SBill Paul 	}
215695d67482SBill Paul 
2157f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
2158e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2159e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
2160f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2161f41ac2beSBill Paul 	else
2162f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2163f41ac2beSBill Paul 
2164f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2165fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2166fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2167f41ac2beSBill Paul 		bge_release_resources(sc);
2168f41ac2beSBill Paul 		error = ENXIO;
2169f41ac2beSBill Paul 		goto fail;
2170f41ac2beSBill Paul 	}
2171f41ac2beSBill Paul 
217295d67482SBill Paul 	/* Set default tuneable values. */
217395d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
217495d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
217595d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
217695d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
217795d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
217895d67482SBill Paul 
217995d67482SBill Paul 	/* Set up ifnet structure */
2180fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2181fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2182fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2183fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2184fc74a9f9SBrooks Davis 		error = ENXIO;
2185fc74a9f9SBrooks Davis 		goto fail;
2186fc74a9f9SBrooks Davis 	}
218795d67482SBill Paul 	ifp->if_softc = sc;
21889bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
218995d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
219095d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
219195d67482SBill Paul 	ifp->if_start = bge_start;
219295d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
219395d67482SBill Paul 	ifp->if_init = bge_init;
219495d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
21954d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
21964d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
21974d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
219895d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2199d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
2200479b23b7SGleb Smirnoff 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM;
220195d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
220275719184SGleb Smirnoff #ifdef DEVICE_POLLING
220375719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
220475719184SGleb Smirnoff #endif
220595d67482SBill Paul 
2206a1d52896SBill Paul         /*
2207d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2208d375e524SGleb Smirnoff 	 * to hardware bugs.
2209d375e524SGleb Smirnoff 	 */
2210d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2211d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2212d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2213d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2214d375e524SGleb Smirnoff 	}
2215d375e524SGleb Smirnoff 
2216d375e524SGleb Smirnoff 	/*
2217a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
221841abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
221941abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
222041abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
222141abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
222241abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
222341abcc1bSPaul Saab 	 * SK-9D41.
2224a1d52896SBill Paul 	 */
222541abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
222641abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
222741abcc1bSPaul Saab 	else {
2228f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2229f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2230fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2231f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2232f6789fbaSPyun YongHyeon 			error = ENXIO;
2233f6789fbaSPyun YongHyeon 			goto fail;
2234f6789fbaSPyun YongHyeon 		}
223541abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
223641abcc1bSPaul Saab 	}
223741abcc1bSPaul Saab 
223841abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2239a1d52896SBill Paul 		sc->bge_tbi = 1;
2240a1d52896SBill Paul 
224195d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
224295d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
224395d67482SBill Paul 		sc->bge_tbi = 1;
224495d67482SBill Paul 
224595d67482SBill Paul 	if (sc->bge_tbi) {
224695d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
224795d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
224895d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
224995d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
225095d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
225195d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
225295d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2253da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
225495d67482SBill Paul 	} else {
225595d67482SBill Paul 		/*
225695d67482SBill Paul 		 * Do transceiver setup.
225795d67482SBill Paul 		 */
225895d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
225995d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
2260fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
226195d67482SBill Paul 			bge_release_resources(sc);
226295d67482SBill Paul 			error = ENXIO;
226395d67482SBill Paul 			goto fail;
226495d67482SBill Paul 		}
226595d67482SBill Paul 	}
226695d67482SBill Paul 
226795d67482SBill Paul 	/*
2268e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2269e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2270e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2271e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2272e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2273e255b776SJohn Polstra 	 * payloads by copying the received packets.
2274e255b776SJohn Polstra 	 */
2275e0ced696SPaul Saab 	switch (sc->bge_chipid) {
2276e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_A0:
2277e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B0:
2278e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B2:
2279e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B5:
2280e255b776SJohn Polstra 		/* If in PCI-X mode, work around the alignment bug. */
2281e255b776SJohn Polstra 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
2282e255b776SJohn Polstra 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2283e255b776SJohn Polstra 		    BGE_PCISTATE_PCI_BUSSPEED)
2284e255b776SJohn Polstra 			sc->bge_rx_alignment_bug = 1;
2285e255b776SJohn Polstra 		break;
2286e255b776SJohn Polstra 	}
2287e255b776SJohn Polstra 
2288e255b776SJohn Polstra 	/*
228995d67482SBill Paul 	 * Call MI attach routine.
229095d67482SBill Paul 	 */
2291fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
22920f9bd73bSSam Leffler 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
22930f9bd73bSSam Leffler 
22940f9bd73bSSam Leffler 	/*
22950f9bd73bSSam Leffler 	 * Hookup IRQ last.
22960f9bd73bSSam Leffler 	 */
22970f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
22980f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
22990f9bd73bSSam Leffler 
23000f9bd73bSSam Leffler 	if (error) {
2301fc74a9f9SBrooks Davis 		bge_detach(dev);
2302fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
23030f9bd73bSSam Leffler 	}
230495d67482SBill Paul 
230595d67482SBill Paul fail:
230695d67482SBill Paul 	return(error);
230795d67482SBill Paul }
230895d67482SBill Paul 
230995d67482SBill Paul static int
231095d67482SBill Paul bge_detach(dev)
231195d67482SBill Paul 	device_t dev;
231295d67482SBill Paul {
231395d67482SBill Paul 	struct bge_softc *sc;
231495d67482SBill Paul 	struct ifnet *ifp;
231595d67482SBill Paul 
231695d67482SBill Paul 	sc = device_get_softc(dev);
2317fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
231895d67482SBill Paul 
231975719184SGleb Smirnoff #ifdef DEVICE_POLLING
232075719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
232175719184SGleb Smirnoff 		ether_poll_deregister(ifp);
232275719184SGleb Smirnoff #endif
232375719184SGleb Smirnoff 
23240f9bd73bSSam Leffler 	BGE_LOCK(sc);
232595d67482SBill Paul 	bge_stop(sc);
232695d67482SBill Paul 	bge_reset(sc);
23270f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
23280f9bd73bSSam Leffler 
23290f9bd73bSSam Leffler 	ether_ifdetach(ifp);
233095d67482SBill Paul 
233195d67482SBill Paul 	if (sc->bge_tbi) {
233295d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
233395d67482SBill Paul 	} else {
233495d67482SBill Paul 		bus_generic_detach(dev);
233595d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
233695d67482SBill Paul 	}
233795d67482SBill Paul 
233895d67482SBill Paul 	bge_release_resources(sc);
233995d67482SBill Paul 
234095d67482SBill Paul 	return(0);
234195d67482SBill Paul }
234295d67482SBill Paul 
234395d67482SBill Paul static void
234495d67482SBill Paul bge_release_resources(sc)
234595d67482SBill Paul 	struct bge_softc *sc;
234695d67482SBill Paul {
234795d67482SBill Paul 	device_t dev;
234895d67482SBill Paul 
234995d67482SBill Paul 	dev = sc->bge_dev;
235095d67482SBill Paul 
235195d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
235295d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
235395d67482SBill Paul 
235495d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
235595d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
235695d67482SBill Paul 
235795d67482SBill Paul 	if (sc->bge_intrhand != NULL)
235895d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
235995d67482SBill Paul 
236095d67482SBill Paul 	if (sc->bge_irq != NULL)
236195d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
236295d67482SBill Paul 
236395d67482SBill Paul 	if (sc->bge_res != NULL)
236495d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
236595d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
236695d67482SBill Paul 
2367ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2368ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2369ad61f896SRuslan Ermilov 
2370f41ac2beSBill Paul 	bge_dma_free(sc);
237195d67482SBill Paul 
23720f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
23730f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
23740f9bd73bSSam Leffler 
237595d67482SBill Paul 	return;
237695d67482SBill Paul }
237795d67482SBill Paul 
237895d67482SBill Paul static void
237995d67482SBill Paul bge_reset(sc)
238095d67482SBill Paul 	struct bge_softc *sc;
238195d67482SBill Paul {
238295d67482SBill Paul 	device_t dev;
2383e53d81eeSPaul Saab 	u_int32_t cachesize, command, pcistate, reset;
238495d67482SBill Paul 	int i, val = 0;
238595d67482SBill Paul 
238695d67482SBill Paul 	dev = sc->bge_dev;
238795d67482SBill Paul 
238895d67482SBill Paul 	/* Save some important PCI state. */
238995d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
239095d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
239195d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
239295d67482SBill Paul 
239395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
239495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2395e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
239695d67482SBill Paul 
2397e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2398e53d81eeSPaul Saab 
2399e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2400e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2401e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2402e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2403e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2404e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2405e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2406e53d81eeSPaul Saab 			reset |= (1<<29);
2407e53d81eeSPaul Saab 		}
2408e53d81eeSPaul Saab 	}
2409e53d81eeSPaul Saab 
241095d67482SBill Paul 	/* Issue global reset */
2411e53d81eeSPaul Saab 	bge_writereg_ind(sc, BGE_MISC_CFG, reset);
241295d67482SBill Paul 
241395d67482SBill Paul 	DELAY(1000);
241495d67482SBill Paul 
2415e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2416e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2417e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2418e53d81eeSPaul Saab 			uint32_t v;
2419e53d81eeSPaul Saab 
2420e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2421e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2422e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2423e53d81eeSPaul Saab 		}
2424e53d81eeSPaul Saab 		/* Set PCIE max payload size and clear error status. */
2425e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2426e53d81eeSPaul Saab 	}
2427e53d81eeSPaul Saab 
242895d67482SBill Paul 	/* Reset some of the PCI state that got zapped by reset */
242995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
243095d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2431e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
243295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
243395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
243495d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
243595d67482SBill Paul 
2436a7b0c314SPaul Saab 	/* Enable memory arbiter. */
24375dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2438e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
2439a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2440a7b0c314SPaul Saab 
244195d67482SBill Paul 	/*
244295d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
244395d67482SBill Paul 	 * general communications memory at 0xB50.
244495d67482SBill Paul 	 */
244595d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
244695d67482SBill Paul 	/*
244795d67482SBill Paul 	 * Poll the value location we just wrote until
244895d67482SBill Paul 	 * we see the 1's complement of the magic number.
244995d67482SBill Paul 	 * This indicates that the firmware initialization
245095d67482SBill Paul 	 * is complete.
245195d67482SBill Paul 	 */
245295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
245395d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
245495d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
245595d67482SBill Paul 			break;
245695d67482SBill Paul 		DELAY(10);
245795d67482SBill Paul 	}
245895d67482SBill Paul 
245995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2460fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "firmware handshake timed out\n");
246195d67482SBill Paul 		return;
246295d67482SBill Paul 	}
246395d67482SBill Paul 
246495d67482SBill Paul 	/*
246595d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
246695d67482SBill Paul 	 * return to its original pre-reset state. This is a
246795d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
246895d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
246995d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
247095d67482SBill Paul 	 * results.
247195d67482SBill Paul 	 */
247295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
247395d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
247495d67482SBill Paul 			break;
247595d67482SBill Paul 		DELAY(10);
247695d67482SBill Paul 	}
247795d67482SBill Paul 
247895d67482SBill Paul 	/* Fix up byte swapping */
2479e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
248095d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
248195d67482SBill Paul 
248295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
248395d67482SBill Paul 
2484da3003f0SBill Paul 	/*
2485da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2486da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2487da3003f0SBill Paul 	 * to 1.2V.
2488da3003f0SBill Paul 	 */
2489da3003f0SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 && sc->bge_tbi) {
2490da3003f0SBill Paul 		uint32_t serdescfg;
2491da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2492da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2493da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2494da3003f0SBill Paul 	}
2495da3003f0SBill Paul 
2496e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2497e53d81eeSPaul Saab 	if (sc->bge_pcie && sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2498e53d81eeSPaul Saab 		uint32_t v;
2499e53d81eeSPaul Saab 
2500e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2501e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2502e53d81eeSPaul Saab 	}
250395d67482SBill Paul 	DELAY(10000);
250495d67482SBill Paul 
250595d67482SBill Paul 	return;
250695d67482SBill Paul }
250795d67482SBill Paul 
250895d67482SBill Paul /*
250995d67482SBill Paul  * Frame reception handling. This is called if there's a frame
251095d67482SBill Paul  * on the receive return list.
251195d67482SBill Paul  *
251295d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
25131be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
251495d67482SBill Paul  * 2) the frame is from the standard receive ring
251595d67482SBill Paul  */
251695d67482SBill Paul 
251795d67482SBill Paul static void
251895d67482SBill Paul bge_rxeof(sc)
251995d67482SBill Paul 	struct bge_softc *sc;
252095d67482SBill Paul {
252195d67482SBill Paul 	struct ifnet *ifp;
252295d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
252395d67482SBill Paul 
25240f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
25250f9bd73bSSam Leffler 
2526cfcb5025SOleg Bulyzhin 	/* Nothing to do */
2527cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
2528cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
2529cfcb5025SOleg Bulyzhin 		return;
2530cfcb5025SOleg Bulyzhin 
2531fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
253295d67482SBill Paul 
2533f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2534e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2535f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2536f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
25375dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2538e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2539f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2540f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2541f41ac2beSBill Paul 		    BUS_DMASYNC_POSTREAD);
2542f41ac2beSBill Paul 	}
2543f41ac2beSBill Paul 
254495d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2545f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
254695d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
254795d67482SBill Paul 		u_int32_t		rxidx;
254895d67482SBill Paul 		struct mbuf		*m = NULL;
254995d67482SBill Paul 		u_int16_t		vlan_tag = 0;
255095d67482SBill Paul 		int			have_tag = 0;
255195d67482SBill Paul 
255275719184SGleb Smirnoff #ifdef DEVICE_POLLING
255375719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
255475719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
255575719184SGleb Smirnoff 				break;
255675719184SGleb Smirnoff 			sc->rxcycles--;
255775719184SGleb Smirnoff 		}
255875719184SGleb Smirnoff #endif
255975719184SGleb Smirnoff 
256095d67482SBill Paul 		cur_rx =
2561f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
256295d67482SBill Paul 
256395d67482SBill Paul 		rxidx = cur_rx->bge_idx;
25640434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
256595d67482SBill Paul 
256695d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
256795d67482SBill Paul 			have_tag = 1;
256895d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
256995d67482SBill Paul 		}
257095d67482SBill Paul 
257195d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
257295d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2573f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2574f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2575f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2576f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2577f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
257895d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
257995d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
258095d67482SBill Paul 			jumbocnt++;
258195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
258295d67482SBill Paul 				ifp->if_ierrors++;
258395d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
258495d67482SBill Paul 				continue;
258595d67482SBill Paul 			}
258695d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
258795d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
258895d67482SBill Paul 				ifp->if_ierrors++;
258995d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
259095d67482SBill Paul 				continue;
259195d67482SBill Paul 			}
259295d67482SBill Paul 		} else {
259395d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2594f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2595f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2596f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2597f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2598f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
259995d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
260095d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
260195d67482SBill Paul 			stdcnt++;
260295d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
260395d67482SBill Paul 				ifp->if_ierrors++;
260495d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
260595d67482SBill Paul 				continue;
260695d67482SBill Paul 			}
260795d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
260895d67482SBill Paul 			    NULL) == ENOBUFS) {
260995d67482SBill Paul 				ifp->if_ierrors++;
261095d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
261195d67482SBill Paul 				continue;
261295d67482SBill Paul 			}
261395d67482SBill Paul 		}
261495d67482SBill Paul 
261595d67482SBill Paul 		ifp->if_ipackets++;
2616e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2617e255b776SJohn Polstra 		/*
2618e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2619e65bed95SPyun YongHyeon 		 * the payload is aligned.
2620e255b776SJohn Polstra 		 */
2621e255b776SJohn Polstra 		if (sc->bge_rx_alignment_bug) {
2622e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2623e255b776SJohn Polstra 			    cur_rx->bge_len);
2624e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2625e255b776SJohn Polstra 		}
2626e255b776SJohn Polstra #endif
2627473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
262895d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
262995d67482SBill Paul 
2630b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
263178178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
263295d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
263395d67482SBill Paul 				if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
263495d67482SBill Paul 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
263578178cd1SGleb Smirnoff 			}
2636d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2637d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
263895d67482SBill Paul 				m->m_pkthdr.csum_data =
263995d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
2640ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
2641ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
264295d67482SBill Paul 			}
264395d67482SBill Paul 		}
264495d67482SBill Paul 
264595d67482SBill Paul 		/*
2646673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2647673d9191SSam Leffler 		 * attach that information to the packet.
264895d67482SBill Paul 		 */
2649d147662cSGleb Smirnoff 		if (have_tag) {
2650d147662cSGleb Smirnoff 			VLAN_INPUT_TAG(ifp, m, vlan_tag);
2651d147662cSGleb Smirnoff 			if (m == NULL)
2652d147662cSGleb Smirnoff 				continue;
2653d147662cSGleb Smirnoff 		}
265495d67482SBill Paul 
26550f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2656673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
26570f9bd73bSSam Leffler 		BGE_LOCK(sc);
265895d67482SBill Paul 	}
265995d67482SBill Paul 
2660e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2661f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2662e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
26635dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2664e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2665e65bed95SPyun YongHyeon 		if (jumbocnt > 0)
2666f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2667f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_ring_map,
2668e65bed95SPyun YongHyeon 			    BUS_DMASYNC_PREWRITE);
2669f41ac2beSBill Paul 	}
2670f41ac2beSBill Paul 
267195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
267295d67482SBill Paul 	if (stdcnt)
267395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
267495d67482SBill Paul 	if (jumbocnt)
267595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
267695d67482SBill Paul }
267795d67482SBill Paul 
267895d67482SBill Paul static void
267995d67482SBill Paul bge_txeof(sc)
268095d67482SBill Paul 	struct bge_softc *sc;
268195d67482SBill Paul {
268295d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
268395d67482SBill Paul 	struct ifnet *ifp;
268495d67482SBill Paul 
26850f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
26860f9bd73bSSam Leffler 
2687cfcb5025SOleg Bulyzhin 	/* Nothing to do */
2688cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
2689cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
2690cfcb5025SOleg Bulyzhin 		return;
2691cfcb5025SOleg Bulyzhin 
2692fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
269395d67482SBill Paul 
2694e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2695e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2696e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
269795d67482SBill Paul 	/*
269895d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
269995d67482SBill Paul 	 * frames that have been sent.
270095d67482SBill Paul 	 */
270195d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2702f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
270395d67482SBill Paul 		u_int32_t		idx = 0;
270495d67482SBill Paul 
270595d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2706f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
270795d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
270895d67482SBill Paul 			ifp->if_opackets++;
270995d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2710e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2711e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2712e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2713f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2714f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2715e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2716e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
271795d67482SBill Paul 		}
271895d67482SBill Paul 		sc->bge_txcnt--;
271995d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
272095d67482SBill Paul 		ifp->if_timer = 0;
272195d67482SBill Paul 	}
272295d67482SBill Paul 
272395d67482SBill Paul 	if (cur_tx != NULL)
272413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
272595d67482SBill Paul }
272695d67482SBill Paul 
272775719184SGleb Smirnoff #ifdef DEVICE_POLLING
272875719184SGleb Smirnoff static void
272975719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
273075719184SGleb Smirnoff {
273175719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
273275719184SGleb Smirnoff 
273375719184SGleb Smirnoff 	BGE_LOCK(sc);
273475719184SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
273575719184SGleb Smirnoff 		bge_poll_locked(ifp, cmd, count);
273675719184SGleb Smirnoff 	BGE_UNLOCK(sc);
273775719184SGleb Smirnoff }
273875719184SGleb Smirnoff 
273975719184SGleb Smirnoff static void
274075719184SGleb Smirnoff bge_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
274175719184SGleb Smirnoff {
274275719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
2743366454f2SOleg Bulyzhin 	uint32_t statusword;
274475719184SGleb Smirnoff 
274575719184SGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
274675719184SGleb Smirnoff 
2747dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2748e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2749dab5cd05SOleg Bulyzhin 
2750dab5cd05SOleg Bulyzhin 	statusword = atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
2751dab5cd05SOleg Bulyzhin 
2752dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2753e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2754366454f2SOleg Bulyzhin 
2755366454f2SOleg Bulyzhin 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS cmd */
2756366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2757366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
2758366454f2SOleg Bulyzhin 
2759366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
2760366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2761366454f2SOleg Bulyzhin 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) ||
2762366454f2SOleg Bulyzhin 		    sc->bge_link_evt || sc->bge_tbi)
2763366454f2SOleg Bulyzhin 			bge_link_upd(sc);
2764366454f2SOleg Bulyzhin 
2765366454f2SOleg Bulyzhin 	sc->rxcycles = count;
2766366454f2SOleg Bulyzhin 	bge_rxeof(sc);
2767366454f2SOleg Bulyzhin 	bge_txeof(sc);
2768366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2769366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
277075719184SGleb Smirnoff }
277175719184SGleb Smirnoff #endif /* DEVICE_POLLING */
277275719184SGleb Smirnoff 
277395d67482SBill Paul static void
277495d67482SBill Paul bge_intr(xsc)
277595d67482SBill Paul 	void *xsc;
277695d67482SBill Paul {
277795d67482SBill Paul 	struct bge_softc *sc;
277895d67482SBill Paul 	struct ifnet *ifp;
2779dab5cd05SOleg Bulyzhin 	uint32_t statusword;
278095d67482SBill Paul 
278195d67482SBill Paul 	sc = xsc;
2782f41ac2beSBill Paul 
27830f9bd73bSSam Leffler 	BGE_LOCK(sc);
27840f9bd73bSSam Leffler 
2785dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2786dab5cd05SOleg Bulyzhin 
278775719184SGleb Smirnoff #ifdef DEVICE_POLLING
278875719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
278975719184SGleb Smirnoff 		BGE_UNLOCK(sc);
279075719184SGleb Smirnoff 		return;
279175719184SGleb Smirnoff 	}
279275719184SGleb Smirnoff #endif
279375719184SGleb Smirnoff 
2794f30cbfc6SScott Long 	/*
2795f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
2796f30cbfc6SScott Long 	 */
2797f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
2798f41ac2beSBill Paul 
279995d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
280095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
280195d67482SBill Paul 
2802f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
2803f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2804f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2805f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2806f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2807f30cbfc6SScott Long 
28081f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
28091f313773SOleg Bulyzhin 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) ||
2810f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
2811dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
281295d67482SBill Paul 
281313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
281495d67482SBill Paul 		/* Check RX return ring producer/consumer */
281595d67482SBill Paul 		bge_rxeof(sc);
281695d67482SBill Paul 
281795d67482SBill Paul 		/* Check TX ring producer/consumer */
281895d67482SBill Paul 		bge_txeof(sc);
281995d67482SBill Paul 	}
282095d67482SBill Paul 
282195d67482SBill Paul 	/* Re-enable interrupts. */
282295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
282395d67482SBill Paul 
282413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
282513f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
28260f9bd73bSSam Leffler 		bge_start_locked(ifp);
28270f9bd73bSSam Leffler 
28280f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
282995d67482SBill Paul 
283095d67482SBill Paul 	return;
283195d67482SBill Paul }
283295d67482SBill Paul 
283395d67482SBill Paul static void
28340f9bd73bSSam Leffler bge_tick_locked(sc)
283595d67482SBill Paul 	struct bge_softc *sc;
28360f9bd73bSSam Leffler {
283795d67482SBill Paul 	struct mii_data *mii = NULL;
283895d67482SBill Paul 
28390f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
284095d67482SBill Paul 
2841e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2842e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
28430434d1b8SBill Paul 		bge_stats_update_regs(sc);
28440434d1b8SBill Paul 	else
284595d67482SBill Paul 		bge_stats_update(sc);
284695d67482SBill Paul 
28471f313773SOleg Bulyzhin 	if (!sc->bge_tbi) {
284895d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
284995d67482SBill Paul 		mii_tick(mii);
28507b97099dSOleg Bulyzhin 	} else {
28517b97099dSOleg Bulyzhin 		/*
28527b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
28537b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
28547b97099dSOleg Bulyzhin 		 * and trigger interrupt.
28557b97099dSOleg Bulyzhin 		 */
28567b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
28577b97099dSOleg Bulyzhin 		/* In polling mode we poll link state in bge_poll_locked() */
28587b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
28597b97099dSOleg Bulyzhin #endif
28607b97099dSOleg Bulyzhin 		{
28617b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
28627b97099dSOleg Bulyzhin 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
28637b97099dSOleg Bulyzhin 		}
2864dab5cd05SOleg Bulyzhin 	}
286595d67482SBill Paul 
2866dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
286795d67482SBill Paul }
286895d67482SBill Paul 
286995d67482SBill Paul static void
28700f9bd73bSSam Leffler bge_tick(xsc)
28710f9bd73bSSam Leffler 	void *xsc;
28720f9bd73bSSam Leffler {
28730f9bd73bSSam Leffler 	struct bge_softc *sc;
28740f9bd73bSSam Leffler 
28750f9bd73bSSam Leffler 	sc = xsc;
28760f9bd73bSSam Leffler 
28770f9bd73bSSam Leffler 	BGE_LOCK(sc);
28780f9bd73bSSam Leffler 	bge_tick_locked(sc);
28790f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
28800f9bd73bSSam Leffler }
28810f9bd73bSSam Leffler 
28820f9bd73bSSam Leffler static void
28830434d1b8SBill Paul bge_stats_update_regs(sc)
28840434d1b8SBill Paul 	struct bge_softc *sc;
28850434d1b8SBill Paul {
28860434d1b8SBill Paul 	struct ifnet *ifp;
28870434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
28880434d1b8SBill Paul 	u_int32_t *s;
28896fb34dd2SOleg Bulyzhin 	u_long cnt;			/* current register value */
28900434d1b8SBill Paul 	int i;
28910434d1b8SBill Paul 
2892fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
28930434d1b8SBill Paul 
28940434d1b8SBill Paul 	s = (u_int32_t *)&stats;
28950434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
28960434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
28970434d1b8SBill Paul 		s++;
28980434d1b8SBill Paul 	}
28990434d1b8SBill Paul 
29006fb34dd2SOleg Bulyzhin 	cnt = stats.dot3StatsSingleCollisionFrames +
29010434d1b8SBill Paul 	    stats.dot3StatsMultipleCollisionFrames +
29020434d1b8SBill Paul 	    stats.dot3StatsExcessiveCollisions +
29036fb34dd2SOleg Bulyzhin 	    stats.dot3StatsLateCollisions;
29046fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
29056fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
29066fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
29070434d1b8SBill Paul }
29080434d1b8SBill Paul 
29090434d1b8SBill Paul static void
291095d67482SBill Paul bge_stats_update(sc)
291195d67482SBill Paul 	struct bge_softc *sc;
291295d67482SBill Paul {
291395d67482SBill Paul 	struct ifnet *ifp;
2914e907febfSPyun YongHyeon 	bus_size_t stats;
29156fb34dd2SOleg Bulyzhin 	u_long cnt;			/* current register value */
291695d67482SBill Paul 
2917fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
291895d67482SBill Paul 
2919e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2920e907febfSPyun YongHyeon 
2921e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
2922e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
292395d67482SBill Paul 
29246fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats,
29256fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsSingleCollisionFrames.bge_addr_lo);
29266fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
29276fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo);
29286fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
29296fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsExcessiveCollisions.bge_addr_lo);
29306fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
29316fb34dd2SOleg Bulyzhin 		txstats.dot3StatsLateCollisions.bge_addr_lo);
29326fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
29336fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
29346fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
29356fb34dd2SOleg Bulyzhin 
29366fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
29376fb34dd2SOleg Bulyzhin 	ifp->if_ierrors += cnt >= sc->bge_rx_discards ?
29386fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_rx_discards : cnt;
29396fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
29406fb34dd2SOleg Bulyzhin 
29416fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
29426fb34dd2SOleg Bulyzhin 	ifp->if_oerrors += cnt >= sc->bge_tx_discards ?
29436fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_discards : cnt;
29446fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
294595d67482SBill Paul 
2946e907febfSPyun YongHyeon #undef READ_STAT
294795d67482SBill Paul }
294895d67482SBill Paul 
294995d67482SBill Paul /*
2950d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
2951d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
2952d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
2953d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
2954d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
2955d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
2956d375e524SGleb Smirnoff  */
2957d375e524SGleb Smirnoff static __inline int
2958d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
2959d375e524SGleb Smirnoff {
2960d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
2961d375e524SGleb Smirnoff 	struct mbuf *last;
2962d375e524SGleb Smirnoff 
2963d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
2964d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
2965d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
2966d375e524SGleb Smirnoff 		last = m;
2967d375e524SGleb Smirnoff 	} else {
2968d375e524SGleb Smirnoff 		/*
2969d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
2970d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
2971d375e524SGleb Smirnoff 		 */
2972d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
2973d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
2974d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
2975d375e524SGleb Smirnoff 			struct mbuf *n;
2976d375e524SGleb Smirnoff 
2977d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
2978d375e524SGleb Smirnoff 			if (n == NULL)
2979d375e524SGleb Smirnoff 				return (ENOBUFS);
2980d375e524SGleb Smirnoff 			n->m_len = 0;
2981d375e524SGleb Smirnoff 			last->m_next = n;
2982d375e524SGleb Smirnoff 			last = n;
2983d375e524SGleb Smirnoff 		}
2984d375e524SGleb Smirnoff 	}
2985d375e524SGleb Smirnoff 
2986d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
2987d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
2988d375e524SGleb Smirnoff 	last->m_len += padlen;
2989d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
2990d375e524SGleb Smirnoff 
2991d375e524SGleb Smirnoff 	return (0);
2992d375e524SGleb Smirnoff }
2993d375e524SGleb Smirnoff 
2994d375e524SGleb Smirnoff /*
299595d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
299695d67482SBill Paul  * pointers to descriptors.
299795d67482SBill Paul  */
299895d67482SBill Paul static int
299995d67482SBill Paul bge_encap(sc, m_head, txidx)
300095d67482SBill Paul 	struct bge_softc *sc;
300195d67482SBill Paul 	struct mbuf *m_head;
30027e27542aSGleb Smirnoff 	uint32_t *txidx;
300395d67482SBill Paul {
30047e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
3005f41ac2beSBill Paul 	bus_dmamap_t		map;
30067e27542aSGleb Smirnoff 	struct bge_tx_bd	*d = NULL;
30077e27542aSGleb Smirnoff 	struct m_tag		*mtag;
30087e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
30097e27542aSGleb Smirnoff 	uint16_t		csum_flags = 0;
30107e27542aSGleb Smirnoff 	int			nsegs, i, error;
301195d67482SBill Paul 
301295d67482SBill Paul 	if (m_head->m_pkthdr.csum_flags) {
301395d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
301495d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3015d375e524SGleb Smirnoff 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
301695d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3017d375e524SGleb Smirnoff 			if (m_head->m_pkthdr.len < ETHER_MIN_NOPAD &&
3018d375e524SGleb Smirnoff 			    bge_cksum_pad(m_head) != 0)
3019d375e524SGleb Smirnoff 				return (ENOBUFS);
3020d375e524SGleb Smirnoff 		}
302195d67482SBill Paul 		if (m_head->m_flags & M_LASTFRAG)
302295d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
302395d67482SBill Paul 		else if (m_head->m_flags & M_FRAG)
302495d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
302595d67482SBill Paul 	}
302695d67482SBill Paul 
3027fc74a9f9SBrooks Davis 	mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m_head);
3028673d9191SSam Leffler 
30297e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
30307e27542aSGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map,
30317e27542aSGleb Smirnoff 	    m_head, segs, &nsegs, BUS_DMA_NOWAIT);
30327e27542aSGleb Smirnoff         if (error) {
30337e27542aSGleb Smirnoff 		if (error == EFBIG) {
30347e27542aSGleb Smirnoff 			struct mbuf *m0;
30357e27542aSGleb Smirnoff 
30367e27542aSGleb Smirnoff 			m0 = m_defrag(m_head, M_DONTWAIT);
30377e27542aSGleb Smirnoff 			if (m0 == NULL)
30387e27542aSGleb Smirnoff 				return (ENOBUFS);
30397e27542aSGleb Smirnoff 			m_head = m0;
30407e27542aSGleb Smirnoff 			error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag,
30417e27542aSGleb Smirnoff 			    map, m_head, segs, &nsegs, BUS_DMA_NOWAIT);
30427e27542aSGleb Smirnoff 		}
30437e27542aSGleb Smirnoff 		if (error)
30447e27542aSGleb Smirnoff 			return (error);
30457e27542aSGleb Smirnoff 	}
30467e27542aSGleb Smirnoff 
304795d67482SBill Paul 	/*
304895d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
304995d67482SBill Paul 	 * of the end of the ring.
305095d67482SBill Paul 	 */
30517e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
30527e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
305395d67482SBill Paul 		return (ENOBUFS);
30547e27542aSGleb Smirnoff 	}
30557e27542aSGleb Smirnoff 
3056e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3057e65bed95SPyun YongHyeon 
30587e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
30597e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
30607e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
30617e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
30627e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
30637e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
30647e27542aSGleb Smirnoff 		if (i == nsegs - 1)
30657e27542aSGleb Smirnoff 			break;
30667e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
30677e27542aSGleb Smirnoff 	}
30687e27542aSGleb Smirnoff 
30697e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
30707e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
30717e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
30727e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
30737e27542aSGleb Smirnoff 	if (mtag != NULL) {
30747e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
30757e27542aSGleb Smirnoff 		d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
30767e27542aSGleb Smirnoff 	} else
30777e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
3078f41ac2beSBill Paul 
3079f41ac2beSBill Paul 	/*
3080f41ac2beSBill Paul 	 * Insure that the map for this transmission
3081f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3082f41ac2beSBill Paul 	 * in this chain.
3083f41ac2beSBill Paul 	 */
30847e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
30857e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
30867e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m_head;
30877e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
308895d67482SBill Paul 
30897e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
30907e27542aSGleb Smirnoff 	*txidx = idx;
309195d67482SBill Paul 
309295d67482SBill Paul 	return (0);
309395d67482SBill Paul }
309495d67482SBill Paul 
309595d67482SBill Paul /*
309695d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
309795d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
309895d67482SBill Paul  */
309995d67482SBill Paul static void
31000f9bd73bSSam Leffler bge_start_locked(ifp)
310195d67482SBill Paul 	struct ifnet *ifp;
310295d67482SBill Paul {
310395d67482SBill Paul 	struct bge_softc *sc;
310495d67482SBill Paul 	struct mbuf *m_head = NULL;
310514bbd30fSGleb Smirnoff 	uint32_t prodidx;
3106303a718cSDag-Erling Smørgrav 	int count = 0;
310795d67482SBill Paul 
310895d67482SBill Paul 	sc = ifp->if_softc;
310995d67482SBill Paul 
3110dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
311195d67482SBill Paul 		return;
311295d67482SBill Paul 
311314bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
311495d67482SBill Paul 
311595d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
31164d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
311795d67482SBill Paul 		if (m_head == NULL)
311895d67482SBill Paul 			break;
311995d67482SBill Paul 
312095d67482SBill Paul 		/*
312195d67482SBill Paul 		 * XXX
3122b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3123b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3124b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3125b874fdd4SYaroslav Tykhiy 		 *
3126b874fdd4SYaroslav Tykhiy 		 * XXX
312795d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
312895d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
312995d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
313095d67482SBill Paul 		 * chain at once.
313195d67482SBill Paul 		 * (paranoia -- may not actually be needed)
313295d67482SBill Paul 		 */
313395d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
313495d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
313595d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
313695d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
31374d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
313813f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
313995d67482SBill Paul 				break;
314095d67482SBill Paul 			}
314195d67482SBill Paul 		}
314295d67482SBill Paul 
314395d67482SBill Paul 		/*
314495d67482SBill Paul 		 * Pack the data into the transmit ring. If we
314595d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
314695d67482SBill Paul 		 * for the NIC to drain the ring.
314795d67482SBill Paul 		 */
314895d67482SBill Paul 		if (bge_encap(sc, m_head, &prodidx)) {
31494d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
315013f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
315195d67482SBill Paul 			break;
315295d67482SBill Paul 		}
3153303a718cSDag-Erling Smørgrav 		++count;
315495d67482SBill Paul 
315595d67482SBill Paul 		/*
315695d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
315795d67482SBill Paul 		 * to him.
315895d67482SBill Paul 		 */
3159673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
316095d67482SBill Paul 	}
316195d67482SBill Paul 
3162303a718cSDag-Erling Smørgrav 	if (count == 0) {
3163303a718cSDag-Erling Smørgrav 		/* no packets were dequeued */
3164303a718cSDag-Erling Smørgrav 		return;
3165303a718cSDag-Erling Smørgrav 	}
3166303a718cSDag-Erling Smørgrav 
316795d67482SBill Paul 	/* Transmit */
316895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
31693927098fSPaul Saab 	/* 5700 b2 errata */
3170e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
31713927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
317295d67482SBill Paul 
317314bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
317414bbd30fSGleb Smirnoff 
317595d67482SBill Paul 	/*
317695d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
317795d67482SBill Paul 	 */
317895d67482SBill Paul 	ifp->if_timer = 5;
317995d67482SBill Paul 
318095d67482SBill Paul 	return;
318195d67482SBill Paul }
318295d67482SBill Paul 
31830f9bd73bSSam Leffler /*
31840f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
31850f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
31860f9bd73bSSam Leffler  */
318795d67482SBill Paul static void
31880f9bd73bSSam Leffler bge_start(ifp)
31890f9bd73bSSam Leffler 	struct ifnet *ifp;
319095d67482SBill Paul {
31910f9bd73bSSam Leffler 	struct bge_softc *sc;
31920f9bd73bSSam Leffler 
31930f9bd73bSSam Leffler 	sc = ifp->if_softc;
31940f9bd73bSSam Leffler 	BGE_LOCK(sc);
31950f9bd73bSSam Leffler 	bge_start_locked(ifp);
31960f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
31970f9bd73bSSam Leffler }
31980f9bd73bSSam Leffler 
31990f9bd73bSSam Leffler static void
32000f9bd73bSSam Leffler bge_init_locked(sc)
32010f9bd73bSSam Leffler 	struct bge_softc *sc;
32020f9bd73bSSam Leffler {
320395d67482SBill Paul 	struct ifnet *ifp;
320495d67482SBill Paul 	u_int16_t *m;
320595d67482SBill Paul 
32060f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
320795d67482SBill Paul 
3208fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
320995d67482SBill Paul 
321013f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
321195d67482SBill Paul 		return;
321295d67482SBill Paul 
321395d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
321495d67482SBill Paul 	bge_stop(sc);
321595d67482SBill Paul 	bge_reset(sc);
321695d67482SBill Paul 	bge_chipinit(sc);
321795d67482SBill Paul 
321895d67482SBill Paul 	/*
321995d67482SBill Paul 	 * Init the various state machines, ring
322095d67482SBill Paul 	 * control blocks and firmware.
322195d67482SBill Paul 	 */
322295d67482SBill Paul 	if (bge_blockinit(sc)) {
3223fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
322495d67482SBill Paul 		return;
322595d67482SBill Paul 	}
322695d67482SBill Paul 
3227fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
322895d67482SBill Paul 
322995d67482SBill Paul 	/* Specify MTU. */
323095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3231859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
323295d67482SBill Paul 
323395d67482SBill Paul 	/* Load our MAC address. */
32344a0d6638SRuslan Ermilov 	m = (u_int16_t *)IF_LLADDR(sc->bge_ifp);
323595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
323695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
323795d67482SBill Paul 
323895d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
323995d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
324095d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
324195d67482SBill Paul 	} else {
324295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
324395d67482SBill Paul 	}
324495d67482SBill Paul 
324595d67482SBill Paul 	/* Program multicast filter. */
324695d67482SBill Paul 	bge_setmulti(sc);
324795d67482SBill Paul 
324895d67482SBill Paul 	/* Init RX ring. */
324995d67482SBill Paul 	bge_init_rx_ring_std(sc);
325095d67482SBill Paul 
32510434d1b8SBill Paul 	/*
32520434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
32530434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
32540434d1b8SBill Paul 	 * entry of the ring.
32550434d1b8SBill Paul 	 */
32560434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
32570434d1b8SBill Paul 		u_int32_t		v, i;
32580434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
32590434d1b8SBill Paul 			DELAY(20);
32600434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
32610434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
32620434d1b8SBill Paul 				break;
32630434d1b8SBill Paul 		}
32640434d1b8SBill Paul 		if (i == 10)
3265fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3266fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
32670434d1b8SBill Paul 	}
32680434d1b8SBill Paul 
326995d67482SBill Paul 	/* Init jumbo RX ring. */
327095d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
327195d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
327295d67482SBill Paul 
327395d67482SBill Paul 	/* Init our RX return ring index */
327495d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
327595d67482SBill Paul 
327695d67482SBill Paul 	/* Init TX ring. */
327795d67482SBill Paul 	bge_init_tx_ring(sc);
327895d67482SBill Paul 
327995d67482SBill Paul 	/* Turn on transmitter */
328095d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
328195d67482SBill Paul 
328295d67482SBill Paul 	/* Turn on receiver */
328395d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
328495d67482SBill Paul 
328595d67482SBill Paul 	/* Tell firmware we're alive. */
328695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
328795d67482SBill Paul 
328875719184SGleb Smirnoff #ifdef DEVICE_POLLING
328975719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
329075719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
329175719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
329275719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
329375719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
329475719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
329575719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
329675719184SGleb Smirnoff 	} else
329775719184SGleb Smirnoff #endif
329875719184SGleb Smirnoff 
329995d67482SBill Paul 	/* Enable host interrupts. */
330075719184SGleb Smirnoff 	{
330195d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
330295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
330395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
330475719184SGleb Smirnoff 	}
330595d67482SBill Paul 
330695d67482SBill Paul 	bge_ifmedia_upd(ifp);
330795d67482SBill Paul 
330813f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
330913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
331095d67482SBill Paul 
33110f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
33120f9bd73bSSam Leffler }
33130f9bd73bSSam Leffler 
33140f9bd73bSSam Leffler static void
33150f9bd73bSSam Leffler bge_init(xsc)
33160f9bd73bSSam Leffler 	void *xsc;
33170f9bd73bSSam Leffler {
33180f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
33190f9bd73bSSam Leffler 
33200f9bd73bSSam Leffler 	BGE_LOCK(sc);
33210f9bd73bSSam Leffler 	bge_init_locked(sc);
33220f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
332395d67482SBill Paul 
332495d67482SBill Paul 	return;
332595d67482SBill Paul }
332695d67482SBill Paul 
332795d67482SBill Paul /*
332895d67482SBill Paul  * Set media options.
332995d67482SBill Paul  */
333095d67482SBill Paul static int
333195d67482SBill Paul bge_ifmedia_upd(ifp)
333295d67482SBill Paul 	struct ifnet *ifp;
333395d67482SBill Paul {
333495d67482SBill Paul 	struct bge_softc *sc;
333595d67482SBill Paul 	struct mii_data *mii;
333695d67482SBill Paul 	struct ifmedia *ifm;
333795d67482SBill Paul 
333895d67482SBill Paul 	sc = ifp->if_softc;
333995d67482SBill Paul 	ifm = &sc->bge_ifmedia;
334095d67482SBill Paul 
334195d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
334295d67482SBill Paul 	if (sc->bge_tbi) {
334395d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
334495d67482SBill Paul 			return(EINVAL);
334595d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
334695d67482SBill Paul 		case IFM_AUTO:
3347ff50922bSDoug White 			/*
3348ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3349ff50922bSDoug White 			 * mechanism for programming the autoneg
3350ff50922bSDoug White 			 * advertisement registers in TBI mode.
3351ff50922bSDoug White 			 */
3352c4529f41SMichael Reifenberger 			if (bge_fake_autoneg == 0 &&
3353c4529f41SMichael Reifenberger 			    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3354ff50922bSDoug White 				uint32_t sgdig;
3355ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3356ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3357ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3358ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3359ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3360ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3361ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3362ff50922bSDoug White 				DELAY(5);
3363ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3364ff50922bSDoug White 			}
336595d67482SBill Paul 			break;
336695d67482SBill Paul 		case IFM_1000_SX:
336795d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
336895d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
336995d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
337095d67482SBill Paul 			} else {
337195d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
337295d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
337395d67482SBill Paul 			}
337495d67482SBill Paul 			break;
337595d67482SBill Paul 		default:
337695d67482SBill Paul 			return(EINVAL);
337795d67482SBill Paul 		}
337895d67482SBill Paul 		return(0);
337995d67482SBill Paul 	}
338095d67482SBill Paul 
33811493e883SOleg Bulyzhin 	sc->bge_link_evt++;
338295d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
338395d67482SBill Paul 	if (mii->mii_instance) {
338495d67482SBill Paul 		struct mii_softc *miisc;
338595d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
338695d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
338795d67482SBill Paul 			mii_phy_reset(miisc);
338895d67482SBill Paul 	}
338995d67482SBill Paul 	mii_mediachg(mii);
339095d67482SBill Paul 
339195d67482SBill Paul 	return(0);
339295d67482SBill Paul }
339395d67482SBill Paul 
339495d67482SBill Paul /*
339595d67482SBill Paul  * Report current media status.
339695d67482SBill Paul  */
339795d67482SBill Paul static void
339895d67482SBill Paul bge_ifmedia_sts(ifp, ifmr)
339995d67482SBill Paul 	struct ifnet *ifp;
340095d67482SBill Paul 	struct ifmediareq *ifmr;
340195d67482SBill Paul {
340295d67482SBill Paul 	struct bge_softc *sc;
340395d67482SBill Paul 	struct mii_data *mii;
340495d67482SBill Paul 
340595d67482SBill Paul 	sc = ifp->if_softc;
340695d67482SBill Paul 
340795d67482SBill Paul 	if (sc->bge_tbi) {
340895d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
340995d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
341095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
341195d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
341295d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
341395d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
341495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
341595d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
341695d67482SBill Paul 		else
341795d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
341895d67482SBill Paul 		return;
341995d67482SBill Paul 	}
342095d67482SBill Paul 
342195d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
342295d67482SBill Paul 	mii_pollstat(mii);
342395d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
342495d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
342595d67482SBill Paul 
342695d67482SBill Paul 	return;
342795d67482SBill Paul }
342895d67482SBill Paul 
342995d67482SBill Paul static int
343095d67482SBill Paul bge_ioctl(ifp, command, data)
343195d67482SBill Paul 	struct ifnet *ifp;
343295d67482SBill Paul 	u_long command;
343395d67482SBill Paul 	caddr_t data;
343495d67482SBill Paul {
343595d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
343695d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
34370f9bd73bSSam Leffler 	int mask, error = 0;
343895d67482SBill Paul 	struct mii_data *mii;
343995d67482SBill Paul 
344095d67482SBill Paul 	switch(command) {
344195d67482SBill Paul 	case SIOCSIFMTU:
34420434d1b8SBill Paul 		/* Disallow jumbo frames on 5705. */
3443e53d81eeSPaul Saab 		if (((sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
3444e53d81eeSPaul Saab 		      sc->bge_asicrev == BGE_ASICREV_BCM5750) &&
34450434d1b8SBill Paul 		    ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU)
344695d67482SBill Paul 			error = EINVAL;
344795d67482SBill Paul 		else {
344895d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
344913f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
345095d67482SBill Paul 			bge_init(sc);
345195d67482SBill Paul 		}
345295d67482SBill Paul 		break;
345395d67482SBill Paul 	case SIOCSIFFLAGS:
34540f9bd73bSSam Leffler 		BGE_LOCK(sc);
345595d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
345695d67482SBill Paul 			/*
345795d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
345895d67482SBill Paul 			 * then just use the 'set promisc mode' command
345995d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
346095d67482SBill Paul 			 * a full re-init means reloading the firmware and
346195d67482SBill Paul 			 * waiting for it to start up, which may take a
3462d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
346395d67482SBill Paul 			 */
346413f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
346595d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
346695d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
346795d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
346895d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
346913f4c340SRobert Watson 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
347095d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
347195d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
347295d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
347395d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
3474d183af7fSRuslan Ermilov 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3475d183af7fSRuslan Ermilov 			    (ifp->if_flags ^ sc->bge_if_flags) & IFF_ALLMULTI) {
3476d183af7fSRuslan Ermilov 				bge_setmulti(sc);
347795d67482SBill Paul 			} else
34780f9bd73bSSam Leffler 				bge_init_locked(sc);
347995d67482SBill Paul 		} else {
348013f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
348195d67482SBill Paul 				bge_stop(sc);
348295d67482SBill Paul 			}
348395d67482SBill Paul 		}
348495d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
34850f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
348695d67482SBill Paul 		error = 0;
348795d67482SBill Paul 		break;
348895d67482SBill Paul 	case SIOCADDMULTI:
348995d67482SBill Paul 	case SIOCDELMULTI:
349013f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
34910f9bd73bSSam Leffler 			BGE_LOCK(sc);
349295d67482SBill Paul 			bge_setmulti(sc);
34930f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
349495d67482SBill Paul 			error = 0;
349595d67482SBill Paul 		}
349695d67482SBill Paul 		break;
349795d67482SBill Paul 	case SIOCSIFMEDIA:
349895d67482SBill Paul 	case SIOCGIFMEDIA:
349995d67482SBill Paul 		if (sc->bge_tbi) {
350095d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
350195d67482SBill Paul 			    &sc->bge_ifmedia, command);
350295d67482SBill Paul 		} else {
350395d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
350495d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
350595d67482SBill Paul 			    &mii->mii_media, command);
350695d67482SBill Paul 		}
350795d67482SBill Paul 		break;
350895d67482SBill Paul 	case SIOCSIFCAP:
350995d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
351075719184SGleb Smirnoff #ifdef DEVICE_POLLING
351175719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
351275719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
351375719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
351475719184SGleb Smirnoff 				if (error)
351575719184SGleb Smirnoff 					return(error);
351675719184SGleb Smirnoff 				BGE_LOCK(sc);
351775719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
351875719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
351975719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
352075719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
352175719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
352275719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
352375719184SGleb Smirnoff 				BGE_UNLOCK(sc);
352475719184SGleb Smirnoff 			} else {
352575719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
352675719184SGleb Smirnoff 				/* Enable interrupt even in error case */
352775719184SGleb Smirnoff 				BGE_LOCK(sc);
352875719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
352975719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
353075719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
353175719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
353275719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
353375719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
353475719184SGleb Smirnoff 				BGE_UNLOCK(sc);
353575719184SGleb Smirnoff 			}
353675719184SGleb Smirnoff 		}
353775719184SGleb Smirnoff #endif
3538d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3539d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3540d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3541d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3542b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
354395d67482SBill Paul 			else
3544b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
3545479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
354695d67482SBill Paul 		}
354795d67482SBill Paul 		break;
354895d67482SBill Paul 	default:
3549673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
355095d67482SBill Paul 		break;
355195d67482SBill Paul 	}
355295d67482SBill Paul 
355395d67482SBill Paul 	return(error);
355495d67482SBill Paul }
355595d67482SBill Paul 
355695d67482SBill Paul static void
355795d67482SBill Paul bge_watchdog(ifp)
355895d67482SBill Paul 	struct ifnet *ifp;
355995d67482SBill Paul {
356095d67482SBill Paul 	struct bge_softc *sc;
356195d67482SBill Paul 
356295d67482SBill Paul 	sc = ifp->if_softc;
356395d67482SBill Paul 
3564fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
356595d67482SBill Paul 
356613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
356795d67482SBill Paul 	bge_init(sc);
356895d67482SBill Paul 
356995d67482SBill Paul 	ifp->if_oerrors++;
357095d67482SBill Paul 
357195d67482SBill Paul 	return;
357295d67482SBill Paul }
357395d67482SBill Paul 
357495d67482SBill Paul /*
357595d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
357695d67482SBill Paul  * RX and TX lists.
357795d67482SBill Paul  */
357895d67482SBill Paul static void
357995d67482SBill Paul bge_stop(sc)
358095d67482SBill Paul 	struct bge_softc *sc;
358195d67482SBill Paul {
358295d67482SBill Paul 	struct ifnet *ifp;
358395d67482SBill Paul 	struct ifmedia_entry *ifm;
358495d67482SBill Paul 	struct mii_data *mii = NULL;
358595d67482SBill Paul 	int mtmp, itmp;
358695d67482SBill Paul 
35870f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
35880f9bd73bSSam Leffler 
3589fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
359095d67482SBill Paul 
359195d67482SBill Paul 	if (!sc->bge_tbi)
359295d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
359395d67482SBill Paul 
35940f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
359595d67482SBill Paul 
359695d67482SBill Paul 	/*
359795d67482SBill Paul 	 * Disable all of the receiver blocks
359895d67482SBill Paul 	 */
359995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
360095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
360195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
36025dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3603e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
360495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
360595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
360695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
360795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
360895d67482SBill Paul 
360995d67482SBill Paul 	/*
361095d67482SBill Paul 	 * Disable all of the transmit blocks
361195d67482SBill Paul 	 */
361295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
361395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
361495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
361595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
361695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
36175dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3618e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
361995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
362095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
362195d67482SBill Paul 
362295d67482SBill Paul 	/*
362395d67482SBill Paul 	 * Shut down all of the memory managers and related
362495d67482SBill Paul 	 * state machines.
362595d67482SBill Paul 	 */
362695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
362795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
36285dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3629e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
363095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
363195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
363295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
36335dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3634e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
363595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
363695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
36370434d1b8SBill Paul 	}
363895d67482SBill Paul 
363995d67482SBill Paul 	/* Disable host interrupts. */
364095d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
364195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
364295d67482SBill Paul 
364395d67482SBill Paul 	/*
364495d67482SBill Paul 	 * Tell firmware we're shutting down.
364595d67482SBill Paul 	 */
364695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
364795d67482SBill Paul 
364895d67482SBill Paul 	/* Free the RX lists. */
364995d67482SBill Paul 	bge_free_rx_ring_std(sc);
365095d67482SBill Paul 
365195d67482SBill Paul 	/* Free jumbo RX list. */
36525dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3653e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
365495d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
365595d67482SBill Paul 
365695d67482SBill Paul 	/* Free TX buffers. */
365795d67482SBill Paul 	bge_free_tx_ring(sc);
365895d67482SBill Paul 
365995d67482SBill Paul 	/*
366095d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
366195d67482SBill Paul 	 * unchanged so that things will be put back to normal when
366295d67482SBill Paul 	 * we bring the interface back up.
366395d67482SBill Paul 	 */
366495d67482SBill Paul 	if (!sc->bge_tbi) {
366595d67482SBill Paul 		itmp = ifp->if_flags;
366695d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3667dcc34049SPawel Jakub Dawidek 		/*
3668dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3669dcc34049SPawel Jakub Dawidek 		 */
3670dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
367195d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
367295d67482SBill Paul 			mtmp = ifm->ifm_media;
367395d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
367495d67482SBill Paul 			mii_mediachg(mii);
367595d67482SBill Paul 			ifm->ifm_media = mtmp;
3676dcc34049SPawel Jakub Dawidek 		}
367795d67482SBill Paul 		ifp->if_flags = itmp;
367895d67482SBill Paul 	}
367995d67482SBill Paul 
368095d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
368195d67482SBill Paul 
36821493e883SOleg Bulyzhin 	/*
36831493e883SOleg Bulyzhin 	 * We can't just call bge_link_upd() cause chip is almost stopped so
36841493e883SOleg Bulyzhin 	 * bge_link_upd -> bge_tick_locked -> bge_stats_update sequence may
36851493e883SOleg Bulyzhin 	 * lead to hardware deadlock. So we just clearing MAC's link state
36861493e883SOleg Bulyzhin 	 * (PHY may still have link UP).
36871493e883SOleg Bulyzhin 	 */
36881493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
36891493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
36901493e883SOleg Bulyzhin 	sc->bge_link = 0;
369195d67482SBill Paul 
36921493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
369395d67482SBill Paul }
369495d67482SBill Paul 
369595d67482SBill Paul /*
369695d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
369795d67482SBill Paul  * get confused by errant DMAs when rebooting.
369895d67482SBill Paul  */
369995d67482SBill Paul static void
370095d67482SBill Paul bge_shutdown(dev)
370195d67482SBill Paul 	device_t dev;
370295d67482SBill Paul {
370395d67482SBill Paul 	struct bge_softc *sc;
370495d67482SBill Paul 
370595d67482SBill Paul 	sc = device_get_softc(dev);
370695d67482SBill Paul 
37070f9bd73bSSam Leffler 	BGE_LOCK(sc);
370895d67482SBill Paul 	bge_stop(sc);
370995d67482SBill Paul 	bge_reset(sc);
37100f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
371195d67482SBill Paul 
371295d67482SBill Paul 	return;
371395d67482SBill Paul }
371414afefa3SPawel Jakub Dawidek 
371514afefa3SPawel Jakub Dawidek static int
371614afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
371714afefa3SPawel Jakub Dawidek {
371814afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
371914afefa3SPawel Jakub Dawidek 
372014afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
372114afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
372214afefa3SPawel Jakub Dawidek 	bge_stop(sc);
372314afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
372414afefa3SPawel Jakub Dawidek 
372514afefa3SPawel Jakub Dawidek 	return (0);
372614afefa3SPawel Jakub Dawidek }
372714afefa3SPawel Jakub Dawidek 
372814afefa3SPawel Jakub Dawidek static int
372914afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
373014afefa3SPawel Jakub Dawidek {
373114afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
373214afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
373314afefa3SPawel Jakub Dawidek 
373414afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
373514afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
373614afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
373714afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
373814afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
373914afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
374014afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
374114afefa3SPawel Jakub Dawidek 	}
374214afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
374314afefa3SPawel Jakub Dawidek 
374414afefa3SPawel Jakub Dawidek 	return (0);
374514afefa3SPawel Jakub Dawidek }
3746dab5cd05SOleg Bulyzhin 
3747dab5cd05SOleg Bulyzhin static void
3748dab5cd05SOleg Bulyzhin bge_link_upd(sc)
3749dab5cd05SOleg Bulyzhin 	struct bge_softc *sc;
3750dab5cd05SOleg Bulyzhin {
37511f313773SOleg Bulyzhin 	struct mii_data *mii;
37521f313773SOleg Bulyzhin 	uint32_t link, status;
3753dab5cd05SOleg Bulyzhin 
3754dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
37551f313773SOleg Bulyzhin 
37567b97099dSOleg Bulyzhin 	/* Clear 'pending link event' flag */
37577b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
37587b97099dSOleg Bulyzhin 
3759dab5cd05SOleg Bulyzhin 	/*
3760dab5cd05SOleg Bulyzhin 	 * Process link state changes.
3761dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
3762dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
3763dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
3764dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
3765dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
3766dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
3767dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
3768dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
37691f313773SOleg Bulyzhin 	 *
37701f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
37711f313773SOleg Bulyzhin 	 * BGE_CHIPID_BCM5700_B1 can be used for others BCM5700 revisions.
3772dab5cd05SOleg Bulyzhin 	 */
3773dab5cd05SOleg Bulyzhin 
37741f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
37751f313773SOleg Bulyzhin 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) {
3776dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
3777dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
3778dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3779dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
37801f313773SOleg Bulyzhin 
37811f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
37821f313773SOleg Bulyzhin 			if (!sc->bge_link &&
37831f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
37841f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
37851f313773SOleg Bulyzhin 				sc->bge_link++;
37861f313773SOleg Bulyzhin 				if (bootverbose)
37871f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
37881f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
37891f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
37901f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
37911f313773SOleg Bulyzhin 				sc->bge_link = 0;
37921f313773SOleg Bulyzhin 				if (bootverbose)
37931f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
37941f313773SOleg Bulyzhin 			}
37951f313773SOleg Bulyzhin 
3796dab5cd05SOleg Bulyzhin 			/* Clear the interrupt */
3797dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
3798dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
3799dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
3800dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
3801dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
3802dab5cd05SOleg Bulyzhin 		}
3803dab5cd05SOleg Bulyzhin 		return;
3804dab5cd05SOleg Bulyzhin 	}
3805dab5cd05SOleg Bulyzhin 
38061f313773SOleg Bulyzhin 	if (sc->bge_tbi) {
38071f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
38087b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
38097b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
38101f313773SOleg Bulyzhin 				sc->bge_link++;
38111f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
38121f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
38131f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
38141f313773SOleg Bulyzhin 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
38151f313773SOleg Bulyzhin 				if (bootverbose)
38161f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
38177b97099dSOleg Bulyzhin 				if_link_state_change(sc->bge_ifp, LINK_STATE_UP);
38187b97099dSOleg Bulyzhin 			}
38191f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
3820dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
38211f313773SOleg Bulyzhin 			if (bootverbose)
38221f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
38237b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
38241f313773SOleg Bulyzhin 		}
38251493e883SOleg Bulyzhin 	/* Discard link events for MII/GMII cards if MI auto-polling disabled */
38261493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
38271f313773SOleg Bulyzhin 		/*
38281f313773SOleg Bulyzhin 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
38291f313773SOleg Bulyzhin 		 * in status word always set. Workaround this bug by reading
38301f313773SOleg Bulyzhin 		 * PHY link status directly.
38311f313773SOleg Bulyzhin 		 */
38321f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
38331f313773SOleg Bulyzhin 
38341f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
38351f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
3836dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3837dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
38381f313773SOleg Bulyzhin 
38391f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
38401f313773SOleg Bulyzhin 			if (!sc->bge_link &&
38411f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
38421f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
38431f313773SOleg Bulyzhin 				sc->bge_link++;
38441f313773SOleg Bulyzhin 				if (bootverbose)
38451f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
38461f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
38471f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
38481f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
38491f313773SOleg Bulyzhin 				sc->bge_link = 0;
38501f313773SOleg Bulyzhin 				if (bootverbose)
38511f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
38521f313773SOleg Bulyzhin 			}
38531f313773SOleg Bulyzhin 		}
3854dab5cd05SOleg Bulyzhin 	}
3855dab5cd05SOleg Bulyzhin 
38561493e883SOleg Bulyzhin 	/* Clear the attention */
3857dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
3858dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
3859dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
3860dab5cd05SOleg Bulyzhin }
3861