xref: /freebsd/sys/dev/bge/if_bge.c (revision 3f74909a86bdb7d0e995b4e5dd0dfebfe5590e8b)
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/bus.h>
9995d67482SBill Paul #include <machine/resource.h>
10095d67482SBill Paul #include <sys/bus.h>
10195d67482SBill Paul #include <sys/rman.h>
10295d67482SBill Paul 
10395d67482SBill Paul #include <dev/mii/mii.h>
10495d67482SBill Paul #include <dev/mii/miivar.h>
1052d3ce713SDavid E. O'Brien #include "miidevs.h"
10695d67482SBill Paul #include <dev/mii/brgphyreg.h>
10795d67482SBill Paul 
1084fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1094fbd232cSWarner Losh #include <dev/pci/pcivar.h>
11095d67482SBill Paul 
11195d67482SBill Paul #include <dev/bge/if_bgereg.h>
11295d67482SBill Paul 
1135ddc5794SJohn Polstra #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
114d375e524SGleb Smirnoff #define ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
11595d67482SBill Paul 
116f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
117f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
11895d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
11995d67482SBill Paul 
1207b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12195d67482SBill Paul #include "miibus_if.h"
12295d67482SBill Paul 
12395d67482SBill Paul /*
12495d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
12595d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
12695d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
12795d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
12895d67482SBill Paul  */
129029e2ee3SJohn Polstra #define BGE_DEVDESC_MAX		64	/* Maximum device description length */
13095d67482SBill Paul 
13195d67482SBill Paul static struct bge_type bge_devs[] = {
13295d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5700,
13395d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
13495d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5701,
13595d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
13695d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5700,
13795d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
13895d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5701,
13995d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
1400434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702,
1410434d1b8SBill Paul 		"Broadcom BCM5702 Gigabit Ethernet" },
14201598b8dSMitsuru IWASAKI 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702X,
14301598b8dSMitsuru IWASAKI 		"Broadcom BCM5702X Gigabit Ethernet" },
1440434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703,
1450434d1b8SBill Paul 		"Broadcom BCM5703 Gigabit Ethernet" },
146b1265c1aSJohn Polstra 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703X,
147b1265c1aSJohn Polstra 		"Broadcom BCM5703X Gigabit Ethernet" },
1486ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704C,
1496ac6d2c8SPaul Saab 		"Broadcom BCM5704C Dual Gigabit Ethernet" },
1506ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704S,
1516ac6d2c8SPaul Saab 		"Broadcom BCM5704S Dual Gigabit Ethernet" },
1520434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705,
1530434d1b8SBill Paul 		"Broadcom BCM5705 Gigabit Ethernet" },
154c001ccf2SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705K,
155c001ccf2SPaul Saab 		"Broadcom BCM5705K Gigabit Ethernet" },
1560434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M,
1570434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
1580434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M_ALT,
1590434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
160419c028bSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5714C,
161419c028bSPaul Saab 		"Broadcom BCM5714C Gigabit Ethernet" },
16235ca8069SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5721,
16335ca8069SPaul Saab 		"Broadcom BCM5721 Gigabit Ethernet" },
164e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5750,
165e53d81eeSPaul Saab 		"Broadcom BCM5750 Gigabit Ethernet" },
166e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5750M,
167e53d81eeSPaul Saab 		"Broadcom BCM5750M Gigabit Ethernet" },
168e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5751,
169e53d81eeSPaul Saab 		"Broadcom BCM5751 Gigabit Ethernet" },
170d2014b30STai-hwa Liang 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5751M,
171d2014b30STai-hwa Liang 		"Broadcom BCM5751M Gigabit Ethernet" },
172560c1670SGleb Smirnoff 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5752,
173560c1670SGleb Smirnoff 		"Broadcom BCM5752 Gigabit Ethernet" },
1740434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5782,
1750434d1b8SBill Paul 		"Broadcom BCM5782 Gigabit Ethernet" },
1769f71a4c2SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5788,
1779f71a4c2SBill Paul 		"Broadcom BCM5788 Gigabit Ethernet" },
178c3615d48SMike Silbersack 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5789,
179c3615d48SMike Silbersack 		"Broadcom BCM5789 Gigabit Ethernet" },
1805d99c641SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901,
1815d99c641SBill Paul 		"Broadcom BCM5901 Fast Ethernet" },
1825d99c641SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901A2,
1835d99c641SBill Paul 		"Broadcom BCM5901A2 Fast Ethernet" },
18495d67482SBill Paul 	{ SK_VENDORID, SK_DEVICEID_ALTIMA,
18595d67482SBill Paul 		"SysKonnect Gigabit Ethernet" },
186586d7c2eSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000,
187586d7c2eSJohn Polstra 		"Altima AC1000 Gigabit Ethernet" },
1882aae6624SBill Paul 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1002,
1892aae6624SBill Paul 		"Altima AC1002 Gigabit Ethernet" },
190470bd96aSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100,
191470bd96aSJohn Polstra 		"Altima AC9100 Gigabit Ethernet" },
19295d67482SBill Paul 	{ 0, 0, NULL }
19395d67482SBill Paul };
19495d67482SBill Paul 
195e51a25f8SAlfred Perlstein static int bge_probe(device_t);
196e51a25f8SAlfred Perlstein static int bge_attach(device_t);
197e51a25f8SAlfred Perlstein static int bge_detach(device_t);
19814afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
19914afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
2003f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
201f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
202f41ac2beSBill Paul static int bge_dma_alloc(device_t);
203f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
204f41ac2beSBill Paul 
205e51a25f8SAlfred Perlstein static void bge_txeof(struct bge_softc *);
206e51a25f8SAlfred Perlstein static void bge_rxeof(struct bge_softc *);
20795d67482SBill Paul 
2080f9bd73bSSam Leffler static void bge_tick_locked(struct bge_softc *);
209e51a25f8SAlfred Perlstein static void bge_tick(void *);
210e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
2113f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
2123f74909aSGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf *, uint32_t *);
21395d67482SBill Paul 
214e51a25f8SAlfred Perlstein static void bge_intr(void *);
2150f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
216e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
217e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
2180f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
219e51a25f8SAlfred Perlstein static void bge_init(void *);
220e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
221e51a25f8SAlfred Perlstein static void bge_watchdog(struct ifnet *);
222e51a25f8SAlfred Perlstein static void bge_shutdown(device_t);
223e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
224e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
22595d67482SBill Paul 
2263f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
227e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
22895d67482SBill Paul 
229e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
23095d67482SBill Paul 
231e51a25f8SAlfred Perlstein static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *);
232e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
233e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
234e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
235e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
236e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
237e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
238e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
23995d67482SBill Paul 
240e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
241e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
24295d67482SBill Paul 
2431b4a3b2fSPeter Wemm #ifdef notdef
2443f74909aSGleb Smirnoff static uint8_t bge_vpd_readbyte(struct bge_softc *, int);
245e51a25f8SAlfred Perlstein static void bge_vpd_read_res(struct bge_softc *, struct vpd_res *, int);
246e51a25f8SAlfred Perlstein static void bge_vpd_read(struct bge_softc *);
2471b4a3b2fSPeter Wemm #endif
24895d67482SBill Paul 
2493f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
250e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
25195d67482SBill Paul #ifdef notdef
2523f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
25395d67482SBill Paul #endif
254e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
25595d67482SBill Paul 
256e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
257e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
258e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
25975719184SGleb Smirnoff #ifdef DEVICE_POLLING
2603f74909aSGleb Smirnoff static void bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
26175719184SGleb Smirnoff #endif
26295d67482SBill Paul 
263e51a25f8SAlfred Perlstein static void bge_reset(struct bge_softc *);
264dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
26595d67482SBill Paul 
26695d67482SBill Paul static device_method_t bge_methods[] = {
26795d67482SBill Paul 	/* Device interface */
26895d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
26995d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
27095d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
27195d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
27214afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
27314afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
27495d67482SBill Paul 
27595d67482SBill Paul 	/* bus interface */
27695d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
27795d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
27895d67482SBill Paul 
27995d67482SBill Paul 	/* MII interface */
28095d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
28195d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
28295d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
28395d67482SBill Paul 
28495d67482SBill Paul 	{ 0, 0 }
28595d67482SBill Paul };
28695d67482SBill Paul 
28795d67482SBill Paul static driver_t bge_driver = {
28895d67482SBill Paul 	"bge",
28995d67482SBill Paul 	bge_methods,
29095d67482SBill Paul 	sizeof(struct bge_softc)
29195d67482SBill Paul };
29295d67482SBill Paul 
29395d67482SBill Paul static devclass_t bge_devclass;
29495d67482SBill Paul 
295f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
29695d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
29795d67482SBill Paul 
298c4529f41SMichael Reifenberger static int bge_fake_autoneg = 0;
299c4529f41SMichael Reifenberger TUNABLE_INT("hw.bge.fake_autoneg", &bge_fake_autoneg);
300c4529f41SMichael Reifenberger 
3013f74909aSGleb Smirnoff static uint32_t
3023f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
30395d67482SBill Paul {
30495d67482SBill Paul 	device_t dev;
30595d67482SBill Paul 
30695d67482SBill Paul 	dev = sc->bge_dev;
30795d67482SBill Paul 
30895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
30995d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
31095d67482SBill Paul }
31195d67482SBill Paul 
31295d67482SBill Paul static void
3133f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
31495d67482SBill Paul {
31595d67482SBill Paul 	device_t dev;
31695d67482SBill Paul 
31795d67482SBill Paul 	dev = sc->bge_dev;
31895d67482SBill Paul 
31995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
32095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
32195d67482SBill Paul }
32295d67482SBill Paul 
32395d67482SBill Paul #ifdef notdef
3243f74909aSGleb Smirnoff static uint32_t
3253f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
32695d67482SBill Paul {
32795d67482SBill Paul 	device_t dev;
32895d67482SBill Paul 
32995d67482SBill Paul 	dev = sc->bge_dev;
33095d67482SBill Paul 
33195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
33295d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
33395d67482SBill Paul }
33495d67482SBill Paul #endif
33595d67482SBill Paul 
33695d67482SBill Paul static void
3373f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
33895d67482SBill Paul {
33995d67482SBill Paul 	device_t dev;
34095d67482SBill Paul 
34195d67482SBill Paul 	dev = sc->bge_dev;
34295d67482SBill Paul 
34395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
34495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
34595d67482SBill Paul }
34695d67482SBill Paul 
347f41ac2beSBill Paul /*
348f41ac2beSBill Paul  * Map a single buffer address.
349f41ac2beSBill Paul  */
350f41ac2beSBill Paul 
351f41ac2beSBill Paul static void
3523f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
353f41ac2beSBill Paul {
354f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
355f41ac2beSBill Paul 
356f41ac2beSBill Paul 	if (error)
357f41ac2beSBill Paul 		return;
358f41ac2beSBill Paul 
359f41ac2beSBill Paul 	ctx = arg;
360f41ac2beSBill Paul 
361f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
362f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
363f41ac2beSBill Paul 		return;
364f41ac2beSBill Paul 	}
365f41ac2beSBill Paul 
366f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
367f41ac2beSBill Paul }
368f41ac2beSBill Paul 
3691b4a3b2fSPeter Wemm #ifdef notdef
3703f74909aSGleb Smirnoff static uint8_t
3713f74909aSGleb Smirnoff bge_vpd_readbyte(struct bge_softc *sc, int addr)
37295d67482SBill Paul {
37395d67482SBill Paul 	int i;
37495d67482SBill Paul 	device_t dev;
3753f74909aSGleb Smirnoff 	uint32_t val;
37695d67482SBill Paul 
37795d67482SBill Paul 	dev = sc->bge_dev;
37895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
37995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
38095d67482SBill Paul 		DELAY(10);
38195d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
38295d67482SBill Paul 			break;
38395d67482SBill Paul 	}
38495d67482SBill Paul 
38595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
386fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "VPD read timed out\n");
38795d67482SBill Paul 		return (0);
38895d67482SBill Paul 	}
38995d67482SBill Paul 
39095d67482SBill Paul 	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
39195d67482SBill Paul 
39295d67482SBill Paul 	return ((val >> ((addr % 4) * 8)) & 0xFF);
39395d67482SBill Paul }
39495d67482SBill Paul 
39595d67482SBill Paul static void
3963f74909aSGleb Smirnoff bge_vpd_read_res(struct bge_softc *sc, struct vpd_res *res, int addr)
39795d67482SBill Paul {
39895d67482SBill Paul 	int i;
3993f74909aSGleb Smirnoff 	uint8_t *ptr;
40095d67482SBill Paul 
4013f74909aSGleb Smirnoff 	ptr = (uint8_t *)res;
40295d67482SBill Paul 	for (i = 0; i < sizeof(struct vpd_res); i++)
40395d67482SBill Paul 		ptr[i] = bge_vpd_readbyte(sc, i + addr);
40495d67482SBill Paul }
40595d67482SBill Paul 
40695d67482SBill Paul static void
4073f74909aSGleb Smirnoff bge_vpd_read(struct bge_softc *sc)
40895d67482SBill Paul {
40995d67482SBill Paul 	struct vpd_res res;
4103f74909aSGleb Smirnoff 	int i, pos = 0;
41195d67482SBill Paul 
41295d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
41395d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
41495d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
41595d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
41695d67482SBill Paul 	sc->bge_vpd_prodname = NULL;
41795d67482SBill Paul 	sc->bge_vpd_readonly = NULL;
41895d67482SBill Paul 
41995d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
42095d67482SBill Paul 
42195d67482SBill Paul 	if (res.vr_id != VPD_RES_ID) {
422fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
423fe806fdaSPyun YongHyeon 		    "bad VPD resource id: expected %x got %x\n", VPD_RES_ID,
424fe806fdaSPyun YongHyeon 		    res.vr_id);
42595d67482SBill Paul 		return;
42695d67482SBill Paul 	}
42795d67482SBill Paul 
42895d67482SBill Paul 	pos += sizeof(res);
42995d67482SBill Paul 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
43095d67482SBill Paul 	for (i = 0; i < res.vr_len; i++)
43195d67482SBill Paul 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
43295d67482SBill Paul 	sc->bge_vpd_prodname[i] = '\0';
43395d67482SBill Paul 	pos += i;
43495d67482SBill Paul 
43595d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
43695d67482SBill Paul 
43795d67482SBill Paul 	if (res.vr_id != VPD_RES_READ) {
438fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
439fe806fdaSPyun YongHyeon 		    "bad VPD resource id: expected %x got %x\n", VPD_RES_READ,
440fe806fdaSPyun YongHyeon 		    res.vr_id);
44195d67482SBill Paul 		return;
44295d67482SBill Paul 	}
44395d67482SBill Paul 
44495d67482SBill Paul 	pos += sizeof(res);
44595d67482SBill Paul 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
44695d67482SBill Paul 	for (i = 0; i < res.vr_len + 1; i++)
44795d67482SBill Paul 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
44895d67482SBill Paul }
4491b4a3b2fSPeter Wemm #endif
45095d67482SBill Paul 
45195d67482SBill Paul /*
45295d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
45395d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
45495d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
45595d67482SBill Paul  * access method.
45695d67482SBill Paul  */
4573f74909aSGleb Smirnoff static uint8_t
4583f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
45995d67482SBill Paul {
46095d67482SBill Paul 	int i;
4613f74909aSGleb Smirnoff 	uint32_t byte = 0;
46295d67482SBill Paul 
46395d67482SBill Paul 	/*
46495d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
46595d67482SBill Paul 	 * having to use the bitbang method.
46695d67482SBill Paul 	 */
46795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
46895d67482SBill Paul 
46995d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
47095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
47195d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
47295d67482SBill Paul 	DELAY(20);
47395d67482SBill Paul 
47495d67482SBill Paul 	/* Issue the read EEPROM command. */
47595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
47695d67482SBill Paul 
47795d67482SBill Paul 	/* Wait for completion */
47895d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
47995d67482SBill Paul 		DELAY(10);
48095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
48195d67482SBill Paul 			break;
48295d67482SBill Paul 	}
48395d67482SBill Paul 
48495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
485fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
486f6789fbaSPyun YongHyeon 		return (1);
48795d67482SBill Paul 	}
48895d67482SBill Paul 
48995d67482SBill Paul 	/* Get result. */
49095d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
49195d67482SBill Paul 
49295d67482SBill Paul 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
49395d67482SBill Paul 
49495d67482SBill Paul 	return (0);
49595d67482SBill Paul }
49695d67482SBill Paul 
49795d67482SBill Paul /*
49895d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
49995d67482SBill Paul  */
50095d67482SBill Paul static int
5013f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
50295d67482SBill Paul {
5033f74909aSGleb Smirnoff 	int i, error = 0;
5043f74909aSGleb Smirnoff 	uint8_t byte = 0;
50595d67482SBill Paul 
50695d67482SBill Paul 	for (i = 0; i < cnt; i++) {
5073f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
5083f74909aSGleb Smirnoff 		if (error)
50995d67482SBill Paul 			break;
51095d67482SBill Paul 		*(dest + i) = byte;
51195d67482SBill Paul 	}
51295d67482SBill Paul 
5133f74909aSGleb Smirnoff 	return (error ? 1 : 0);
51495d67482SBill Paul }
51595d67482SBill Paul 
51695d67482SBill Paul static int
5173f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
51895d67482SBill Paul {
51995d67482SBill Paul 	struct bge_softc *sc;
5203f74909aSGleb Smirnoff 	uint32_t val, autopoll;
52195d67482SBill Paul 	int i;
52295d67482SBill Paul 
52395d67482SBill Paul 	sc = device_get_softc(dev);
52495d67482SBill Paul 
5250434d1b8SBill Paul 	/*
5260434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5270434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5280434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5290434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5300434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5310434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5320434d1b8SBill Paul 	 * special-cased.
5330434d1b8SBill Paul 	 */
534b1265c1aSJohn Polstra 	if (phy != 1)
53598b28ee5SBill Paul 		return (0);
53698b28ee5SBill Paul 
53737ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
53837ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
53937ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
54037ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
54137ceeb4dSPaul Saab 		DELAY(40);
54237ceeb4dSPaul Saab 	}
54337ceeb4dSPaul Saab 
54495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
54595d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
54695d67482SBill Paul 
54795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
54895d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
54995d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
55095d67482SBill Paul 			break;
55195d67482SBill Paul 	}
55295d67482SBill Paul 
55395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
554fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
55537ceeb4dSPaul Saab 		val = 0;
55637ceeb4dSPaul Saab 		goto done;
55795d67482SBill Paul 	}
55895d67482SBill Paul 
55995d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
56095d67482SBill Paul 
56137ceeb4dSPaul Saab done:
56237ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
56337ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
56437ceeb4dSPaul Saab 		DELAY(40);
56537ceeb4dSPaul Saab 	}
56637ceeb4dSPaul Saab 
56795d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
56895d67482SBill Paul 		return (0);
56995d67482SBill Paul 
57095d67482SBill Paul 	return (val & 0xFFFF);
57195d67482SBill Paul }
57295d67482SBill Paul 
57395d67482SBill Paul static int
5743f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
57595d67482SBill Paul {
57695d67482SBill Paul 	struct bge_softc *sc;
5773f74909aSGleb Smirnoff 	uint32_t autopoll;
57895d67482SBill Paul 	int i;
57995d67482SBill Paul 
58095d67482SBill Paul 	sc = device_get_softc(dev);
58195d67482SBill Paul 
58237ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
58337ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
58437ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
58537ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
58637ceeb4dSPaul Saab 		DELAY(40);
58737ceeb4dSPaul Saab 	}
58837ceeb4dSPaul Saab 
58995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
59095d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
59195d67482SBill Paul 
59295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
59395d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
59495d67482SBill Paul 			break;
59595d67482SBill Paul 	}
59695d67482SBill Paul 
59737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
59837ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
59937ceeb4dSPaul Saab 		DELAY(40);
60037ceeb4dSPaul Saab 	}
60137ceeb4dSPaul Saab 
60295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
603fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
60495d67482SBill Paul 		return (0);
60595d67482SBill Paul 	}
60695d67482SBill Paul 
60795d67482SBill Paul 	return (0);
60895d67482SBill Paul }
60995d67482SBill Paul 
61095d67482SBill Paul static void
6113f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
61295d67482SBill Paul {
61395d67482SBill Paul 	struct bge_softc *sc;
61495d67482SBill Paul 	struct mii_data *mii;
61595d67482SBill Paul 
61695d67482SBill Paul 	sc = device_get_softc(dev);
61795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
61895d67482SBill Paul 
61995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
6203f74909aSGleb Smirnoff 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
62195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
6223f74909aSGleb Smirnoff 	else
62395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
62495d67482SBill Paul 
6253f74909aSGleb Smirnoff 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
62695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
6273f74909aSGleb Smirnoff 	else
62895d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
62995d67482SBill Paul }
63095d67482SBill Paul 
63195d67482SBill Paul /*
63295d67482SBill Paul  * Intialize a standard receive ring descriptor.
63395d67482SBill Paul  */
63495d67482SBill Paul static int
6353f74909aSGleb Smirnoff bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m)
63695d67482SBill Paul {
63795d67482SBill Paul 	struct mbuf *m_new = NULL;
63895d67482SBill Paul 	struct bge_rx_bd *r;
639f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
640f41ac2beSBill Paul 	int error;
64195d67482SBill Paul 
64295d67482SBill Paul 	if (m == NULL) {
643c3a56752SGleb Smirnoff 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
644c3a56752SGleb Smirnoff 		if (m_new == NULL)
64595d67482SBill Paul 			return (ENOBUFS);
64695d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
64795d67482SBill Paul 	} else {
64895d67482SBill Paul 		m_new = m;
64995d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
65095d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
65195d67482SBill Paul 	}
65295d67482SBill Paul 
653e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
65495d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
65595d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
656f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
657f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
658f41ac2beSBill Paul 	ctx.sc = sc;
659f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
660f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
661f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
662f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
663f7cea149SGleb Smirnoff 		if (m == NULL) {
664f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
665f41ac2beSBill Paul 			m_freem(m_new);
666f7cea149SGleb Smirnoff 		}
667f41ac2beSBill Paul 		return (ENOMEM);
668f41ac2beSBill Paul 	}
669e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
670e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
671e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
672e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
673e907febfSPyun YongHyeon 	r->bge_idx = i;
674f41ac2beSBill Paul 
675f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
676f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
677f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
67895d67482SBill Paul 
67995d67482SBill Paul 	return (0);
68095d67482SBill Paul }
68195d67482SBill Paul 
68295d67482SBill Paul /*
68395d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
68495d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
68595d67482SBill Paul  */
68695d67482SBill Paul static int
6873f74909aSGleb Smirnoff bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
68895d67482SBill Paul {
6891be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
6901be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
69195d67482SBill Paul 	struct mbuf *m_new = NULL;
6921be6acb7SGleb Smirnoff 	int nsegs;
693f41ac2beSBill Paul 	int error;
69495d67482SBill Paul 
69595d67482SBill Paul 	if (m == NULL) {
696a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
6971be6acb7SGleb Smirnoff 		if (m_new == NULL)
69895d67482SBill Paul 			return (ENOBUFS);
69995d67482SBill Paul 
7001be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7011be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
70295d67482SBill Paul 			m_freem(m_new);
70395d67482SBill Paul 			return (ENOBUFS);
70495d67482SBill Paul 		}
7051be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
70695d67482SBill Paul 	} else {
70795d67482SBill Paul 		m_new = m;
7081be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
70995d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
71095d67482SBill Paul 	}
71195d67482SBill Paul 
712e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
71395d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7141be6acb7SGleb Smirnoff 
7151be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7161be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7171be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7181be6acb7SGleb Smirnoff 	if (error) {
7191be6acb7SGleb Smirnoff 		if (m == NULL)
720f41ac2beSBill Paul 			m_freem(m_new);
7211be6acb7SGleb Smirnoff 		return (error);
722f7cea149SGleb Smirnoff 	}
7231be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
7241be6acb7SGleb Smirnoff 
7251be6acb7SGleb Smirnoff 	/*
7261be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
7271be6acb7SGleb Smirnoff 	 */
7281be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
7294e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING|BGE_RXBDFLAG_END;
7304e7ba1abSGleb Smirnoff 	r->bge_idx = i;
7314e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
7324e7ba1abSGleb Smirnoff 	switch (nsegs) {
7334e7ba1abSGleb Smirnoff 	case 4:
7344e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
7354e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
7364e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
7374e7ba1abSGleb Smirnoff 	case 3:
738e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
739e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
740e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
7414e7ba1abSGleb Smirnoff 	case 2:
7424e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
7434e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
7444e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
7454e7ba1abSGleb Smirnoff 	case 1:
7464e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
7474e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
7484e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
7494e7ba1abSGleb Smirnoff 		break;
7504e7ba1abSGleb Smirnoff 	default:
7514e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
7524e7ba1abSGleb Smirnoff 	}
753f41ac2beSBill Paul 
754f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
755f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
756f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
75795d67482SBill Paul 
75895d67482SBill Paul 	return (0);
75995d67482SBill Paul }
76095d67482SBill Paul 
76195d67482SBill Paul /*
76295d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
76395d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
76495d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
76595d67482SBill Paul  * the NIC.
76695d67482SBill Paul  */
76795d67482SBill Paul static int
7683f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
76995d67482SBill Paul {
77095d67482SBill Paul 	int i;
77195d67482SBill Paul 
77295d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
77395d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
77495d67482SBill Paul 			return (ENOBUFS);
77595d67482SBill Paul 	};
77695d67482SBill Paul 
777f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
778f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
779f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
780f41ac2beSBill Paul 
78195d67482SBill Paul 	sc->bge_std = i - 1;
78295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
78395d67482SBill Paul 
78495d67482SBill Paul 	return (0);
78595d67482SBill Paul }
78695d67482SBill Paul 
78795d67482SBill Paul static void
7883f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
78995d67482SBill Paul {
79095d67482SBill Paul 	int i;
79195d67482SBill Paul 
79295d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
79395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
794e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
795e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
796e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
797f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
798f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
799e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
800e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
80195d67482SBill Paul 		}
802f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
80395d67482SBill Paul 		    sizeof(struct bge_rx_bd));
80495d67482SBill Paul 	}
80595d67482SBill Paul }
80695d67482SBill Paul 
80795d67482SBill Paul static int
8083f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
80995d67482SBill Paul {
81095d67482SBill Paul 	struct bge_rcb *rcb;
8111be6acb7SGleb Smirnoff 	int i;
81295d67482SBill Paul 
81395d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
81495d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
81595d67482SBill Paul 			return (ENOBUFS);
81695d67482SBill Paul 	};
81795d67482SBill Paul 
818f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
819f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
820f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
821f41ac2beSBill Paul 
82295d67482SBill Paul 	sc->bge_jumbo = i - 1;
82395d67482SBill Paul 
824f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8251be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8261be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
82767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
82895d67482SBill Paul 
82995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
83095d67482SBill Paul 
83195d67482SBill Paul 	return (0);
83295d67482SBill Paul }
83395d67482SBill Paul 
83495d67482SBill Paul static void
8353f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
83695d67482SBill Paul {
83795d67482SBill Paul 	int i;
83895d67482SBill Paul 
83995d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
84095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
841e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
842e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
843e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
844f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
845f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
846e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
847e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
84895d67482SBill Paul 		}
849f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
8501be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
85195d67482SBill Paul 	}
85295d67482SBill Paul }
85395d67482SBill Paul 
85495d67482SBill Paul static void
8553f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
85695d67482SBill Paul {
85795d67482SBill Paul 	int i;
85895d67482SBill Paul 
859f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
86095d67482SBill Paul 		return;
86195d67482SBill Paul 
86295d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
86395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
864e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
865e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
866e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
867f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
868f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
869e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
870e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
87195d67482SBill Paul 		}
872f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
87395d67482SBill Paul 		    sizeof(struct bge_tx_bd));
87495d67482SBill Paul 	}
87595d67482SBill Paul }
87695d67482SBill Paul 
87795d67482SBill Paul static int
8783f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
87995d67482SBill Paul {
88095d67482SBill Paul 	sc->bge_txcnt = 0;
88195d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
8823927098fSPaul Saab 
88314bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
88414bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
88514bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
88614bbd30fSGleb Smirnoff 
8873927098fSPaul Saab 	/* 5700 b2 errata */
888e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
88914bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
8903927098fSPaul Saab 
89114bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
8923927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
8933927098fSPaul Saab 	/* 5700 b2 errata */
894e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
89595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
89695d67482SBill Paul 
89795d67482SBill Paul 	return (0);
89895d67482SBill Paul }
89995d67482SBill Paul 
90095d67482SBill Paul static void
9013f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
90295d67482SBill Paul {
90395d67482SBill Paul 	struct ifnet *ifp;
90495d67482SBill Paul 	struct ifmultiaddr *ifma;
9053f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
90695d67482SBill Paul 	int h, i;
90795d67482SBill Paul 
9080f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9090f9bd73bSSam Leffler 
910fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
91195d67482SBill Paul 
91295d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
91395d67482SBill Paul 		for (i = 0; i < 4; i++)
91495d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
91595d67482SBill Paul 		return;
91695d67482SBill Paul 	}
91795d67482SBill Paul 
91895d67482SBill Paul 	/* First, zot all the existing filters. */
91995d67482SBill Paul 	for (i = 0; i < 4; i++)
92095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
92195d67482SBill Paul 
92295d67482SBill Paul 	/* Now program new ones. */
92313b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
92495d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
92595d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
92695d67482SBill Paul 			continue;
9270e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
9280e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
92995d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
93095d67482SBill Paul 	}
93113b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
93295d67482SBill Paul 
93395d67482SBill Paul 	for (i = 0; i < 4; i++)
93495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
93595d67482SBill Paul }
93695d67482SBill Paul 
93795d67482SBill Paul /*
93895d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
93995d67482SBill Paul  * self-test results.
94095d67482SBill Paul  */
94195d67482SBill Paul static int
9423f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
94395d67482SBill Paul {
9443f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
94595d67482SBill Paul 	int i;
94695d67482SBill Paul 
947e907febfSPyun YongHyeon 	/* Set endian type before we access any non-PCI registers. */
948e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
94995d67482SBill Paul 
95095d67482SBill Paul 	/*
95195d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
95295d67482SBill Paul 	 * self-tests passed.
95395d67482SBill Paul 	 */
95495d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
955fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
95695d67482SBill Paul 		return (ENODEV);
95795d67482SBill Paul 	}
95895d67482SBill Paul 
95995d67482SBill Paul 	/* Clear the MAC control register */
96095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
96195d67482SBill Paul 
96295d67482SBill Paul 	/*
96395d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
96495d67482SBill Paul 	 * internal memory.
96595d67482SBill Paul 	 */
96695d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
9673f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
96895d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
96995d67482SBill Paul 
97095d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
9713f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
97295d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
97395d67482SBill Paul 
97495d67482SBill Paul 	/* Set up the PCI DMA control register. */
975e53d81eeSPaul Saab 	if (sc->bge_pcie) {
976e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
977e53d81eeSPaul Saab 		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
978e53d81eeSPaul Saab 		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
979e53d81eeSPaul Saab 	} else if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
9808287860eSJohn Polstra 	    BGE_PCISTATE_PCI_BUSMODE) {
9818287860eSJohn Polstra 		/* Conventional PCI bus */
9825cba12d3SPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
9835cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
9845cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
9855cba12d3SPaul Saab 		    (0x0F);
9868287860eSJohn Polstra 	} else {
9878287860eSJohn Polstra 		/* PCI-X bus */
9885cba12d3SPaul Saab 		/*
9895cba12d3SPaul Saab 		 * The 5704 uses a different encoding of read/write
9905cba12d3SPaul Saab 		 * watermarks.
9915cba12d3SPaul Saab 		 */
992e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
9935cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
9945cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
9955cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
9965cba12d3SPaul Saab 		else
9975cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
9985cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
9995cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10005cba12d3SPaul Saab 			    (0x0F);
10015cba12d3SPaul Saab 
10025cba12d3SPaul Saab 		/*
10035cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
10045cba12d3SPaul Saab 		 * for hardware bugs.
10055cba12d3SPaul Saab 		 */
1006e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1007e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
10083f74909aSGleb Smirnoff 			uint32_t tmp;
10095cba12d3SPaul Saab 
10105cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
10115cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
10125cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
10138287860eSJohn Polstra 		}
10145cba12d3SPaul Saab 	}
10155cba12d3SPaul Saab 
1016e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
10170434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
1018e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1019e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
10205cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
10215cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
102295d67482SBill Paul 
102395d67482SBill Paul 	/*
102495d67482SBill Paul 	 * Set up general mode register.
102595d67482SBill Paul 	 */
1026e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
102795d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1028ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
102995d67482SBill Paul 
103095d67482SBill Paul 	/*
1031ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1032ea13bdd5SJohn Polstra 	 * properly by these devices.
103395d67482SBill Paul 	 */
1034ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
103595d67482SBill Paul 
103695d67482SBill Paul #ifdef __brokenalpha__
103795d67482SBill Paul 	/*
103895d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
103995d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
104095d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
104195d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
104295d67482SBill Paul 	 */
104362f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
104462f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
104595d67482SBill Paul #endif
104695d67482SBill Paul 
104795d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
104895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
104995d67482SBill Paul 
105095d67482SBill Paul 	return (0);
105195d67482SBill Paul }
105295d67482SBill Paul 
105395d67482SBill Paul static int
10543f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
105595d67482SBill Paul {
105695d67482SBill Paul 	struct bge_rcb *rcb;
1057e907febfSPyun YongHyeon 	bus_size_t vrcb;
1058e907febfSPyun YongHyeon 	bge_hostaddr taddr;
105995d67482SBill Paul 	int i;
106095d67482SBill Paul 
106195d67482SBill Paul 	/*
106295d67482SBill Paul 	 * Initialize the memory window pointer register so that
106395d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
106495d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
106595d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
106695d67482SBill Paul 	 */
106795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
106895d67482SBill Paul 
1069822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1070822f63fcSBill Paul 
10715dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1072e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
107395d67482SBill Paul 		/* Configure mbuf memory pool */
107495d67482SBill Paul 		if (sc->bge_extram) {
10750434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
10760434d1b8SBill Paul 			    BGE_EXT_SSRAM);
1077822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1078822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1079822f63fcSBill Paul 			else
108095d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
108195d67482SBill Paul 		} else {
10820434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
10830434d1b8SBill Paul 			    BGE_BUFFPOOL_1);
1084822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1085822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1086822f63fcSBill Paul 			else
108795d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
108895d67482SBill Paul 		}
108995d67482SBill Paul 
109095d67482SBill Paul 		/* Configure DMA resource pool */
10910434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
10920434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
109395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
10940434d1b8SBill Paul 	}
109595d67482SBill Paul 
109695d67482SBill Paul 	/* Configure mbuf pool watermarks */
1097e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1098e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750) {
10990434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11000434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
11010434d1b8SBill Paul 	} else {
1102fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1103fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
11040434d1b8SBill Paul 	}
1105fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
110695d67482SBill Paul 
110795d67482SBill Paul 	/* Configure DMA resource watermarks */
110895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
110995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
111095d67482SBill Paul 
111195d67482SBill Paul 	/* Enable buffer manager */
11125dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1113e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
111495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
111595d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
111695d67482SBill Paul 
111795d67482SBill Paul 		/* Poll for buffer manager start indication */
111895d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
111995d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
112095d67482SBill Paul 				break;
112195d67482SBill Paul 			DELAY(10);
112295d67482SBill Paul 		}
112395d67482SBill Paul 
112495d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1125fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1126fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
112795d67482SBill Paul 			return (ENXIO);
112895d67482SBill Paul 		}
11290434d1b8SBill Paul 	}
113095d67482SBill Paul 
113195d67482SBill Paul 	/* Enable flow-through queues */
113295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
113395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
113495d67482SBill Paul 
113595d67482SBill Paul 	/* Wait until queue initialization is complete */
113695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
113795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
113895d67482SBill Paul 			break;
113995d67482SBill Paul 		DELAY(10);
114095d67482SBill Paul 	}
114195d67482SBill Paul 
114295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1143fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
114495d67482SBill Paul 		return (ENXIO);
114595d67482SBill Paul 	}
114695d67482SBill Paul 
114795d67482SBill Paul 	/* Initialize the standard RX ring control block */
1148f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1149f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1150f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1151f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1152f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1153f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1154f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
1155e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1156e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
11570434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
11580434d1b8SBill Paul 	else
11590434d1b8SBill Paul 		rcb->bge_maxlen_flags =
11600434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
116195d67482SBill Paul 	if (sc->bge_extram)
116295d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
116395d67482SBill Paul 	else
116495d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
116567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
116667111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1167f41ac2beSBill Paul 
116867111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
116967111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
117095d67482SBill Paul 
117195d67482SBill Paul 	/*
117295d67482SBill Paul 	 * Initialize the jumbo RX ring control block
117395d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
117495d67482SBill Paul 	 * field until we're actually ready to start
117595d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
117695d67482SBill Paul 	 * high enough to require it).
117795d67482SBill Paul 	 */
11785dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1179e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1180f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1181f41ac2beSBill Paul 
1182f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1183f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1184f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1185f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1186f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1187f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1188f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
11891be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
11901be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD|BGE_RCB_FLAG_RING_DISABLED);
119195d67482SBill Paul 		if (sc->bge_extram)
119295d67482SBill Paul 			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
119395d67482SBill Paul 		else
119495d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
119567111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
119667111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
119767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
119867111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1199f41ac2beSBill Paul 
12000434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
12010434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
120267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
120395d67482SBill Paul 
120495d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1205f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
120667111612SJohn Polstra 		rcb->bge_maxlen_flags =
120767111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
12080434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
12090434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
12100434d1b8SBill Paul 	}
121195d67482SBill Paul 
121295d67482SBill Paul 	/*
121395d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
121495d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
121595d67482SBill Paul 	 * each ring.
121695d67482SBill Paul 	 */
121795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
121895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
121995d67482SBill Paul 
122095d67482SBill Paul 	/*
122195d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
122295d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
122395d67482SBill Paul 	 * These are located in NIC memory.
122495d67482SBill Paul 	 */
1225e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
122695d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1227e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1228e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1229e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1230e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
123195d67482SBill Paul 	}
123295d67482SBill Paul 
123395d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1234e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1235e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1236e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1237e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1238e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1239e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
12405dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1241e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1242e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1243e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
124495d67482SBill Paul 
124595d67482SBill Paul 	/* Disable all unused RX return rings */
1246e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
124795d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1248e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1249e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1250e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
12510434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1252e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1253e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
125495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
12553f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1256e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
125795d67482SBill Paul 	}
125895d67482SBill Paul 
125995d67482SBill Paul 	/* Initialize RX ring indexes */
126095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
126195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
126295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
126395d67482SBill Paul 
126495d67482SBill Paul 	/*
126595d67482SBill Paul 	 * Set up RX return ring 0
126695d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
126795d67482SBill Paul 	 * The return rings live entirely within the host, so the
126895d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
126995d67482SBill Paul 	 */
1270e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1271e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1272e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1273e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1274e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1275e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1276e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
127795d67482SBill Paul 
127895d67482SBill Paul 	/* Set random backoff seed for TX */
127995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
12804a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
12814a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
12824a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
128395d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
128495d67482SBill Paul 
128595d67482SBill Paul 	/* Set inter-packet gap */
128695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
128795d67482SBill Paul 
128895d67482SBill Paul 	/*
128995d67482SBill Paul 	 * Specify which ring to use for packets that don't match
129095d67482SBill Paul 	 * any RX rules.
129195d67482SBill Paul 	 */
129295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
129395d67482SBill Paul 
129495d67482SBill Paul 	/*
129595d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
129695d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
129795d67482SBill Paul 	 */
129895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
129995d67482SBill Paul 
130095d67482SBill Paul 	/* Inialize RX list placement stats mask. */
130195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
130295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
130395d67482SBill Paul 
130495d67482SBill Paul 	/* Disable host coalescing until we get it set up */
130595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
130695d67482SBill Paul 
130795d67482SBill Paul 	/* Poll to make sure it's shut down. */
130895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
130995d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
131095d67482SBill Paul 			break;
131195d67482SBill Paul 		DELAY(10);
131295d67482SBill Paul 	}
131395d67482SBill Paul 
131495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1315fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1316fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
131795d67482SBill Paul 		return (ENXIO);
131895d67482SBill Paul 	}
131995d67482SBill Paul 
132095d67482SBill Paul 	/* Set up host coalescing defaults */
132195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
132295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
132395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
132495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
13255dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1326e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
132795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
132895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
13290434d1b8SBill Paul 	}
133095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
133195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
133295d67482SBill Paul 
133395d67482SBill Paul 	/* Set up address of statistics block */
13345dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1335e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1336f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1337f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
133895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1339f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
13400434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
134195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
13420434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
13430434d1b8SBill Paul 	}
13440434d1b8SBill Paul 
13450434d1b8SBill Paul 	/* Set up address of status block */
1346f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1347f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
134895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1349f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1350f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1351f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
135295d67482SBill Paul 
135395d67482SBill Paul 	/* Turn on host coalescing state machine */
135495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
135595d67482SBill Paul 
135695d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
135795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
135895d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
135995d67482SBill Paul 
136095d67482SBill Paul 	/* Turn on RX list placement state machine */
136195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
136295d67482SBill Paul 
136395d67482SBill Paul 	/* Turn on RX list selector state machine. */
13645dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1365e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
136695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
136795d67482SBill Paul 
136895d67482SBill Paul 	/* Turn on DMA, clear stats */
136995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
137095d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
137195d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
137295d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
137395d67482SBill Paul 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
137495d67482SBill Paul 
137595d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
137695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
137795d67482SBill Paul 
137895d67482SBill Paul #ifdef notdef
137995d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
138095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
138195d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
138295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
138395d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
138495d67482SBill Paul #endif
138595d67482SBill Paul 
138695d67482SBill Paul 	/* Turn on DMA completion state machine */
13875dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1388e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
138995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
139095d67482SBill Paul 
139195d67482SBill Paul 	/* Turn on write DMA state machine */
139295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
139395d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
139495d67482SBill Paul 
139595d67482SBill Paul 	/* Turn on read DMA state machine */
139695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
139795d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
139895d67482SBill Paul 
139995d67482SBill Paul 	/* Turn on RX data completion state machine */
140095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
140195d67482SBill Paul 
140295d67482SBill Paul 	/* Turn on RX BD initiator state machine */
140395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
140495d67482SBill Paul 
140595d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
140695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
140795d67482SBill Paul 
140895d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
14095dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1410e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
141195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
141295d67482SBill Paul 
141395d67482SBill Paul 	/* Turn on send BD completion state machine */
141495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
141595d67482SBill Paul 
141695d67482SBill Paul 	/* Turn on send data completion state machine */
141795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
141895d67482SBill Paul 
141995d67482SBill Paul 	/* Turn on send data initiator state machine */
142095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
142195d67482SBill Paul 
142295d67482SBill Paul 	/* Turn on send BD initiator state machine */
142395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
142495d67482SBill Paul 
142595d67482SBill Paul 	/* Turn on send BD selector state machine */
142695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
142795d67482SBill Paul 
142895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
142995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
143095d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
143195d67482SBill Paul 
143295d67482SBill Paul 	/* ack/clear link change events */
143395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
14340434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
14350434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1436f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
143795d67482SBill Paul 
143895d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
143995d67482SBill Paul 	if (sc->bge_tbi) {
144095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1441a1d52896SBill Paul 	} else {
144295d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
14431f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
14441f313773SOleg Bulyzhin 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B1)
1445a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1446a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1447a1d52896SBill Paul 	}
144895d67482SBill Paul 
14491f313773SOleg Bulyzhin 	/*
14501f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
14511f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
14521f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
14531f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
14541f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
14551f313773SOleg Bulyzhin 	 */
14561f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
14571f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
14581f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
14591f313773SOleg Bulyzhin 
146095d67482SBill Paul 	/* Enable link state change attentions. */
146195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
146295d67482SBill Paul 
146395d67482SBill Paul 	return (0);
146495d67482SBill Paul }
146595d67482SBill Paul 
146695d67482SBill Paul /*
146795d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
146895d67482SBill Paul  * against our list and return its name if we find a match. Note
146995d67482SBill Paul  * that since the Broadcom controller contains VPD support, we
147095d67482SBill Paul  * can get the device name string from the controller itself instead
147195d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
147295d67482SBill Paul  * we'll always announce the right product name.
147395d67482SBill Paul  */
147495d67482SBill Paul static int
14753f74909aSGleb Smirnoff bge_probe(device_t dev)
147695d67482SBill Paul {
147795d67482SBill Paul 	struct bge_type *t;
147895d67482SBill Paul 	struct bge_softc *sc;
1479029e2ee3SJohn Polstra 	char *descbuf;
148095d67482SBill Paul 
148195d67482SBill Paul 	t = bge_devs;
148295d67482SBill Paul 
148395d67482SBill Paul 	sc = device_get_softc(dev);
148495d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
148595d67482SBill Paul 	sc->bge_dev = dev;
148695d67482SBill Paul 
148795d67482SBill Paul 	while(t->bge_name != NULL) {
148895d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
148995d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
149095d67482SBill Paul #ifdef notdef
149195d67482SBill Paul 			bge_vpd_read(sc);
149295d67482SBill Paul 			device_set_desc(dev, sc->bge_vpd_prodname);
149395d67482SBill Paul #endif
1494029e2ee3SJohn Polstra 			descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
1495029e2ee3SJohn Polstra 			if (descbuf == NULL)
1496029e2ee3SJohn Polstra 				return (ENOMEM);
1497029e2ee3SJohn Polstra 			snprintf(descbuf, BGE_DEVDESC_MAX,
1498029e2ee3SJohn Polstra 			    "%s, ASIC rev. %#04x", t->bge_name,
1499029e2ee3SJohn Polstra 			    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16);
1500029e2ee3SJohn Polstra 			device_set_desc_copy(dev, descbuf);
15016d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
15026d2a9bd6SDoug Ambrisko 				sc->bge_no_3_led = 1;
1503029e2ee3SJohn Polstra 			free(descbuf, M_TEMP);
150495d67482SBill Paul 			return (0);
150595d67482SBill Paul 		}
150695d67482SBill Paul 		t++;
150795d67482SBill Paul 	}
150895d67482SBill Paul 
150995d67482SBill Paul 	return (ENXIO);
151095d67482SBill Paul }
151195d67482SBill Paul 
1512f41ac2beSBill Paul static void
15133f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
1514f41ac2beSBill Paul {
1515f41ac2beSBill Paul 	int i;
1516f41ac2beSBill Paul 
15173f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
1518f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1519f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1520f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1521f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1522f41ac2beSBill Paul 	}
1523f41ac2beSBill Paul 
15243f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
1525f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1526f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1527f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1528f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1529f41ac2beSBill Paul 	}
1530f41ac2beSBill Paul 
15313f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
1532f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1533f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1534f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1535f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1536f41ac2beSBill Paul 	}
1537f41ac2beSBill Paul 
1538f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1539f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1540f41ac2beSBill Paul 
1541f41ac2beSBill Paul 
15423f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
1543e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1544e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1545e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1546e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1547f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1548f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1549f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1550f41ac2beSBill Paul 
1551f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1552f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1553f41ac2beSBill Paul 
15543f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
1555e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1556e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1557e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1558e65bed95SPyun YongHyeon 
1559e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1560e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1561f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1562f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1563f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1564f41ac2beSBill Paul 
1565f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1566f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1567f41ac2beSBill Paul 
15683f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
1569e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1570e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1571e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1572e65bed95SPyun YongHyeon 
1573e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1574e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1575f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1576f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1577f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1578f41ac2beSBill Paul 
1579f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1580f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1581f41ac2beSBill Paul 
15823f74909aSGleb Smirnoff 	/* Destroy TX ring. */
1583e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1584e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1585e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1586e65bed95SPyun YongHyeon 
1587e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1588f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1589f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1590f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1591f41ac2beSBill Paul 
1592f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1593f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1594f41ac2beSBill Paul 
15953f74909aSGleb Smirnoff 	/* Destroy status block. */
1596e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1597e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1598e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1599e65bed95SPyun YongHyeon 
1600e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1601f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1602f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1603f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1604f41ac2beSBill Paul 
1605f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1606f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1607f41ac2beSBill Paul 
16083f74909aSGleb Smirnoff 	/* Destroy statistics block. */
1609e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1610e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1611e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1612e65bed95SPyun YongHyeon 
1613e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1614f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1615f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1616f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1617f41ac2beSBill Paul 
1618f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1619f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1620f41ac2beSBill Paul 
16213f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
1622f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1623f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1624f41ac2beSBill Paul }
1625f41ac2beSBill Paul 
1626f41ac2beSBill Paul static int
16273f74909aSGleb Smirnoff bge_dma_alloc(device_t dev)
1628f41ac2beSBill Paul {
16293f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
1630f41ac2beSBill Paul 	struct bge_softc *sc;
16311be6acb7SGleb Smirnoff 	int i, error;
1632f41ac2beSBill Paul 
1633f41ac2beSBill Paul 	sc = device_get_softc(dev);
1634f41ac2beSBill Paul 
1635f41ac2beSBill Paul 	/*
1636f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1637f41ac2beSBill Paul 	 */
1638f41ac2beSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1639f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1640f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
16412f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1642f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1643f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1644f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
16458a40c10eSScott Long 			0,			/* flags */
1646f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1647f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1648f41ac2beSBill Paul 
1649e65bed95SPyun YongHyeon 	if (error != 0) {
1650fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1651fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1652e65bed95SPyun YongHyeon 		return (ENOMEM);
1653e65bed95SPyun YongHyeon 	}
1654e65bed95SPyun YongHyeon 
1655f41ac2beSBill Paul 	/*
1656f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1657f41ac2beSBill Paul 	 */
16588a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1659f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
16601be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
16611be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1662f41ac2beSBill Paul 
1663f41ac2beSBill Paul 	if (error) {
1664fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1665f41ac2beSBill Paul 		return (ENOMEM);
1666f41ac2beSBill Paul 	}
1667f41ac2beSBill Paul 
16683f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
1669f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1670f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1671f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1672f41ac2beSBill Paul 		if (error) {
1673fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1674fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1675f41ac2beSBill Paul 			return (ENOMEM);
1676f41ac2beSBill Paul 		}
1677f41ac2beSBill Paul 	}
1678f41ac2beSBill Paul 
16793f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
1680f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1681f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1682f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1683f41ac2beSBill Paul 		if (error) {
1684fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1685fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1686f41ac2beSBill Paul 			return (ENOMEM);
1687f41ac2beSBill Paul 		}
1688f41ac2beSBill Paul 	}
1689f41ac2beSBill Paul 
16903f74909aSGleb Smirnoff 	/* Create tag for standard RX ring. */
1691f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1692f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1693f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1694f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1695f41ac2beSBill Paul 
1696f41ac2beSBill Paul 	if (error) {
1697fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1698f41ac2beSBill Paul 		return (ENOMEM);
1699f41ac2beSBill Paul 	}
1700f41ac2beSBill Paul 
17013f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for standard RX ring. */
1702f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1703f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1704f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1705f41ac2beSBill Paul 	if (error)
1706f41ac2beSBill Paul 		return (ENOMEM);
1707f41ac2beSBill Paul 
1708f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1709f41ac2beSBill Paul 
17103f74909aSGleb Smirnoff 	/* Load the address of the standard RX ring. */
1711f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1712f41ac2beSBill Paul 	ctx.sc = sc;
1713f41ac2beSBill Paul 
1714f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1715f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1716f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1717f41ac2beSBill Paul 
1718f41ac2beSBill Paul 	if (error)
1719f41ac2beSBill Paul 		return (ENOMEM);
1720f41ac2beSBill Paul 
1721f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1722f41ac2beSBill Paul 
17233f74909aSGleb Smirnoff 	/* Create tags for jumbo mbufs. */
17245dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1725e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1726f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
17278a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
17281be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
17291be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1730f41ac2beSBill Paul 		if (error) {
1731fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
17323f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
1733f41ac2beSBill Paul 			return (ENOMEM);
1734f41ac2beSBill Paul 		}
1735f41ac2beSBill Paul 
17363f74909aSGleb Smirnoff 		/* Create tag for jumbo RX ring. */
1737f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1738f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1739f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1740f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1741f41ac2beSBill Paul 
1742f41ac2beSBill Paul 		if (error) {
1743fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
17443f74909aSGleb Smirnoff 			    "could not allocate jumbo ring dma tag\n");
1745f41ac2beSBill Paul 			return (ENOMEM);
1746f41ac2beSBill Paul 		}
1747f41ac2beSBill Paul 
17483f74909aSGleb Smirnoff 		/* Allocate DMA'able memory for jumbo RX ring. */
1749f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
17501be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
17511be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1752f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1753f41ac2beSBill Paul 		if (error)
1754f41ac2beSBill Paul 			return (ENOMEM);
1755f41ac2beSBill Paul 
17563f74909aSGleb Smirnoff 		/* Load the address of the jumbo RX ring. */
1757f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1758f41ac2beSBill Paul 		ctx.sc = sc;
1759f41ac2beSBill Paul 
1760f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1761f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1762f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1763f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1764f41ac2beSBill Paul 
1765f41ac2beSBill Paul 		if (error)
1766f41ac2beSBill Paul 			return (ENOMEM);
1767f41ac2beSBill Paul 
1768f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1769f41ac2beSBill Paul 
17703f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
1771f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1772f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1773f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1774f41ac2beSBill Paul 			if (error) {
1775fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
17763f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
1777f41ac2beSBill Paul 				return (ENOMEM);
1778f41ac2beSBill Paul 			}
1779f41ac2beSBill Paul 		}
1780f41ac2beSBill Paul 
1781f41ac2beSBill Paul 	}
1782f41ac2beSBill Paul 
17833f74909aSGleb Smirnoff 	/* Create tag for RX return ring. */
1784f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1785f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1786f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1787f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1788f41ac2beSBill Paul 
1789f41ac2beSBill Paul 	if (error) {
1790fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1791f41ac2beSBill Paul 		return (ENOMEM);
1792f41ac2beSBill Paul 	}
1793f41ac2beSBill Paul 
17943f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for RX return ring. */
1795f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1796f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1797f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1798f41ac2beSBill Paul 	if (error)
1799f41ac2beSBill Paul 		return (ENOMEM);
1800f41ac2beSBill Paul 
1801f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1802f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1803f41ac2beSBill Paul 
18043f74909aSGleb Smirnoff 	/* Load the address of the RX return ring. */
1805f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1806f41ac2beSBill Paul 	ctx.sc = sc;
1807f41ac2beSBill Paul 
1808f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
1809f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
1810f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
1811f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1812f41ac2beSBill Paul 
1813f41ac2beSBill Paul 	if (error)
1814f41ac2beSBill Paul 		return (ENOMEM);
1815f41ac2beSBill Paul 
1816f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
1817f41ac2beSBill Paul 
18183f74909aSGleb Smirnoff 	/* Create tag for TX ring. */
1819f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1820f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1821f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
1822f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
1823f41ac2beSBill Paul 
1824f41ac2beSBill Paul 	if (error) {
1825fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1826f41ac2beSBill Paul 		return (ENOMEM);
1827f41ac2beSBill Paul 	}
1828f41ac2beSBill Paul 
18293f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for TX ring. */
1830f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
1831f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
1832f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
1833f41ac2beSBill Paul 	if (error)
1834f41ac2beSBill Paul 		return (ENOMEM);
1835f41ac2beSBill Paul 
1836f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1837f41ac2beSBill Paul 
18383f74909aSGleb Smirnoff 	/* Load the address of the TX ring. */
1839f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1840f41ac2beSBill Paul 	ctx.sc = sc;
1841f41ac2beSBill Paul 
1842f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
1843f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
1844f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1845f41ac2beSBill Paul 
1846f41ac2beSBill Paul 	if (error)
1847f41ac2beSBill Paul 		return (ENOMEM);
1848f41ac2beSBill Paul 
1849f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
1850f41ac2beSBill Paul 
18513f74909aSGleb Smirnoff 	/* Create tag for status block. */
1852f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1853f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1854f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
1855f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
1856f41ac2beSBill Paul 
1857f41ac2beSBill Paul 	if (error) {
1858fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1859f41ac2beSBill Paul 		return (ENOMEM);
1860f41ac2beSBill Paul 	}
1861f41ac2beSBill Paul 
18623f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for status block. */
1863f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
1864f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
1865f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
1866f41ac2beSBill Paul 	if (error)
1867f41ac2beSBill Paul 		return (ENOMEM);
1868f41ac2beSBill Paul 
1869f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1870f41ac2beSBill Paul 
18713f74909aSGleb Smirnoff 	/* Load the address of the status block. */
1872f41ac2beSBill Paul 	ctx.sc = sc;
1873f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1874f41ac2beSBill Paul 
1875f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
1876f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
1877f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1878f41ac2beSBill Paul 
1879f41ac2beSBill Paul 	if (error)
1880f41ac2beSBill Paul 		return (ENOMEM);
1881f41ac2beSBill Paul 
1882f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
1883f41ac2beSBill Paul 
18843f74909aSGleb Smirnoff 	/* Create tag for statistics block. */
1885f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1886f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1887f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
1888f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
1889f41ac2beSBill Paul 
1890f41ac2beSBill Paul 	if (error) {
1891fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1892f41ac2beSBill Paul 		return (ENOMEM);
1893f41ac2beSBill Paul 	}
1894f41ac2beSBill Paul 
18953f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for statistics block. */
1896f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
1897f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
1898f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
1899f41ac2beSBill Paul 	if (error)
1900f41ac2beSBill Paul 		return (ENOMEM);
1901f41ac2beSBill Paul 
1902f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
1903f41ac2beSBill Paul 
19043f74909aSGleb Smirnoff 	/* Load the address of the statstics block. */
1905f41ac2beSBill Paul 	ctx.sc = sc;
1906f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1907f41ac2beSBill Paul 
1908f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
1909f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
1910f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1911f41ac2beSBill Paul 
1912f41ac2beSBill Paul 	if (error)
1913f41ac2beSBill Paul 		return (ENOMEM);
1914f41ac2beSBill Paul 
1915f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
1916f41ac2beSBill Paul 
1917f41ac2beSBill Paul 	return (0);
1918f41ac2beSBill Paul }
1919f41ac2beSBill Paul 
192095d67482SBill Paul static int
19213f74909aSGleb Smirnoff bge_attach(device_t dev)
192295d67482SBill Paul {
192395d67482SBill Paul 	struct ifnet *ifp;
192495d67482SBill Paul 	struct bge_softc *sc;
19253f74909aSGleb Smirnoff 	uint32_t hwcfg = 0;
19263f74909aSGleb Smirnoff 	uint32_t mac_tmp = 0;
1927fc74a9f9SBrooks Davis 	u_char eaddr[6];
1928fe806fdaSPyun YongHyeon 	int error = 0, rid;
192995d67482SBill Paul 
193095d67482SBill Paul 	sc = device_get_softc(dev);
193195d67482SBill Paul 	sc->bge_dev = dev;
193295d67482SBill Paul 
193395d67482SBill Paul 	/*
193495d67482SBill Paul 	 * Map control/status registers.
193595d67482SBill Paul 	 */
193695d67482SBill Paul 	pci_enable_busmaster(dev);
193795d67482SBill Paul 
193895d67482SBill Paul 	rid = BGE_PCI_BAR0;
19395f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
19405f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
194195d67482SBill Paul 
194295d67482SBill Paul 	if (sc->bge_res == NULL) {
1943fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
194495d67482SBill Paul 		error = ENXIO;
194595d67482SBill Paul 		goto fail;
194695d67482SBill Paul 	}
194795d67482SBill Paul 
194895d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
194995d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
195095d67482SBill Paul 
19513f74909aSGleb Smirnoff 	/* Allocate interrupt. */
195295d67482SBill Paul 	rid = 0;
195395d67482SBill Paul 
19545f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
195595d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
195695d67482SBill Paul 
195795d67482SBill Paul 	if (sc->bge_irq == NULL) {
1958fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
195995d67482SBill Paul 		error = ENXIO;
196095d67482SBill Paul 		goto fail;
196195d67482SBill Paul 	}
196295d67482SBill Paul 
19630f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
19640f9bd73bSSam Leffler 
1965e53d81eeSPaul Saab 	/* Save ASIC rev. */
1966e53d81eeSPaul Saab 
1967e53d81eeSPaul Saab 	sc->bge_chipid =
1968e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
1969e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
1970e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
1971e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
1972e53d81eeSPaul Saab 
1973e53d81eeSPaul Saab 	/*
1974560c1670SGleb Smirnoff 	 * Treat the 5714 and the 5752 like the 5750 until we have more info
1975419c028bSPaul Saab 	 * on this chip.
1976419c028bSPaul Saab 	 */
1977560c1670SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5714 ||
1978560c1670SGleb Smirnoff             sc->bge_asicrev == BGE_ASICREV_BCM5752)
1979419c028bSPaul Saab 		sc->bge_asicrev = BGE_ASICREV_BCM5750;
1980419c028bSPaul Saab 
1981419c028bSPaul Saab 	/*
1982e53d81eeSPaul Saab 	 * XXX: Broadcom Linux driver.  Not in specs or eratta.
1983e53d81eeSPaul Saab 	 * PCI-Express?
1984e53d81eeSPaul Saab 	 */
1985e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
19863f74909aSGleb Smirnoff 		uint32_t v;
1987e53d81eeSPaul Saab 
1988e53d81eeSPaul Saab 		v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4);
1989e53d81eeSPaul Saab 		if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) {
1990e53d81eeSPaul Saab 			v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
1991e53d81eeSPaul Saab 			if ((v & 0xff) == BGE_PCIE_CAPID)
1992e53d81eeSPaul Saab 				sc->bge_pcie = 1;
1993e53d81eeSPaul Saab 		}
1994e53d81eeSPaul Saab 	}
1995e53d81eeSPaul Saab 
199695d67482SBill Paul 	/* Try to reset the chip. */
199795d67482SBill Paul 	bge_reset(sc);
199895d67482SBill Paul 
199995d67482SBill Paul 	if (bge_chipinit(sc)) {
2000fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
200195d67482SBill Paul 		bge_release_resources(sc);
200295d67482SBill Paul 		error = ENXIO;
200395d67482SBill Paul 		goto fail;
200495d67482SBill Paul 	}
200595d67482SBill Paul 
200695d67482SBill Paul 	/*
200795d67482SBill Paul 	 * Get station address from the EEPROM.
200895d67482SBill Paul 	 */
2009fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2010fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2011fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2012fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2013fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2014fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2015fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2016fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2017fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2018fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
201995d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2020fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
202195d67482SBill Paul 		bge_release_resources(sc);
202295d67482SBill Paul 		error = ENXIO;
202395d67482SBill Paul 		goto fail;
202495d67482SBill Paul 	}
202595d67482SBill Paul 
2026f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
2027e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2028e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
2029f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2030f41ac2beSBill Paul 	else
2031f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2032f41ac2beSBill Paul 
2033f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2034fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2035fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2036f41ac2beSBill Paul 		bge_release_resources(sc);
2037f41ac2beSBill Paul 		error = ENXIO;
2038f41ac2beSBill Paul 		goto fail;
2039f41ac2beSBill Paul 	}
2040f41ac2beSBill Paul 
204195d67482SBill Paul 	/* Set default tuneable values. */
204295d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
204395d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
204495d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
204595d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
204695d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
204795d67482SBill Paul 
204895d67482SBill Paul 	/* Set up ifnet structure */
2049fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2050fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2051fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2052fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2053fc74a9f9SBrooks Davis 		error = ENXIO;
2054fc74a9f9SBrooks Davis 		goto fail;
2055fc74a9f9SBrooks Davis 	}
205695d67482SBill Paul 	ifp->if_softc = sc;
20579bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
205895d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
205995d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
206095d67482SBill Paul 	ifp->if_start = bge_start;
206195d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
206295d67482SBill Paul 	ifp->if_init = bge_init;
206395d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
20644d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
20654d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
20664d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
206795d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2068d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
2069479b23b7SGleb Smirnoff 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM;
207095d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
207175719184SGleb Smirnoff #ifdef DEVICE_POLLING
207275719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
207375719184SGleb Smirnoff #endif
207495d67482SBill Paul 
2075a1d52896SBill Paul         /*
2076d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2077d375e524SGleb Smirnoff 	 * to hardware bugs.
2078d375e524SGleb Smirnoff 	 */
2079d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2080d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2081d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2082d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2083d375e524SGleb Smirnoff 	}
2084d375e524SGleb Smirnoff 
2085d375e524SGleb Smirnoff 	/*
2086a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
208741abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
208841abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
208941abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
209041abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
209141abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
209241abcc1bSPaul Saab 	 * SK-9D41.
2093a1d52896SBill Paul 	 */
209441abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
209541abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
209641abcc1bSPaul Saab 	else {
2097f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2098f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2099fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2100f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2101f6789fbaSPyun YongHyeon 			error = ENXIO;
2102f6789fbaSPyun YongHyeon 			goto fail;
2103f6789fbaSPyun YongHyeon 		}
210441abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
210541abcc1bSPaul Saab 	}
210641abcc1bSPaul Saab 
210741abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2108a1d52896SBill Paul 		sc->bge_tbi = 1;
2109a1d52896SBill Paul 
211095d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
211195d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
211295d67482SBill Paul 		sc->bge_tbi = 1;
211395d67482SBill Paul 
211495d67482SBill Paul 	if (sc->bge_tbi) {
211595d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
211695d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
211795d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
211895d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
211995d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
212095d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
212195d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2122da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
212395d67482SBill Paul 	} else {
212495d67482SBill Paul 		/*
212595d67482SBill Paul 		 * Do transceiver setup.
212695d67482SBill Paul 		 */
212795d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
212895d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
2129fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
213095d67482SBill Paul 			bge_release_resources(sc);
213195d67482SBill Paul 			error = ENXIO;
213295d67482SBill Paul 			goto fail;
213395d67482SBill Paul 		}
213495d67482SBill Paul 	}
213595d67482SBill Paul 
213695d67482SBill Paul 	/*
2137e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2138e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2139e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2140e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2141e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2142e255b776SJohn Polstra 	 * payloads by copying the received packets.
2143e255b776SJohn Polstra 	 */
2144e0ced696SPaul Saab 	switch (sc->bge_chipid) {
2145e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_A0:
2146e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B0:
2147e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B2:
2148e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B5:
2149e255b776SJohn Polstra 		/* If in PCI-X mode, work around the alignment bug. */
2150e255b776SJohn Polstra 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
2151e255b776SJohn Polstra 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2152e255b776SJohn Polstra 		    BGE_PCISTATE_PCI_BUSSPEED)
2153e255b776SJohn Polstra 			sc->bge_rx_alignment_bug = 1;
2154e255b776SJohn Polstra 		break;
2155e255b776SJohn Polstra 	}
2156e255b776SJohn Polstra 
2157e255b776SJohn Polstra 	/*
215895d67482SBill Paul 	 * Call MI attach routine.
215995d67482SBill Paul 	 */
2160fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
21610f9bd73bSSam Leffler 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
21620f9bd73bSSam Leffler 
21630f9bd73bSSam Leffler 	/*
21640f9bd73bSSam Leffler 	 * Hookup IRQ last.
21650f9bd73bSSam Leffler 	 */
21660f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
21670f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
21680f9bd73bSSam Leffler 
21690f9bd73bSSam Leffler 	if (error) {
2170fc74a9f9SBrooks Davis 		bge_detach(dev);
2171fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
21720f9bd73bSSam Leffler 	}
217395d67482SBill Paul 
217495d67482SBill Paul fail:
217595d67482SBill Paul 	return (error);
217695d67482SBill Paul }
217795d67482SBill Paul 
217895d67482SBill Paul static int
21793f74909aSGleb Smirnoff bge_detach(device_t dev)
218095d67482SBill Paul {
218195d67482SBill Paul 	struct bge_softc *sc;
218295d67482SBill Paul 	struct ifnet *ifp;
218395d67482SBill Paul 
218495d67482SBill Paul 	sc = device_get_softc(dev);
2185fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
218695d67482SBill Paul 
218775719184SGleb Smirnoff #ifdef DEVICE_POLLING
218875719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
218975719184SGleb Smirnoff 		ether_poll_deregister(ifp);
219075719184SGleb Smirnoff #endif
219175719184SGleb Smirnoff 
21920f9bd73bSSam Leffler 	BGE_LOCK(sc);
219395d67482SBill Paul 	bge_stop(sc);
219495d67482SBill Paul 	bge_reset(sc);
21950f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
21960f9bd73bSSam Leffler 
21970f9bd73bSSam Leffler 	ether_ifdetach(ifp);
219895d67482SBill Paul 
219995d67482SBill Paul 	if (sc->bge_tbi) {
220095d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
220195d67482SBill Paul 	} else {
220295d67482SBill Paul 		bus_generic_detach(dev);
220395d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
220495d67482SBill Paul 	}
220595d67482SBill Paul 
220695d67482SBill Paul 	bge_release_resources(sc);
220795d67482SBill Paul 
220895d67482SBill Paul 	return (0);
220995d67482SBill Paul }
221095d67482SBill Paul 
221195d67482SBill Paul static void
22123f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
221395d67482SBill Paul {
221495d67482SBill Paul 	device_t dev;
221595d67482SBill Paul 
221695d67482SBill Paul 	dev = sc->bge_dev;
221795d67482SBill Paul 
221895d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
221995d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
222095d67482SBill Paul 
222195d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
222295d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
222395d67482SBill Paul 
222495d67482SBill Paul 	if (sc->bge_intrhand != NULL)
222595d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
222695d67482SBill Paul 
222795d67482SBill Paul 	if (sc->bge_irq != NULL)
222895d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
222995d67482SBill Paul 
223095d67482SBill Paul 	if (sc->bge_res != NULL)
223195d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
223295d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
223395d67482SBill Paul 
2234ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2235ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2236ad61f896SRuslan Ermilov 
2237f41ac2beSBill Paul 	bge_dma_free(sc);
223895d67482SBill Paul 
22390f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
22400f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
224195d67482SBill Paul }
224295d67482SBill Paul 
224395d67482SBill Paul static void
22443f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
224595d67482SBill Paul {
224695d67482SBill Paul 	device_t dev;
22473f74909aSGleb Smirnoff 	uint32_t cachesize, command, pcistate, reset;
224895d67482SBill Paul 	int i, val = 0;
224995d67482SBill Paul 
225095d67482SBill Paul 	dev = sc->bge_dev;
225195d67482SBill Paul 
225295d67482SBill Paul 	/* Save some important PCI state. */
225395d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
225495d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
225595d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
225695d67482SBill Paul 
225795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
225895d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2259e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
226095d67482SBill Paul 
2261e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2262e53d81eeSPaul Saab 
2263e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2264e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2265e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2266e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2267e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2268e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2269e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2270e53d81eeSPaul Saab 			reset |= (1<<29);
2271e53d81eeSPaul Saab 		}
2272e53d81eeSPaul Saab 	}
2273e53d81eeSPaul Saab 
227495d67482SBill Paul 	/* Issue global reset */
2275e53d81eeSPaul Saab 	bge_writereg_ind(sc, BGE_MISC_CFG, reset);
227695d67482SBill Paul 
227795d67482SBill Paul 	DELAY(1000);
227895d67482SBill Paul 
2279e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2280e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2281e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2282e53d81eeSPaul Saab 			uint32_t v;
2283e53d81eeSPaul Saab 
2284e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2285e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2286e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2287e53d81eeSPaul Saab 		}
2288e53d81eeSPaul Saab 		/* Set PCIE max payload size and clear error status. */
2289e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2290e53d81eeSPaul Saab 	}
2291e53d81eeSPaul Saab 
22923f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
229395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
229495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2295e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
229695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
229795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
229895d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
229995d67482SBill Paul 
2300a7b0c314SPaul Saab 	/* Enable memory arbiter. */
23015dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2302e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
2303a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2304a7b0c314SPaul Saab 
230595d67482SBill Paul 	/*
230695d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
230795d67482SBill Paul 	 * general communications memory at 0xB50.
230895d67482SBill Paul 	 */
230995d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
231095d67482SBill Paul 	/*
231195d67482SBill Paul 	 * Poll the value location we just wrote until
231295d67482SBill Paul 	 * we see the 1's complement of the magic number.
231395d67482SBill Paul 	 * This indicates that the firmware initialization
231495d67482SBill Paul 	 * is complete.
231595d67482SBill Paul 	 */
231695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
231795d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
231895d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
231995d67482SBill Paul 			break;
232095d67482SBill Paul 		DELAY(10);
232195d67482SBill Paul 	}
232295d67482SBill Paul 
232395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2324fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "firmware handshake timed out\n");
232595d67482SBill Paul 		return;
232695d67482SBill Paul 	}
232795d67482SBill Paul 
232895d67482SBill Paul 	/*
232995d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
233095d67482SBill Paul 	 * return to its original pre-reset state. This is a
233195d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
233295d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
233395d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
233495d67482SBill Paul 	 * results.
233595d67482SBill Paul 	 */
233695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
233795d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
233895d67482SBill Paul 			break;
233995d67482SBill Paul 		DELAY(10);
234095d67482SBill Paul 	}
234195d67482SBill Paul 
23423f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
2343e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
234495d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
234595d67482SBill Paul 
234695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
234795d67482SBill Paul 
2348da3003f0SBill Paul 	/*
2349da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2350da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2351da3003f0SBill Paul 	 * to 1.2V.
2352da3003f0SBill Paul 	 */
2353da3003f0SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 && sc->bge_tbi) {
2354da3003f0SBill Paul 		uint32_t serdescfg;
2355da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2356da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2357da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2358da3003f0SBill Paul 	}
2359da3003f0SBill Paul 
2360e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2361e53d81eeSPaul Saab 	if (sc->bge_pcie && sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2362e53d81eeSPaul Saab 		uint32_t v;
2363e53d81eeSPaul Saab 
2364e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2365e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2366e53d81eeSPaul Saab 	}
236795d67482SBill Paul 	DELAY(10000);
236895d67482SBill Paul }
236995d67482SBill Paul 
237095d67482SBill Paul /*
237195d67482SBill Paul  * Frame reception handling. This is called if there's a frame
237295d67482SBill Paul  * on the receive return list.
237395d67482SBill Paul  *
237495d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
23751be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
237695d67482SBill Paul  * 2) the frame is from the standard receive ring
237795d67482SBill Paul  */
237895d67482SBill Paul 
237995d67482SBill Paul static void
23803f74909aSGleb Smirnoff bge_rxeof(struct bge_softc *sc)
238195d67482SBill Paul {
238295d67482SBill Paul 	struct ifnet *ifp;
238395d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
238495d67482SBill Paul 
23850f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
23860f9bd73bSSam Leffler 
23873f74909aSGleb Smirnoff 	/* Nothing to do. */
2388cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
2389cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
2390cfcb5025SOleg Bulyzhin 		return;
2391cfcb5025SOleg Bulyzhin 
2392fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
239395d67482SBill Paul 
2394f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2395e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2396f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2397f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
23985dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2399e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2400f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2401f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2402f41ac2beSBill Paul 		    BUS_DMASYNC_POSTREAD);
2403f41ac2beSBill Paul 	}
2404f41ac2beSBill Paul 
240595d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2406f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
240795d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
24083f74909aSGleb Smirnoff 		uint32_t		rxidx;
240995d67482SBill Paul 		struct mbuf		*m = NULL;
24103f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
241195d67482SBill Paul 		int			have_tag = 0;
241295d67482SBill Paul 
241375719184SGleb Smirnoff #ifdef DEVICE_POLLING
241475719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
241575719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
241675719184SGleb Smirnoff 				break;
241775719184SGleb Smirnoff 			sc->rxcycles--;
241875719184SGleb Smirnoff 		}
241975719184SGleb Smirnoff #endif
242075719184SGleb Smirnoff 
242195d67482SBill Paul 		cur_rx =
2422f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
242395d67482SBill Paul 
242495d67482SBill Paul 		rxidx = cur_rx->bge_idx;
24250434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
242695d67482SBill Paul 
242795d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
242895d67482SBill Paul 			have_tag = 1;
242995d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
243095d67482SBill Paul 		}
243195d67482SBill Paul 
243295d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
243395d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2434f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2435f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2436f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2437f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2438f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
243995d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
244095d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
244195d67482SBill Paul 			jumbocnt++;
244295d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
244395d67482SBill Paul 				ifp->if_ierrors++;
244495d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
244595d67482SBill Paul 				continue;
244695d67482SBill Paul 			}
244795d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
244895d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
244995d67482SBill Paul 				ifp->if_ierrors++;
245095d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
245195d67482SBill Paul 				continue;
245295d67482SBill Paul 			}
245395d67482SBill Paul 		} else {
245495d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2455f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2456f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2457f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2458f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2459f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
246095d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
246195d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
246295d67482SBill Paul 			stdcnt++;
246395d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
246495d67482SBill Paul 				ifp->if_ierrors++;
246595d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
246695d67482SBill Paul 				continue;
246795d67482SBill Paul 			}
246895d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
246995d67482SBill Paul 			    NULL) == ENOBUFS) {
247095d67482SBill Paul 				ifp->if_ierrors++;
247195d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
247295d67482SBill Paul 				continue;
247395d67482SBill Paul 			}
247495d67482SBill Paul 		}
247595d67482SBill Paul 
247695d67482SBill Paul 		ifp->if_ipackets++;
2477e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2478e255b776SJohn Polstra 		/*
2479e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2480e65bed95SPyun YongHyeon 		 * the payload is aligned.
2481e255b776SJohn Polstra 		 */
2482e255b776SJohn Polstra 		if (sc->bge_rx_alignment_bug) {
2483e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2484e255b776SJohn Polstra 			    cur_rx->bge_len);
2485e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2486e255b776SJohn Polstra 		}
2487e255b776SJohn Polstra #endif
2488473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
248995d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
249095d67482SBill Paul 
2491b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
249278178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
249395d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
249495d67482SBill Paul 				if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
249595d67482SBill Paul 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
249678178cd1SGleb Smirnoff 			}
2497d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2498d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
249995d67482SBill Paul 				m->m_pkthdr.csum_data =
250095d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
2501ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
2502ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
250395d67482SBill Paul 			}
250495d67482SBill Paul 		}
250595d67482SBill Paul 
250695d67482SBill Paul 		/*
2507673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2508673d9191SSam Leffler 		 * attach that information to the packet.
250995d67482SBill Paul 		 */
2510d147662cSGleb Smirnoff 		if (have_tag) {
2511d147662cSGleb Smirnoff 			VLAN_INPUT_TAG(ifp, m, vlan_tag);
2512d147662cSGleb Smirnoff 			if (m == NULL)
2513d147662cSGleb Smirnoff 				continue;
2514d147662cSGleb Smirnoff 		}
251595d67482SBill Paul 
25160f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2517673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
25180f9bd73bSSam Leffler 		BGE_LOCK(sc);
251995d67482SBill Paul 	}
252095d67482SBill Paul 
2521e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2522f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2523e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
25245dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2525e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2526e65bed95SPyun YongHyeon 		if (jumbocnt > 0)
2527f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2528f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_ring_map,
2529e65bed95SPyun YongHyeon 			    BUS_DMASYNC_PREWRITE);
2530f41ac2beSBill Paul 	}
2531f41ac2beSBill Paul 
253295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
253395d67482SBill Paul 	if (stdcnt)
253495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
253595d67482SBill Paul 	if (jumbocnt)
253695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
253795d67482SBill Paul }
253895d67482SBill Paul 
253995d67482SBill Paul static void
25403f74909aSGleb Smirnoff bge_txeof(struct bge_softc *sc)
254195d67482SBill Paul {
254295d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
254395d67482SBill Paul 	struct ifnet *ifp;
254495d67482SBill Paul 
25450f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
25460f9bd73bSSam Leffler 
25473f74909aSGleb Smirnoff 	/* Nothing to do. */
2548cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
2549cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
2550cfcb5025SOleg Bulyzhin 		return;
2551cfcb5025SOleg Bulyzhin 
2552fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
255395d67482SBill Paul 
2554e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2555e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2556e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
255795d67482SBill Paul 	/*
255895d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
255995d67482SBill Paul 	 * frames that have been sent.
256095d67482SBill Paul 	 */
256195d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2562f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
25633f74909aSGleb Smirnoff 		uint32_t		idx = 0;
256495d67482SBill Paul 
256595d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2566f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
256795d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
256895d67482SBill Paul 			ifp->if_opackets++;
256995d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2570e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2571e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2572e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2573f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2574f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2575e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2576e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
257795d67482SBill Paul 		}
257895d67482SBill Paul 		sc->bge_txcnt--;
257995d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
258095d67482SBill Paul 		ifp->if_timer = 0;
258195d67482SBill Paul 	}
258295d67482SBill Paul 
258395d67482SBill Paul 	if (cur_tx != NULL)
258413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
258595d67482SBill Paul }
258695d67482SBill Paul 
258775719184SGleb Smirnoff #ifdef DEVICE_POLLING
258875719184SGleb Smirnoff static void
258975719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
259075719184SGleb Smirnoff {
259175719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
2592366454f2SOleg Bulyzhin 	uint32_t statusword;
259375719184SGleb Smirnoff 
25943f74909aSGleb Smirnoff 	BGE_LOCK(sc);
25953f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
25963f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
25973f74909aSGleb Smirnoff 		return;
25983f74909aSGleb Smirnoff 	}
259975719184SGleb Smirnoff 
2600dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2601e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2602dab5cd05SOleg Bulyzhin 
26033f74909aSGleb Smirnoff 	statusword = atomic_readandclear_32(
26043f74909aSGleb Smirnoff 	    &sc->bge_ldata.bge_status_block->bge_status);
2605dab5cd05SOleg Bulyzhin 
2606dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2607e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2608366454f2SOleg Bulyzhin 
2609366454f2SOleg Bulyzhin 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS cmd */
2610366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2611366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
2612366454f2SOleg Bulyzhin 
2613366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
2614366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2615366454f2SOleg Bulyzhin 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) ||
2616366454f2SOleg Bulyzhin 		    sc->bge_link_evt || sc->bge_tbi)
2617366454f2SOleg Bulyzhin 			bge_link_upd(sc);
2618366454f2SOleg Bulyzhin 
2619366454f2SOleg Bulyzhin 	sc->rxcycles = count;
2620366454f2SOleg Bulyzhin 	bge_rxeof(sc);
2621366454f2SOleg Bulyzhin 	bge_txeof(sc);
2622366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2623366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
26243f74909aSGleb Smirnoff 
26253f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
262675719184SGleb Smirnoff }
262775719184SGleb Smirnoff #endif /* DEVICE_POLLING */
262875719184SGleb Smirnoff 
262995d67482SBill Paul static void
26303f74909aSGleb Smirnoff bge_intr(void *xsc)
263195d67482SBill Paul {
263295d67482SBill Paul 	struct bge_softc *sc;
263395d67482SBill Paul 	struct ifnet *ifp;
2634dab5cd05SOleg Bulyzhin 	uint32_t statusword;
263595d67482SBill Paul 
263695d67482SBill Paul 	sc = xsc;
2637f41ac2beSBill Paul 
26380f9bd73bSSam Leffler 	BGE_LOCK(sc);
26390f9bd73bSSam Leffler 
2640dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2641dab5cd05SOleg Bulyzhin 
264275719184SGleb Smirnoff #ifdef DEVICE_POLLING
264375719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
264475719184SGleb Smirnoff 		BGE_UNLOCK(sc);
264575719184SGleb Smirnoff 		return;
264675719184SGleb Smirnoff 	}
264775719184SGleb Smirnoff #endif
264875719184SGleb Smirnoff 
2649f30cbfc6SScott Long 	/*
2650f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
2651f30cbfc6SScott Long 	 */
2652f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
2653f41ac2beSBill Paul 
265495d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
265595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
265695d67482SBill Paul 
2657f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
2658f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2659f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2660f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2661f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2662f30cbfc6SScott Long 
26631f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
26641f313773SOleg Bulyzhin 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) ||
2665f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
2666dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
266795d67482SBill Paul 
266813f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
26693f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
267095d67482SBill Paul 		bge_rxeof(sc);
267195d67482SBill Paul 
26723f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
267395d67482SBill Paul 		bge_txeof(sc);
267495d67482SBill Paul 	}
267595d67482SBill Paul 
267695d67482SBill Paul 	/* Re-enable interrupts. */
267795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
267895d67482SBill Paul 
267913f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
268013f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
26810f9bd73bSSam Leffler 		bge_start_locked(ifp);
26820f9bd73bSSam Leffler 
26830f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
268495d67482SBill Paul }
268595d67482SBill Paul 
268695d67482SBill Paul static void
26873f74909aSGleb Smirnoff bge_tick_locked(struct bge_softc *sc)
26880f9bd73bSSam Leffler {
268995d67482SBill Paul 	struct mii_data *mii = NULL;
269095d67482SBill Paul 
26910f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
269295d67482SBill Paul 
2693e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2694e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
26950434d1b8SBill Paul 		bge_stats_update_regs(sc);
26960434d1b8SBill Paul 	else
269795d67482SBill Paul 		bge_stats_update(sc);
269895d67482SBill Paul 
26991f313773SOleg Bulyzhin 	if (!sc->bge_tbi) {
270095d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
270195d67482SBill Paul 		mii_tick(mii);
27027b97099dSOleg Bulyzhin 	} else {
27037b97099dSOleg Bulyzhin 		/*
27047b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
27057b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
27067b97099dSOleg Bulyzhin 		 * and trigger interrupt.
27077b97099dSOleg Bulyzhin 		 */
27087b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
27093f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
27107b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
27117b97099dSOleg Bulyzhin #endif
27127b97099dSOleg Bulyzhin 		{
27137b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
27147b97099dSOleg Bulyzhin 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
27157b97099dSOleg Bulyzhin 		}
2716dab5cd05SOleg Bulyzhin 	}
271795d67482SBill Paul 
2718dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
271995d67482SBill Paul }
272095d67482SBill Paul 
272195d67482SBill Paul static void
27223f74909aSGleb Smirnoff bge_tick(void *xsc)
27230f9bd73bSSam Leffler {
27240f9bd73bSSam Leffler 	struct bge_softc *sc;
27250f9bd73bSSam Leffler 
27260f9bd73bSSam Leffler 	sc = xsc;
27270f9bd73bSSam Leffler 
27280f9bd73bSSam Leffler 	BGE_LOCK(sc);
27290f9bd73bSSam Leffler 	bge_tick_locked(sc);
27300f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
27310f9bd73bSSam Leffler }
27320f9bd73bSSam Leffler 
27330f9bd73bSSam Leffler static void
27343f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
27350434d1b8SBill Paul {
27360434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
27373f74909aSGleb Smirnoff 	struct ifnet *ifp;
27383f74909aSGleb Smirnoff 	uint32_t *s;
27396fb34dd2SOleg Bulyzhin 	u_long cnt;	/* current register value */
27400434d1b8SBill Paul 	int i;
27410434d1b8SBill Paul 
2742fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
27430434d1b8SBill Paul 
27443f74909aSGleb Smirnoff 	s = (uint32_t *)&stats;
27450434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
27460434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
27470434d1b8SBill Paul 		s++;
27480434d1b8SBill Paul 	}
27490434d1b8SBill Paul 
27506fb34dd2SOleg Bulyzhin 	cnt = stats.dot3StatsSingleCollisionFrames +
27510434d1b8SBill Paul 	    stats.dot3StatsMultipleCollisionFrames +
27520434d1b8SBill Paul 	    stats.dot3StatsExcessiveCollisions +
27536fb34dd2SOleg Bulyzhin 	    stats.dot3StatsLateCollisions;
27546fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
27556fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
27566fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
27570434d1b8SBill Paul }
27580434d1b8SBill Paul 
27590434d1b8SBill Paul static void
27603f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
276195d67482SBill Paul {
276295d67482SBill Paul 	struct ifnet *ifp;
2763e907febfSPyun YongHyeon 	bus_size_t stats;
27646fb34dd2SOleg Bulyzhin 	u_long cnt;	/* current register value */
276595d67482SBill Paul 
2766fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
276795d67482SBill Paul 
2768e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2769e907febfSPyun YongHyeon 
2770e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
2771e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
277295d67482SBill Paul 
27736fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats,
27746fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsSingleCollisionFrames.bge_addr_lo);
27756fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
27766fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo);
27776fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
27786fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsExcessiveCollisions.bge_addr_lo);
27796fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
27806fb34dd2SOleg Bulyzhin 		txstats.dot3StatsLateCollisions.bge_addr_lo);
27816fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
27826fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
27836fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
27846fb34dd2SOleg Bulyzhin 
27856fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
27866fb34dd2SOleg Bulyzhin 	ifp->if_ierrors += cnt >= sc->bge_rx_discards ?
27876fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_rx_discards : cnt;
27886fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
27896fb34dd2SOleg Bulyzhin 
27906fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
27916fb34dd2SOleg Bulyzhin 	ifp->if_oerrors += cnt >= sc->bge_tx_discards ?
27926fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_discards : cnt;
27936fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
279495d67482SBill Paul 
2795e907febfSPyun YongHyeon #undef READ_STAT
279695d67482SBill Paul }
279795d67482SBill Paul 
279895d67482SBill Paul /*
2799d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
2800d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
2801d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
2802d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
2803d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
2804d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
2805d375e524SGleb Smirnoff  */
2806d375e524SGleb Smirnoff static __inline int
2807d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
2808d375e524SGleb Smirnoff {
2809d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
2810d375e524SGleb Smirnoff 	struct mbuf *last;
2811d375e524SGleb Smirnoff 
2812d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
2813d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
2814d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
2815d375e524SGleb Smirnoff 		last = m;
2816d375e524SGleb Smirnoff 	} else {
2817d375e524SGleb Smirnoff 		/*
2818d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
2819d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
2820d375e524SGleb Smirnoff 		 */
2821d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
2822d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
2823d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
2824d375e524SGleb Smirnoff 			struct mbuf *n;
2825d375e524SGleb Smirnoff 
2826d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
2827d375e524SGleb Smirnoff 			if (n == NULL)
2828d375e524SGleb Smirnoff 				return (ENOBUFS);
2829d375e524SGleb Smirnoff 			n->m_len = 0;
2830d375e524SGleb Smirnoff 			last->m_next = n;
2831d375e524SGleb Smirnoff 			last = n;
2832d375e524SGleb Smirnoff 		}
2833d375e524SGleb Smirnoff 	}
2834d375e524SGleb Smirnoff 
2835d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
2836d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
2837d375e524SGleb Smirnoff 	last->m_len += padlen;
2838d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
2839d375e524SGleb Smirnoff 
2840d375e524SGleb Smirnoff 	return (0);
2841d375e524SGleb Smirnoff }
2842d375e524SGleb Smirnoff 
2843d375e524SGleb Smirnoff /*
284495d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
284595d67482SBill Paul  * pointers to descriptors.
284695d67482SBill Paul  */
284795d67482SBill Paul static int
28483f74909aSGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf *m_head, uint32_t *txidx)
284995d67482SBill Paul {
28507e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
2851f41ac2beSBill Paul 	bus_dmamap_t		map;
28527e27542aSGleb Smirnoff 	struct bge_tx_bd	*d = NULL;
28537e27542aSGleb Smirnoff 	struct m_tag		*mtag;
28547e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
28557e27542aSGleb Smirnoff 	uint16_t		csum_flags = 0;
28567e27542aSGleb Smirnoff 	int			nsegs, i, error;
285795d67482SBill Paul 
285895d67482SBill Paul 	if (m_head->m_pkthdr.csum_flags) {
285995d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
286095d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
2861d375e524SGleb Smirnoff 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
286295d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
2863d375e524SGleb Smirnoff 			if (m_head->m_pkthdr.len < ETHER_MIN_NOPAD &&
2864d375e524SGleb Smirnoff 			    bge_cksum_pad(m_head) != 0)
2865d375e524SGleb Smirnoff 				return (ENOBUFS);
2866d375e524SGleb Smirnoff 		}
286795d67482SBill Paul 		if (m_head->m_flags & M_LASTFRAG)
286895d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
286995d67482SBill Paul 		else if (m_head->m_flags & M_FRAG)
287095d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
287195d67482SBill Paul 	}
287295d67482SBill Paul 
2873fc74a9f9SBrooks Davis 	mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m_head);
2874673d9191SSam Leffler 
28757e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
28767e27542aSGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map,
28777e27542aSGleb Smirnoff 	    m_head, segs, &nsegs, BUS_DMA_NOWAIT);
28787e27542aSGleb Smirnoff         if (error) {
28797e27542aSGleb Smirnoff 		if (error == EFBIG) {
28807e27542aSGleb Smirnoff 			struct mbuf *m0;
28817e27542aSGleb Smirnoff 
28827e27542aSGleb Smirnoff 			m0 = m_defrag(m_head, M_DONTWAIT);
28837e27542aSGleb Smirnoff 			if (m0 == NULL)
28847e27542aSGleb Smirnoff 				return (ENOBUFS);
28857e27542aSGleb Smirnoff 			m_head = m0;
28867e27542aSGleb Smirnoff 			error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag,
28877e27542aSGleb Smirnoff 			    map, m_head, segs, &nsegs, BUS_DMA_NOWAIT);
28887e27542aSGleb Smirnoff 		}
28897e27542aSGleb Smirnoff 		if (error)
28907e27542aSGleb Smirnoff 			return (error);
28917e27542aSGleb Smirnoff 	}
28927e27542aSGleb Smirnoff 
289395d67482SBill Paul 	/*
289495d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
289595d67482SBill Paul 	 * of the end of the ring.
289695d67482SBill Paul 	 */
28977e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
28987e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
289995d67482SBill Paul 		return (ENOBUFS);
29007e27542aSGleb Smirnoff 	}
29017e27542aSGleb Smirnoff 
2902e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
2903e65bed95SPyun YongHyeon 
29047e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
29057e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
29067e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
29077e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
29087e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
29097e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
29107e27542aSGleb Smirnoff 		if (i == nsegs - 1)
29117e27542aSGleb Smirnoff 			break;
29127e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
29137e27542aSGleb Smirnoff 	}
29147e27542aSGleb Smirnoff 
29157e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
29167e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
29177e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
29187e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
29197e27542aSGleb Smirnoff 	if (mtag != NULL) {
29207e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
29217e27542aSGleb Smirnoff 		d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
29227e27542aSGleb Smirnoff 	} else
29237e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
2924f41ac2beSBill Paul 
2925f41ac2beSBill Paul 	/*
2926f41ac2beSBill Paul 	 * Insure that the map for this transmission
2927f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
2928f41ac2beSBill Paul 	 * in this chain.
2929f41ac2beSBill Paul 	 */
29307e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
29317e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
29327e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m_head;
29337e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
293495d67482SBill Paul 
29357e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
29367e27542aSGleb Smirnoff 	*txidx = idx;
293795d67482SBill Paul 
293895d67482SBill Paul 	return (0);
293995d67482SBill Paul }
294095d67482SBill Paul 
294195d67482SBill Paul /*
294295d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
294395d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
294495d67482SBill Paul  */
294595d67482SBill Paul static void
29463f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
294795d67482SBill Paul {
294895d67482SBill Paul 	struct bge_softc *sc;
294995d67482SBill Paul 	struct mbuf *m_head = NULL;
295014bbd30fSGleb Smirnoff 	uint32_t prodidx;
2951303a718cSDag-Erling Smørgrav 	int count = 0;
295295d67482SBill Paul 
295395d67482SBill Paul 	sc = ifp->if_softc;
295495d67482SBill Paul 
2955dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
295695d67482SBill Paul 		return;
295795d67482SBill Paul 
295814bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
295995d67482SBill Paul 
296095d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
29614d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
296295d67482SBill Paul 		if (m_head == NULL)
296395d67482SBill Paul 			break;
296495d67482SBill Paul 
296595d67482SBill Paul 		/*
296695d67482SBill Paul 		 * XXX
2967b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
2968b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
2969b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
2970b874fdd4SYaroslav Tykhiy 		 *
2971b874fdd4SYaroslav Tykhiy 		 * XXX
297295d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
297395d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
297495d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
297595d67482SBill Paul 		 * chain at once.
297695d67482SBill Paul 		 * (paranoia -- may not actually be needed)
297795d67482SBill Paul 		 */
297895d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
297995d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
298095d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
298195d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
29824d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
298313f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
298495d67482SBill Paul 				break;
298595d67482SBill Paul 			}
298695d67482SBill Paul 		}
298795d67482SBill Paul 
298895d67482SBill Paul 		/*
298995d67482SBill Paul 		 * Pack the data into the transmit ring. If we
299095d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
299195d67482SBill Paul 		 * for the NIC to drain the ring.
299295d67482SBill Paul 		 */
299395d67482SBill Paul 		if (bge_encap(sc, m_head, &prodidx)) {
29944d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
299513f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
299695d67482SBill Paul 			break;
299795d67482SBill Paul 		}
2998303a718cSDag-Erling Smørgrav 		++count;
299995d67482SBill Paul 
300095d67482SBill Paul 		/*
300195d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
300295d67482SBill Paul 		 * to him.
300395d67482SBill Paul 		 */
3004673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
300595d67482SBill Paul 	}
300695d67482SBill Paul 
30073f74909aSGleb Smirnoff 	if (count == 0)
30083f74909aSGleb Smirnoff 		/* No packets were dequeued. */
3009303a718cSDag-Erling Smørgrav 		return;
3010303a718cSDag-Erling Smørgrav 
30113f74909aSGleb Smirnoff 	/* Transmit. */
301295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
30133927098fSPaul Saab 	/* 5700 b2 errata */
3014e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
30153927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
301695d67482SBill Paul 
301714bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
301814bbd30fSGleb Smirnoff 
301995d67482SBill Paul 	/*
302095d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
302195d67482SBill Paul 	 */
302295d67482SBill Paul 	ifp->if_timer = 5;
302395d67482SBill Paul }
302495d67482SBill Paul 
30250f9bd73bSSam Leffler /*
30260f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
30270f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
30280f9bd73bSSam Leffler  */
302995d67482SBill Paul static void
30303f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
303195d67482SBill Paul {
30320f9bd73bSSam Leffler 	struct bge_softc *sc;
30330f9bd73bSSam Leffler 
30340f9bd73bSSam Leffler 	sc = ifp->if_softc;
30350f9bd73bSSam Leffler 	BGE_LOCK(sc);
30360f9bd73bSSam Leffler 	bge_start_locked(ifp);
30370f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
30380f9bd73bSSam Leffler }
30390f9bd73bSSam Leffler 
30400f9bd73bSSam Leffler static void
30413f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
30420f9bd73bSSam Leffler {
304395d67482SBill Paul 	struct ifnet *ifp;
30443f74909aSGleb Smirnoff 	uint16_t *m;
304595d67482SBill Paul 
30460f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
304795d67482SBill Paul 
3048fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
304995d67482SBill Paul 
305013f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
305195d67482SBill Paul 		return;
305295d67482SBill Paul 
305395d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
305495d67482SBill Paul 	bge_stop(sc);
305595d67482SBill Paul 	bge_reset(sc);
305695d67482SBill Paul 	bge_chipinit(sc);
305795d67482SBill Paul 
305895d67482SBill Paul 	/*
305995d67482SBill Paul 	 * Init the various state machines, ring
306095d67482SBill Paul 	 * control blocks and firmware.
306195d67482SBill Paul 	 */
306295d67482SBill Paul 	if (bge_blockinit(sc)) {
3063fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
306495d67482SBill Paul 		return;
306595d67482SBill Paul 	}
306695d67482SBill Paul 
3067fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
306895d67482SBill Paul 
306995d67482SBill Paul 	/* Specify MTU. */
307095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3071859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
307295d67482SBill Paul 
307395d67482SBill Paul 	/* Load our MAC address. */
30743f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
307595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
307695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
307795d67482SBill Paul 
307895d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
307995d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
308095d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
308195d67482SBill Paul 	} else {
308295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
308395d67482SBill Paul 	}
308495d67482SBill Paul 
308595d67482SBill Paul 	/* Program multicast filter. */
308695d67482SBill Paul 	bge_setmulti(sc);
308795d67482SBill Paul 
308895d67482SBill Paul 	/* Init RX ring. */
308995d67482SBill Paul 	bge_init_rx_ring_std(sc);
309095d67482SBill Paul 
30910434d1b8SBill Paul 	/*
30920434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
30930434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
30940434d1b8SBill Paul 	 * entry of the ring.
30950434d1b8SBill Paul 	 */
30960434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
30973f74909aSGleb Smirnoff 		uint32_t		v, i;
30980434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
30990434d1b8SBill Paul 			DELAY(20);
31000434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
31010434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
31020434d1b8SBill Paul 				break;
31030434d1b8SBill Paul 		}
31040434d1b8SBill Paul 		if (i == 10)
3105fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3106fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
31070434d1b8SBill Paul 	}
31080434d1b8SBill Paul 
310995d67482SBill Paul 	/* Init jumbo RX ring. */
311095d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
311195d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
311295d67482SBill Paul 
31133f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
311495d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
311595d67482SBill Paul 
311695d67482SBill Paul 	/* Init TX ring. */
311795d67482SBill Paul 	bge_init_tx_ring(sc);
311895d67482SBill Paul 
31193f74909aSGleb Smirnoff 	/* Turn on transmitter. */
312095d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
312195d67482SBill Paul 
31223f74909aSGleb Smirnoff 	/* Turn on receiver. */
312395d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
312495d67482SBill Paul 
312595d67482SBill Paul 	/* Tell firmware we're alive. */
312695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
312795d67482SBill Paul 
312875719184SGleb Smirnoff #ifdef DEVICE_POLLING
312975719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
313075719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
313175719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
313275719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
313375719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
313475719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
313575719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
313675719184SGleb Smirnoff 	} else
313775719184SGleb Smirnoff #endif
313875719184SGleb Smirnoff 
313995d67482SBill Paul 	/* Enable host interrupts. */
314075719184SGleb Smirnoff 	{
314195d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
314295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
314395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
314475719184SGleb Smirnoff 	}
314595d67482SBill Paul 
314695d67482SBill Paul 	bge_ifmedia_upd(ifp);
314795d67482SBill Paul 
314813f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
314913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
315095d67482SBill Paul 
31510f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
31520f9bd73bSSam Leffler }
31530f9bd73bSSam Leffler 
31540f9bd73bSSam Leffler static void
31553f74909aSGleb Smirnoff bge_init(void *xsc)
31560f9bd73bSSam Leffler {
31570f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
31580f9bd73bSSam Leffler 
31590f9bd73bSSam Leffler 	BGE_LOCK(sc);
31600f9bd73bSSam Leffler 	bge_init_locked(sc);
31610f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
316295d67482SBill Paul }
316395d67482SBill Paul 
316495d67482SBill Paul /*
316595d67482SBill Paul  * Set media options.
316695d67482SBill Paul  */
316795d67482SBill Paul static int
31683f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
316995d67482SBill Paul {
317095d67482SBill Paul 	struct bge_softc *sc;
317195d67482SBill Paul 	struct mii_data *mii;
317295d67482SBill Paul 	struct ifmedia *ifm;
317395d67482SBill Paul 
317495d67482SBill Paul 	sc = ifp->if_softc;
317595d67482SBill Paul 	ifm = &sc->bge_ifmedia;
317695d67482SBill Paul 
317795d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
317895d67482SBill Paul 	if (sc->bge_tbi) {
317995d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
318095d67482SBill Paul 			return (EINVAL);
318195d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
318295d67482SBill Paul 		case IFM_AUTO:
3183ff50922bSDoug White 			/*
3184ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3185ff50922bSDoug White 			 * mechanism for programming the autoneg
3186ff50922bSDoug White 			 * advertisement registers in TBI mode.
3187ff50922bSDoug White 			 */
3188c4529f41SMichael Reifenberger 			if (bge_fake_autoneg == 0 &&
3189c4529f41SMichael Reifenberger 			    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3190ff50922bSDoug White 				uint32_t sgdig;
3191ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3192ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3193ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3194ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3195ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3196ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3197ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3198ff50922bSDoug White 				DELAY(5);
3199ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3200ff50922bSDoug White 			}
320195d67482SBill Paul 			break;
320295d67482SBill Paul 		case IFM_1000_SX:
320395d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
320495d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
320595d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
320695d67482SBill Paul 			} else {
320795d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
320895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
320995d67482SBill Paul 			}
321095d67482SBill Paul 			break;
321195d67482SBill Paul 		default:
321295d67482SBill Paul 			return (EINVAL);
321395d67482SBill Paul 		}
321495d67482SBill Paul 		return (0);
321595d67482SBill Paul 	}
321695d67482SBill Paul 
32171493e883SOleg Bulyzhin 	sc->bge_link_evt++;
321895d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
321995d67482SBill Paul 	if (mii->mii_instance) {
322095d67482SBill Paul 		struct mii_softc *miisc;
322195d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
322295d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
322395d67482SBill Paul 			mii_phy_reset(miisc);
322495d67482SBill Paul 	}
322595d67482SBill Paul 	mii_mediachg(mii);
322695d67482SBill Paul 
322795d67482SBill Paul 	return (0);
322895d67482SBill Paul }
322995d67482SBill Paul 
323095d67482SBill Paul /*
323195d67482SBill Paul  * Report current media status.
323295d67482SBill Paul  */
323395d67482SBill Paul static void
32343f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
323595d67482SBill Paul {
323695d67482SBill Paul 	struct bge_softc *sc;
323795d67482SBill Paul 	struct mii_data *mii;
323895d67482SBill Paul 
323995d67482SBill Paul 	sc = ifp->if_softc;
324095d67482SBill Paul 
324195d67482SBill Paul 	if (sc->bge_tbi) {
324295d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
324395d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
324495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
324595d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
324695d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
324795d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
324895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
324995d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
325095d67482SBill Paul 		else
325195d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
325295d67482SBill Paul 		return;
325395d67482SBill Paul 	}
325495d67482SBill Paul 
325595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
325695d67482SBill Paul 	mii_pollstat(mii);
325795d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
325895d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
325995d67482SBill Paul }
326095d67482SBill Paul 
326195d67482SBill Paul static int
32623f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
326395d67482SBill Paul {
326495d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
326595d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
326695d67482SBill Paul 	struct mii_data *mii;
32673f74909aSGleb Smirnoff 	int mask, error = 0;
326895d67482SBill Paul 
326995d67482SBill Paul 	switch (command) {
327095d67482SBill Paul 	case SIOCSIFMTU:
32710434d1b8SBill Paul 		/* Disallow jumbo frames on 5705. */
3272e53d81eeSPaul Saab 		if (((sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
3273e53d81eeSPaul Saab 		      sc->bge_asicrev == BGE_ASICREV_BCM5750) &&
32740434d1b8SBill Paul 		    ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU)
327595d67482SBill Paul 			error = EINVAL;
327695d67482SBill Paul 		else {
327795d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
327813f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
327995d67482SBill Paul 			bge_init(sc);
328095d67482SBill Paul 		}
328195d67482SBill Paul 		break;
328295d67482SBill Paul 	case SIOCSIFFLAGS:
32830f9bd73bSSam Leffler 		BGE_LOCK(sc);
328495d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
328595d67482SBill Paul 			/*
328695d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
328795d67482SBill Paul 			 * then just use the 'set promisc mode' command
328895d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
328995d67482SBill Paul 			 * a full re-init means reloading the firmware and
329095d67482SBill Paul 			 * waiting for it to start up, which may take a
3291d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
329295d67482SBill Paul 			 */
329313f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
329495d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
329595d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
329695d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
329795d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
329813f4c340SRobert Watson 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
329995d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
330095d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
330195d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
330295d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
3303d183af7fSRuslan Ermilov 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3304d183af7fSRuslan Ermilov 			    (ifp->if_flags ^ sc->bge_if_flags) & IFF_ALLMULTI) {
3305d183af7fSRuslan Ermilov 				bge_setmulti(sc);
330695d67482SBill Paul 			} else
33070f9bd73bSSam Leffler 				bge_init_locked(sc);
330895d67482SBill Paul 		} else {
330913f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
331095d67482SBill Paul 				bge_stop(sc);
331195d67482SBill Paul 			}
331295d67482SBill Paul 		}
331395d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
33140f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
331595d67482SBill Paul 		error = 0;
331695d67482SBill Paul 		break;
331795d67482SBill Paul 	case SIOCADDMULTI:
331895d67482SBill Paul 	case SIOCDELMULTI:
331913f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
33200f9bd73bSSam Leffler 			BGE_LOCK(sc);
332195d67482SBill Paul 			bge_setmulti(sc);
33220f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
332395d67482SBill Paul 			error = 0;
332495d67482SBill Paul 		}
332595d67482SBill Paul 		break;
332695d67482SBill Paul 	case SIOCSIFMEDIA:
332795d67482SBill Paul 	case SIOCGIFMEDIA:
332895d67482SBill Paul 		if (sc->bge_tbi) {
332995d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
333095d67482SBill Paul 			    &sc->bge_ifmedia, command);
333195d67482SBill Paul 		} else {
333295d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
333395d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
333495d67482SBill Paul 			    &mii->mii_media, command);
333595d67482SBill Paul 		}
333695d67482SBill Paul 		break;
333795d67482SBill Paul 	case SIOCSIFCAP:
333895d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
333975719184SGleb Smirnoff #ifdef DEVICE_POLLING
334075719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
334175719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
334275719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
334375719184SGleb Smirnoff 				if (error)
334475719184SGleb Smirnoff 					return (error);
334575719184SGleb Smirnoff 				BGE_LOCK(sc);
334675719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
334775719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
334875719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
334975719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
335075719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
335175719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
335275719184SGleb Smirnoff 				BGE_UNLOCK(sc);
335375719184SGleb Smirnoff 			} else {
335475719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
335575719184SGleb Smirnoff 				/* Enable interrupt even in error case */
335675719184SGleb Smirnoff 				BGE_LOCK(sc);
335775719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
335875719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
335975719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
336075719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
336175719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
336275719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
336375719184SGleb Smirnoff 				BGE_UNLOCK(sc);
336475719184SGleb Smirnoff 			}
336575719184SGleb Smirnoff 		}
336675719184SGleb Smirnoff #endif
3367d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3368d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3369d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3370d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3371b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
337295d67482SBill Paul 			else
3373b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
3374479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
337595d67482SBill Paul 		}
337695d67482SBill Paul 		break;
337795d67482SBill Paul 	default:
3378673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
337995d67482SBill Paul 		break;
338095d67482SBill Paul 	}
338195d67482SBill Paul 
338295d67482SBill Paul 	return (error);
338395d67482SBill Paul }
338495d67482SBill Paul 
338595d67482SBill Paul static void
33863f74909aSGleb Smirnoff bge_watchdog(struct ifnet *ifp)
338795d67482SBill Paul {
338895d67482SBill Paul 	struct bge_softc *sc;
338995d67482SBill Paul 
339095d67482SBill Paul 	sc = ifp->if_softc;
339195d67482SBill Paul 
3392fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
339395d67482SBill Paul 
339413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
339595d67482SBill Paul 	bge_init(sc);
339695d67482SBill Paul 
339795d67482SBill Paul 	ifp->if_oerrors++;
339895d67482SBill Paul }
339995d67482SBill Paul 
340095d67482SBill Paul /*
340195d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
340295d67482SBill Paul  * RX and TX lists.
340395d67482SBill Paul  */
340495d67482SBill Paul static void
34053f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
340695d67482SBill Paul {
340795d67482SBill Paul 	struct ifnet *ifp;
340895d67482SBill Paul 	struct ifmedia_entry *ifm;
340995d67482SBill Paul 	struct mii_data *mii = NULL;
341095d67482SBill Paul 	int mtmp, itmp;
341195d67482SBill Paul 
34120f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
34130f9bd73bSSam Leffler 
3414fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
341595d67482SBill Paul 
341695d67482SBill Paul 	if (!sc->bge_tbi)
341795d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
341895d67482SBill Paul 
34190f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
342095d67482SBill Paul 
342195d67482SBill Paul 	/*
34223f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
342395d67482SBill Paul 	 */
342495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
342595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
342695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
34275dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3428e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
342995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
343095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
343195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
343295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
343395d67482SBill Paul 
343495d67482SBill Paul 	/*
34353f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
343695d67482SBill Paul 	 */
343795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
343895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
343995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
344095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
344195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
34425dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3443e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
344495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
344595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
344695d67482SBill Paul 
344795d67482SBill Paul 	/*
344895d67482SBill Paul 	 * Shut down all of the memory managers and related
344995d67482SBill Paul 	 * state machines.
345095d67482SBill Paul 	 */
345195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
345295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
34535dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3454e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
345595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
345695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
345795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
34585dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3459e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
346095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
346195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
34620434d1b8SBill Paul 	}
346395d67482SBill Paul 
346495d67482SBill Paul 	/* Disable host interrupts. */
346595d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
346695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
346795d67482SBill Paul 
346895d67482SBill Paul 	/*
346995d67482SBill Paul 	 * Tell firmware we're shutting down.
347095d67482SBill Paul 	 */
347195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
347295d67482SBill Paul 
347395d67482SBill Paul 	/* Free the RX lists. */
347495d67482SBill Paul 	bge_free_rx_ring_std(sc);
347595d67482SBill Paul 
347695d67482SBill Paul 	/* Free jumbo RX list. */
34775dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3478e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
347995d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
348095d67482SBill Paul 
348195d67482SBill Paul 	/* Free TX buffers. */
348295d67482SBill Paul 	bge_free_tx_ring(sc);
348395d67482SBill Paul 
348495d67482SBill Paul 	/*
348595d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
348695d67482SBill Paul 	 * unchanged so that things will be put back to normal when
348795d67482SBill Paul 	 * we bring the interface back up.
348895d67482SBill Paul 	 */
348995d67482SBill Paul 	if (!sc->bge_tbi) {
349095d67482SBill Paul 		itmp = ifp->if_flags;
349195d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3492dcc34049SPawel Jakub Dawidek 		/*
3493dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3494dcc34049SPawel Jakub Dawidek 		 */
3495dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
349695d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
349795d67482SBill Paul 			mtmp = ifm->ifm_media;
349895d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
349995d67482SBill Paul 			mii_mediachg(mii);
350095d67482SBill Paul 			ifm->ifm_media = mtmp;
3501dcc34049SPawel Jakub Dawidek 		}
350295d67482SBill Paul 		ifp->if_flags = itmp;
350395d67482SBill Paul 	}
350495d67482SBill Paul 
350595d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
350695d67482SBill Paul 
35071493e883SOleg Bulyzhin 	/*
35081493e883SOleg Bulyzhin 	 * We can't just call bge_link_upd() cause chip is almost stopped so
35091493e883SOleg Bulyzhin 	 * bge_link_upd -> bge_tick_locked -> bge_stats_update sequence may
35101493e883SOleg Bulyzhin 	 * lead to hardware deadlock. So we just clearing MAC's link state
35111493e883SOleg Bulyzhin 	 * (PHY may still have link UP).
35121493e883SOleg Bulyzhin 	 */
35131493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
35141493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
35151493e883SOleg Bulyzhin 	sc->bge_link = 0;
351695d67482SBill Paul 
35171493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
351895d67482SBill Paul }
351995d67482SBill Paul 
352095d67482SBill Paul /*
352195d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
352295d67482SBill Paul  * get confused by errant DMAs when rebooting.
352395d67482SBill Paul  */
352495d67482SBill Paul static void
35253f74909aSGleb Smirnoff bge_shutdown(device_t dev)
352695d67482SBill Paul {
352795d67482SBill Paul 	struct bge_softc *sc;
352895d67482SBill Paul 
352995d67482SBill Paul 	sc = device_get_softc(dev);
353095d67482SBill Paul 
35310f9bd73bSSam Leffler 	BGE_LOCK(sc);
353295d67482SBill Paul 	bge_stop(sc);
353395d67482SBill Paul 	bge_reset(sc);
35340f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
353595d67482SBill Paul }
353614afefa3SPawel Jakub Dawidek 
353714afefa3SPawel Jakub Dawidek static int
353814afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
353914afefa3SPawel Jakub Dawidek {
354014afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
354114afefa3SPawel Jakub Dawidek 
354214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
354314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
354414afefa3SPawel Jakub Dawidek 	bge_stop(sc);
354514afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
354614afefa3SPawel Jakub Dawidek 
354714afefa3SPawel Jakub Dawidek 	return (0);
354814afefa3SPawel Jakub Dawidek }
354914afefa3SPawel Jakub Dawidek 
355014afefa3SPawel Jakub Dawidek static int
355114afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
355214afefa3SPawel Jakub Dawidek {
355314afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
355414afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
355514afefa3SPawel Jakub Dawidek 
355614afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
355714afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
355814afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
355914afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
356014afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
356114afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
356214afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
356314afefa3SPawel Jakub Dawidek 	}
356414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
356514afefa3SPawel Jakub Dawidek 
356614afefa3SPawel Jakub Dawidek 	return (0);
356714afefa3SPawel Jakub Dawidek }
3568dab5cd05SOleg Bulyzhin 
3569dab5cd05SOleg Bulyzhin static void
35703f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
3571dab5cd05SOleg Bulyzhin {
35721f313773SOleg Bulyzhin 	struct mii_data *mii;
35731f313773SOleg Bulyzhin 	uint32_t link, status;
3574dab5cd05SOleg Bulyzhin 
3575dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
35761f313773SOleg Bulyzhin 
35773f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
35787b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
35797b97099dSOleg Bulyzhin 
3580dab5cd05SOleg Bulyzhin 	/*
3581dab5cd05SOleg Bulyzhin 	 * Process link state changes.
3582dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
3583dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
3584dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
3585dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
3586dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
3587dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
3588dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
3589dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
35901f313773SOleg Bulyzhin 	 *
35911f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
35921f313773SOleg Bulyzhin 	 * BGE_CHIPID_BCM5700_B1 can be used for others BCM5700 revisions.
3593dab5cd05SOleg Bulyzhin 	 */
3594dab5cd05SOleg Bulyzhin 
35951f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
35961f313773SOleg Bulyzhin 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) {
3597dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
3598dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
3599dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3600dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
36011f313773SOleg Bulyzhin 
36021f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
36031f313773SOleg Bulyzhin 			if (!sc->bge_link &&
36041f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
36051f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
36061f313773SOleg Bulyzhin 				sc->bge_link++;
36071f313773SOleg Bulyzhin 				if (bootverbose)
36081f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
36091f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
36101f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
36111f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
36121f313773SOleg Bulyzhin 				sc->bge_link = 0;
36131f313773SOleg Bulyzhin 				if (bootverbose)
36141f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
36151f313773SOleg Bulyzhin 			}
36161f313773SOleg Bulyzhin 
36173f74909aSGleb Smirnoff 			/* Clear the interrupt. */
3618dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
3619dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
3620dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
3621dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
3622dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
3623dab5cd05SOleg Bulyzhin 		}
3624dab5cd05SOleg Bulyzhin 		return;
3625dab5cd05SOleg Bulyzhin 	}
3626dab5cd05SOleg Bulyzhin 
36271f313773SOleg Bulyzhin 	if (sc->bge_tbi) {
36281f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
36297b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
36307b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
36311f313773SOleg Bulyzhin 				sc->bge_link++;
36321f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
36331f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
36341f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
36351f313773SOleg Bulyzhin 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
36361f313773SOleg Bulyzhin 				if (bootverbose)
36371f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
36383f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
36393f74909aSGleb Smirnoff 				    LINK_STATE_UP);
36407b97099dSOleg Bulyzhin 			}
36411f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
3642dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
36431f313773SOleg Bulyzhin 			if (bootverbose)
36441f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
36457b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
36461f313773SOleg Bulyzhin 		}
36471493e883SOleg Bulyzhin 	/* Discard link events for MII/GMII cards if MI auto-polling disabled */
36481493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
36491f313773SOleg Bulyzhin 		/*
36501f313773SOleg Bulyzhin 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
36511f313773SOleg Bulyzhin 		 * in status word always set. Workaround this bug by reading
36521f313773SOleg Bulyzhin 		 * PHY link status directly.
36531f313773SOleg Bulyzhin 		 */
36541f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
36551f313773SOleg Bulyzhin 
36561f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
36571f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
3658dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3659dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
36601f313773SOleg Bulyzhin 
36611f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
36621f313773SOleg Bulyzhin 			if (!sc->bge_link &&
36631f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
36641f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
36651f313773SOleg Bulyzhin 				sc->bge_link++;
36661f313773SOleg Bulyzhin 				if (bootverbose)
36671f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
36681f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
36691f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
36701f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
36711f313773SOleg Bulyzhin 				sc->bge_link = 0;
36721f313773SOleg Bulyzhin 				if (bootverbose)
36731f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
36741f313773SOleg Bulyzhin 			}
36751f313773SOleg Bulyzhin 		}
3676dab5cd05SOleg Bulyzhin 	}
3677dab5cd05SOleg Bulyzhin 
36783f74909aSGleb Smirnoff 	/* Clear the attention. */
3679dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
3680dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
3681dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
3682dab5cd05SOleg Bulyzhin }
3683