xref: /freebsd/sys/dev/bge/if_bge.c (revision 4e7ba1ab94f971c347f55041cdd9b29e087727fd)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
3895d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4195d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4295d67482SBill Paul  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
8295d67482SBill Paul 
8395d67482SBill Paul #include <net/if.h>
8495d67482SBill Paul #include <net/if_arp.h>
8595d67482SBill Paul #include <net/ethernet.h>
8695d67482SBill Paul #include <net/if_dl.h>
8795d67482SBill Paul #include <net/if_media.h>
8895d67482SBill Paul 
8995d67482SBill Paul #include <net/bpf.h>
9095d67482SBill Paul 
9195d67482SBill Paul #include <net/if_types.h>
9295d67482SBill Paul #include <net/if_vlan_var.h>
9395d67482SBill Paul 
9495d67482SBill Paul #include <netinet/in_systm.h>
9595d67482SBill Paul #include <netinet/in.h>
9695d67482SBill Paul #include <netinet/ip.h>
9795d67482SBill Paul 
9895d67482SBill Paul #include <machine/clock.h>      /* for DELAY */
9995d67482SBill Paul #include <machine/bus.h>
10095d67482SBill Paul #include <machine/resource.h>
10195d67482SBill Paul #include <sys/bus.h>
10295d67482SBill Paul #include <sys/rman.h>
10395d67482SBill Paul 
10495d67482SBill Paul #include <dev/mii/mii.h>
10595d67482SBill Paul #include <dev/mii/miivar.h>
1062d3ce713SDavid E. O'Brien #include "miidevs.h"
10795d67482SBill Paul #include <dev/mii/brgphyreg.h>
10895d67482SBill Paul 
1094fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1104fbd232cSWarner Losh #include <dev/pci/pcivar.h>
11195d67482SBill Paul 
11295d67482SBill Paul #include <dev/bge/if_bgereg.h>
11395d67482SBill Paul 
114ff50922bSDoug White #include "opt_bge.h"
115ff50922bSDoug White 
1165ddc5794SJohn Polstra #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
117d375e524SGleb Smirnoff #define ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
11895d67482SBill Paul 
119f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
120f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12195d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12295d67482SBill Paul 
1237b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12495d67482SBill Paul #include "miibus_if.h"
12595d67482SBill Paul 
12695d67482SBill Paul /*
12795d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
12895d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
12995d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13095d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13195d67482SBill Paul  */
132029e2ee3SJohn Polstra #define BGE_DEVDESC_MAX		64	/* Maximum device description length */
13395d67482SBill Paul 
13495d67482SBill Paul static struct bge_type bge_devs[] = {
13595d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5700,
13695d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
13795d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5701,
13895d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
13995d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5700,
14095d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
14195d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5701,
14295d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
1430434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702,
1440434d1b8SBill Paul 		"Broadcom BCM5702 Gigabit Ethernet" },
14501598b8dSMitsuru IWASAKI 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702X,
14601598b8dSMitsuru IWASAKI 		"Broadcom BCM5702X Gigabit Ethernet" },
1470434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703,
1480434d1b8SBill Paul 		"Broadcom BCM5703 Gigabit Ethernet" },
149b1265c1aSJohn Polstra 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703X,
150b1265c1aSJohn Polstra 		"Broadcom BCM5703X Gigabit Ethernet" },
1516ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704C,
1526ac6d2c8SPaul Saab 		"Broadcom BCM5704C Dual Gigabit Ethernet" },
1536ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704S,
1546ac6d2c8SPaul Saab 		"Broadcom BCM5704S Dual Gigabit Ethernet" },
1550434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705,
1560434d1b8SBill Paul 		"Broadcom BCM5705 Gigabit Ethernet" },
157c001ccf2SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705K,
158c001ccf2SPaul Saab 		"Broadcom BCM5705K Gigabit Ethernet" },
1590434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M,
1600434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
1610434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M_ALT,
1620434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
163419c028bSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5714C,
164419c028bSPaul Saab 		"Broadcom BCM5714C Gigabit Ethernet" },
16535ca8069SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5721,
16635ca8069SPaul Saab 		"Broadcom BCM5721 Gigabit Ethernet" },
167e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5750,
168e53d81eeSPaul Saab 		"Broadcom BCM5750 Gigabit Ethernet" },
169e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5750M,
170e53d81eeSPaul Saab 		"Broadcom BCM5750M Gigabit Ethernet" },
171e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5751,
172e53d81eeSPaul Saab 		"Broadcom BCM5751 Gigabit Ethernet" },
173d2014b30STai-hwa Liang 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5751M,
174d2014b30STai-hwa Liang 		"Broadcom BCM5751M Gigabit Ethernet" },
175560c1670SGleb Smirnoff 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5752,
176560c1670SGleb Smirnoff 		"Broadcom BCM5752 Gigabit Ethernet" },
1770434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5782,
1780434d1b8SBill Paul 		"Broadcom BCM5782 Gigabit Ethernet" },
1799f71a4c2SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5788,
1809f71a4c2SBill Paul 		"Broadcom BCM5788 Gigabit Ethernet" },
181c3615d48SMike Silbersack 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5789,
182c3615d48SMike Silbersack 		"Broadcom BCM5789 Gigabit Ethernet" },
1835d99c641SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901,
1845d99c641SBill Paul 		"Broadcom BCM5901 Fast Ethernet" },
1855d99c641SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901A2,
1865d99c641SBill Paul 		"Broadcom BCM5901A2 Fast Ethernet" },
18795d67482SBill Paul 	{ SK_VENDORID, SK_DEVICEID_ALTIMA,
18895d67482SBill Paul 		"SysKonnect Gigabit Ethernet" },
189586d7c2eSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000,
190586d7c2eSJohn Polstra 		"Altima AC1000 Gigabit Ethernet" },
1912aae6624SBill Paul 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1002,
1922aae6624SBill Paul 		"Altima AC1002 Gigabit Ethernet" },
193470bd96aSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100,
194470bd96aSJohn Polstra 		"Altima AC9100 Gigabit Ethernet" },
19595d67482SBill Paul 	{ 0, 0, NULL }
19695d67482SBill Paul };
19795d67482SBill Paul 
198e51a25f8SAlfred Perlstein static int bge_probe		(device_t);
199e51a25f8SAlfred Perlstein static int bge_attach		(device_t);
200e51a25f8SAlfred Perlstein static int bge_detach		(device_t);
20114afefa3SPawel Jakub Dawidek static int bge_suspend		(device_t);
20214afefa3SPawel Jakub Dawidek static int bge_resume		(device_t);
20395d67482SBill Paul static void bge_release_resources
204e51a25f8SAlfred Perlstein 				(struct bge_softc *);
205f41ac2beSBill Paul static void bge_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
206f41ac2beSBill Paul static int bge_dma_alloc	(device_t);
207f41ac2beSBill Paul static void bge_dma_free	(struct bge_softc *);
208f41ac2beSBill Paul 
209e51a25f8SAlfred Perlstein static void bge_txeof		(struct bge_softc *);
210e51a25f8SAlfred Perlstein static void bge_rxeof		(struct bge_softc *);
21195d67482SBill Paul 
2120f9bd73bSSam Leffler static void bge_tick_locked	(struct bge_softc *);
213e51a25f8SAlfred Perlstein static void bge_tick		(void *);
214e51a25f8SAlfred Perlstein static void bge_stats_update	(struct bge_softc *);
2150434d1b8SBill Paul static void bge_stats_update_regs
2160434d1b8SBill Paul 				(struct bge_softc *);
217e51a25f8SAlfred Perlstein static int bge_encap		(struct bge_softc *, struct mbuf *,
218e51a25f8SAlfred Perlstein 					u_int32_t *);
21995d67482SBill Paul 
220e51a25f8SAlfred Perlstein static void bge_intr		(void *);
2210f9bd73bSSam Leffler static void bge_start_locked	(struct ifnet *);
222e51a25f8SAlfred Perlstein static void bge_start		(struct ifnet *);
223e51a25f8SAlfred Perlstein static int bge_ioctl		(struct ifnet *, u_long, caddr_t);
2240f9bd73bSSam Leffler static void bge_init_locked	(struct bge_softc *);
225e51a25f8SAlfred Perlstein static void bge_init		(void *);
226e51a25f8SAlfred Perlstein static void bge_stop		(struct bge_softc *);
227e51a25f8SAlfred Perlstein static void bge_watchdog		(struct ifnet *);
228e51a25f8SAlfred Perlstein static void bge_shutdown		(device_t);
229e51a25f8SAlfred Perlstein static int bge_ifmedia_upd	(struct ifnet *);
230e51a25f8SAlfred Perlstein static void bge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
23195d67482SBill Paul 
232e51a25f8SAlfred Perlstein static u_int8_t	bge_eeprom_getbyte	(struct bge_softc *, int, u_int8_t *);
233e51a25f8SAlfred Perlstein static int bge_read_eeprom	(struct bge_softc *, caddr_t, int, int);
23495d67482SBill Paul 
235e51a25f8SAlfred Perlstein static void bge_setmulti	(struct bge_softc *);
23695d67482SBill Paul 
237e51a25f8SAlfred Perlstein static int bge_newbuf_std	(struct bge_softc *, int, struct mbuf *);
238e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo	(struct bge_softc *, int, struct mbuf *);
239e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std	(struct bge_softc *);
240e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std	(struct bge_softc *);
241e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo	(struct bge_softc *);
242e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo	(struct bge_softc *);
243e51a25f8SAlfred Perlstein static void bge_free_tx_ring	(struct bge_softc *);
244e51a25f8SAlfred Perlstein static int bge_init_tx_ring	(struct bge_softc *);
24595d67482SBill Paul 
246e51a25f8SAlfred Perlstein static int bge_chipinit		(struct bge_softc *);
247e51a25f8SAlfred Perlstein static int bge_blockinit	(struct bge_softc *);
24895d67482SBill Paul 
2491b4a3b2fSPeter Wemm #ifdef notdef
250e51a25f8SAlfred Perlstein static u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
251e51a25f8SAlfred Perlstein static void bge_vpd_read_res	(struct bge_softc *, struct vpd_res *, int);
252e51a25f8SAlfred Perlstein static void bge_vpd_read	(struct bge_softc *);
2531b4a3b2fSPeter Wemm #endif
25495d67482SBill Paul 
25595d67482SBill Paul static u_int32_t bge_readmem_ind
256e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
257e51a25f8SAlfred Perlstein static void bge_writemem_ind	(struct bge_softc *, int, int);
25895d67482SBill Paul #ifdef notdef
25995d67482SBill Paul static u_int32_t bge_readreg_ind
260e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
26195d67482SBill Paul #endif
262e51a25f8SAlfred Perlstein static void bge_writereg_ind	(struct bge_softc *, int, int);
26395d67482SBill Paul 
264e51a25f8SAlfred Perlstein static int bge_miibus_readreg	(device_t, int, int);
265e51a25f8SAlfred Perlstein static int bge_miibus_writereg	(device_t, int, int, int);
266e51a25f8SAlfred Perlstein static void bge_miibus_statchg	(device_t);
26775719184SGleb Smirnoff #ifdef DEVICE_POLLING
26875719184SGleb Smirnoff static void bge_poll		(struct ifnet *ifp, enum poll_cmd cmd,
26975719184SGleb Smirnoff 				    int count);
27075719184SGleb Smirnoff static void bge_poll_locked	(struct ifnet *ifp, enum poll_cmd cmd,
27175719184SGleb Smirnoff 				    int count);
27275719184SGleb Smirnoff #endif
27395d67482SBill Paul 
274e51a25f8SAlfred Perlstein static void bge_reset		(struct bge_softc *);
275dab5cd05SOleg Bulyzhin static void bge_link_upd	(struct bge_softc *);
27695d67482SBill Paul 
27795d67482SBill Paul static device_method_t bge_methods[] = {
27895d67482SBill Paul 	/* Device interface */
27995d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
28095d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
28195d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
28295d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
28314afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
28414afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
28595d67482SBill Paul 
28695d67482SBill Paul 	/* bus interface */
28795d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
28895d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
28995d67482SBill Paul 
29095d67482SBill Paul 	/* MII interface */
29195d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
29295d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
29395d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
29495d67482SBill Paul 
29595d67482SBill Paul 	{ 0, 0 }
29695d67482SBill Paul };
29795d67482SBill Paul 
29895d67482SBill Paul static driver_t bge_driver = {
29995d67482SBill Paul 	"bge",
30095d67482SBill Paul 	bge_methods,
30195d67482SBill Paul 	sizeof(struct bge_softc)
30295d67482SBill Paul };
30395d67482SBill Paul 
30495d67482SBill Paul static devclass_t bge_devclass;
30595d67482SBill Paul 
306f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
30795d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
30895d67482SBill Paul 
30995d67482SBill Paul static u_int32_t
31095d67482SBill Paul bge_readmem_ind(sc, off)
31195d67482SBill Paul 	struct bge_softc *sc;
31295d67482SBill Paul 	int off;
31395d67482SBill Paul {
31495d67482SBill Paul 	device_t dev;
31595d67482SBill Paul 
31695d67482SBill Paul 	dev = sc->bge_dev;
31795d67482SBill Paul 
31895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
31995d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
32095d67482SBill Paul }
32195d67482SBill Paul 
32295d67482SBill Paul static void
32395d67482SBill Paul bge_writemem_ind(sc, off, val)
32495d67482SBill Paul 	struct bge_softc *sc;
32595d67482SBill Paul 	int off, val;
32695d67482SBill Paul {
32795d67482SBill Paul 	device_t dev;
32895d67482SBill Paul 
32995d67482SBill Paul 	dev = sc->bge_dev;
33095d67482SBill Paul 
33195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
33295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
33395d67482SBill Paul 
33495d67482SBill Paul 	return;
33595d67482SBill Paul }
33695d67482SBill Paul 
33795d67482SBill Paul #ifdef notdef
33895d67482SBill Paul static u_int32_t
33995d67482SBill Paul bge_readreg_ind(sc, off)
34095d67482SBill Paul 	struct bge_softc *sc;
34195d67482SBill Paul 	int off;
34295d67482SBill Paul {
34395d67482SBill Paul 	device_t dev;
34495d67482SBill Paul 
34595d67482SBill Paul 	dev = sc->bge_dev;
34695d67482SBill Paul 
34795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
34895d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
34995d67482SBill Paul }
35095d67482SBill Paul #endif
35195d67482SBill Paul 
35295d67482SBill Paul static void
35395d67482SBill Paul bge_writereg_ind(sc, off, val)
35495d67482SBill Paul 	struct bge_softc *sc;
35595d67482SBill Paul 	int off, val;
35695d67482SBill Paul {
35795d67482SBill Paul 	device_t dev;
35895d67482SBill Paul 
35995d67482SBill Paul 	dev = sc->bge_dev;
36095d67482SBill Paul 
36195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
36295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
36395d67482SBill Paul 
36495d67482SBill Paul 	return;
36595d67482SBill Paul }
36695d67482SBill Paul 
367f41ac2beSBill Paul /*
368f41ac2beSBill Paul  * Map a single buffer address.
369f41ac2beSBill Paul  */
370f41ac2beSBill Paul 
371f41ac2beSBill Paul static void
372f41ac2beSBill Paul bge_dma_map_addr(arg, segs, nseg, error)
373f41ac2beSBill Paul 	void *arg;
374f41ac2beSBill Paul 	bus_dma_segment_t *segs;
375f41ac2beSBill Paul 	int nseg;
376f41ac2beSBill Paul 	int error;
377f41ac2beSBill Paul {
378f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
379f41ac2beSBill Paul 
380f41ac2beSBill Paul 	if (error)
381f41ac2beSBill Paul 		return;
382f41ac2beSBill Paul 
383f41ac2beSBill Paul 	ctx = arg;
384f41ac2beSBill Paul 
385f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
386f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
387f41ac2beSBill Paul 		return;
388f41ac2beSBill Paul 	}
389f41ac2beSBill Paul 
390f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
391f41ac2beSBill Paul 
392f41ac2beSBill Paul 	return;
393f41ac2beSBill Paul }
394f41ac2beSBill Paul 
3951b4a3b2fSPeter Wemm #ifdef notdef
39695d67482SBill Paul static u_int8_t
39795d67482SBill Paul bge_vpd_readbyte(sc, addr)
39895d67482SBill Paul 	struct bge_softc *sc;
39995d67482SBill Paul 	int addr;
40095d67482SBill Paul {
40195d67482SBill Paul 	int i;
40295d67482SBill Paul 	device_t dev;
40395d67482SBill Paul 	u_int32_t val;
40495d67482SBill Paul 
40595d67482SBill Paul 	dev = sc->bge_dev;
40695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
40795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
40895d67482SBill Paul 		DELAY(10);
40995d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
41095d67482SBill Paul 			break;
41195d67482SBill Paul 	}
41295d67482SBill Paul 
41395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
414fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "VPD read timed out\n");
41595d67482SBill Paul 		return(0);
41695d67482SBill Paul 	}
41795d67482SBill Paul 
41895d67482SBill Paul 	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
41995d67482SBill Paul 
42095d67482SBill Paul 	return((val >> ((addr % 4) * 8)) & 0xFF);
42195d67482SBill Paul }
42295d67482SBill Paul 
42395d67482SBill Paul static void
42495d67482SBill Paul bge_vpd_read_res(sc, res, addr)
42595d67482SBill Paul 	struct bge_softc *sc;
42695d67482SBill Paul 	struct vpd_res *res;
42795d67482SBill Paul 	int addr;
42895d67482SBill Paul {
42995d67482SBill Paul 	int i;
43095d67482SBill Paul 	u_int8_t *ptr;
43195d67482SBill Paul 
43295d67482SBill Paul 	ptr = (u_int8_t *)res;
43395d67482SBill Paul 	for (i = 0; i < sizeof(struct vpd_res); i++)
43495d67482SBill Paul 		ptr[i] = bge_vpd_readbyte(sc, i + addr);
43595d67482SBill Paul 
43695d67482SBill Paul 	return;
43795d67482SBill Paul }
43895d67482SBill Paul 
43995d67482SBill Paul static void
44095d67482SBill Paul bge_vpd_read(sc)
44195d67482SBill Paul 	struct bge_softc *sc;
44295d67482SBill Paul {
44395d67482SBill Paul 	int pos = 0, i;
44495d67482SBill Paul 	struct vpd_res res;
44595d67482SBill Paul 
44695d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
44795d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
44895d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
44995d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
45095d67482SBill Paul 	sc->bge_vpd_prodname = NULL;
45195d67482SBill Paul 	sc->bge_vpd_readonly = NULL;
45295d67482SBill Paul 
45395d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
45495d67482SBill Paul 
45595d67482SBill Paul 	if (res.vr_id != VPD_RES_ID) {
456fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
457fe806fdaSPyun YongHyeon 		    "bad VPD resource id: expected %x got %x\n", VPD_RES_ID,
458fe806fdaSPyun YongHyeon 		    res.vr_id);
45995d67482SBill Paul 		return;
46095d67482SBill Paul 	}
46195d67482SBill Paul 
46295d67482SBill Paul 	pos += sizeof(res);
46395d67482SBill Paul 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
46495d67482SBill Paul 	for (i = 0; i < res.vr_len; i++)
46595d67482SBill Paul 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
46695d67482SBill Paul 	sc->bge_vpd_prodname[i] = '\0';
46795d67482SBill Paul 	pos += i;
46895d67482SBill Paul 
46995d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
47095d67482SBill Paul 
47195d67482SBill Paul 	if (res.vr_id != VPD_RES_READ) {
472fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
473fe806fdaSPyun YongHyeon 		    "bad VPD resource id: expected %x got %x\n", VPD_RES_READ,
474fe806fdaSPyun YongHyeon 		    res.vr_id);
47595d67482SBill Paul 		return;
47695d67482SBill Paul 	}
47795d67482SBill Paul 
47895d67482SBill Paul 	pos += sizeof(res);
47995d67482SBill Paul 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
48095d67482SBill Paul 	for (i = 0; i < res.vr_len + 1; i++)
48195d67482SBill Paul 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
48295d67482SBill Paul 
48395d67482SBill Paul 	return;
48495d67482SBill Paul }
4851b4a3b2fSPeter Wemm #endif
48695d67482SBill Paul 
48795d67482SBill Paul /*
48895d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
48995d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
49095d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
49195d67482SBill Paul  * access method.
49295d67482SBill Paul  */
49395d67482SBill Paul static u_int8_t
49495d67482SBill Paul bge_eeprom_getbyte(sc, addr, dest)
49595d67482SBill Paul 	struct bge_softc *sc;
49695d67482SBill Paul 	int addr;
49795d67482SBill Paul 	u_int8_t *dest;
49895d67482SBill Paul {
49995d67482SBill Paul 	int i;
50095d67482SBill Paul 	u_int32_t byte = 0;
50195d67482SBill Paul 
50295d67482SBill Paul 	/*
50395d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
50495d67482SBill Paul 	 * having to use the bitbang method.
50595d67482SBill Paul 	 */
50695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
50795d67482SBill Paul 
50895d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
50995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
51095d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
51195d67482SBill Paul 	DELAY(20);
51295d67482SBill Paul 
51395d67482SBill Paul 	/* Issue the read EEPROM command. */
51495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
51595d67482SBill Paul 
51695d67482SBill Paul 	/* Wait for completion */
51795d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
51895d67482SBill Paul 		DELAY(10);
51995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
52095d67482SBill Paul 			break;
52195d67482SBill Paul 	}
52295d67482SBill Paul 
52395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
524fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
525f6789fbaSPyun YongHyeon 		return(1);
52695d67482SBill Paul 	}
52795d67482SBill Paul 
52895d67482SBill Paul 	/* Get result. */
52995d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
53095d67482SBill Paul 
53195d67482SBill Paul 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
53295d67482SBill Paul 
53395d67482SBill Paul 	return(0);
53495d67482SBill Paul }
53595d67482SBill Paul 
53695d67482SBill Paul /*
53795d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
53895d67482SBill Paul  */
53995d67482SBill Paul static int
54095d67482SBill Paul bge_read_eeprom(sc, dest, off, cnt)
54195d67482SBill Paul 	struct bge_softc *sc;
54295d67482SBill Paul 	caddr_t dest;
54395d67482SBill Paul 	int off;
54495d67482SBill Paul 	int cnt;
54595d67482SBill Paul {
54695d67482SBill Paul 	int err = 0, i;
54795d67482SBill Paul 	u_int8_t byte = 0;
54895d67482SBill Paul 
54995d67482SBill Paul 	for (i = 0; i < cnt; i++) {
55095d67482SBill Paul 		err = bge_eeprom_getbyte(sc, off + i, &byte);
55195d67482SBill Paul 		if (err)
55295d67482SBill Paul 			break;
55395d67482SBill Paul 		*(dest + i) = byte;
55495d67482SBill Paul 	}
55595d67482SBill Paul 
55695d67482SBill Paul 	return(err ? 1 : 0);
55795d67482SBill Paul }
55895d67482SBill Paul 
55995d67482SBill Paul static int
56095d67482SBill Paul bge_miibus_readreg(dev, phy, reg)
56195d67482SBill Paul 	device_t dev;
56295d67482SBill Paul 	int phy, reg;
56395d67482SBill Paul {
56495d67482SBill Paul 	struct bge_softc *sc;
56537ceeb4dSPaul Saab 	u_int32_t val, autopoll;
56695d67482SBill Paul 	int i;
56795d67482SBill Paul 
56895d67482SBill Paul 	sc = device_get_softc(dev);
56995d67482SBill Paul 
5700434d1b8SBill Paul 	/*
5710434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5720434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5730434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5740434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5750434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5760434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5770434d1b8SBill Paul 	 * special-cased.
5780434d1b8SBill Paul 	 */
579b1265c1aSJohn Polstra 	if (phy != 1)
58098b28ee5SBill Paul 		return(0);
58198b28ee5SBill Paul 
58237ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
58337ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
58437ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
58537ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
58637ceeb4dSPaul Saab 		DELAY(40);
58737ceeb4dSPaul Saab 	}
58837ceeb4dSPaul Saab 
58995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
59095d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
59195d67482SBill Paul 
59295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
59395d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
59495d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
59595d67482SBill Paul 			break;
59695d67482SBill Paul 	}
59795d67482SBill Paul 
59895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
599fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
60037ceeb4dSPaul Saab 		val = 0;
60137ceeb4dSPaul Saab 		goto done;
60295d67482SBill Paul 	}
60395d67482SBill Paul 
60495d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
60595d67482SBill Paul 
60637ceeb4dSPaul Saab done:
60737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
60837ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
60937ceeb4dSPaul Saab 		DELAY(40);
61037ceeb4dSPaul Saab 	}
61137ceeb4dSPaul Saab 
61295d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
61395d67482SBill Paul 		return(0);
61495d67482SBill Paul 
61595d67482SBill Paul 	return(val & 0xFFFF);
61695d67482SBill Paul }
61795d67482SBill Paul 
61895d67482SBill Paul static int
61995d67482SBill Paul bge_miibus_writereg(dev, phy, reg, val)
62095d67482SBill Paul 	device_t dev;
62195d67482SBill Paul 	int phy, reg, val;
62295d67482SBill Paul {
62395d67482SBill Paul 	struct bge_softc *sc;
62437ceeb4dSPaul Saab 	u_int32_t autopoll;
62595d67482SBill Paul 	int i;
62695d67482SBill Paul 
62795d67482SBill Paul 	sc = device_get_softc(dev);
62895d67482SBill Paul 
62937ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
63037ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
63137ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
63237ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
63337ceeb4dSPaul Saab 		DELAY(40);
63437ceeb4dSPaul Saab 	}
63537ceeb4dSPaul Saab 
63695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
63795d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
63895d67482SBill Paul 
63995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
64095d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
64195d67482SBill Paul 			break;
64295d67482SBill Paul 	}
64395d67482SBill Paul 
64437ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
64537ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
64637ceeb4dSPaul Saab 		DELAY(40);
64737ceeb4dSPaul Saab 	}
64837ceeb4dSPaul Saab 
64995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
650fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
65195d67482SBill Paul 		return(0);
65295d67482SBill Paul 	}
65395d67482SBill Paul 
65495d67482SBill Paul 	return(0);
65595d67482SBill Paul }
65695d67482SBill Paul 
65795d67482SBill Paul static void
65895d67482SBill Paul bge_miibus_statchg(dev)
65995d67482SBill Paul 	device_t dev;
66095d67482SBill Paul {
66195d67482SBill Paul 	struct bge_softc *sc;
66295d67482SBill Paul 	struct mii_data *mii;
66395d67482SBill Paul 
66495d67482SBill Paul 	sc = device_get_softc(dev);
66595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
66695d67482SBill Paul 
66795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
668b418ad5cSPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
66995d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
67095d67482SBill Paul 	} else {
67195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
67295d67482SBill Paul 	}
67395d67482SBill Paul 
67495d67482SBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
67595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
67695d67482SBill Paul 	} else {
67795d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
67895d67482SBill Paul 	}
67995d67482SBill Paul 
68095d67482SBill Paul 	return;
68195d67482SBill Paul }
68295d67482SBill Paul 
68395d67482SBill Paul /*
68495d67482SBill Paul  * Intialize a standard receive ring descriptor.
68595d67482SBill Paul  */
68695d67482SBill Paul static int
68795d67482SBill Paul bge_newbuf_std(sc, i, m)
68895d67482SBill Paul 	struct bge_softc	*sc;
68995d67482SBill Paul 	int			i;
69095d67482SBill Paul 	struct mbuf		*m;
69195d67482SBill Paul {
69295d67482SBill Paul 	struct mbuf		*m_new = NULL;
69395d67482SBill Paul 	struct bge_rx_bd	*r;
694f41ac2beSBill Paul 	struct bge_dmamap_arg	ctx;
695f41ac2beSBill Paul 	int			error;
69695d67482SBill Paul 
69795d67482SBill Paul 	if (m == NULL) {
698a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
69995d67482SBill Paul 		if (m_new == NULL) {
70095d67482SBill Paul 			return(ENOBUFS);
70195d67482SBill Paul 		}
70295d67482SBill Paul 
703a163d034SWarner Losh 		MCLGET(m_new, M_DONTWAIT);
70495d67482SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
70595d67482SBill Paul 			m_freem(m_new);
70695d67482SBill Paul 			return(ENOBUFS);
70795d67482SBill Paul 		}
70895d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70995d67482SBill Paul 	} else {
71095d67482SBill Paul 		m_new = m;
71195d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
71295d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
71395d67482SBill Paul 	}
71495d67482SBill Paul 
715e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
71695d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
71795d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
718f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
719f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
720f41ac2beSBill Paul 	ctx.sc = sc;
721f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
722f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
723f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
724f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
725f7cea149SGleb Smirnoff 		if (m == NULL) {
726f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
727f41ac2beSBill Paul 			m_freem(m_new);
728f7cea149SGleb Smirnoff 		}
729f41ac2beSBill Paul 		return(ENOMEM);
730f41ac2beSBill Paul 	}
731e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
732e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
733e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
734e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
735e907febfSPyun YongHyeon 	r->bge_idx = i;
736f41ac2beSBill Paul 
737f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
738f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
739f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
74095d67482SBill Paul 
74195d67482SBill Paul 	return(0);
74295d67482SBill Paul }
74395d67482SBill Paul 
74495d67482SBill Paul /*
74595d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
74695d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
74795d67482SBill Paul  */
74895d67482SBill Paul static int
74995d67482SBill Paul bge_newbuf_jumbo(sc, i, m)
75095d67482SBill Paul 	struct bge_softc *sc;
75195d67482SBill Paul 	int i;
75295d67482SBill Paul 	struct mbuf *m;
75395d67482SBill Paul {
7541be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
7551be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
75695d67482SBill Paul 	struct mbuf *m_new = NULL;
7571be6acb7SGleb Smirnoff 	int nsegs;
758f41ac2beSBill Paul 	int error;
75995d67482SBill Paul 
76095d67482SBill Paul 	if (m == NULL) {
761a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
7621be6acb7SGleb Smirnoff 		if (m_new == NULL)
76395d67482SBill Paul 			return(ENOBUFS);
76495d67482SBill Paul 
7651be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7661be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
76795d67482SBill Paul 			m_freem(m_new);
76895d67482SBill Paul 			return(ENOBUFS);
76995d67482SBill Paul 		}
7701be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
77195d67482SBill Paul 	} else {
77295d67482SBill Paul 		m_new = m;
7731be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
77495d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
77595d67482SBill Paul 	}
77695d67482SBill Paul 
777e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
77895d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7791be6acb7SGleb Smirnoff 
7801be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7811be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7821be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7831be6acb7SGleb Smirnoff 	if (error) {
7841be6acb7SGleb Smirnoff 		if (m == NULL)
785f41ac2beSBill Paul 			m_freem(m_new);
7861be6acb7SGleb Smirnoff 		return(error);
787f7cea149SGleb Smirnoff 	}
7881be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
7891be6acb7SGleb Smirnoff 
7901be6acb7SGleb Smirnoff 	/*
7911be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
7921be6acb7SGleb Smirnoff 	 */
7931be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
7944e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING|BGE_RXBDFLAG_END;
7954e7ba1abSGleb Smirnoff 	r->bge_idx = i;
7964e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
7974e7ba1abSGleb Smirnoff 	switch (nsegs) {
7984e7ba1abSGleb Smirnoff 	case 4:
7994e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
8004e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
8014e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
8024e7ba1abSGleb Smirnoff 	case 3:
803e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
804e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
805e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
8064e7ba1abSGleb Smirnoff 	case 2:
8074e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
8084e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
8094e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
8104e7ba1abSGleb Smirnoff 	case 1:
8114e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
8124e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
8134e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
8144e7ba1abSGleb Smirnoff 		break;
8154e7ba1abSGleb Smirnoff 	default:
8164e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
8174e7ba1abSGleb Smirnoff 	}
818f41ac2beSBill Paul 
819f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
820f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
821f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
82295d67482SBill Paul 
82395d67482SBill Paul 	return (0);
82495d67482SBill Paul }
82595d67482SBill Paul 
82695d67482SBill Paul /*
82795d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
82895d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
82995d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
83095d67482SBill Paul  * the NIC.
83195d67482SBill Paul  */
83295d67482SBill Paul static int
83395d67482SBill Paul bge_init_rx_ring_std(sc)
83495d67482SBill Paul 	struct bge_softc *sc;
83595d67482SBill Paul {
83695d67482SBill Paul 	int i;
83795d67482SBill Paul 
83895d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
83995d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
84095d67482SBill Paul 			return(ENOBUFS);
84195d67482SBill Paul 	};
84295d67482SBill Paul 
843f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
844f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
845f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
846f41ac2beSBill Paul 
84795d67482SBill Paul 	sc->bge_std = i - 1;
84895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
84995d67482SBill Paul 
85095d67482SBill Paul 	return(0);
85195d67482SBill Paul }
85295d67482SBill Paul 
85395d67482SBill Paul static void
85495d67482SBill Paul bge_free_rx_ring_std(sc)
85595d67482SBill Paul 	struct bge_softc *sc;
85695d67482SBill Paul {
85795d67482SBill Paul 	int i;
85895d67482SBill Paul 
85995d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
86095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
861e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
862e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
863e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
864f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
865f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
866e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
867e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
86895d67482SBill Paul 		}
869f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
87095d67482SBill Paul 		    sizeof(struct bge_rx_bd));
87195d67482SBill Paul 	}
87295d67482SBill Paul 
87395d67482SBill Paul 	return;
87495d67482SBill Paul }
87595d67482SBill Paul 
87695d67482SBill Paul static int
87795d67482SBill Paul bge_init_rx_ring_jumbo(sc)
87895d67482SBill Paul 	struct bge_softc *sc;
87995d67482SBill Paul {
88095d67482SBill Paul 	struct bge_rcb *rcb;
8811be6acb7SGleb Smirnoff 	int i;
88295d67482SBill Paul 
88395d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
88495d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
88595d67482SBill Paul 			return(ENOBUFS);
88695d67482SBill Paul 	};
88795d67482SBill Paul 
888f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
889f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
890f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
891f41ac2beSBill Paul 
89295d67482SBill Paul 	sc->bge_jumbo = i - 1;
89395d67482SBill Paul 
894f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8951be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8961be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
89767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
89895d67482SBill Paul 
89995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
90095d67482SBill Paul 
90195d67482SBill Paul 	return(0);
90295d67482SBill Paul }
90395d67482SBill Paul 
90495d67482SBill Paul static void
90595d67482SBill Paul bge_free_rx_ring_jumbo(sc)
90695d67482SBill Paul 	struct bge_softc *sc;
90795d67482SBill Paul {
90895d67482SBill Paul 	int i;
90995d67482SBill Paul 
91095d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
91195d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
912e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
913e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
914e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
915f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
916f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
917e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
918e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
91995d67482SBill Paul 		}
920f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
9211be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
92295d67482SBill Paul 	}
92395d67482SBill Paul 
92495d67482SBill Paul 	return;
92595d67482SBill Paul }
92695d67482SBill Paul 
92795d67482SBill Paul static void
92895d67482SBill Paul bge_free_tx_ring(sc)
92995d67482SBill Paul 	struct bge_softc *sc;
93095d67482SBill Paul {
93195d67482SBill Paul 	int i;
93295d67482SBill Paul 
933f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
93495d67482SBill Paul 		return;
93595d67482SBill Paul 
93695d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
93795d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
938e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
939e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
940e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
941f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
942f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
943e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
944e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
94595d67482SBill Paul 		}
946f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
94795d67482SBill Paul 		    sizeof(struct bge_tx_bd));
94895d67482SBill Paul 	}
94995d67482SBill Paul 
95095d67482SBill Paul 	return;
95195d67482SBill Paul }
95295d67482SBill Paul 
95395d67482SBill Paul static int
95495d67482SBill Paul bge_init_tx_ring(sc)
95595d67482SBill Paul 	struct bge_softc *sc;
95695d67482SBill Paul {
95795d67482SBill Paul 	sc->bge_txcnt = 0;
95895d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9593927098fSPaul Saab 
96014bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
96114bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
96214bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
96314bbd30fSGleb Smirnoff 
9643927098fSPaul Saab 	/* 5700 b2 errata */
965e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
96614bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
9673927098fSPaul Saab 
96814bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
9693927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
9703927098fSPaul Saab 	/* 5700 b2 errata */
971e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
97295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
97395d67482SBill Paul 
97495d67482SBill Paul 	return(0);
97595d67482SBill Paul }
97695d67482SBill Paul 
97795d67482SBill Paul static void
97895d67482SBill Paul bge_setmulti(sc)
97995d67482SBill Paul 	struct bge_softc *sc;
98095d67482SBill Paul {
98195d67482SBill Paul 	struct ifnet *ifp;
98295d67482SBill Paul 	struct ifmultiaddr *ifma;
98395d67482SBill Paul 	u_int32_t hashes[4] = { 0, 0, 0, 0 };
98495d67482SBill Paul 	int h, i;
98595d67482SBill Paul 
9860f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9870f9bd73bSSam Leffler 
988fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
98995d67482SBill Paul 
99095d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
99195d67482SBill Paul 		for (i = 0; i < 4; i++)
99295d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
99395d67482SBill Paul 		return;
99495d67482SBill Paul 	}
99595d67482SBill Paul 
99695d67482SBill Paul 	/* First, zot all the existing filters. */
99795d67482SBill Paul 	for (i = 0; i < 4; i++)
99895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
99995d67482SBill Paul 
100095d67482SBill Paul 	/* Now program new ones. */
100113b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
100295d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
100395d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
100495d67482SBill Paul 			continue;
10050e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
10060e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
100795d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
100895d67482SBill Paul 	}
100913b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
101095d67482SBill Paul 
101195d67482SBill Paul 	for (i = 0; i < 4; i++)
101295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
101395d67482SBill Paul 
101495d67482SBill Paul 	return;
101595d67482SBill Paul }
101695d67482SBill Paul 
101795d67482SBill Paul /*
101895d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
101995d67482SBill Paul  * self-test results.
102095d67482SBill Paul  */
102195d67482SBill Paul static int
102295d67482SBill Paul bge_chipinit(sc)
102395d67482SBill Paul 	struct bge_softc *sc;
102495d67482SBill Paul {
102595d67482SBill Paul 	int			i;
10265cba12d3SPaul Saab 	u_int32_t		dma_rw_ctl;
102795d67482SBill Paul 
1028e907febfSPyun YongHyeon 	/* Set endian type before we access any non-PCI registers. */
1029e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
103095d67482SBill Paul 
103195d67482SBill Paul 	/*
103295d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
103395d67482SBill Paul 	 * self-tests passed.
103495d67482SBill Paul 	 */
103595d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1036fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
103795d67482SBill Paul 		return(ENODEV);
103895d67482SBill Paul 	}
103995d67482SBill Paul 
104095d67482SBill Paul 	/* Clear the MAC control register */
104195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
104295d67482SBill Paul 
104395d67482SBill Paul 	/*
104495d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
104595d67482SBill Paul 	 * internal memory.
104695d67482SBill Paul 	 */
104795d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
104895d67482SBill Paul 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
104995d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
105095d67482SBill Paul 
105195d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
105295d67482SBill Paul 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
105395d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
105495d67482SBill Paul 
105595d67482SBill Paul 	/* Set up the PCI DMA control register. */
1056e53d81eeSPaul Saab 	if (sc->bge_pcie) {
1057e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1058e53d81eeSPaul Saab 		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1059e53d81eeSPaul Saab 		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1060e53d81eeSPaul Saab 	} else if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
10618287860eSJohn Polstra 	    BGE_PCISTATE_PCI_BUSMODE) {
10628287860eSJohn Polstra 		/* Conventional PCI bus */
10635cba12d3SPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10645cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10655cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10665cba12d3SPaul Saab 		    (0x0F);
10678287860eSJohn Polstra 	} else {
10688287860eSJohn Polstra 		/* PCI-X bus */
10695cba12d3SPaul Saab 		/*
10705cba12d3SPaul Saab 		 * The 5704 uses a different encoding of read/write
10715cba12d3SPaul Saab 		 * watermarks.
10725cba12d3SPaul Saab 		 */
1073e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
10745cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10755cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10765cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
10775cba12d3SPaul Saab 		else
10785cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10795cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10805cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10815cba12d3SPaul Saab 			    (0x0F);
10825cba12d3SPaul Saab 
10835cba12d3SPaul Saab 		/*
10845cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
10855cba12d3SPaul Saab 		 * for hardware bugs.
10865cba12d3SPaul Saab 		 */
1087e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1088e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
10895cba12d3SPaul Saab 			u_int32_t tmp;
10905cba12d3SPaul Saab 
10915cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
10925cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
10935cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
10948287860eSJohn Polstra 		}
10955cba12d3SPaul Saab 	}
10965cba12d3SPaul Saab 
1097e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
10980434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
1099e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1100e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
11015cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
11025cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
110395d67482SBill Paul 
110495d67482SBill Paul 	/*
110595d67482SBill Paul 	 * Set up general mode register.
110695d67482SBill Paul 	 */
1107e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
110895d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1109e446dc86SPaul Saab 	    BGE_MODECTL_TX_NO_PHDR_CSUM|BGE_MODECTL_RX_NO_PHDR_CSUM);
111095d67482SBill Paul 
111195d67482SBill Paul 	/*
1112ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1113ea13bdd5SJohn Polstra 	 * properly by these devices.
111495d67482SBill Paul 	 */
1115ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
111695d67482SBill Paul 
111795d67482SBill Paul #ifdef __brokenalpha__
111895d67482SBill Paul 	/*
111995d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
112095d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
112195d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
112295d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
112395d67482SBill Paul 	 */
112462f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
112562f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
112695d67482SBill Paul #endif
112795d67482SBill Paul 
112895d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
112995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
113095d67482SBill Paul 
113195d67482SBill Paul 	return(0);
113295d67482SBill Paul }
113395d67482SBill Paul 
113495d67482SBill Paul static int
113595d67482SBill Paul bge_blockinit(sc)
113695d67482SBill Paul 	struct bge_softc *sc;
113795d67482SBill Paul {
113895d67482SBill Paul 	struct bge_rcb *rcb;
1139e907febfSPyun YongHyeon 	bus_size_t vrcb;
1140e907febfSPyun YongHyeon 	bge_hostaddr taddr;
114195d67482SBill Paul 	int i;
114295d67482SBill Paul 
114395d67482SBill Paul 	/*
114495d67482SBill Paul 	 * Initialize the memory window pointer register so that
114595d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
114695d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
114795d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
114895d67482SBill Paul 	 */
114995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
115095d67482SBill Paul 
1151822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1152822f63fcSBill Paul 
11535dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1154e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
115595d67482SBill Paul 		/* Configure mbuf memory pool */
115695d67482SBill Paul 		if (sc->bge_extram) {
11570434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11580434d1b8SBill Paul 			    BGE_EXT_SSRAM);
1159822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1160822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1161822f63fcSBill Paul 			else
116295d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
116395d67482SBill Paul 		} else {
11640434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11650434d1b8SBill Paul 			    BGE_BUFFPOOL_1);
1166822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1167822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1168822f63fcSBill Paul 			else
116995d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
117095d67482SBill Paul 		}
117195d67482SBill Paul 
117295d67482SBill Paul 		/* Configure DMA resource pool */
11730434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
11740434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
117595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
11760434d1b8SBill Paul 	}
117795d67482SBill Paul 
117895d67482SBill Paul 	/* Configure mbuf pool watermarks */
1179e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1180e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750) {
11810434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11820434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
11830434d1b8SBill Paul 	} else {
1184fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1185fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
11860434d1b8SBill Paul 	}
1187fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
118895d67482SBill Paul 
118995d67482SBill Paul 	/* Configure DMA resource watermarks */
119095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
119195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
119295d67482SBill Paul 
119395d67482SBill Paul 	/* Enable buffer manager */
11945dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1195e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
119695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
119795d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
119895d67482SBill Paul 
119995d67482SBill Paul 		/* Poll for buffer manager start indication */
120095d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
120195d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
120295d67482SBill Paul 				break;
120395d67482SBill Paul 			DELAY(10);
120495d67482SBill Paul 		}
120595d67482SBill Paul 
120695d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1207fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1208fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
120995d67482SBill Paul 			return(ENXIO);
121095d67482SBill Paul 		}
12110434d1b8SBill Paul 	}
121295d67482SBill Paul 
121395d67482SBill Paul 	/* Enable flow-through queues */
121495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
121595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
121695d67482SBill Paul 
121795d67482SBill Paul 	/* Wait until queue initialization is complete */
121895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
121995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
122095d67482SBill Paul 			break;
122195d67482SBill Paul 		DELAY(10);
122295d67482SBill Paul 	}
122395d67482SBill Paul 
122495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1225fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
122695d67482SBill Paul 		return(ENXIO);
122795d67482SBill Paul 	}
122895d67482SBill Paul 
122995d67482SBill Paul 	/* Initialize the standard RX ring control block */
1230f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1231f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1232f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1233f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1234f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1235f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1236f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
1237e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1238e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
12390434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
12400434d1b8SBill Paul 	else
12410434d1b8SBill Paul 		rcb->bge_maxlen_flags =
12420434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
124395d67482SBill Paul 	if (sc->bge_extram)
124495d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
124595d67482SBill Paul 	else
124695d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
124767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
124867111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1249f41ac2beSBill Paul 
125067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
125167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
125295d67482SBill Paul 
125395d67482SBill Paul 	/*
125495d67482SBill Paul 	 * Initialize the jumbo RX ring control block
125595d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
125695d67482SBill Paul 	 * field until we're actually ready to start
125795d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
125895d67482SBill Paul 	 * high enough to require it).
125995d67482SBill Paul 	 */
12605dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1261e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1262f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1263f41ac2beSBill Paul 
1264f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1265f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1266f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1267f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1268f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1269f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1270f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
12711be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
12721be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD|BGE_RCB_FLAG_RING_DISABLED);
127395d67482SBill Paul 		if (sc->bge_extram)
127495d67482SBill Paul 			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
127595d67482SBill Paul 		else
127695d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
127767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
127867111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
127967111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
128067111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1281f41ac2beSBill Paul 
12820434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
12830434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
128467111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
128595d67482SBill Paul 
128695d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1287f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
128867111612SJohn Polstra 		rcb->bge_maxlen_flags =
128967111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
12900434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
12910434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
12920434d1b8SBill Paul 	}
129395d67482SBill Paul 
129495d67482SBill Paul 	/*
129595d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
129695d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
129795d67482SBill Paul 	 * each ring.
129895d67482SBill Paul 	 */
129995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
130095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
130195d67482SBill Paul 
130295d67482SBill Paul 	/*
130395d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
130495d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
130595d67482SBill Paul 	 * These are located in NIC memory.
130695d67482SBill Paul 	 */
1307e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
130895d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1309e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1310e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1311e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1312e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
131395d67482SBill Paul 	}
131495d67482SBill Paul 
131595d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1316e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1317e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1318e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1319e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1320e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1321e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
13225dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1323e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1324e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1325e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
132695d67482SBill Paul 
132795d67482SBill Paul 	/* Disable all unused RX return rings */
1328e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
132995d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1330e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1331e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1332e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
13330434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1334e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1335e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
133695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
133795d67482SBill Paul 		    (i * (sizeof(u_int64_t))), 0);
1338e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
133995d67482SBill Paul 	}
134095d67482SBill Paul 
134195d67482SBill Paul 	/* Initialize RX ring indexes */
134295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
134395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
134495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
134595d67482SBill Paul 
134695d67482SBill Paul 	/*
134795d67482SBill Paul 	 * Set up RX return ring 0
134895d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
134995d67482SBill Paul 	 * The return rings live entirely within the host, so the
135095d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
135195d67482SBill Paul 	 */
1352e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1353e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1354e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1355e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1356e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1357e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1358e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
135995d67482SBill Paul 
136095d67482SBill Paul 	/* Set random backoff seed for TX */
136195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
13624a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
13634a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
13644a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
136595d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
136695d67482SBill Paul 
136795d67482SBill Paul 	/* Set inter-packet gap */
136895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
136995d67482SBill Paul 
137095d67482SBill Paul 	/*
137195d67482SBill Paul 	 * Specify which ring to use for packets that don't match
137295d67482SBill Paul 	 * any RX rules.
137395d67482SBill Paul 	 */
137495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
137595d67482SBill Paul 
137695d67482SBill Paul 	/*
137795d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
137895d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
137995d67482SBill Paul 	 */
138095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
138195d67482SBill Paul 
138295d67482SBill Paul 	/* Inialize RX list placement stats mask. */
138395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
138495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
138595d67482SBill Paul 
138695d67482SBill Paul 	/* Disable host coalescing until we get it set up */
138795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
138895d67482SBill Paul 
138995d67482SBill Paul 	/* Poll to make sure it's shut down. */
139095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
139195d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
139295d67482SBill Paul 			break;
139395d67482SBill Paul 		DELAY(10);
139495d67482SBill Paul 	}
139595d67482SBill Paul 
139695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1397fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1398fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
139995d67482SBill Paul 		return(ENXIO);
140095d67482SBill Paul 	}
140195d67482SBill Paul 
140295d67482SBill Paul 	/* Set up host coalescing defaults */
140395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
140495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
140595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
140695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
14075dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1408e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
140995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
141095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
14110434d1b8SBill Paul 	}
141295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
141395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
141495d67482SBill Paul 
141595d67482SBill Paul 	/* Set up address of statistics block */
14165dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1417e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1418f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1419f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
142095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1421f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
14220434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
142395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
14240434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
14250434d1b8SBill Paul 	}
14260434d1b8SBill Paul 
14270434d1b8SBill Paul 	/* Set up address of status block */
1428f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1429f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
143095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1431f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1432f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1433f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
143495d67482SBill Paul 
143595d67482SBill Paul 	/* Turn on host coalescing state machine */
143695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
143795d67482SBill Paul 
143895d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
143995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
144095d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
144195d67482SBill Paul 
144295d67482SBill Paul 	/* Turn on RX list placement state machine */
144395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
144495d67482SBill Paul 
144595d67482SBill Paul 	/* Turn on RX list selector state machine. */
14465dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1447e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
144895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
144995d67482SBill Paul 
145095d67482SBill Paul 	/* Turn on DMA, clear stats */
145195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
145295d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
145395d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
145495d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
145595d67482SBill Paul 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
145695d67482SBill Paul 
145795d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
145895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
145995d67482SBill Paul 
146095d67482SBill Paul #ifdef notdef
146195d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
146295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
146395d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
146495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
146595d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
146695d67482SBill Paul #endif
146795d67482SBill Paul 
146895d67482SBill Paul 	/* Turn on DMA completion state machine */
14695dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1470e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
147195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
147295d67482SBill Paul 
147395d67482SBill Paul 	/* Turn on write DMA state machine */
147495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
147595d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
147695d67482SBill Paul 
147795d67482SBill Paul 	/* Turn on read DMA state machine */
147895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
147995d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
148095d67482SBill Paul 
148195d67482SBill Paul 	/* Turn on RX data completion state machine */
148295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
148395d67482SBill Paul 
148495d67482SBill Paul 	/* Turn on RX BD initiator state machine */
148595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
148695d67482SBill Paul 
148795d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
148895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
148995d67482SBill Paul 
149095d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
14915dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1492e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
149395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
149495d67482SBill Paul 
149595d67482SBill Paul 	/* Turn on send BD completion state machine */
149695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
149795d67482SBill Paul 
149895d67482SBill Paul 	/* Turn on send data completion state machine */
149995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
150095d67482SBill Paul 
150195d67482SBill Paul 	/* Turn on send data initiator state machine */
150295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
150395d67482SBill Paul 
150495d67482SBill Paul 	/* Turn on send BD initiator state machine */
150595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
150695d67482SBill Paul 
150795d67482SBill Paul 	/* Turn on send BD selector state machine */
150895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
150995d67482SBill Paul 
151095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
151195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
151295d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
151395d67482SBill Paul 
151495d67482SBill Paul 	/* ack/clear link change events */
151595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15160434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15170434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1518f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
151995d67482SBill Paul 
152095d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
152195d67482SBill Paul 	if (sc->bge_tbi) {
152295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1523a1d52896SBill Paul 	} else {
152495d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
15251f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
15261f313773SOleg Bulyzhin 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B1)
1527a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1528a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1529a1d52896SBill Paul 	}
153095d67482SBill Paul 
15311f313773SOleg Bulyzhin 	/*
15321f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
15331f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
15341f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
15351f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
15361f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
15371f313773SOleg Bulyzhin 	 */
15381f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15391f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15401f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
15411f313773SOleg Bulyzhin 
154295d67482SBill Paul 	/* Enable link state change attentions. */
154395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
154495d67482SBill Paul 
154595d67482SBill Paul 	return(0);
154695d67482SBill Paul }
154795d67482SBill Paul 
154895d67482SBill Paul /*
154995d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
155095d67482SBill Paul  * against our list and return its name if we find a match. Note
155195d67482SBill Paul  * that since the Broadcom controller contains VPD support, we
155295d67482SBill Paul  * can get the device name string from the controller itself instead
155395d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
155495d67482SBill Paul  * we'll always announce the right product name.
155595d67482SBill Paul  */
155695d67482SBill Paul static int
155795d67482SBill Paul bge_probe(dev)
155895d67482SBill Paul 	device_t dev;
155995d67482SBill Paul {
156095d67482SBill Paul 	struct bge_type *t;
156195d67482SBill Paul 	struct bge_softc *sc;
1562029e2ee3SJohn Polstra 	char *descbuf;
156395d67482SBill Paul 
156495d67482SBill Paul 	t = bge_devs;
156595d67482SBill Paul 
156695d67482SBill Paul 	sc = device_get_softc(dev);
156795d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
156895d67482SBill Paul 	sc->bge_dev = dev;
156995d67482SBill Paul 
157095d67482SBill Paul 	while(t->bge_name != NULL) {
157195d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
157295d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
157395d67482SBill Paul #ifdef notdef
157495d67482SBill Paul 			bge_vpd_read(sc);
157595d67482SBill Paul 			device_set_desc(dev, sc->bge_vpd_prodname);
157695d67482SBill Paul #endif
1577029e2ee3SJohn Polstra 			descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
1578029e2ee3SJohn Polstra 			if (descbuf == NULL)
1579029e2ee3SJohn Polstra 				return(ENOMEM);
1580029e2ee3SJohn Polstra 			snprintf(descbuf, BGE_DEVDESC_MAX,
1581029e2ee3SJohn Polstra 			    "%s, ASIC rev. %#04x", t->bge_name,
1582029e2ee3SJohn Polstra 			    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16);
1583029e2ee3SJohn Polstra 			device_set_desc_copy(dev, descbuf);
15846d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
15856d2a9bd6SDoug Ambrisko 				sc->bge_no_3_led = 1;
1586029e2ee3SJohn Polstra 			free(descbuf, M_TEMP);
158795d67482SBill Paul 			return(0);
158895d67482SBill Paul 		}
158995d67482SBill Paul 		t++;
159095d67482SBill Paul 	}
159195d67482SBill Paul 
159295d67482SBill Paul 	return(ENXIO);
159395d67482SBill Paul }
159495d67482SBill Paul 
1595f41ac2beSBill Paul static void
1596f41ac2beSBill Paul bge_dma_free(sc)
1597f41ac2beSBill Paul 	struct bge_softc *sc;
1598f41ac2beSBill Paul {
1599f41ac2beSBill Paul 	int i;
1600f41ac2beSBill Paul 
1601f41ac2beSBill Paul 
1602f41ac2beSBill Paul 	/* Destroy DMA maps for RX buffers */
1603f41ac2beSBill Paul 
1604f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1605f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1606f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1607f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1608f41ac2beSBill Paul 	}
1609f41ac2beSBill Paul 
1610f41ac2beSBill Paul 	/* Destroy DMA maps for jumbo RX buffers */
1611f41ac2beSBill Paul 
1612f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1613f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1614f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1615f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1616f41ac2beSBill Paul 	}
1617f41ac2beSBill Paul 
1618f41ac2beSBill Paul 	/* Destroy DMA maps for TX buffers */
1619f41ac2beSBill Paul 
1620f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1621f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1622f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1623f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1624f41ac2beSBill Paul 	}
1625f41ac2beSBill Paul 
1626f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1627f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1628f41ac2beSBill Paul 
1629f41ac2beSBill Paul 
1630f41ac2beSBill Paul 	/* Destroy standard RX ring */
1631f41ac2beSBill Paul 
1632e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1633e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1634e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1635e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1636f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1637f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1638f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1639f41ac2beSBill Paul 
1640f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1641f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1642f41ac2beSBill Paul 
1643f41ac2beSBill Paul 	/* Destroy jumbo RX ring */
1644f41ac2beSBill Paul 
1645e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1646e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1647e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1648e65bed95SPyun YongHyeon 
1649e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1650e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1651f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1652f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1653f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1654f41ac2beSBill Paul 
1655f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1656f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1657f41ac2beSBill Paul 
1658f41ac2beSBill Paul 	/* Destroy RX return ring */
1659f41ac2beSBill Paul 
1660e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1661e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1662e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1663e65bed95SPyun YongHyeon 
1664e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1665e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1666f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1667f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1668f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1669f41ac2beSBill Paul 
1670f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1671f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1672f41ac2beSBill Paul 
1673f41ac2beSBill Paul 	/* Destroy TX ring */
1674f41ac2beSBill Paul 
1675e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1676e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1677e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1678e65bed95SPyun YongHyeon 
1679e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1680f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1681f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1682f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1683f41ac2beSBill Paul 
1684f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1685f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1686f41ac2beSBill Paul 
1687f41ac2beSBill Paul 	/* Destroy status block */
1688f41ac2beSBill Paul 
1689e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1690e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1691e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1692e65bed95SPyun YongHyeon 
1693e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1694f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1695f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1696f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1697f41ac2beSBill Paul 
1698f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1699f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1700f41ac2beSBill Paul 
1701f41ac2beSBill Paul 	/* Destroy statistics block */
1702f41ac2beSBill Paul 
1703e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1704e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1705e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1706e65bed95SPyun YongHyeon 
1707e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1708f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1709f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1710f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1711f41ac2beSBill Paul 
1712f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1713f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1714f41ac2beSBill Paul 
1715f41ac2beSBill Paul 	/* Destroy the parent tag */
1716f41ac2beSBill Paul 
1717f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1718f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1719f41ac2beSBill Paul 
1720f41ac2beSBill Paul 	return;
1721f41ac2beSBill Paul }
1722f41ac2beSBill Paul 
1723f41ac2beSBill Paul static int
1724f41ac2beSBill Paul bge_dma_alloc(dev)
1725f41ac2beSBill Paul 	device_t dev;
1726f41ac2beSBill Paul {
1727f41ac2beSBill Paul 	struct bge_softc *sc;
17281be6acb7SGleb Smirnoff 	int i, error;
1729f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
1730f41ac2beSBill Paul 
1731f41ac2beSBill Paul 	sc = device_get_softc(dev);
1732f41ac2beSBill Paul 
1733f41ac2beSBill Paul 	/*
1734f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1735f41ac2beSBill Paul 	 */
1736f41ac2beSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1737f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1738f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
17392f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1740f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1741f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1742f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
17438a40c10eSScott Long 			0,			/* flags */
1744f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1745f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1746f41ac2beSBill Paul 
1747e65bed95SPyun YongHyeon 	if (error != 0) {
1748fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1749fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1750e65bed95SPyun YongHyeon 		return (ENOMEM);
1751e65bed95SPyun YongHyeon 	}
1752e65bed95SPyun YongHyeon 
1753f41ac2beSBill Paul 	/*
1754f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1755f41ac2beSBill Paul 	 */
17568a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1757f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
17581be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
17591be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1760f41ac2beSBill Paul 
1761f41ac2beSBill Paul 	if (error) {
1762fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1763f41ac2beSBill Paul 		return (ENOMEM);
1764f41ac2beSBill Paul 	}
1765f41ac2beSBill Paul 
1766f41ac2beSBill Paul 	/* Create DMA maps for RX buffers */
1767f41ac2beSBill Paul 
1768f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1769f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1770f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1771f41ac2beSBill Paul 		if (error) {
1772fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1773fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1774f41ac2beSBill Paul 			return(ENOMEM);
1775f41ac2beSBill Paul 		}
1776f41ac2beSBill Paul 	}
1777f41ac2beSBill Paul 
1778f41ac2beSBill Paul 	/* Create DMA maps for TX buffers */
1779f41ac2beSBill Paul 
1780f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1781f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1782f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1783f41ac2beSBill Paul 		if (error) {
1784fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1785fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1786f41ac2beSBill Paul 			return(ENOMEM);
1787f41ac2beSBill Paul 		}
1788f41ac2beSBill Paul 	}
1789f41ac2beSBill Paul 
1790f41ac2beSBill Paul 	/* Create tag for standard RX ring */
1791f41ac2beSBill Paul 
1792f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1793f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1794f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1795f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1796f41ac2beSBill Paul 
1797f41ac2beSBill Paul 	if (error) {
1798fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1799f41ac2beSBill Paul 		return (ENOMEM);
1800f41ac2beSBill Paul 	}
1801f41ac2beSBill Paul 
1802f41ac2beSBill Paul 	/* Allocate DMA'able memory for standard RX ring */
1803f41ac2beSBill Paul 
1804f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1805f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1806f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1807f41ac2beSBill Paul 	if (error)
1808f41ac2beSBill Paul 		return (ENOMEM);
1809f41ac2beSBill Paul 
1810f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1811f41ac2beSBill Paul 
1812f41ac2beSBill Paul 	/* Load the address of the standard RX ring */
1813f41ac2beSBill Paul 
1814f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1815f41ac2beSBill Paul 	ctx.sc = sc;
1816f41ac2beSBill Paul 
1817f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1818f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1819f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1820f41ac2beSBill Paul 
1821f41ac2beSBill Paul 	if (error)
1822f41ac2beSBill Paul 		return (ENOMEM);
1823f41ac2beSBill Paul 
1824f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1825f41ac2beSBill Paul 
18265dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1827e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1828f41ac2beSBill Paul 
1829f41ac2beSBill Paul 		/*
1830f41ac2beSBill Paul 		 * Create tag for jumbo mbufs.
1831f41ac2beSBill Paul 		 * This is really a bit of a kludge. We allocate a special
1832f41ac2beSBill Paul 		 * jumbo buffer pool which (thanks to the way our DMA
1833f41ac2beSBill Paul 		 * memory allocation works) will consist of contiguous
1834f41ac2beSBill Paul 		 * pages. This means that even though a jumbo buffer might
1835f41ac2beSBill Paul 		 * be larger than a page size, we don't really need to
1836f41ac2beSBill Paul 		 * map it into more than one DMA segment. However, the
1837f41ac2beSBill Paul 		 * default mbuf tag will result in multi-segment mappings,
1838f41ac2beSBill Paul 		 * so we have to create a special jumbo mbuf tag that
1839f41ac2beSBill Paul 		 * lets us get away with mapping the jumbo buffers as
1840f41ac2beSBill Paul 		 * a single segment. I think eventually the driver should
1841f41ac2beSBill Paul 		 * be changed so that it uses ordinary mbufs and cluster
1842f41ac2beSBill Paul 		 * buffers, i.e. jumbo frames can span multiple DMA
1843f41ac2beSBill Paul 		 * descriptors. But that's a project for another day.
1844f41ac2beSBill Paul 		 */
1845f41ac2beSBill Paul 
1846f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
18478a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
18481be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
18491be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1850f41ac2beSBill Paul 
1851f41ac2beSBill Paul 		if (error) {
1852fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1853fe806fdaSPyun YongHyeon 			    "could not allocate dma tag\n");
1854f41ac2beSBill Paul 			return (ENOMEM);
1855f41ac2beSBill Paul 		}
1856f41ac2beSBill Paul 
1857f41ac2beSBill Paul 		/* Create tag for jumbo RX ring */
1858f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1859f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1860f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1861f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1862f41ac2beSBill Paul 
1863f41ac2beSBill Paul 		if (error) {
1864fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1865fe806fdaSPyun YongHyeon 			    "could not allocate dma tag\n");
1866f41ac2beSBill Paul 			return (ENOMEM);
1867f41ac2beSBill Paul 		}
1868f41ac2beSBill Paul 
1869f41ac2beSBill Paul 		/* Allocate DMA'able memory for jumbo RX ring */
1870f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
18711be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
18721be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1873f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1874f41ac2beSBill Paul 		if (error)
1875f41ac2beSBill Paul 			return (ENOMEM);
1876f41ac2beSBill Paul 
1877f41ac2beSBill Paul 		/* Load the address of the jumbo RX ring */
1878f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1879f41ac2beSBill Paul 		ctx.sc = sc;
1880f41ac2beSBill Paul 
1881f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1882f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1883f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1884f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1885f41ac2beSBill Paul 
1886f41ac2beSBill Paul 		if (error)
1887f41ac2beSBill Paul 			return (ENOMEM);
1888f41ac2beSBill Paul 
1889f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1890f41ac2beSBill Paul 
1891f41ac2beSBill Paul 		/* Create DMA maps for jumbo RX buffers */
1892f41ac2beSBill Paul 
1893f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1894f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1895f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1896f41ac2beSBill Paul 			if (error) {
1897fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
1898f41ac2beSBill Paul 				    "can't create DMA map for RX\n");
1899f41ac2beSBill Paul 				return(ENOMEM);
1900f41ac2beSBill Paul 			}
1901f41ac2beSBill Paul 		}
1902f41ac2beSBill Paul 
1903f41ac2beSBill Paul 	}
1904f41ac2beSBill Paul 
1905f41ac2beSBill Paul 	/* Create tag for RX return ring */
1906f41ac2beSBill Paul 
1907f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1908f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1909f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1910f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1911f41ac2beSBill Paul 
1912f41ac2beSBill Paul 	if (error) {
1913fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1914f41ac2beSBill Paul 		return (ENOMEM);
1915f41ac2beSBill Paul 	}
1916f41ac2beSBill Paul 
1917f41ac2beSBill Paul 	/* Allocate DMA'able memory for RX return ring */
1918f41ac2beSBill Paul 
1919f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1920f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1921f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1922f41ac2beSBill Paul 	if (error)
1923f41ac2beSBill Paul 		return (ENOMEM);
1924f41ac2beSBill Paul 
1925f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1926f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1927f41ac2beSBill Paul 
1928f41ac2beSBill Paul 	/* Load the address of the RX return ring */
1929f41ac2beSBill Paul 
1930f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1931f41ac2beSBill Paul 	ctx.sc = sc;
1932f41ac2beSBill Paul 
1933f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
1934f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
1935f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
1936f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1937f41ac2beSBill Paul 
1938f41ac2beSBill Paul 	if (error)
1939f41ac2beSBill Paul 		return (ENOMEM);
1940f41ac2beSBill Paul 
1941f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
1942f41ac2beSBill Paul 
1943f41ac2beSBill Paul 	/* Create tag for TX ring */
1944f41ac2beSBill Paul 
1945f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1946f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1947f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
1948f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
1949f41ac2beSBill Paul 
1950f41ac2beSBill Paul 	if (error) {
1951fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1952f41ac2beSBill Paul 		return (ENOMEM);
1953f41ac2beSBill Paul 	}
1954f41ac2beSBill Paul 
1955f41ac2beSBill Paul 	/* Allocate DMA'able memory for TX ring */
1956f41ac2beSBill Paul 
1957f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
1958f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
1959f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
1960f41ac2beSBill Paul 	if (error)
1961f41ac2beSBill Paul 		return (ENOMEM);
1962f41ac2beSBill Paul 
1963f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1964f41ac2beSBill Paul 
1965f41ac2beSBill Paul 	/* Load the address of the TX ring */
1966f41ac2beSBill Paul 
1967f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1968f41ac2beSBill Paul 	ctx.sc = sc;
1969f41ac2beSBill Paul 
1970f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
1971f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
1972f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1973f41ac2beSBill Paul 
1974f41ac2beSBill Paul 	if (error)
1975f41ac2beSBill Paul 		return (ENOMEM);
1976f41ac2beSBill Paul 
1977f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
1978f41ac2beSBill Paul 
1979f41ac2beSBill Paul 	/* Create tag for status block */
1980f41ac2beSBill Paul 
1981f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1982f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1983f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
1984f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
1985f41ac2beSBill Paul 
1986f41ac2beSBill Paul 	if (error) {
1987fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1988f41ac2beSBill Paul 		return (ENOMEM);
1989f41ac2beSBill Paul 	}
1990f41ac2beSBill Paul 
1991f41ac2beSBill Paul 	/* Allocate DMA'able memory for status block */
1992f41ac2beSBill Paul 
1993f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
1994f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
1995f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
1996f41ac2beSBill Paul 	if (error)
1997f41ac2beSBill Paul 		return (ENOMEM);
1998f41ac2beSBill Paul 
1999f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2000f41ac2beSBill Paul 
2001f41ac2beSBill Paul 	/* Load the address of the status block */
2002f41ac2beSBill Paul 
2003f41ac2beSBill Paul 	ctx.sc = sc;
2004f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2005f41ac2beSBill Paul 
2006f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2007f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2008f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2009f41ac2beSBill Paul 
2010f41ac2beSBill Paul 	if (error)
2011f41ac2beSBill Paul 		return (ENOMEM);
2012f41ac2beSBill Paul 
2013f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2014f41ac2beSBill Paul 
2015f41ac2beSBill Paul 	/* Create tag for statistics block */
2016f41ac2beSBill Paul 
2017f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2018f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2019f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2020f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2021f41ac2beSBill Paul 
2022f41ac2beSBill Paul 	if (error) {
2023fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2024f41ac2beSBill Paul 		return (ENOMEM);
2025f41ac2beSBill Paul 	}
2026f41ac2beSBill Paul 
2027f41ac2beSBill Paul 	/* Allocate DMA'able memory for statistics block */
2028f41ac2beSBill Paul 
2029f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2030f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2031f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2032f41ac2beSBill Paul 	if (error)
2033f41ac2beSBill Paul 		return (ENOMEM);
2034f41ac2beSBill Paul 
2035f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2036f41ac2beSBill Paul 
2037f41ac2beSBill Paul 	/* Load the address of the statstics block */
2038f41ac2beSBill Paul 
2039f41ac2beSBill Paul 	ctx.sc = sc;
2040f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2041f41ac2beSBill Paul 
2042f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2043f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2044f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2045f41ac2beSBill Paul 
2046f41ac2beSBill Paul 	if (error)
2047f41ac2beSBill Paul 		return (ENOMEM);
2048f41ac2beSBill Paul 
2049f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2050f41ac2beSBill Paul 
2051f41ac2beSBill Paul 	return(0);
2052f41ac2beSBill Paul }
2053f41ac2beSBill Paul 
205495d67482SBill Paul static int
205595d67482SBill Paul bge_attach(dev)
205695d67482SBill Paul 	device_t dev;
205795d67482SBill Paul {
205895d67482SBill Paul 	struct ifnet *ifp;
205995d67482SBill Paul 	struct bge_softc *sc;
2060a1d52896SBill Paul 	u_int32_t hwcfg = 0;
2061fc74a9f9SBrooks Davis 	u_int32_t mac_tmp = 0;
2062fc74a9f9SBrooks Davis 	u_char eaddr[6];
2063fe806fdaSPyun YongHyeon 	int error = 0, rid;
206495d67482SBill Paul 
206595d67482SBill Paul 	sc = device_get_softc(dev);
206695d67482SBill Paul 	sc->bge_dev = dev;
206795d67482SBill Paul 
206895d67482SBill Paul 	/*
206995d67482SBill Paul 	 * Map control/status registers.
207095d67482SBill Paul 	 */
207195d67482SBill Paul 	pci_enable_busmaster(dev);
207295d67482SBill Paul 
207395d67482SBill Paul 	rid = BGE_PCI_BAR0;
20745f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
20755f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
207695d67482SBill Paul 
207795d67482SBill Paul 	if (sc->bge_res == NULL) {
2078fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
207995d67482SBill Paul 		error = ENXIO;
208095d67482SBill Paul 		goto fail;
208195d67482SBill Paul 	}
208295d67482SBill Paul 
208395d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
208495d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
208595d67482SBill Paul 
208695d67482SBill Paul 	/* Allocate interrupt */
208795d67482SBill Paul 	rid = 0;
208895d67482SBill Paul 
20895f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
209095d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
209195d67482SBill Paul 
209295d67482SBill Paul 	if (sc->bge_irq == NULL) {
2093fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
209495d67482SBill Paul 		error = ENXIO;
209595d67482SBill Paul 		goto fail;
209695d67482SBill Paul 	}
209795d67482SBill Paul 
20980f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
20990f9bd73bSSam Leffler 
2100e53d81eeSPaul Saab 	/* Save ASIC rev. */
2101e53d81eeSPaul Saab 
2102e53d81eeSPaul Saab 	sc->bge_chipid =
2103e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2104e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2105e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2106e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2107e53d81eeSPaul Saab 
2108e53d81eeSPaul Saab 	/*
2109560c1670SGleb Smirnoff 	 * Treat the 5714 and the 5752 like the 5750 until we have more info
2110419c028bSPaul Saab 	 * on this chip.
2111419c028bSPaul Saab 	 */
2112560c1670SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5714 ||
2113560c1670SGleb Smirnoff             sc->bge_asicrev == BGE_ASICREV_BCM5752)
2114419c028bSPaul Saab 		sc->bge_asicrev = BGE_ASICREV_BCM5750;
2115419c028bSPaul Saab 
2116419c028bSPaul Saab 	/*
2117e53d81eeSPaul Saab 	 * XXX: Broadcom Linux driver.  Not in specs or eratta.
2118e53d81eeSPaul Saab 	 * PCI-Express?
2119e53d81eeSPaul Saab 	 */
2120e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
2121e53d81eeSPaul Saab 		u_int32_t v;
2122e53d81eeSPaul Saab 
2123e53d81eeSPaul Saab 		v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4);
2124e53d81eeSPaul Saab 		if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) {
2125e53d81eeSPaul Saab 			v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
2126e53d81eeSPaul Saab 			if ((v & 0xff) == BGE_PCIE_CAPID)
2127e53d81eeSPaul Saab 				sc->bge_pcie = 1;
2128e53d81eeSPaul Saab 		}
2129e53d81eeSPaul Saab 	}
2130e53d81eeSPaul Saab 
213195d67482SBill Paul 	/* Try to reset the chip. */
213295d67482SBill Paul 	bge_reset(sc);
213395d67482SBill Paul 
213495d67482SBill Paul 	if (bge_chipinit(sc)) {
2135fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
213695d67482SBill Paul 		bge_release_resources(sc);
213795d67482SBill Paul 		error = ENXIO;
213895d67482SBill Paul 		goto fail;
213995d67482SBill Paul 	}
214095d67482SBill Paul 
214195d67482SBill Paul 	/*
214295d67482SBill Paul 	 * Get station address from the EEPROM.
214395d67482SBill Paul 	 */
2144fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2145fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2146fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2147fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2148fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2149fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2150fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2151fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2152fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2153fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
215495d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2155fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
215695d67482SBill Paul 		bge_release_resources(sc);
215795d67482SBill Paul 		error = ENXIO;
215895d67482SBill Paul 		goto fail;
215995d67482SBill Paul 	}
216095d67482SBill Paul 
2161f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
2162e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2163e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
2164f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2165f41ac2beSBill Paul 	else
2166f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2167f41ac2beSBill Paul 
2168f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2169fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2170fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2171f41ac2beSBill Paul 		bge_release_resources(sc);
2172f41ac2beSBill Paul 		error = ENXIO;
2173f41ac2beSBill Paul 		goto fail;
2174f41ac2beSBill Paul 	}
2175f41ac2beSBill Paul 
217695d67482SBill Paul 	/* Set default tuneable values. */
217795d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
217895d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
217995d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
218095d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
218195d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
218295d67482SBill Paul 
218395d67482SBill Paul 	/* Set up ifnet structure */
2184fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2185fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2186fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2187fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2188fc74a9f9SBrooks Davis 		error = ENXIO;
2189fc74a9f9SBrooks Davis 		goto fail;
2190fc74a9f9SBrooks Davis 	}
219195d67482SBill Paul 	ifp->if_softc = sc;
21929bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
219395d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
219495d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
219595d67482SBill Paul 	ifp->if_start = bge_start;
219695d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
219795d67482SBill Paul 	ifp->if_init = bge_init;
219895d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
21994d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
22004d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
22014d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
220295d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2203d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
22040434d1b8SBill Paul 	    IFCAP_VLAN_MTU;
220595d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
220675719184SGleb Smirnoff #ifdef DEVICE_POLLING
220775719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
220875719184SGleb Smirnoff #endif
220995d67482SBill Paul 
2210a1d52896SBill Paul         /*
2211d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2212d375e524SGleb Smirnoff 	 * to hardware bugs.
2213d375e524SGleb Smirnoff 	 */
2214d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2215d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2216d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2217d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2218d375e524SGleb Smirnoff 	}
2219d375e524SGleb Smirnoff 
2220d375e524SGleb Smirnoff 	/*
2221a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
222241abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
222341abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
222441abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
222541abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
222641abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
222741abcc1bSPaul Saab 	 * SK-9D41.
2228a1d52896SBill Paul 	 */
222941abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
223041abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
223141abcc1bSPaul Saab 	else {
2232f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2233f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2234fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2235f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2236f6789fbaSPyun YongHyeon 			error = ENXIO;
2237f6789fbaSPyun YongHyeon 			goto fail;
2238f6789fbaSPyun YongHyeon 		}
223941abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
224041abcc1bSPaul Saab 	}
224141abcc1bSPaul Saab 
224241abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2243a1d52896SBill Paul 		sc->bge_tbi = 1;
2244a1d52896SBill Paul 
224595d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
224695d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
224795d67482SBill Paul 		sc->bge_tbi = 1;
224895d67482SBill Paul 
224995d67482SBill Paul 	if (sc->bge_tbi) {
225095d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
225195d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
225295d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
225395d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
225495d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
225595d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
225695d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2257da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
225895d67482SBill Paul 	} else {
225995d67482SBill Paul 		/*
226095d67482SBill Paul 		 * Do transceiver setup.
226195d67482SBill Paul 		 */
226295d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
226395d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
2264fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
226595d67482SBill Paul 			bge_release_resources(sc);
226695d67482SBill Paul 			error = ENXIO;
226795d67482SBill Paul 			goto fail;
226895d67482SBill Paul 		}
226995d67482SBill Paul 	}
227095d67482SBill Paul 
227195d67482SBill Paul 	/*
2272e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2273e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2274e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2275e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2276e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2277e255b776SJohn Polstra 	 * payloads by copying the received packets.
2278e255b776SJohn Polstra 	 */
2279e0ced696SPaul Saab 	switch (sc->bge_chipid) {
2280e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_A0:
2281e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B0:
2282e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B2:
2283e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B5:
2284e255b776SJohn Polstra 		/* If in PCI-X mode, work around the alignment bug. */
2285e255b776SJohn Polstra 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
2286e255b776SJohn Polstra 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2287e255b776SJohn Polstra 		    BGE_PCISTATE_PCI_BUSSPEED)
2288e255b776SJohn Polstra 			sc->bge_rx_alignment_bug = 1;
2289e255b776SJohn Polstra 		break;
2290e255b776SJohn Polstra 	}
2291e255b776SJohn Polstra 
2292e255b776SJohn Polstra 	/*
229395d67482SBill Paul 	 * Call MI attach routine.
229495d67482SBill Paul 	 */
2295fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
22960f9bd73bSSam Leffler 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
22970f9bd73bSSam Leffler 
22980f9bd73bSSam Leffler 	/*
22990f9bd73bSSam Leffler 	 * Hookup IRQ last.
23000f9bd73bSSam Leffler 	 */
23010f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
23020f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
23030f9bd73bSSam Leffler 
23040f9bd73bSSam Leffler 	if (error) {
2305fc74a9f9SBrooks Davis 		bge_detach(dev);
2306fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
23070f9bd73bSSam Leffler 	}
230895d67482SBill Paul 
230995d67482SBill Paul fail:
231095d67482SBill Paul 	return(error);
231195d67482SBill Paul }
231295d67482SBill Paul 
231395d67482SBill Paul static int
231495d67482SBill Paul bge_detach(dev)
231595d67482SBill Paul 	device_t dev;
231695d67482SBill Paul {
231795d67482SBill Paul 	struct bge_softc *sc;
231895d67482SBill Paul 	struct ifnet *ifp;
231995d67482SBill Paul 
232095d67482SBill Paul 	sc = device_get_softc(dev);
2321fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
232295d67482SBill Paul 
232375719184SGleb Smirnoff #ifdef DEVICE_POLLING
232475719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
232575719184SGleb Smirnoff 		ether_poll_deregister(ifp);
232675719184SGleb Smirnoff #endif
232775719184SGleb Smirnoff 
23280f9bd73bSSam Leffler 	BGE_LOCK(sc);
232995d67482SBill Paul 	bge_stop(sc);
233095d67482SBill Paul 	bge_reset(sc);
23310f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
23320f9bd73bSSam Leffler 
23330f9bd73bSSam Leffler 	ether_ifdetach(ifp);
233495d67482SBill Paul 
233595d67482SBill Paul 	if (sc->bge_tbi) {
233695d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
233795d67482SBill Paul 	} else {
233895d67482SBill Paul 		bus_generic_detach(dev);
233995d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
234095d67482SBill Paul 	}
234195d67482SBill Paul 
234295d67482SBill Paul 	bge_release_resources(sc);
234395d67482SBill Paul 
234495d67482SBill Paul 	return(0);
234595d67482SBill Paul }
234695d67482SBill Paul 
234795d67482SBill Paul static void
234895d67482SBill Paul bge_release_resources(sc)
234995d67482SBill Paul 	struct bge_softc *sc;
235095d67482SBill Paul {
235195d67482SBill Paul 	device_t dev;
235295d67482SBill Paul 
235395d67482SBill Paul 	dev = sc->bge_dev;
235495d67482SBill Paul 
235595d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
235695d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
235795d67482SBill Paul 
235895d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
235995d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
236095d67482SBill Paul 
236195d67482SBill Paul 	if (sc->bge_intrhand != NULL)
236295d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
236395d67482SBill Paul 
236495d67482SBill Paul 	if (sc->bge_irq != NULL)
236595d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
236695d67482SBill Paul 
236795d67482SBill Paul 	if (sc->bge_res != NULL)
236895d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
236995d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
237095d67482SBill Paul 
2371ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2372ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2373ad61f896SRuslan Ermilov 
2374f41ac2beSBill Paul 	bge_dma_free(sc);
237595d67482SBill Paul 
23760f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
23770f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
23780f9bd73bSSam Leffler 
237995d67482SBill Paul 	return;
238095d67482SBill Paul }
238195d67482SBill Paul 
238295d67482SBill Paul static void
238395d67482SBill Paul bge_reset(sc)
238495d67482SBill Paul 	struct bge_softc *sc;
238595d67482SBill Paul {
238695d67482SBill Paul 	device_t dev;
2387e53d81eeSPaul Saab 	u_int32_t cachesize, command, pcistate, reset;
238895d67482SBill Paul 	int i, val = 0;
238995d67482SBill Paul 
239095d67482SBill Paul 	dev = sc->bge_dev;
239195d67482SBill Paul 
239295d67482SBill Paul 	/* Save some important PCI state. */
239395d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
239495d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
239595d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
239695d67482SBill Paul 
239795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
239895d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2399e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
240095d67482SBill Paul 
2401e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2402e53d81eeSPaul Saab 
2403e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2404e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2405e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2406e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2407e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2408e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2409e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2410e53d81eeSPaul Saab 			reset |= (1<<29);
2411e53d81eeSPaul Saab 		}
2412e53d81eeSPaul Saab 	}
2413e53d81eeSPaul Saab 
241495d67482SBill Paul 	/* Issue global reset */
2415e53d81eeSPaul Saab 	bge_writereg_ind(sc, BGE_MISC_CFG, reset);
241695d67482SBill Paul 
241795d67482SBill Paul 	DELAY(1000);
241895d67482SBill Paul 
2419e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2420e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2421e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2422e53d81eeSPaul Saab 			uint32_t v;
2423e53d81eeSPaul Saab 
2424e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2425e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2426e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2427e53d81eeSPaul Saab 		}
2428e53d81eeSPaul Saab 		/* Set PCIE max payload size and clear error status. */
2429e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2430e53d81eeSPaul Saab 	}
2431e53d81eeSPaul Saab 
243295d67482SBill Paul 	/* Reset some of the PCI state that got zapped by reset */
243395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
243495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2435e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
243695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
243795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
243895d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
243995d67482SBill Paul 
2440a7b0c314SPaul Saab 	/* Enable memory arbiter. */
24415dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2442e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
2443a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2444a7b0c314SPaul Saab 
244595d67482SBill Paul 	/*
244695d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
244795d67482SBill Paul 	 * general communications memory at 0xB50.
244895d67482SBill Paul 	 */
244995d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
245095d67482SBill Paul 	/*
245195d67482SBill Paul 	 * Poll the value location we just wrote until
245295d67482SBill Paul 	 * we see the 1's complement of the magic number.
245395d67482SBill Paul 	 * This indicates that the firmware initialization
245495d67482SBill Paul 	 * is complete.
245595d67482SBill Paul 	 */
245695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
245795d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
245895d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
245995d67482SBill Paul 			break;
246095d67482SBill Paul 		DELAY(10);
246195d67482SBill Paul 	}
246295d67482SBill Paul 
246395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2464fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "firmware handshake timed out\n");
246595d67482SBill Paul 		return;
246695d67482SBill Paul 	}
246795d67482SBill Paul 
246895d67482SBill Paul 	/*
246995d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
247095d67482SBill Paul 	 * return to its original pre-reset state. This is a
247195d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
247295d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
247395d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
247495d67482SBill Paul 	 * results.
247595d67482SBill Paul 	 */
247695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
247795d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
247895d67482SBill Paul 			break;
247995d67482SBill Paul 		DELAY(10);
248095d67482SBill Paul 	}
248195d67482SBill Paul 
248295d67482SBill Paul 	/* Fix up byte swapping */
2483e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
248495d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
248595d67482SBill Paul 
248695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
248795d67482SBill Paul 
2488da3003f0SBill Paul 	/*
2489da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2490da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2491da3003f0SBill Paul 	 * to 1.2V.
2492da3003f0SBill Paul 	 */
2493da3003f0SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 && sc->bge_tbi) {
2494da3003f0SBill Paul 		uint32_t serdescfg;
2495da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2496da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2497da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2498da3003f0SBill Paul 	}
2499da3003f0SBill Paul 
2500e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2501e53d81eeSPaul Saab 	if (sc->bge_pcie && sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2502e53d81eeSPaul Saab 		uint32_t v;
2503e53d81eeSPaul Saab 
2504e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2505e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2506e53d81eeSPaul Saab 	}
250795d67482SBill Paul 	DELAY(10000);
250895d67482SBill Paul 
250995d67482SBill Paul 	return;
251095d67482SBill Paul }
251195d67482SBill Paul 
251295d67482SBill Paul /*
251395d67482SBill Paul  * Frame reception handling. This is called if there's a frame
251495d67482SBill Paul  * on the receive return list.
251595d67482SBill Paul  *
251695d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
25171be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
251895d67482SBill Paul  * 2) the frame is from the standard receive ring
251995d67482SBill Paul  */
252095d67482SBill Paul 
252195d67482SBill Paul static void
252295d67482SBill Paul bge_rxeof(sc)
252395d67482SBill Paul 	struct bge_softc *sc;
252495d67482SBill Paul {
252595d67482SBill Paul 	struct ifnet *ifp;
252695d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
252795d67482SBill Paul 
25280f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
25290f9bd73bSSam Leffler 
2530fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
253195d67482SBill Paul 
2532f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2533e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2534f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2535f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
25365dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2537e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2538f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2539f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2540f41ac2beSBill Paul 		    BUS_DMASYNC_POSTREAD);
2541f41ac2beSBill Paul 	}
2542f41ac2beSBill Paul 
254395d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2544f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
254595d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
254695d67482SBill Paul 		u_int32_t		rxidx;
254795d67482SBill Paul 		struct ether_header	*eh;
254895d67482SBill Paul 		struct mbuf		*m = NULL;
254995d67482SBill Paul 		u_int16_t		vlan_tag = 0;
255095d67482SBill Paul 		int			have_tag = 0;
255195d67482SBill Paul 
255275719184SGleb Smirnoff #ifdef DEVICE_POLLING
255375719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
255475719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
255575719184SGleb Smirnoff 				break;
255675719184SGleb Smirnoff 			sc->rxcycles--;
255775719184SGleb Smirnoff 		}
255875719184SGleb Smirnoff #endif
255975719184SGleb Smirnoff 
256095d67482SBill Paul 		cur_rx =
2561f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
256295d67482SBill Paul 
256395d67482SBill Paul 		rxidx = cur_rx->bge_idx;
25640434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
256595d67482SBill Paul 
256695d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
256795d67482SBill Paul 			have_tag = 1;
256895d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
256995d67482SBill Paul 		}
257095d67482SBill Paul 
257195d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
257295d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2573f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2574f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2575f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2576f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2577f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
257895d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
257995d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
258095d67482SBill Paul 			jumbocnt++;
258195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
258295d67482SBill Paul 				ifp->if_ierrors++;
258395d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
258495d67482SBill Paul 				continue;
258595d67482SBill Paul 			}
258695d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
258795d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
258895d67482SBill Paul 				ifp->if_ierrors++;
258995d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
259095d67482SBill Paul 				continue;
259195d67482SBill Paul 			}
259295d67482SBill Paul 		} else {
259395d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2594f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2595f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2596f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2597f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2598f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
259995d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
260095d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
260195d67482SBill Paul 			stdcnt++;
260295d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
260395d67482SBill Paul 				ifp->if_ierrors++;
260495d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
260595d67482SBill Paul 				continue;
260695d67482SBill Paul 			}
260795d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
260895d67482SBill Paul 			    NULL) == ENOBUFS) {
260995d67482SBill Paul 				ifp->if_ierrors++;
261095d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
261195d67482SBill Paul 				continue;
261295d67482SBill Paul 			}
261395d67482SBill Paul 		}
261495d67482SBill Paul 
261595d67482SBill Paul 		ifp->if_ipackets++;
2616e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2617e255b776SJohn Polstra 		/*
2618e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2619e65bed95SPyun YongHyeon 		 * the payload is aligned.
2620e255b776SJohn Polstra 		 */
2621e255b776SJohn Polstra 		if (sc->bge_rx_alignment_bug) {
2622e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2623e255b776SJohn Polstra 			    cur_rx->bge_len);
2624e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2625e255b776SJohn Polstra 		}
2626e255b776SJohn Polstra #endif
262795d67482SBill Paul 		eh = mtod(m, struct ether_header *);
2628473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
262995d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
263095d67482SBill Paul 
2631b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
263295d67482SBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
263395d67482SBill Paul 			if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
263495d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2635d375e524SGleb Smirnoff 
2636d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2637d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
263895d67482SBill Paul 				m->m_pkthdr.csum_data =
263995d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
26400189c944SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
264195d67482SBill Paul 			}
264295d67482SBill Paul 		}
264395d67482SBill Paul 
264495d67482SBill Paul 		/*
2645673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2646673d9191SSam Leffler 		 * attach that information to the packet.
264795d67482SBill Paul 		 */
2648d147662cSGleb Smirnoff 		if (have_tag) {
2649d147662cSGleb Smirnoff 			VLAN_INPUT_TAG(ifp, m, vlan_tag);
2650d147662cSGleb Smirnoff 			if (m == NULL)
2651d147662cSGleb Smirnoff 				continue;
2652d147662cSGleb Smirnoff 		}
265395d67482SBill Paul 
26540f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2655673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
26560f9bd73bSSam Leffler 		BGE_LOCK(sc);
265795d67482SBill Paul 	}
265895d67482SBill Paul 
2659e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2660f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2661e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
26625dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2663e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2664e65bed95SPyun YongHyeon 		if (jumbocnt > 0)
2665f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2666f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_ring_map,
2667e65bed95SPyun YongHyeon 			    BUS_DMASYNC_PREWRITE);
2668f41ac2beSBill Paul 	}
2669f41ac2beSBill Paul 
267095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
267195d67482SBill Paul 	if (stdcnt)
267295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
267395d67482SBill Paul 	if (jumbocnt)
267495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
267595d67482SBill Paul 
267695d67482SBill Paul 	return;
267795d67482SBill Paul }
267895d67482SBill Paul 
267995d67482SBill Paul static void
268095d67482SBill Paul bge_txeof(sc)
268195d67482SBill Paul 	struct bge_softc *sc;
268295d67482SBill Paul {
268395d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
268495d67482SBill Paul 	struct ifnet *ifp;
268595d67482SBill Paul 
26860f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
26870f9bd73bSSam Leffler 
2688fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
268995d67482SBill Paul 
2690e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2691e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2692e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
269395d67482SBill Paul 	/*
269495d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
269595d67482SBill Paul 	 * frames that have been sent.
269695d67482SBill Paul 	 */
269795d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2698f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
269995d67482SBill Paul 		u_int32_t		idx = 0;
270095d67482SBill Paul 
270195d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2702f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
270395d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
270495d67482SBill Paul 			ifp->if_opackets++;
270595d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2706e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2707e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2708e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2709f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2710f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2711e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2712e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
271395d67482SBill Paul 		}
271495d67482SBill Paul 		sc->bge_txcnt--;
271595d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
271695d67482SBill Paul 		ifp->if_timer = 0;
271795d67482SBill Paul 	}
271895d67482SBill Paul 
271995d67482SBill Paul 	if (cur_tx != NULL)
272013f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
272195d67482SBill Paul 
272295d67482SBill Paul 	return;
272395d67482SBill Paul }
272495d67482SBill Paul 
272575719184SGleb Smirnoff #ifdef DEVICE_POLLING
272675719184SGleb Smirnoff static void
272775719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
272875719184SGleb Smirnoff {
272975719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
273075719184SGleb Smirnoff 
273175719184SGleb Smirnoff 	BGE_LOCK(sc);
273275719184SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
273375719184SGleb Smirnoff 		bge_poll_locked(ifp, cmd, count);
273475719184SGleb Smirnoff 	BGE_UNLOCK(sc);
273575719184SGleb Smirnoff }
273675719184SGleb Smirnoff 
273775719184SGleb Smirnoff static void
273875719184SGleb Smirnoff bge_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
273975719184SGleb Smirnoff {
274075719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
274175719184SGleb Smirnoff 
274275719184SGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
274375719184SGleb Smirnoff 
274475719184SGleb Smirnoff 	sc->rxcycles = count;
274575719184SGleb Smirnoff 	bge_rxeof(sc);
274675719184SGleb Smirnoff 	bge_txeof(sc);
274775719184SGleb Smirnoff 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
274875719184SGleb Smirnoff 		bge_start_locked(ifp);
274975719184SGleb Smirnoff 
275075719184SGleb Smirnoff 	if (cmd == POLL_AND_CHECK_STATUS) {
2751dab5cd05SOleg Bulyzhin 		uint32_t statusword;
275275719184SGleb Smirnoff 
2753dab5cd05SOleg Bulyzhin 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2754e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2755dab5cd05SOleg Bulyzhin 
2756dab5cd05SOleg Bulyzhin 	    	statusword = atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
2757dab5cd05SOleg Bulyzhin 
27581f313773SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
27591f313773SOleg Bulyzhin 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) ||
2760dab5cd05SOleg Bulyzhin 		    statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2761dab5cd05SOleg Bulyzhin 			bge_link_upd(sc);
2762dab5cd05SOleg Bulyzhin 
2763dab5cd05SOleg Bulyzhin 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2764e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
276575719184SGleb Smirnoff 	}
276675719184SGleb Smirnoff }
276775719184SGleb Smirnoff #endif /* DEVICE_POLLING */
276875719184SGleb Smirnoff 
276995d67482SBill Paul static void
277095d67482SBill Paul bge_intr(xsc)
277195d67482SBill Paul 	void *xsc;
277295d67482SBill Paul {
277395d67482SBill Paul 	struct bge_softc *sc;
277495d67482SBill Paul 	struct ifnet *ifp;
2775dab5cd05SOleg Bulyzhin 	uint32_t statusword;
277695d67482SBill Paul 
277795d67482SBill Paul 	sc = xsc;
2778f41ac2beSBill Paul 
27790f9bd73bSSam Leffler 	BGE_LOCK(sc);
27800f9bd73bSSam Leffler 
2781dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2782dab5cd05SOleg Bulyzhin 
278375719184SGleb Smirnoff #ifdef DEVICE_POLLING
278475719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
278575719184SGleb Smirnoff 		BGE_UNLOCK(sc);
278675719184SGleb Smirnoff 		return;
278775719184SGleb Smirnoff 	}
278875719184SGleb Smirnoff #endif
278975719184SGleb Smirnoff 
2790f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2791e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2792f41ac2beSBill Paul 
2793487a8c7eSPaul Saab 	statusword =
2794f41ac2beSBill Paul 	    atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
279595d67482SBill Paul 
279695d67482SBill Paul #ifdef notdef
279795d67482SBill Paul 	/* Avoid this for now -- checking this register is expensive. */
279895d67482SBill Paul 	/* Make sure this is really our interrupt. */
279995d67482SBill Paul 	if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
280095d67482SBill Paul 		return;
280195d67482SBill Paul #endif
280295d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
280395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
280495d67482SBill Paul 
28051f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
28061f313773SOleg Bulyzhin 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) ||
2807dab5cd05SOleg Bulyzhin 	    statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2808dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
280995d67482SBill Paul 
281013f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
281195d67482SBill Paul 		/* Check RX return ring producer/consumer */
281295d67482SBill Paul 		bge_rxeof(sc);
281395d67482SBill Paul 
281495d67482SBill Paul 		/* Check TX ring producer/consumer */
281595d67482SBill Paul 		bge_txeof(sc);
281695d67482SBill Paul 	}
281795d67482SBill Paul 
281895d67482SBill Paul 	/* Re-enable interrupts. */
281995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
282095d67482SBill Paul 
282113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
282213f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
28230f9bd73bSSam Leffler 		bge_start_locked(ifp);
28240f9bd73bSSam Leffler 
28250f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
282695d67482SBill Paul 
282795d67482SBill Paul 	return;
282895d67482SBill Paul }
282995d67482SBill Paul 
283095d67482SBill Paul static void
28310f9bd73bSSam Leffler bge_tick_locked(sc)
283295d67482SBill Paul 	struct bge_softc *sc;
28330f9bd73bSSam Leffler {
283495d67482SBill Paul 	struct mii_data *mii = NULL;
283595d67482SBill Paul 	struct ifnet *ifp;
283695d67482SBill Paul 
28370f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
283895d67482SBill Paul 
2839dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2840dab5cd05SOleg Bulyzhin 
2841e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2842e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
28430434d1b8SBill Paul 		bge_stats_update_regs(sc);
28440434d1b8SBill Paul 	else
284595d67482SBill Paul 		bge_stats_update(sc);
284695d67482SBill Paul 
28471f313773SOleg Bulyzhin 	if (!sc->bge_tbi) {
284895d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
284995d67482SBill Paul 		mii_tick(mii);
2850dab5cd05SOleg Bulyzhin 	}
285195d67482SBill Paul 
2852dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
285395d67482SBill Paul }
285495d67482SBill Paul 
285595d67482SBill Paul static void
28560f9bd73bSSam Leffler bge_tick(xsc)
28570f9bd73bSSam Leffler 	void *xsc;
28580f9bd73bSSam Leffler {
28590f9bd73bSSam Leffler 	struct bge_softc *sc;
28600f9bd73bSSam Leffler 
28610f9bd73bSSam Leffler 	sc = xsc;
28620f9bd73bSSam Leffler 
28630f9bd73bSSam Leffler 	BGE_LOCK(sc);
28640f9bd73bSSam Leffler 	bge_tick_locked(sc);
28650f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
28660f9bd73bSSam Leffler }
28670f9bd73bSSam Leffler 
28680f9bd73bSSam Leffler static void
28690434d1b8SBill Paul bge_stats_update_regs(sc)
28700434d1b8SBill Paul 	struct bge_softc *sc;
28710434d1b8SBill Paul {
28720434d1b8SBill Paul 	struct ifnet *ifp;
28730434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
28740434d1b8SBill Paul 	u_int32_t *s;
28756fb34dd2SOleg Bulyzhin 	u_long cnt;			/* current register value */
28760434d1b8SBill Paul 	int i;
28770434d1b8SBill Paul 
2878fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
28790434d1b8SBill Paul 
28800434d1b8SBill Paul 	s = (u_int32_t *)&stats;
28810434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
28820434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
28830434d1b8SBill Paul 		s++;
28840434d1b8SBill Paul 	}
28850434d1b8SBill Paul 
28866fb34dd2SOleg Bulyzhin 	cnt = stats.dot3StatsSingleCollisionFrames +
28870434d1b8SBill Paul 	    stats.dot3StatsMultipleCollisionFrames +
28880434d1b8SBill Paul 	    stats.dot3StatsExcessiveCollisions +
28896fb34dd2SOleg Bulyzhin 	    stats.dot3StatsLateCollisions;
28906fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
28916fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
28926fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
28930434d1b8SBill Paul }
28940434d1b8SBill Paul 
28950434d1b8SBill Paul static void
289695d67482SBill Paul bge_stats_update(sc)
289795d67482SBill Paul 	struct bge_softc *sc;
289895d67482SBill Paul {
289995d67482SBill Paul 	struct ifnet *ifp;
2900e907febfSPyun YongHyeon 	bus_size_t stats;
29016fb34dd2SOleg Bulyzhin 	u_long cnt;			/* current register value */
290295d67482SBill Paul 
2903fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
290495d67482SBill Paul 
2905e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2906e907febfSPyun YongHyeon 
2907e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
2908e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
290995d67482SBill Paul 
29106fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats,
29116fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsSingleCollisionFrames.bge_addr_lo);
29126fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
29136fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo);
29146fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
29156fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsExcessiveCollisions.bge_addr_lo);
29166fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
29176fb34dd2SOleg Bulyzhin 		txstats.dot3StatsLateCollisions.bge_addr_lo);
29186fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
29196fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
29206fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
29216fb34dd2SOleg Bulyzhin 
29226fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
29236fb34dd2SOleg Bulyzhin 	ifp->if_ierrors += cnt >= sc->bge_rx_discards ?
29246fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_rx_discards : cnt;
29256fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
29266fb34dd2SOleg Bulyzhin 
29276fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
29286fb34dd2SOleg Bulyzhin 	ifp->if_oerrors += cnt >= sc->bge_tx_discards ?
29296fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_discards : cnt;
29306fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
293195d67482SBill Paul 
2932e907febfSPyun YongHyeon #undef READ_STAT
293395d67482SBill Paul }
293495d67482SBill Paul 
293595d67482SBill Paul /*
2936d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
2937d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
2938d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
2939d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
2940d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
2941d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
2942d375e524SGleb Smirnoff  */
2943d375e524SGleb Smirnoff static __inline int
2944d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
2945d375e524SGleb Smirnoff {
2946d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
2947d375e524SGleb Smirnoff 	struct mbuf *last;
2948d375e524SGleb Smirnoff 
2949d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
2950d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
2951d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
2952d375e524SGleb Smirnoff 		last = m;
2953d375e524SGleb Smirnoff 	} else {
2954d375e524SGleb Smirnoff 		/*
2955d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
2956d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
2957d375e524SGleb Smirnoff 		 */
2958d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
2959d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
2960d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
2961d375e524SGleb Smirnoff 			struct mbuf *n;
2962d375e524SGleb Smirnoff 
2963d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
2964d375e524SGleb Smirnoff 			if (n == NULL)
2965d375e524SGleb Smirnoff 				return (ENOBUFS);
2966d375e524SGleb Smirnoff 			n->m_len = 0;
2967d375e524SGleb Smirnoff 			last->m_next = n;
2968d375e524SGleb Smirnoff 			last = n;
2969d375e524SGleb Smirnoff 		}
2970d375e524SGleb Smirnoff 	}
2971d375e524SGleb Smirnoff 
2972d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
2973d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
2974d375e524SGleb Smirnoff 	last->m_len += padlen;
2975d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
2976d375e524SGleb Smirnoff 
2977d375e524SGleb Smirnoff 	return (0);
2978d375e524SGleb Smirnoff }
2979d375e524SGleb Smirnoff 
2980d375e524SGleb Smirnoff /*
298195d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
298295d67482SBill Paul  * pointers to descriptors.
298395d67482SBill Paul  */
298495d67482SBill Paul static int
298595d67482SBill Paul bge_encap(sc, m_head, txidx)
298695d67482SBill Paul 	struct bge_softc *sc;
298795d67482SBill Paul 	struct mbuf *m_head;
29887e27542aSGleb Smirnoff 	uint32_t *txidx;
298995d67482SBill Paul {
29907e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
2991f41ac2beSBill Paul 	bus_dmamap_t		map;
29927e27542aSGleb Smirnoff 	struct bge_tx_bd	*d = NULL;
29937e27542aSGleb Smirnoff 	struct m_tag		*mtag;
29947e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
29957e27542aSGleb Smirnoff 	uint16_t		csum_flags = 0;
29967e27542aSGleb Smirnoff 	int			nsegs, i, error;
299795d67482SBill Paul 
299895d67482SBill Paul 	if (m_head->m_pkthdr.csum_flags) {
299995d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
300095d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3001d375e524SGleb Smirnoff 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
300295d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3003d375e524SGleb Smirnoff 			if (m_head->m_pkthdr.len < ETHER_MIN_NOPAD &&
3004d375e524SGleb Smirnoff 			    bge_cksum_pad(m_head) != 0)
3005d375e524SGleb Smirnoff 				return (ENOBUFS);
3006d375e524SGleb Smirnoff 		}
300795d67482SBill Paul 		if (m_head->m_flags & M_LASTFRAG)
300895d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
300995d67482SBill Paul 		else if (m_head->m_flags & M_FRAG)
301095d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
301195d67482SBill Paul 	}
301295d67482SBill Paul 
3013fc74a9f9SBrooks Davis 	mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m_head);
3014673d9191SSam Leffler 
30157e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
30167e27542aSGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map,
30177e27542aSGleb Smirnoff 	    m_head, segs, &nsegs, BUS_DMA_NOWAIT);
30187e27542aSGleb Smirnoff         if (error) {
30197e27542aSGleb Smirnoff 		if (error == EFBIG) {
30207e27542aSGleb Smirnoff 			struct mbuf *m0;
30217e27542aSGleb Smirnoff 
30227e27542aSGleb Smirnoff 			m0 = m_defrag(m_head, M_DONTWAIT);
30237e27542aSGleb Smirnoff 			if (m0 == NULL)
30247e27542aSGleb Smirnoff 				return (ENOBUFS);
30257e27542aSGleb Smirnoff 			m_head = m0;
30267e27542aSGleb Smirnoff 			error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag,
30277e27542aSGleb Smirnoff 			    map, m_head, segs, &nsegs, BUS_DMA_NOWAIT);
30287e27542aSGleb Smirnoff 		}
30297e27542aSGleb Smirnoff 		if (error)
30307e27542aSGleb Smirnoff 			return (error);
30317e27542aSGleb Smirnoff 	}
30327e27542aSGleb Smirnoff 
303395d67482SBill Paul 	/*
303495d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
303595d67482SBill Paul 	 * of the end of the ring.
303695d67482SBill Paul 	 */
30377e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
30387e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
303995d67482SBill Paul 		return (ENOBUFS);
30407e27542aSGleb Smirnoff 	}
30417e27542aSGleb Smirnoff 
3042e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3043e65bed95SPyun YongHyeon 
30447e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
30457e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
30467e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
30477e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
30487e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
30497e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
30507e27542aSGleb Smirnoff 		if (i == nsegs - 1)
30517e27542aSGleb Smirnoff 			break;
30527e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
30537e27542aSGleb Smirnoff 	}
30547e27542aSGleb Smirnoff 
30557e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
30567e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
30577e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
30587e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
30597e27542aSGleb Smirnoff 	if (mtag != NULL) {
30607e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
30617e27542aSGleb Smirnoff 		d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
30627e27542aSGleb Smirnoff 	} else
30637e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
3064f41ac2beSBill Paul 
3065f41ac2beSBill Paul 	/*
3066f41ac2beSBill Paul 	 * Insure that the map for this transmission
3067f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3068f41ac2beSBill Paul 	 * in this chain.
3069f41ac2beSBill Paul 	 */
30707e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
30717e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
30727e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m_head;
30737e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
307495d67482SBill Paul 
30757e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
30767e27542aSGleb Smirnoff 	*txidx = idx;
307795d67482SBill Paul 
307895d67482SBill Paul 	return (0);
307995d67482SBill Paul }
308095d67482SBill Paul 
308195d67482SBill Paul /*
308295d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
308395d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
308495d67482SBill Paul  */
308595d67482SBill Paul static void
30860f9bd73bSSam Leffler bge_start_locked(ifp)
308795d67482SBill Paul 	struct ifnet *ifp;
308895d67482SBill Paul {
308995d67482SBill Paul 	struct bge_softc *sc;
309095d67482SBill Paul 	struct mbuf *m_head = NULL;
309114bbd30fSGleb Smirnoff 	uint32_t prodidx;
3092303a718cSDag-Erling Smørgrav 	int count = 0;
309395d67482SBill Paul 
309495d67482SBill Paul 	sc = ifp->if_softc;
309595d67482SBill Paul 
3096dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
309795d67482SBill Paul 		return;
309895d67482SBill Paul 
309914bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
310095d67482SBill Paul 
310195d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
31024d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
310395d67482SBill Paul 		if (m_head == NULL)
310495d67482SBill Paul 			break;
310595d67482SBill Paul 
310695d67482SBill Paul 		/*
310795d67482SBill Paul 		 * XXX
3108b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3109b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3110b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3111b874fdd4SYaroslav Tykhiy 		 *
3112b874fdd4SYaroslav Tykhiy 		 * XXX
311395d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
311495d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
311595d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
311695d67482SBill Paul 		 * chain at once.
311795d67482SBill Paul 		 * (paranoia -- may not actually be needed)
311895d67482SBill Paul 		 */
311995d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
312095d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
312195d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
312295d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
31234d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
312413f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
312595d67482SBill Paul 				break;
312695d67482SBill Paul 			}
312795d67482SBill Paul 		}
312895d67482SBill Paul 
312995d67482SBill Paul 		/*
313095d67482SBill Paul 		 * Pack the data into the transmit ring. If we
313195d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
313295d67482SBill Paul 		 * for the NIC to drain the ring.
313395d67482SBill Paul 		 */
313495d67482SBill Paul 		if (bge_encap(sc, m_head, &prodidx)) {
31354d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
313613f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
313795d67482SBill Paul 			break;
313895d67482SBill Paul 		}
3139303a718cSDag-Erling Smørgrav 		++count;
314095d67482SBill Paul 
314195d67482SBill Paul 		/*
314295d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
314395d67482SBill Paul 		 * to him.
314495d67482SBill Paul 		 */
3145673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
314695d67482SBill Paul 	}
314795d67482SBill Paul 
3148303a718cSDag-Erling Smørgrav 	if (count == 0) {
3149303a718cSDag-Erling Smørgrav 		/* no packets were dequeued */
3150303a718cSDag-Erling Smørgrav 		return;
3151303a718cSDag-Erling Smørgrav 	}
3152303a718cSDag-Erling Smørgrav 
315395d67482SBill Paul 	/* Transmit */
315495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
31553927098fSPaul Saab 	/* 5700 b2 errata */
3156e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
31573927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
315895d67482SBill Paul 
315914bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
316014bbd30fSGleb Smirnoff 
316195d67482SBill Paul 	/*
316295d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
316395d67482SBill Paul 	 */
316495d67482SBill Paul 	ifp->if_timer = 5;
316595d67482SBill Paul 
316695d67482SBill Paul 	return;
316795d67482SBill Paul }
316895d67482SBill Paul 
31690f9bd73bSSam Leffler /*
31700f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
31710f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
31720f9bd73bSSam Leffler  */
317395d67482SBill Paul static void
31740f9bd73bSSam Leffler bge_start(ifp)
31750f9bd73bSSam Leffler 	struct ifnet *ifp;
317695d67482SBill Paul {
31770f9bd73bSSam Leffler 	struct bge_softc *sc;
31780f9bd73bSSam Leffler 
31790f9bd73bSSam Leffler 	sc = ifp->if_softc;
31800f9bd73bSSam Leffler 	BGE_LOCK(sc);
31810f9bd73bSSam Leffler 	bge_start_locked(ifp);
31820f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
31830f9bd73bSSam Leffler }
31840f9bd73bSSam Leffler 
31850f9bd73bSSam Leffler static void
31860f9bd73bSSam Leffler bge_init_locked(sc)
31870f9bd73bSSam Leffler 	struct bge_softc *sc;
31880f9bd73bSSam Leffler {
318995d67482SBill Paul 	struct ifnet *ifp;
319095d67482SBill Paul 	u_int16_t *m;
319195d67482SBill Paul 
31920f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
319395d67482SBill Paul 
3194fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
319595d67482SBill Paul 
319613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
319795d67482SBill Paul 		return;
319895d67482SBill Paul 
319995d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
320095d67482SBill Paul 	bge_stop(sc);
320195d67482SBill Paul 	bge_reset(sc);
320295d67482SBill Paul 	bge_chipinit(sc);
320395d67482SBill Paul 
320495d67482SBill Paul 	/*
320595d67482SBill Paul 	 * Init the various state machines, ring
320695d67482SBill Paul 	 * control blocks and firmware.
320795d67482SBill Paul 	 */
320895d67482SBill Paul 	if (bge_blockinit(sc)) {
3209fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
321095d67482SBill Paul 		return;
321195d67482SBill Paul 	}
321295d67482SBill Paul 
3213fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
321495d67482SBill Paul 
321595d67482SBill Paul 	/* Specify MTU. */
321695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3217859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
321895d67482SBill Paul 
321995d67482SBill Paul 	/* Load our MAC address. */
32204a0d6638SRuslan Ermilov 	m = (u_int16_t *)IF_LLADDR(sc->bge_ifp);
322195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
322295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
322395d67482SBill Paul 
322495d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
322595d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
322695d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
322795d67482SBill Paul 	} else {
322895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
322995d67482SBill Paul 	}
323095d67482SBill Paul 
323195d67482SBill Paul 	/* Program multicast filter. */
323295d67482SBill Paul 	bge_setmulti(sc);
323395d67482SBill Paul 
323495d67482SBill Paul 	/* Init RX ring. */
323595d67482SBill Paul 	bge_init_rx_ring_std(sc);
323695d67482SBill Paul 
32370434d1b8SBill Paul 	/*
32380434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
32390434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
32400434d1b8SBill Paul 	 * entry of the ring.
32410434d1b8SBill Paul 	 */
32420434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
32430434d1b8SBill Paul 		u_int32_t		v, i;
32440434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
32450434d1b8SBill Paul 			DELAY(20);
32460434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
32470434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
32480434d1b8SBill Paul 				break;
32490434d1b8SBill Paul 		}
32500434d1b8SBill Paul 		if (i == 10)
3251fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3252fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
32530434d1b8SBill Paul 	}
32540434d1b8SBill Paul 
325595d67482SBill Paul 	/* Init jumbo RX ring. */
325695d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
325795d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
325895d67482SBill Paul 
325995d67482SBill Paul 	/* Init our RX return ring index */
326095d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
326195d67482SBill Paul 
326295d67482SBill Paul 	/* Init TX ring. */
326395d67482SBill Paul 	bge_init_tx_ring(sc);
326495d67482SBill Paul 
326595d67482SBill Paul 	/* Turn on transmitter */
326695d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
326795d67482SBill Paul 
326895d67482SBill Paul 	/* Turn on receiver */
326995d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
327095d67482SBill Paul 
327195d67482SBill Paul 	/* Tell firmware we're alive. */
327295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
327395d67482SBill Paul 
327475719184SGleb Smirnoff #ifdef DEVICE_POLLING
327575719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
327675719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
327775719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
327875719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
327975719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
328075719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
328175719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
328275719184SGleb Smirnoff 	} else
328375719184SGleb Smirnoff #endif
328475719184SGleb Smirnoff 
328595d67482SBill Paul 	/* Enable host interrupts. */
328675719184SGleb Smirnoff 	{
328795d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
328895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
328995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
329075719184SGleb Smirnoff 	}
329195d67482SBill Paul 
329295d67482SBill Paul 	bge_ifmedia_upd(ifp);
329395d67482SBill Paul 
329413f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
329513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
329695d67482SBill Paul 
32970f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
32980f9bd73bSSam Leffler }
32990f9bd73bSSam Leffler 
33000f9bd73bSSam Leffler static void
33010f9bd73bSSam Leffler bge_init(xsc)
33020f9bd73bSSam Leffler 	void *xsc;
33030f9bd73bSSam Leffler {
33040f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
33050f9bd73bSSam Leffler 
33060f9bd73bSSam Leffler 	BGE_LOCK(sc);
33070f9bd73bSSam Leffler 	bge_init_locked(sc);
33080f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
330995d67482SBill Paul 
331095d67482SBill Paul 	return;
331195d67482SBill Paul }
331295d67482SBill Paul 
331395d67482SBill Paul /*
331495d67482SBill Paul  * Set media options.
331595d67482SBill Paul  */
331695d67482SBill Paul static int
331795d67482SBill Paul bge_ifmedia_upd(ifp)
331895d67482SBill Paul 	struct ifnet *ifp;
331995d67482SBill Paul {
332095d67482SBill Paul 	struct bge_softc *sc;
332195d67482SBill Paul 	struct mii_data *mii;
332295d67482SBill Paul 	struct ifmedia *ifm;
332395d67482SBill Paul 
332495d67482SBill Paul 	sc = ifp->if_softc;
332595d67482SBill Paul 	ifm = &sc->bge_ifmedia;
332695d67482SBill Paul 
332795d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
332895d67482SBill Paul 	if (sc->bge_tbi) {
332995d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
333095d67482SBill Paul 			return(EINVAL);
333195d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
333295d67482SBill Paul 		case IFM_AUTO:
3333ff50922bSDoug White #ifndef BGE_FAKE_AUTONEG
3334ff50922bSDoug White 			/*
3335ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3336ff50922bSDoug White 			 * mechanism for programming the autoneg
3337ff50922bSDoug White 			 * advertisement registers in TBI mode.
3338ff50922bSDoug White 			 */
3339ff50922bSDoug White 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3340ff50922bSDoug White 				uint32_t sgdig;
3341ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3342ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3343ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3344ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3345ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3346ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3347ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3348ff50922bSDoug White 				DELAY(5);
3349ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3350ff50922bSDoug White 			}
3351ff50922bSDoug White #endif
335295d67482SBill Paul 			break;
335395d67482SBill Paul 		case IFM_1000_SX:
335495d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
335595d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
335695d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
335795d67482SBill Paul 			} else {
335895d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
335995d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
336095d67482SBill Paul 			}
336195d67482SBill Paul 			break;
336295d67482SBill Paul 		default:
336395d67482SBill Paul 			return(EINVAL);
336495d67482SBill Paul 		}
336595d67482SBill Paul 		return(0);
336695d67482SBill Paul 	}
336795d67482SBill Paul 
336895d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
336995d67482SBill Paul 	if (mii->mii_instance) {
337095d67482SBill Paul 		struct mii_softc *miisc;
337195d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
337295d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
337395d67482SBill Paul 			mii_phy_reset(miisc);
337495d67482SBill Paul 	}
337595d67482SBill Paul 	mii_mediachg(mii);
337695d67482SBill Paul 
337795d67482SBill Paul 	return(0);
337895d67482SBill Paul }
337995d67482SBill Paul 
338095d67482SBill Paul /*
338195d67482SBill Paul  * Report current media status.
338295d67482SBill Paul  */
338395d67482SBill Paul static void
338495d67482SBill Paul bge_ifmedia_sts(ifp, ifmr)
338595d67482SBill Paul 	struct ifnet *ifp;
338695d67482SBill Paul 	struct ifmediareq *ifmr;
338795d67482SBill Paul {
338895d67482SBill Paul 	struct bge_softc *sc;
338995d67482SBill Paul 	struct mii_data *mii;
339095d67482SBill Paul 
339195d67482SBill Paul 	sc = ifp->if_softc;
339295d67482SBill Paul 
339395d67482SBill Paul 	if (sc->bge_tbi) {
339495d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
339595d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
339695d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
339795d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
339895d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
339995d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
340095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
340195d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
340295d67482SBill Paul 		else
340395d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
340495d67482SBill Paul 		return;
340595d67482SBill Paul 	}
340695d67482SBill Paul 
340795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
340895d67482SBill Paul 	mii_pollstat(mii);
340995d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
341095d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
341195d67482SBill Paul 
341295d67482SBill Paul 	return;
341395d67482SBill Paul }
341495d67482SBill Paul 
341595d67482SBill Paul static int
341695d67482SBill Paul bge_ioctl(ifp, command, data)
341795d67482SBill Paul 	struct ifnet *ifp;
341895d67482SBill Paul 	u_long command;
341995d67482SBill Paul 	caddr_t data;
342095d67482SBill Paul {
342195d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
342295d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
34230f9bd73bSSam Leffler 	int mask, error = 0;
342495d67482SBill Paul 	struct mii_data *mii;
342595d67482SBill Paul 
342695d67482SBill Paul 	switch(command) {
342795d67482SBill Paul 	case SIOCSIFMTU:
34280434d1b8SBill Paul 		/* Disallow jumbo frames on 5705. */
3429e53d81eeSPaul Saab 		if (((sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
3430e53d81eeSPaul Saab 		      sc->bge_asicrev == BGE_ASICREV_BCM5750) &&
34310434d1b8SBill Paul 		    ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU)
343295d67482SBill Paul 			error = EINVAL;
343395d67482SBill Paul 		else {
343495d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
343513f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
343695d67482SBill Paul 			bge_init(sc);
343795d67482SBill Paul 		}
343895d67482SBill Paul 		break;
343995d67482SBill Paul 	case SIOCSIFFLAGS:
34400f9bd73bSSam Leffler 		BGE_LOCK(sc);
344195d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
344295d67482SBill Paul 			/*
344395d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
344495d67482SBill Paul 			 * then just use the 'set promisc mode' command
344595d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
344695d67482SBill Paul 			 * a full re-init means reloading the firmware and
344795d67482SBill Paul 			 * waiting for it to start up, which may take a
344895d67482SBill Paul 			 * second or two.
344995d67482SBill Paul 			 */
345013f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
345195d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
345295d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
345395d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
345495d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
345513f4c340SRobert Watson 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
345695d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
345795d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
345895d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
345995d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
346095d67482SBill Paul 			} else
34610f9bd73bSSam Leffler 				bge_init_locked(sc);
346295d67482SBill Paul 		} else {
346313f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
346495d67482SBill Paul 				bge_stop(sc);
346595d67482SBill Paul 			}
346695d67482SBill Paul 		}
346795d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
34680f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
346995d67482SBill Paul 		error = 0;
347095d67482SBill Paul 		break;
347195d67482SBill Paul 	case SIOCADDMULTI:
347295d67482SBill Paul 	case SIOCDELMULTI:
347313f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
34740f9bd73bSSam Leffler 			BGE_LOCK(sc);
347595d67482SBill Paul 			bge_setmulti(sc);
34760f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
347795d67482SBill Paul 			error = 0;
347895d67482SBill Paul 		}
347995d67482SBill Paul 		break;
348095d67482SBill Paul 	case SIOCSIFMEDIA:
348195d67482SBill Paul 	case SIOCGIFMEDIA:
348295d67482SBill Paul 		if (sc->bge_tbi) {
348395d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
348495d67482SBill Paul 			    &sc->bge_ifmedia, command);
348595d67482SBill Paul 		} else {
348695d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
348795d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
348895d67482SBill Paul 			    &mii->mii_media, command);
348995d67482SBill Paul 		}
349095d67482SBill Paul 		break;
349195d67482SBill Paul 	case SIOCSIFCAP:
349295d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
349375719184SGleb Smirnoff #ifdef DEVICE_POLLING
349475719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
349575719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
349675719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
349775719184SGleb Smirnoff 				if (error)
349875719184SGleb Smirnoff 					return(error);
349975719184SGleb Smirnoff 				BGE_LOCK(sc);
350075719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
350175719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
350275719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
350375719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
350475719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
350575719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
350675719184SGleb Smirnoff 				BGE_UNLOCK(sc);
350775719184SGleb Smirnoff 			} else {
350875719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
350975719184SGleb Smirnoff 				/* Enable interrupt even in error case */
351075719184SGleb Smirnoff 				BGE_LOCK(sc);
351175719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
351275719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
351375719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
351475719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
351575719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
351675719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
351775719184SGleb Smirnoff 				BGE_UNLOCK(sc);
351875719184SGleb Smirnoff 			}
351975719184SGleb Smirnoff 		}
352075719184SGleb Smirnoff #endif
3521d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3522d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3523d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3524d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3525b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
352695d67482SBill Paul 			else
3527b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
352895d67482SBill Paul 		}
352995d67482SBill Paul 		break;
353095d67482SBill Paul 	default:
3531673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
353295d67482SBill Paul 		break;
353395d67482SBill Paul 	}
353495d67482SBill Paul 
353595d67482SBill Paul 	return(error);
353695d67482SBill Paul }
353795d67482SBill Paul 
353895d67482SBill Paul static void
353995d67482SBill Paul bge_watchdog(ifp)
354095d67482SBill Paul 	struct ifnet *ifp;
354195d67482SBill Paul {
354295d67482SBill Paul 	struct bge_softc *sc;
354395d67482SBill Paul 
354495d67482SBill Paul 	sc = ifp->if_softc;
354595d67482SBill Paul 
3546fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
354795d67482SBill Paul 
354813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
354995d67482SBill Paul 	bge_init(sc);
355095d67482SBill Paul 
355195d67482SBill Paul 	ifp->if_oerrors++;
355295d67482SBill Paul 
355395d67482SBill Paul 	return;
355495d67482SBill Paul }
355595d67482SBill Paul 
355695d67482SBill Paul /*
355795d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
355895d67482SBill Paul  * RX and TX lists.
355995d67482SBill Paul  */
356095d67482SBill Paul static void
356195d67482SBill Paul bge_stop(sc)
356295d67482SBill Paul 	struct bge_softc *sc;
356395d67482SBill Paul {
356495d67482SBill Paul 	struct ifnet *ifp;
356595d67482SBill Paul 	struct ifmedia_entry *ifm;
356695d67482SBill Paul 	struct mii_data *mii = NULL;
356795d67482SBill Paul 	int mtmp, itmp;
356895d67482SBill Paul 
35690f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
35700f9bd73bSSam Leffler 
3571fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
357295d67482SBill Paul 
357395d67482SBill Paul 	if (!sc->bge_tbi)
357495d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
357595d67482SBill Paul 
35760f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
357795d67482SBill Paul 
357895d67482SBill Paul 	/*
357995d67482SBill Paul 	 * Disable all of the receiver blocks
358095d67482SBill Paul 	 */
358195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
358295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
358395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
35845dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3585e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
358695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
358795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
358895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
358995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
359095d67482SBill Paul 
359195d67482SBill Paul 	/*
359295d67482SBill Paul 	 * Disable all of the transmit blocks
359395d67482SBill Paul 	 */
359495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
359595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
359695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
359795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
359895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
35995dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3600e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
360195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
360295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
360395d67482SBill Paul 
360495d67482SBill Paul 	/*
360595d67482SBill Paul 	 * Shut down all of the memory managers and related
360695d67482SBill Paul 	 * state machines.
360795d67482SBill Paul 	 */
360895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
360995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
36105dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3611e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
361295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
361395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
361495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
36155dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3616e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
361795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
361895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
36190434d1b8SBill Paul 	}
362095d67482SBill Paul 
362195d67482SBill Paul 	/* Disable host interrupts. */
362295d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
362395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
362495d67482SBill Paul 
362595d67482SBill Paul 	/*
362695d67482SBill Paul 	 * Tell firmware we're shutting down.
362795d67482SBill Paul 	 */
362895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
362995d67482SBill Paul 
363095d67482SBill Paul 	/* Free the RX lists. */
363195d67482SBill Paul 	bge_free_rx_ring_std(sc);
363295d67482SBill Paul 
363395d67482SBill Paul 	/* Free jumbo RX list. */
36345dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3635e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
363695d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
363795d67482SBill Paul 
363895d67482SBill Paul 	/* Free TX buffers. */
363995d67482SBill Paul 	bge_free_tx_ring(sc);
364095d67482SBill Paul 
364195d67482SBill Paul 	/*
364295d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
364395d67482SBill Paul 	 * unchanged so that things will be put back to normal when
364495d67482SBill Paul 	 * we bring the interface back up.
364595d67482SBill Paul 	 */
364695d67482SBill Paul 	if (!sc->bge_tbi) {
364795d67482SBill Paul 		itmp = ifp->if_flags;
364895d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3649dcc34049SPawel Jakub Dawidek 		/*
3650dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3651dcc34049SPawel Jakub Dawidek 		 */
3652dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
365395d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
365495d67482SBill Paul 			mtmp = ifm->ifm_media;
365595d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
365695d67482SBill Paul 			mii_mediachg(mii);
365795d67482SBill Paul 			ifm->ifm_media = mtmp;
3658dcc34049SPawel Jakub Dawidek 		}
365995d67482SBill Paul 		ifp->if_flags = itmp;
366095d67482SBill Paul 	}
366195d67482SBill Paul 
366295d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
366395d67482SBill Paul 
366413f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
366595d67482SBill Paul 
366695d67482SBill Paul 	return;
366795d67482SBill Paul }
366895d67482SBill Paul 
366995d67482SBill Paul /*
367095d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
367195d67482SBill Paul  * get confused by errant DMAs when rebooting.
367295d67482SBill Paul  */
367395d67482SBill Paul static void
367495d67482SBill Paul bge_shutdown(dev)
367595d67482SBill Paul 	device_t dev;
367695d67482SBill Paul {
367795d67482SBill Paul 	struct bge_softc *sc;
367895d67482SBill Paul 
367995d67482SBill Paul 	sc = device_get_softc(dev);
368095d67482SBill Paul 
36810f9bd73bSSam Leffler 	BGE_LOCK(sc);
368295d67482SBill Paul 	bge_stop(sc);
368395d67482SBill Paul 	bge_reset(sc);
36840f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
368595d67482SBill Paul 
368695d67482SBill Paul 	return;
368795d67482SBill Paul }
368814afefa3SPawel Jakub Dawidek 
368914afefa3SPawel Jakub Dawidek static int
369014afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
369114afefa3SPawel Jakub Dawidek {
369214afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
369314afefa3SPawel Jakub Dawidek 
369414afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
369514afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
369614afefa3SPawel Jakub Dawidek 	bge_stop(sc);
369714afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
369814afefa3SPawel Jakub Dawidek 
369914afefa3SPawel Jakub Dawidek 	return (0);
370014afefa3SPawel Jakub Dawidek }
370114afefa3SPawel Jakub Dawidek 
370214afefa3SPawel Jakub Dawidek static int
370314afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
370414afefa3SPawel Jakub Dawidek {
370514afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
370614afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
370714afefa3SPawel Jakub Dawidek 
370814afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
370914afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
371014afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
371114afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
371214afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
371314afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
371414afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
371514afefa3SPawel Jakub Dawidek 	}
371614afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
371714afefa3SPawel Jakub Dawidek 
371814afefa3SPawel Jakub Dawidek 	return (0);
371914afefa3SPawel Jakub Dawidek }
3720dab5cd05SOleg Bulyzhin 
3721dab5cd05SOleg Bulyzhin static void
3722dab5cd05SOleg Bulyzhin bge_link_upd(sc)
3723dab5cd05SOleg Bulyzhin 	struct bge_softc *sc;
3724dab5cd05SOleg Bulyzhin {
37251f313773SOleg Bulyzhin 	struct mii_data *mii;
37261f313773SOleg Bulyzhin 	uint32_t link, status;
3727dab5cd05SOleg Bulyzhin 
3728dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
37291f313773SOleg Bulyzhin 
3730dab5cd05SOleg Bulyzhin 	/*
3731dab5cd05SOleg Bulyzhin 	 * Process link state changes.
3732dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
3733dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
3734dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
3735dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
3736dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
3737dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
3738dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
3739dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
37401f313773SOleg Bulyzhin 	 *
37411f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
37421f313773SOleg Bulyzhin 	 * BGE_CHIPID_BCM5700_B1 can be used for others BCM5700 revisions.
3743dab5cd05SOleg Bulyzhin 	 */
3744dab5cd05SOleg Bulyzhin 
37451f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
37461f313773SOleg Bulyzhin 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) {
3747dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
3748dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
3749dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3750dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
37511f313773SOleg Bulyzhin 
37521f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
37531f313773SOleg Bulyzhin 			if (!sc->bge_link &&
37541f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
37551f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
37561f313773SOleg Bulyzhin 				sc->bge_link++;
37571f313773SOleg Bulyzhin 				if (bootverbose)
37581f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
37591f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
37601f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
37611f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
37621f313773SOleg Bulyzhin 				sc->bge_link = 0;
37631f313773SOleg Bulyzhin 				if (bootverbose)
37641f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
37651f313773SOleg Bulyzhin 			}
37661f313773SOleg Bulyzhin 
3767dab5cd05SOleg Bulyzhin 			/* Clear the interrupt */
3768dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
3769dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
3770dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
3771dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
3772dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
3773dab5cd05SOleg Bulyzhin 		}
3774dab5cd05SOleg Bulyzhin 		return;
3775dab5cd05SOleg Bulyzhin 	}
3776dab5cd05SOleg Bulyzhin 
37771f313773SOleg Bulyzhin 	if (sc->bge_tbi) {
3778dab5cd05SOleg Bulyzhin 		/*
3779dab5cd05SOleg Bulyzhin 		 * Sometimes PCS encoding errors are detected in
3780dab5cd05SOleg Bulyzhin 		 * TBI mode (on fiber NICs), and for some reason
3781dab5cd05SOleg Bulyzhin 		 * the chip will signal them as link changes.
3782dab5cd05SOleg Bulyzhin 		 * If we get a link change event, but the 'PCS
3783dab5cd05SOleg Bulyzhin 		 * encoding error' bit in the MAC status register
3784dab5cd05SOleg Bulyzhin 		 * is set, don't bother doing a link check.
37851f313773SOleg Bulyzhin 		 * This avoids spurious "link UP" messages
3786dab5cd05SOleg Bulyzhin 		 * that sometimes appear on fiber NICs during
3787dab5cd05SOleg Bulyzhin 		 * periods of heavy traffic. (There should be no
3788dab5cd05SOleg Bulyzhin 		 * effect on copper NICs.)
3789dab5cd05SOleg Bulyzhin 		 */
37901f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
37911f313773SOleg Bulyzhin 		if (!(status & (BGE_MACSTAT_PORT_DECODE_ERROR|
37921f313773SOleg Bulyzhin 		    BGE_MACSTAT_MI_COMPLETE))) {
37931f313773SOleg Bulyzhin 			if (!sc->bge_link &&
37941f313773SOleg Bulyzhin 			    (status & BGE_MACSTAT_TBI_PCS_SYNCHED)) {
37951f313773SOleg Bulyzhin 				sc->bge_link++;
37961f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
37971f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
37981f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
37991f313773SOleg Bulyzhin 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
38001f313773SOleg Bulyzhin 				if (bootverbose)
38011f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
38021f313773SOleg Bulyzhin 			} else if (sc->bge_link) {
3803dab5cd05SOleg Bulyzhin 				sc->bge_link = 0;
38041f313773SOleg Bulyzhin 				if (bootverbose)
38051f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
38061f313773SOleg Bulyzhin 			}
38071f313773SOleg Bulyzhin 		}
38081f313773SOleg Bulyzhin 	} else {
38091f313773SOleg Bulyzhin 		/*
38101f313773SOleg Bulyzhin 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
38111f313773SOleg Bulyzhin 		 * in status word always set. Workaround this bug by reading
38121f313773SOleg Bulyzhin 		 * PHY link status directly.
38131f313773SOleg Bulyzhin 		 */
38141f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
38151f313773SOleg Bulyzhin 
38161f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
38171f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
3818dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3819dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
38201f313773SOleg Bulyzhin 
38211f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
38221f313773SOleg Bulyzhin 			if (!sc->bge_link &&
38231f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
38241f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
38251f313773SOleg Bulyzhin 				sc->bge_link++;
38261f313773SOleg Bulyzhin 				if (bootverbose)
38271f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
38281f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
38291f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
38301f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
38311f313773SOleg Bulyzhin 				sc->bge_link = 0;
38321f313773SOleg Bulyzhin 				if (bootverbose)
38331f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
38341f313773SOleg Bulyzhin 			}
38351f313773SOleg Bulyzhin 		}
3836dab5cd05SOleg Bulyzhin 	}
3837dab5cd05SOleg Bulyzhin 
3838dab5cd05SOleg Bulyzhin 	/* Clear the interrupt */
3839dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
3840dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
3841dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
3842dab5cd05SOleg Bulyzhin }
3843