xref: /freebsd/sys/dev/bge/if_bge.c (revision d375e52490ccc3114f46bc5661e1a96d49fe5209)
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) {
41495d67482SBill Paul 		printf("bge%d: VPD read timed out\n", sc->bge_unit);
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) {
45695d67482SBill Paul 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
45795d67482SBill Paul 			sc->bge_unit, VPD_RES_ID, res.vr_id);
45895d67482SBill Paul 		return;
45995d67482SBill Paul 	}
46095d67482SBill Paul 
46195d67482SBill Paul 	pos += sizeof(res);
46295d67482SBill Paul 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
46395d67482SBill Paul 	for (i = 0; i < res.vr_len; i++)
46495d67482SBill Paul 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
46595d67482SBill Paul 	sc->bge_vpd_prodname[i] = '\0';
46695d67482SBill Paul 	pos += i;
46795d67482SBill Paul 
46895d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
46995d67482SBill Paul 
47095d67482SBill Paul 	if (res.vr_id != VPD_RES_READ) {
47195d67482SBill Paul 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
47295d67482SBill Paul 		    sc->bge_unit, VPD_RES_READ, res.vr_id);
47395d67482SBill Paul 		return;
47495d67482SBill Paul 	}
47595d67482SBill Paul 
47695d67482SBill Paul 	pos += sizeof(res);
47795d67482SBill Paul 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
47895d67482SBill Paul 	for (i = 0; i < res.vr_len + 1; i++)
47995d67482SBill Paul 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
48095d67482SBill Paul 
48195d67482SBill Paul 	return;
48295d67482SBill Paul }
4831b4a3b2fSPeter Wemm #endif
48495d67482SBill Paul 
48595d67482SBill Paul /*
48695d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
48795d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
48895d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
48995d67482SBill Paul  * access method.
49095d67482SBill Paul  */
49195d67482SBill Paul static u_int8_t
49295d67482SBill Paul bge_eeprom_getbyte(sc, addr, dest)
49395d67482SBill Paul 	struct bge_softc *sc;
49495d67482SBill Paul 	int addr;
49595d67482SBill Paul 	u_int8_t *dest;
49695d67482SBill Paul {
49795d67482SBill Paul 	int i;
49895d67482SBill Paul 	u_int32_t byte = 0;
49995d67482SBill Paul 
50095d67482SBill Paul 	/*
50195d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
50295d67482SBill Paul 	 * having to use the bitbang method.
50395d67482SBill Paul 	 */
50495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
50595d67482SBill Paul 
50695d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
50795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
50895d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
50995d67482SBill Paul 	DELAY(20);
51095d67482SBill Paul 
51195d67482SBill Paul 	/* Issue the read EEPROM command. */
51295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
51395d67482SBill Paul 
51495d67482SBill Paul 	/* Wait for completion */
51595d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
51695d67482SBill Paul 		DELAY(10);
51795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
51895d67482SBill Paul 			break;
51995d67482SBill Paul 	}
52095d67482SBill Paul 
52195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
52295d67482SBill Paul 		printf("bge%d: eeprom read timed out\n", sc->bge_unit);
523f6789fbaSPyun YongHyeon 		return(1);
52495d67482SBill Paul 	}
52595d67482SBill Paul 
52695d67482SBill Paul 	/* Get result. */
52795d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
52895d67482SBill Paul 
52995d67482SBill Paul 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
53095d67482SBill Paul 
53195d67482SBill Paul 	return(0);
53295d67482SBill Paul }
53395d67482SBill Paul 
53495d67482SBill Paul /*
53595d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
53695d67482SBill Paul  */
53795d67482SBill Paul static int
53895d67482SBill Paul bge_read_eeprom(sc, dest, off, cnt)
53995d67482SBill Paul 	struct bge_softc *sc;
54095d67482SBill Paul 	caddr_t dest;
54195d67482SBill Paul 	int off;
54295d67482SBill Paul 	int cnt;
54395d67482SBill Paul {
54495d67482SBill Paul 	int err = 0, i;
54595d67482SBill Paul 	u_int8_t byte = 0;
54695d67482SBill Paul 
54795d67482SBill Paul 	for (i = 0; i < cnt; i++) {
54895d67482SBill Paul 		err = bge_eeprom_getbyte(sc, off + i, &byte);
54995d67482SBill Paul 		if (err)
55095d67482SBill Paul 			break;
55195d67482SBill Paul 		*(dest + i) = byte;
55295d67482SBill Paul 	}
55395d67482SBill Paul 
55495d67482SBill Paul 	return(err ? 1 : 0);
55595d67482SBill Paul }
55695d67482SBill Paul 
55795d67482SBill Paul static int
55895d67482SBill Paul bge_miibus_readreg(dev, phy, reg)
55995d67482SBill Paul 	device_t dev;
56095d67482SBill Paul 	int phy, reg;
56195d67482SBill Paul {
56295d67482SBill Paul 	struct bge_softc *sc;
56337ceeb4dSPaul Saab 	u_int32_t val, autopoll;
56495d67482SBill Paul 	int i;
56595d67482SBill Paul 
56695d67482SBill Paul 	sc = device_get_softc(dev);
56795d67482SBill Paul 
5680434d1b8SBill Paul 	/*
5690434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5700434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5710434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5720434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5730434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5740434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5750434d1b8SBill Paul 	 * special-cased.
5760434d1b8SBill Paul 	 */
577b1265c1aSJohn Polstra 	if (phy != 1)
57898b28ee5SBill Paul 		return(0);
57998b28ee5SBill Paul 
58037ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
58137ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
58237ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
58337ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
58437ceeb4dSPaul Saab 		DELAY(40);
58537ceeb4dSPaul Saab 	}
58637ceeb4dSPaul Saab 
58795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
58895d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
58995d67482SBill Paul 
59095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
59195d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
59295d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
59395d67482SBill Paul 			break;
59495d67482SBill Paul 	}
59595d67482SBill Paul 
59695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
59795d67482SBill Paul 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
59837ceeb4dSPaul Saab 		val = 0;
59937ceeb4dSPaul Saab 		goto done;
60095d67482SBill Paul 	}
60195d67482SBill Paul 
60295d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
60395d67482SBill Paul 
60437ceeb4dSPaul Saab done:
60537ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
60637ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
60737ceeb4dSPaul Saab 		DELAY(40);
60837ceeb4dSPaul Saab 	}
60937ceeb4dSPaul Saab 
61095d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
61195d67482SBill Paul 		return(0);
61295d67482SBill Paul 
61395d67482SBill Paul 	return(val & 0xFFFF);
61495d67482SBill Paul }
61595d67482SBill Paul 
61695d67482SBill Paul static int
61795d67482SBill Paul bge_miibus_writereg(dev, phy, reg, val)
61895d67482SBill Paul 	device_t dev;
61995d67482SBill Paul 	int phy, reg, val;
62095d67482SBill Paul {
62195d67482SBill Paul 	struct bge_softc *sc;
62237ceeb4dSPaul Saab 	u_int32_t autopoll;
62395d67482SBill Paul 	int i;
62495d67482SBill Paul 
62595d67482SBill Paul 	sc = device_get_softc(dev);
62695d67482SBill Paul 
62737ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
62837ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
62937ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
63037ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
63137ceeb4dSPaul Saab 		DELAY(40);
63237ceeb4dSPaul Saab 	}
63337ceeb4dSPaul Saab 
63495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
63595d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
63695d67482SBill Paul 
63795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
63895d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
63995d67482SBill Paul 			break;
64095d67482SBill Paul 	}
64195d67482SBill Paul 
64237ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
64337ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
64437ceeb4dSPaul Saab 		DELAY(40);
64537ceeb4dSPaul Saab 	}
64637ceeb4dSPaul Saab 
64795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
64895d67482SBill Paul 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
64995d67482SBill Paul 		return(0);
65095d67482SBill Paul 	}
65195d67482SBill Paul 
65295d67482SBill Paul 	return(0);
65395d67482SBill Paul }
65495d67482SBill Paul 
65595d67482SBill Paul static void
65695d67482SBill Paul bge_miibus_statchg(dev)
65795d67482SBill Paul 	device_t dev;
65895d67482SBill Paul {
65995d67482SBill Paul 	struct bge_softc *sc;
66095d67482SBill Paul 	struct mii_data *mii;
66195d67482SBill Paul 
66295d67482SBill Paul 	sc = device_get_softc(dev);
66395d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
66495d67482SBill Paul 
66595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
666b418ad5cSPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
66795d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
66895d67482SBill Paul 	} else {
66995d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
67095d67482SBill Paul 	}
67195d67482SBill Paul 
67295d67482SBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
67395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
67495d67482SBill Paul 	} else {
67595d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
67695d67482SBill Paul 	}
67795d67482SBill Paul 
67895d67482SBill Paul 	return;
67995d67482SBill Paul }
68095d67482SBill Paul 
68195d67482SBill Paul /*
68295d67482SBill Paul  * Intialize a standard receive ring descriptor.
68395d67482SBill Paul  */
68495d67482SBill Paul static int
68595d67482SBill Paul bge_newbuf_std(sc, i, m)
68695d67482SBill Paul 	struct bge_softc	*sc;
68795d67482SBill Paul 	int			i;
68895d67482SBill Paul 	struct mbuf		*m;
68995d67482SBill Paul {
69095d67482SBill Paul 	struct mbuf		*m_new = NULL;
69195d67482SBill Paul 	struct bge_rx_bd	*r;
692f41ac2beSBill Paul 	struct bge_dmamap_arg	ctx;
693f41ac2beSBill Paul 	int			error;
69495d67482SBill Paul 
69595d67482SBill Paul 	if (m == NULL) {
696a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
69795d67482SBill Paul 		if (m_new == NULL) {
69895d67482SBill Paul 			return(ENOBUFS);
69995d67482SBill Paul 		}
70095d67482SBill Paul 
701a163d034SWarner Losh 		MCLGET(m_new, M_DONTWAIT);
70295d67482SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
70395d67482SBill Paul 			m_freem(m_new);
70495d67482SBill Paul 			return(ENOBUFS);
70595d67482SBill Paul 		}
70695d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70795d67482SBill Paul 	} else {
70895d67482SBill Paul 		m_new = m;
70995d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
71095d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
71195d67482SBill Paul 	}
71295d67482SBill Paul 
713e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
71495d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
71595d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
716f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
717f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
718f41ac2beSBill Paul 	ctx.sc = sc;
719f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
720f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
721f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
722f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
723f7cea149SGleb Smirnoff 		if (m == NULL) {
724f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
725f41ac2beSBill Paul 			m_freem(m_new);
726f7cea149SGleb Smirnoff 		}
727f41ac2beSBill Paul 		return(ENOMEM);
728f41ac2beSBill Paul 	}
729e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
730e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
731e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
732e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
733e907febfSPyun YongHyeon 	r->bge_idx = i;
734f41ac2beSBill Paul 
735f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
736f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
737f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
73895d67482SBill Paul 
73995d67482SBill Paul 	return(0);
74095d67482SBill Paul }
74195d67482SBill Paul 
74295d67482SBill Paul /*
74395d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
74495d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
74595d67482SBill Paul  */
74695d67482SBill Paul static int
74795d67482SBill Paul bge_newbuf_jumbo(sc, i, m)
74895d67482SBill Paul 	struct bge_softc *sc;
74995d67482SBill Paul 	int i;
75095d67482SBill Paul 	struct mbuf *m;
75195d67482SBill Paul {
7521be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
7531be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
75495d67482SBill Paul 	struct mbuf *m_new = NULL;
7551be6acb7SGleb Smirnoff 	int nsegs;
756f41ac2beSBill Paul 	int error;
75795d67482SBill Paul 
75895d67482SBill Paul 	if (m == NULL) {
759a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
7601be6acb7SGleb Smirnoff 		if (m_new == NULL)
76195d67482SBill Paul 			return(ENOBUFS);
76295d67482SBill Paul 
7631be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7641be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
76595d67482SBill Paul 			m_freem(m_new);
76695d67482SBill Paul 			return(ENOBUFS);
76795d67482SBill Paul 		}
7681be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
76995d67482SBill Paul 	} else {
77095d67482SBill Paul 		m_new = m;
7711be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
77295d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
77395d67482SBill Paul 	}
77495d67482SBill Paul 
775e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
77695d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7771be6acb7SGleb Smirnoff 
7781be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7791be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7801be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7811be6acb7SGleb Smirnoff 	if (error) {
7821be6acb7SGleb Smirnoff 		if (m == NULL)
783f41ac2beSBill Paul 			m_freem(m_new);
7841be6acb7SGleb Smirnoff 		return(error);
785f7cea149SGleb Smirnoff 	}
7861be6acb7SGleb Smirnoff 	KASSERT(nsegs == BGE_NSEG_JUMBO, ("%s: %d segments", __func__, nsegs));
7871be6acb7SGleb 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];
794e907febfSPyun YongHyeon 	r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
795e907febfSPyun YongHyeon 	r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
796e907febfSPyun YongHyeon 	r->bge_len0 = segs[0].ds_len;
797e907febfSPyun YongHyeon 	r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
798e907febfSPyun YongHyeon 	r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
799e907febfSPyun YongHyeon 	r->bge_len1 = segs[1].ds_len;
800e907febfSPyun YongHyeon 	r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
801e907febfSPyun YongHyeon 	r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
802e907febfSPyun YongHyeon 	r->bge_len2 = segs[2].ds_len;
803e907febfSPyun YongHyeon 	r->bge_len3 = 0;
804e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING|BGE_RXBDFLAG_END;
805e907febfSPyun YongHyeon 	r->bge_idx = i;
806f41ac2beSBill Paul 
807f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
808f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
809f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
81095d67482SBill Paul 
81195d67482SBill Paul 	return (0);
81295d67482SBill Paul }
81395d67482SBill Paul 
81495d67482SBill Paul /*
81595d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
81695d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
81795d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
81895d67482SBill Paul  * the NIC.
81995d67482SBill Paul  */
82095d67482SBill Paul static int
82195d67482SBill Paul bge_init_rx_ring_std(sc)
82295d67482SBill Paul 	struct bge_softc *sc;
82395d67482SBill Paul {
82495d67482SBill Paul 	int i;
82595d67482SBill Paul 
82695d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
82795d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
82895d67482SBill Paul 			return(ENOBUFS);
82995d67482SBill Paul 	};
83095d67482SBill Paul 
831f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
832f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
833f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
834f41ac2beSBill Paul 
83595d67482SBill Paul 	sc->bge_std = i - 1;
83695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
83795d67482SBill Paul 
83895d67482SBill Paul 	return(0);
83995d67482SBill Paul }
84095d67482SBill Paul 
84195d67482SBill Paul static void
84295d67482SBill Paul bge_free_rx_ring_std(sc)
84395d67482SBill Paul 	struct bge_softc *sc;
84495d67482SBill Paul {
84595d67482SBill Paul 	int i;
84695d67482SBill Paul 
84795d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
84895d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
849e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
850e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
851e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
852f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
853f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
854e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
855e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
85695d67482SBill Paul 		}
857f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
85895d67482SBill Paul 		    sizeof(struct bge_rx_bd));
85995d67482SBill Paul 	}
86095d67482SBill Paul 
86195d67482SBill Paul 	return;
86295d67482SBill Paul }
86395d67482SBill Paul 
86495d67482SBill Paul static int
86595d67482SBill Paul bge_init_rx_ring_jumbo(sc)
86695d67482SBill Paul 	struct bge_softc *sc;
86795d67482SBill Paul {
86895d67482SBill Paul 	struct bge_rcb *rcb;
8691be6acb7SGleb Smirnoff 	int i;
87095d67482SBill Paul 
87195d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
87295d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
87395d67482SBill Paul 			return(ENOBUFS);
87495d67482SBill Paul 	};
87595d67482SBill Paul 
876f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
877f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
878f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
879f41ac2beSBill Paul 
88095d67482SBill Paul 	sc->bge_jumbo = i - 1;
88195d67482SBill Paul 
882f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8831be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8841be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
88567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
88695d67482SBill Paul 
88795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
88895d67482SBill Paul 
88995d67482SBill Paul 	return(0);
89095d67482SBill Paul }
89195d67482SBill Paul 
89295d67482SBill Paul static void
89395d67482SBill Paul bge_free_rx_ring_jumbo(sc)
89495d67482SBill Paul 	struct bge_softc *sc;
89595d67482SBill Paul {
89695d67482SBill Paul 	int i;
89795d67482SBill Paul 
89895d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
89995d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
900e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
901e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
902e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
903f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
904f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
905e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
906e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
90795d67482SBill Paul 		}
908f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
9091be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
91095d67482SBill Paul 	}
91195d67482SBill Paul 
91295d67482SBill Paul 	return;
91395d67482SBill Paul }
91495d67482SBill Paul 
91595d67482SBill Paul static void
91695d67482SBill Paul bge_free_tx_ring(sc)
91795d67482SBill Paul 	struct bge_softc *sc;
91895d67482SBill Paul {
91995d67482SBill Paul 	int i;
92095d67482SBill Paul 
921f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
92295d67482SBill Paul 		return;
92395d67482SBill Paul 
92495d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
92595d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
926e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
927e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
928e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
929f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
930f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
931e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
932e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
93395d67482SBill Paul 		}
934f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
93595d67482SBill Paul 		    sizeof(struct bge_tx_bd));
93695d67482SBill Paul 	}
93795d67482SBill Paul 
93895d67482SBill Paul 	return;
93995d67482SBill Paul }
94095d67482SBill Paul 
94195d67482SBill Paul static int
94295d67482SBill Paul bge_init_tx_ring(sc)
94395d67482SBill Paul 	struct bge_softc *sc;
94495d67482SBill Paul {
94595d67482SBill Paul 	sc->bge_txcnt = 0;
94695d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9473927098fSPaul Saab 
94814bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
94914bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
95014bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
95114bbd30fSGleb Smirnoff 
9523927098fSPaul Saab 	/* 5700 b2 errata */
953e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
95414bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
9553927098fSPaul Saab 
95614bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
9573927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
9583927098fSPaul Saab 	/* 5700 b2 errata */
959e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
96095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
96195d67482SBill Paul 
96295d67482SBill Paul 	return(0);
96395d67482SBill Paul }
96495d67482SBill Paul 
96595d67482SBill Paul static void
96695d67482SBill Paul bge_setmulti(sc)
96795d67482SBill Paul 	struct bge_softc *sc;
96895d67482SBill Paul {
96995d67482SBill Paul 	struct ifnet *ifp;
97095d67482SBill Paul 	struct ifmultiaddr *ifma;
97195d67482SBill Paul 	u_int32_t hashes[4] = { 0, 0, 0, 0 };
97295d67482SBill Paul 	int h, i;
97395d67482SBill Paul 
9740f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9750f9bd73bSSam Leffler 
976fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
97795d67482SBill Paul 
97895d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
97995d67482SBill Paul 		for (i = 0; i < 4; i++)
98095d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
98195d67482SBill Paul 		return;
98295d67482SBill Paul 	}
98395d67482SBill Paul 
98495d67482SBill Paul 	/* First, zot all the existing filters. */
98595d67482SBill Paul 	for (i = 0; i < 4; i++)
98695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
98795d67482SBill Paul 
98895d67482SBill Paul 	/* Now program new ones. */
98913b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
99095d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
99195d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
99295d67482SBill Paul 			continue;
9930e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
9940e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
99595d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
99695d67482SBill Paul 	}
99713b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
99895d67482SBill Paul 
99995d67482SBill Paul 	for (i = 0; i < 4; i++)
100095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
100195d67482SBill Paul 
100295d67482SBill Paul 	return;
100395d67482SBill Paul }
100495d67482SBill Paul 
100595d67482SBill Paul /*
100695d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
100795d67482SBill Paul  * self-test results.
100895d67482SBill Paul  */
100995d67482SBill Paul static int
101095d67482SBill Paul bge_chipinit(sc)
101195d67482SBill Paul 	struct bge_softc *sc;
101295d67482SBill Paul {
101395d67482SBill Paul 	int			i;
10145cba12d3SPaul Saab 	u_int32_t		dma_rw_ctl;
101595d67482SBill Paul 
1016e907febfSPyun YongHyeon 	/* Set endian type before we access any non-PCI registers. */
1017e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
101895d67482SBill Paul 
101995d67482SBill Paul 	/*
102095d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
102195d67482SBill Paul 	 * self-tests passed.
102295d67482SBill Paul 	 */
102395d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
102495d67482SBill Paul 		printf("bge%d: RX CPU self-diagnostics failed!\n",
102595d67482SBill Paul 		    sc->bge_unit);
102695d67482SBill Paul 		return(ENODEV);
102795d67482SBill Paul 	}
102895d67482SBill Paul 
102995d67482SBill Paul 	/* Clear the MAC control register */
103095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
103195d67482SBill Paul 
103295d67482SBill Paul 	/*
103395d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
103495d67482SBill Paul 	 * internal memory.
103595d67482SBill Paul 	 */
103695d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
103795d67482SBill Paul 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
103895d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
103995d67482SBill Paul 
104095d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
104195d67482SBill Paul 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
104295d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
104395d67482SBill Paul 
104495d67482SBill Paul 	/* Set up the PCI DMA control register. */
1045e53d81eeSPaul Saab 	if (sc->bge_pcie) {
1046e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1047e53d81eeSPaul Saab 		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1048e53d81eeSPaul Saab 		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1049e53d81eeSPaul Saab 	} else if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
10508287860eSJohn Polstra 	    BGE_PCISTATE_PCI_BUSMODE) {
10518287860eSJohn Polstra 		/* Conventional PCI bus */
10525cba12d3SPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10535cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10545cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10555cba12d3SPaul Saab 		    (0x0F);
10568287860eSJohn Polstra 	} else {
10578287860eSJohn Polstra 		/* PCI-X bus */
10585cba12d3SPaul Saab 		/*
10595cba12d3SPaul Saab 		 * The 5704 uses a different encoding of read/write
10605cba12d3SPaul Saab 		 * watermarks.
10615cba12d3SPaul Saab 		 */
1062e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
10635cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10645cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10655cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
10665cba12d3SPaul Saab 		else
10675cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10685cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10695cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10705cba12d3SPaul Saab 			    (0x0F);
10715cba12d3SPaul Saab 
10725cba12d3SPaul Saab 		/*
10735cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
10745cba12d3SPaul Saab 		 * for hardware bugs.
10755cba12d3SPaul Saab 		 */
1076e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1077e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
10785cba12d3SPaul Saab 			u_int32_t tmp;
10795cba12d3SPaul Saab 
10805cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
10815cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
10825cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
10838287860eSJohn Polstra 		}
10845cba12d3SPaul Saab 	}
10855cba12d3SPaul Saab 
1086e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
10870434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
1088e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1089e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
10905cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
10915cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
109295d67482SBill Paul 
109395d67482SBill Paul 	/*
109495d67482SBill Paul 	 * Set up general mode register.
109595d67482SBill Paul 	 */
1096e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
109795d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1098e446dc86SPaul Saab 	    BGE_MODECTL_TX_NO_PHDR_CSUM|BGE_MODECTL_RX_NO_PHDR_CSUM);
109995d67482SBill Paul 
110095d67482SBill Paul 	/*
1101ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1102ea13bdd5SJohn Polstra 	 * properly by these devices.
110395d67482SBill Paul 	 */
1104ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
110595d67482SBill Paul 
110695d67482SBill Paul #ifdef __brokenalpha__
110795d67482SBill Paul 	/*
110895d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
110995d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
111095d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
111195d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
111295d67482SBill Paul 	 */
111362f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
111462f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
111595d67482SBill Paul #endif
111695d67482SBill Paul 
111795d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
111895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
111995d67482SBill Paul 
112095d67482SBill Paul 	return(0);
112195d67482SBill Paul }
112295d67482SBill Paul 
112395d67482SBill Paul static int
112495d67482SBill Paul bge_blockinit(sc)
112595d67482SBill Paul 	struct bge_softc *sc;
112695d67482SBill Paul {
112795d67482SBill Paul 	struct bge_rcb *rcb;
1128e907febfSPyun YongHyeon 	bus_size_t vrcb;
1129e907febfSPyun YongHyeon 	bge_hostaddr taddr;
113095d67482SBill Paul 	int i;
113195d67482SBill Paul 
113295d67482SBill Paul 	/*
113395d67482SBill Paul 	 * Initialize the memory window pointer register so that
113495d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
113595d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
113695d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
113795d67482SBill Paul 	 */
113895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
113995d67482SBill Paul 
1140822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1141822f63fcSBill Paul 
11425dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1143e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
114495d67482SBill Paul 		/* Configure mbuf memory pool */
114595d67482SBill Paul 		if (sc->bge_extram) {
11460434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11470434d1b8SBill Paul 			    BGE_EXT_SSRAM);
1148822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1149822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1150822f63fcSBill Paul 			else
115195d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
115295d67482SBill Paul 		} else {
11530434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11540434d1b8SBill Paul 			    BGE_BUFFPOOL_1);
1155822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1156822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1157822f63fcSBill Paul 			else
115895d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
115995d67482SBill Paul 		}
116095d67482SBill Paul 
116195d67482SBill Paul 		/* Configure DMA resource pool */
11620434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
11630434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
116495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
11650434d1b8SBill Paul 	}
116695d67482SBill Paul 
116795d67482SBill Paul 	/* Configure mbuf pool watermarks */
1168e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1169e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750) {
11700434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11710434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
11720434d1b8SBill Paul 	} else {
1173fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1174fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
11750434d1b8SBill Paul 	}
1176fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
117795d67482SBill Paul 
117895d67482SBill Paul 	/* Configure DMA resource watermarks */
117995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
118095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
118195d67482SBill Paul 
118295d67482SBill Paul 	/* Enable buffer manager */
11835dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1184e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
118595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
118695d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
118795d67482SBill Paul 
118895d67482SBill Paul 		/* Poll for buffer manager start indication */
118995d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
119095d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
119195d67482SBill Paul 				break;
119295d67482SBill Paul 			DELAY(10);
119395d67482SBill Paul 		}
119495d67482SBill Paul 
119595d67482SBill Paul 		if (i == BGE_TIMEOUT) {
119695d67482SBill Paul 			printf("bge%d: buffer manager failed to start\n",
119795d67482SBill Paul 			    sc->bge_unit);
119895d67482SBill Paul 			return(ENXIO);
119995d67482SBill Paul 		}
12000434d1b8SBill Paul 	}
120195d67482SBill Paul 
120295d67482SBill Paul 	/* Enable flow-through queues */
120395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
120495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
120595d67482SBill Paul 
120695d67482SBill Paul 	/* Wait until queue initialization is complete */
120795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
120895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
120995d67482SBill Paul 			break;
121095d67482SBill Paul 		DELAY(10);
121195d67482SBill Paul 	}
121295d67482SBill Paul 
121395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
121495d67482SBill Paul 		printf("bge%d: flow-through queue init failed\n",
121595d67482SBill Paul 		    sc->bge_unit);
121695d67482SBill Paul 		return(ENXIO);
121795d67482SBill Paul 	}
121895d67482SBill Paul 
121995d67482SBill Paul 	/* Initialize the standard RX ring control block */
1220f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1221f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1222f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1223f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1224f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1225f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1226f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
1227e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1228e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
12290434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
12300434d1b8SBill Paul 	else
12310434d1b8SBill Paul 		rcb->bge_maxlen_flags =
12320434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
123395d67482SBill Paul 	if (sc->bge_extram)
123495d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
123595d67482SBill Paul 	else
123695d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
123767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
123867111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1239f41ac2beSBill Paul 
124067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
124167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
124295d67482SBill Paul 
124395d67482SBill Paul 	/*
124495d67482SBill Paul 	 * Initialize the jumbo RX ring control block
124595d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
124695d67482SBill Paul 	 * field until we're actually ready to start
124795d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
124895d67482SBill Paul 	 * high enough to require it).
124995d67482SBill Paul 	 */
12505dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1251e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1252f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1253f41ac2beSBill Paul 
1254f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1255f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1256f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1257f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1258f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1259f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1260f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
12611be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
12621be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD|BGE_RCB_FLAG_RING_DISABLED);
126395d67482SBill Paul 		if (sc->bge_extram)
126495d67482SBill Paul 			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
126595d67482SBill Paul 		else
126695d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
126767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
126867111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
126967111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
127067111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1271f41ac2beSBill Paul 
12720434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
12730434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
127467111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
127595d67482SBill Paul 
127695d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1277f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
127867111612SJohn Polstra 		rcb->bge_maxlen_flags =
127967111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
12800434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
12810434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
12820434d1b8SBill Paul 	}
128395d67482SBill Paul 
128495d67482SBill Paul 	/*
128595d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
128695d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
128795d67482SBill Paul 	 * each ring.
128895d67482SBill Paul 	 */
128995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
129095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
129195d67482SBill Paul 
129295d67482SBill Paul 	/*
129395d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
129495d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
129595d67482SBill Paul 	 * These are located in NIC memory.
129695d67482SBill Paul 	 */
1297e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
129895d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1299e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1300e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1301e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1302e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
130395d67482SBill Paul 	}
130495d67482SBill Paul 
130595d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1306e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1307e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1308e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1309e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1310e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1311e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
13125dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1313e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1314e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1315e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
131695d67482SBill Paul 
131795d67482SBill Paul 	/* Disable all unused RX return rings */
1318e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
131995d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1320e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1321e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1322e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
13230434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1324e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1325e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
132695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
132795d67482SBill Paul 		    (i * (sizeof(u_int64_t))), 0);
1328e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
132995d67482SBill Paul 	}
133095d67482SBill Paul 
133195d67482SBill Paul 	/* Initialize RX ring indexes */
133295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
133395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
133495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
133595d67482SBill Paul 
133695d67482SBill Paul 	/*
133795d67482SBill Paul 	 * Set up RX return ring 0
133895d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
133995d67482SBill Paul 	 * The return rings live entirely within the host, so the
134095d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
134195d67482SBill Paul 	 */
1342e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1343e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1344e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1345e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1346e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1347e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1348e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
134995d67482SBill Paul 
135095d67482SBill Paul 	/* Set random backoff seed for TX */
135195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
13524a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
13534a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
13544a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
135595d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
135695d67482SBill Paul 
135795d67482SBill Paul 	/* Set inter-packet gap */
135895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
135995d67482SBill Paul 
136095d67482SBill Paul 	/*
136195d67482SBill Paul 	 * Specify which ring to use for packets that don't match
136295d67482SBill Paul 	 * any RX rules.
136395d67482SBill Paul 	 */
136495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
136595d67482SBill Paul 
136695d67482SBill Paul 	/*
136795d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
136895d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
136995d67482SBill Paul 	 */
137095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
137195d67482SBill Paul 
137295d67482SBill Paul 	/* Inialize RX list placement stats mask. */
137395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
137495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
137595d67482SBill Paul 
137695d67482SBill Paul 	/* Disable host coalescing until we get it set up */
137795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
137895d67482SBill Paul 
137995d67482SBill Paul 	/* Poll to make sure it's shut down. */
138095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
138195d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
138295d67482SBill Paul 			break;
138395d67482SBill Paul 		DELAY(10);
138495d67482SBill Paul 	}
138595d67482SBill Paul 
138695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
138795d67482SBill Paul 		printf("bge%d: host coalescing engine failed to idle\n",
138895d67482SBill Paul 		    sc->bge_unit);
138995d67482SBill Paul 		return(ENXIO);
139095d67482SBill Paul 	}
139195d67482SBill Paul 
139295d67482SBill Paul 	/* Set up host coalescing defaults */
139395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
139495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
139595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
139695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
13975dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1398e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
139995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
140095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
14010434d1b8SBill Paul 	}
140295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
140395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
140495d67482SBill Paul 
140595d67482SBill Paul 	/* Set up address of statistics block */
14065dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1407e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1408f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1409f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
141095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1411f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
14120434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
141395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
14140434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
14150434d1b8SBill Paul 	}
14160434d1b8SBill Paul 
14170434d1b8SBill Paul 	/* Set up address of status block */
1418f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1419f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
142095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1421f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1422f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1423f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
142495d67482SBill Paul 
142595d67482SBill Paul 	/* Turn on host coalescing state machine */
142695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
142795d67482SBill Paul 
142895d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
142995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
143095d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
143195d67482SBill Paul 
143295d67482SBill Paul 	/* Turn on RX list placement state machine */
143395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
143495d67482SBill Paul 
143595d67482SBill Paul 	/* Turn on RX list selector state machine. */
14365dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1437e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
143895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
143995d67482SBill Paul 
144095d67482SBill Paul 	/* Turn on DMA, clear stats */
144195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
144295d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
144395d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
144495d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
144595d67482SBill Paul 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
144695d67482SBill Paul 
144795d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
144895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
144995d67482SBill Paul 
145095d67482SBill Paul #ifdef notdef
145195d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
145295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
145395d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
145495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
145595d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
145695d67482SBill Paul #endif
145795d67482SBill Paul 
145895d67482SBill Paul 	/* Turn on DMA completion state machine */
14595dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1460e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
146195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
146295d67482SBill Paul 
146395d67482SBill Paul 	/* Turn on write DMA state machine */
146495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
146595d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
146695d67482SBill Paul 
146795d67482SBill Paul 	/* Turn on read DMA state machine */
146895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
146995d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
147095d67482SBill Paul 
147195d67482SBill Paul 	/* Turn on RX data completion state machine */
147295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
147395d67482SBill Paul 
147495d67482SBill Paul 	/* Turn on RX BD initiator state machine */
147595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
147695d67482SBill Paul 
147795d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
147895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
147995d67482SBill Paul 
148095d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
14815dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1482e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
148395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
148495d67482SBill Paul 
148595d67482SBill Paul 	/* Turn on send BD completion state machine */
148695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
148795d67482SBill Paul 
148895d67482SBill Paul 	/* Turn on send data completion state machine */
148995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
149095d67482SBill Paul 
149195d67482SBill Paul 	/* Turn on send data initiator state machine */
149295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
149395d67482SBill Paul 
149495d67482SBill Paul 	/* Turn on send BD initiator state machine */
149595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
149695d67482SBill Paul 
149795d67482SBill Paul 	/* Turn on send BD selector state machine */
149895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
149995d67482SBill Paul 
150095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
150195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
150295d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
150395d67482SBill Paul 
150495d67482SBill Paul 	/* ack/clear link change events */
150595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15060434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15070434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1508f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
150995d67482SBill Paul 
151095d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
151195d67482SBill Paul 	if (sc->bge_tbi) {
151295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1513a1d52896SBill Paul 	} else {
151495d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
1515e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700)
1516a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1517a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1518a1d52896SBill Paul 	}
151995d67482SBill Paul 
152095d67482SBill Paul 	/* Enable link state change attentions. */
152195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
152295d67482SBill Paul 
152395d67482SBill Paul 	return(0);
152495d67482SBill Paul }
152595d67482SBill Paul 
152695d67482SBill Paul /*
152795d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
152895d67482SBill Paul  * against our list and return its name if we find a match. Note
152995d67482SBill Paul  * that since the Broadcom controller contains VPD support, we
153095d67482SBill Paul  * can get the device name string from the controller itself instead
153195d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
153295d67482SBill Paul  * we'll always announce the right product name.
153395d67482SBill Paul  */
153495d67482SBill Paul static int
153595d67482SBill Paul bge_probe(dev)
153695d67482SBill Paul 	device_t dev;
153795d67482SBill Paul {
153895d67482SBill Paul 	struct bge_type *t;
153995d67482SBill Paul 	struct bge_softc *sc;
1540029e2ee3SJohn Polstra 	char *descbuf;
154195d67482SBill Paul 
154295d67482SBill Paul 	t = bge_devs;
154395d67482SBill Paul 
154495d67482SBill Paul 	sc = device_get_softc(dev);
154595d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
154695d67482SBill Paul 	sc->bge_unit = device_get_unit(dev);
154795d67482SBill Paul 	sc->bge_dev = dev;
154895d67482SBill Paul 
154995d67482SBill Paul 	while(t->bge_name != NULL) {
155095d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
155195d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
155295d67482SBill Paul #ifdef notdef
155395d67482SBill Paul 			bge_vpd_read(sc);
155495d67482SBill Paul 			device_set_desc(dev, sc->bge_vpd_prodname);
155595d67482SBill Paul #endif
1556029e2ee3SJohn Polstra 			descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
1557029e2ee3SJohn Polstra 			if (descbuf == NULL)
1558029e2ee3SJohn Polstra 				return(ENOMEM);
1559029e2ee3SJohn Polstra 			snprintf(descbuf, BGE_DEVDESC_MAX,
1560029e2ee3SJohn Polstra 			    "%s, ASIC rev. %#04x", t->bge_name,
1561029e2ee3SJohn Polstra 			    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16);
1562029e2ee3SJohn Polstra 			device_set_desc_copy(dev, descbuf);
15636d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
15646d2a9bd6SDoug Ambrisko 				sc->bge_no_3_led = 1;
1565029e2ee3SJohn Polstra 			free(descbuf, M_TEMP);
156695d67482SBill Paul 			return(0);
156795d67482SBill Paul 		}
156895d67482SBill Paul 		t++;
156995d67482SBill Paul 	}
157095d67482SBill Paul 
157195d67482SBill Paul 	return(ENXIO);
157295d67482SBill Paul }
157395d67482SBill Paul 
1574f41ac2beSBill Paul static void
1575f41ac2beSBill Paul bge_dma_free(sc)
1576f41ac2beSBill Paul 	struct bge_softc *sc;
1577f41ac2beSBill Paul {
1578f41ac2beSBill Paul 	int i;
1579f41ac2beSBill Paul 
1580f41ac2beSBill Paul 
1581f41ac2beSBill Paul 	/* Destroy DMA maps for RX buffers */
1582f41ac2beSBill Paul 
1583f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1584f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1585f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1586f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1587f41ac2beSBill Paul 	}
1588f41ac2beSBill Paul 
1589f41ac2beSBill Paul 	/* Destroy DMA maps for jumbo RX buffers */
1590f41ac2beSBill Paul 
1591f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1592f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1593f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1594f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1595f41ac2beSBill Paul 	}
1596f41ac2beSBill Paul 
1597f41ac2beSBill Paul 	/* Destroy DMA maps for TX buffers */
1598f41ac2beSBill Paul 
1599f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1600f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1601f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1602f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1603f41ac2beSBill Paul 	}
1604f41ac2beSBill Paul 
1605f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1606f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1607f41ac2beSBill Paul 
1608f41ac2beSBill Paul 
1609f41ac2beSBill Paul 	/* Destroy standard RX ring */
1610f41ac2beSBill Paul 
1611e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1612e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1613e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1614e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1615f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1616f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1617f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1618f41ac2beSBill Paul 
1619f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1620f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1621f41ac2beSBill Paul 
1622f41ac2beSBill Paul 	/* Destroy jumbo RX ring */
1623f41ac2beSBill Paul 
1624e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1625e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1626e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1627e65bed95SPyun YongHyeon 
1628e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1629e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1630f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1631f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1632f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1633f41ac2beSBill Paul 
1634f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1635f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1636f41ac2beSBill Paul 
1637f41ac2beSBill Paul 	/* Destroy RX return ring */
1638f41ac2beSBill Paul 
1639e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1640e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1641e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1642e65bed95SPyun YongHyeon 
1643e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1644e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1645f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1646f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1647f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1648f41ac2beSBill Paul 
1649f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1650f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1651f41ac2beSBill Paul 
1652f41ac2beSBill Paul 	/* Destroy TX ring */
1653f41ac2beSBill Paul 
1654e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1655e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1656e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1657e65bed95SPyun YongHyeon 
1658e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1659f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1660f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1661f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1662f41ac2beSBill Paul 
1663f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1664f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1665f41ac2beSBill Paul 
1666f41ac2beSBill Paul 	/* Destroy status block */
1667f41ac2beSBill Paul 
1668e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1669e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1670e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1671e65bed95SPyun YongHyeon 
1672e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1673f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1674f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1675f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1676f41ac2beSBill Paul 
1677f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1678f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1679f41ac2beSBill Paul 
1680f41ac2beSBill Paul 	/* Destroy statistics block */
1681f41ac2beSBill Paul 
1682e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1683e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1684e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1685e65bed95SPyun YongHyeon 
1686e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1687f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1688f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1689f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1690f41ac2beSBill Paul 
1691f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1692f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1693f41ac2beSBill Paul 
1694f41ac2beSBill Paul 	/* Destroy the parent tag */
1695f41ac2beSBill Paul 
1696f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1697f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1698f41ac2beSBill Paul 
1699f41ac2beSBill Paul 	return;
1700f41ac2beSBill Paul }
1701f41ac2beSBill Paul 
1702f41ac2beSBill Paul static int
1703f41ac2beSBill Paul bge_dma_alloc(dev)
1704f41ac2beSBill Paul 	device_t dev;
1705f41ac2beSBill Paul {
1706f41ac2beSBill Paul 	struct bge_softc *sc;
17071be6acb7SGleb Smirnoff 	int i, error;
1708f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
1709f41ac2beSBill Paul 
1710f41ac2beSBill Paul 	sc = device_get_softc(dev);
1711f41ac2beSBill Paul 
1712f41ac2beSBill Paul 	/*
1713f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1714f41ac2beSBill Paul 	 */
1715f41ac2beSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1716f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1717f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
17182f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1719f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1720f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1721f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
17228a40c10eSScott Long 			0,			/* flags */
1723f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1724f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1725f41ac2beSBill Paul 
1726e65bed95SPyun YongHyeon 	if (error != 0) {
1727e65bed95SPyun YongHyeon 		device_printf(dev, "could not allocate parent dma tag\n");
1728e65bed95SPyun YongHyeon 		return (ENOMEM);
1729e65bed95SPyun YongHyeon 	}
1730e65bed95SPyun YongHyeon 
1731f41ac2beSBill Paul 	/*
1732f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1733f41ac2beSBill Paul 	 */
17348a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1735f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
17361be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
17371be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1738f41ac2beSBill Paul 
1739f41ac2beSBill Paul 	if (error) {
1740f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1741f41ac2beSBill Paul 		return (ENOMEM);
1742f41ac2beSBill Paul 	}
1743f41ac2beSBill Paul 
1744f41ac2beSBill Paul 	/* Create DMA maps for RX buffers */
1745f41ac2beSBill Paul 
1746f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1747f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1748f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1749f41ac2beSBill Paul 		if (error) {
1750f41ac2beSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1751f41ac2beSBill Paul 			return(ENOMEM);
1752f41ac2beSBill Paul 		}
1753f41ac2beSBill Paul 	}
1754f41ac2beSBill Paul 
1755f41ac2beSBill Paul 	/* Create DMA maps for TX buffers */
1756f41ac2beSBill Paul 
1757f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1758f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1759f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1760f41ac2beSBill Paul 		if (error) {
1761f41ac2beSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1762f41ac2beSBill Paul 			return(ENOMEM);
1763f41ac2beSBill Paul 		}
1764f41ac2beSBill Paul 	}
1765f41ac2beSBill Paul 
1766f41ac2beSBill Paul 	/* Create tag for standard RX ring */
1767f41ac2beSBill Paul 
1768f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1769f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1770f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1771f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1772f41ac2beSBill Paul 
1773f41ac2beSBill Paul 	if (error) {
1774f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1775f41ac2beSBill Paul 		return (ENOMEM);
1776f41ac2beSBill Paul 	}
1777f41ac2beSBill Paul 
1778f41ac2beSBill Paul 	/* Allocate DMA'able memory for standard RX ring */
1779f41ac2beSBill Paul 
1780f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1781f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1782f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1783f41ac2beSBill Paul 	if (error)
1784f41ac2beSBill Paul 		return (ENOMEM);
1785f41ac2beSBill Paul 
1786f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1787f41ac2beSBill Paul 
1788f41ac2beSBill Paul 	/* Load the address of the standard RX ring */
1789f41ac2beSBill Paul 
1790f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1791f41ac2beSBill Paul 	ctx.sc = sc;
1792f41ac2beSBill Paul 
1793f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1794f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1795f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1796f41ac2beSBill Paul 
1797f41ac2beSBill Paul 	if (error)
1798f41ac2beSBill Paul 		return (ENOMEM);
1799f41ac2beSBill Paul 
1800f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1801f41ac2beSBill Paul 
18025dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1803e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1804f41ac2beSBill Paul 
1805f41ac2beSBill Paul 		/*
1806f41ac2beSBill Paul 		 * Create tag for jumbo mbufs.
1807f41ac2beSBill Paul 		 * This is really a bit of a kludge. We allocate a special
1808f41ac2beSBill Paul 		 * jumbo buffer pool which (thanks to the way our DMA
1809f41ac2beSBill Paul 		 * memory allocation works) will consist of contiguous
1810f41ac2beSBill Paul 		 * pages. This means that even though a jumbo buffer might
1811f41ac2beSBill Paul 		 * be larger than a page size, we don't really need to
1812f41ac2beSBill Paul 		 * map it into more than one DMA segment. However, the
1813f41ac2beSBill Paul 		 * default mbuf tag will result in multi-segment mappings,
1814f41ac2beSBill Paul 		 * so we have to create a special jumbo mbuf tag that
1815f41ac2beSBill Paul 		 * lets us get away with mapping the jumbo buffers as
1816f41ac2beSBill Paul 		 * a single segment. I think eventually the driver should
1817f41ac2beSBill Paul 		 * be changed so that it uses ordinary mbufs and cluster
1818f41ac2beSBill Paul 		 * buffers, i.e. jumbo frames can span multiple DMA
1819f41ac2beSBill Paul 		 * descriptors. But that's a project for another day.
1820f41ac2beSBill Paul 		 */
1821f41ac2beSBill Paul 
1822f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
18238a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
18241be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
18251be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1826f41ac2beSBill Paul 
1827f41ac2beSBill Paul 		if (error) {
1828f41ac2beSBill Paul 			device_printf(dev, "could not allocate dma tag\n");
1829f41ac2beSBill Paul 			return (ENOMEM);
1830f41ac2beSBill Paul 		}
1831f41ac2beSBill Paul 
1832f41ac2beSBill Paul 		/* Create tag for jumbo RX ring */
1833f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1834f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1835f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1836f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1837f41ac2beSBill Paul 
1838f41ac2beSBill Paul 		if (error) {
1839f41ac2beSBill Paul 			device_printf(dev, "could not allocate dma tag\n");
1840f41ac2beSBill Paul 			return (ENOMEM);
1841f41ac2beSBill Paul 		}
1842f41ac2beSBill Paul 
1843f41ac2beSBill Paul 		/* Allocate DMA'able memory for jumbo RX ring */
1844f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
18451be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
18461be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1847f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1848f41ac2beSBill Paul 		if (error)
1849f41ac2beSBill Paul 			return (ENOMEM);
1850f41ac2beSBill Paul 
1851f41ac2beSBill Paul 		/* Load the address of the jumbo RX ring */
1852f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1853f41ac2beSBill Paul 		ctx.sc = sc;
1854f41ac2beSBill Paul 
1855f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1856f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1857f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1858f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1859f41ac2beSBill Paul 
1860f41ac2beSBill Paul 		if (error)
1861f41ac2beSBill Paul 			return (ENOMEM);
1862f41ac2beSBill Paul 
1863f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1864f41ac2beSBill Paul 
1865f41ac2beSBill Paul 		/* Create DMA maps for jumbo RX buffers */
1866f41ac2beSBill Paul 
1867f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1868f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1869f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1870f41ac2beSBill Paul 			if (error) {
1871f41ac2beSBill Paul 				device_printf(dev,
1872f41ac2beSBill Paul 				    "can't create DMA map for RX\n");
1873f41ac2beSBill Paul 				return(ENOMEM);
1874f41ac2beSBill Paul 			}
1875f41ac2beSBill Paul 		}
1876f41ac2beSBill Paul 
1877f41ac2beSBill Paul 	}
1878f41ac2beSBill Paul 
1879f41ac2beSBill Paul 	/* Create tag for RX return ring */
1880f41ac2beSBill Paul 
1881f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1882f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1883f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1884f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1885f41ac2beSBill Paul 
1886f41ac2beSBill Paul 	if (error) {
1887f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1888f41ac2beSBill Paul 		return (ENOMEM);
1889f41ac2beSBill Paul 	}
1890f41ac2beSBill Paul 
1891f41ac2beSBill Paul 	/* Allocate DMA'able memory for RX return ring */
1892f41ac2beSBill Paul 
1893f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1894f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1895f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1896f41ac2beSBill Paul 	if (error)
1897f41ac2beSBill Paul 		return (ENOMEM);
1898f41ac2beSBill Paul 
1899f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1900f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1901f41ac2beSBill Paul 
1902f41ac2beSBill Paul 	/* Load the address of the RX return ring */
1903f41ac2beSBill Paul 
1904f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1905f41ac2beSBill Paul 	ctx.sc = sc;
1906f41ac2beSBill Paul 
1907f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
1908f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
1909f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
1910f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1911f41ac2beSBill Paul 
1912f41ac2beSBill Paul 	if (error)
1913f41ac2beSBill Paul 		return (ENOMEM);
1914f41ac2beSBill Paul 
1915f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
1916f41ac2beSBill Paul 
1917f41ac2beSBill Paul 	/* Create tag for TX ring */
1918f41ac2beSBill Paul 
1919f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1920f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1921f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
1922f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
1923f41ac2beSBill Paul 
1924f41ac2beSBill Paul 	if (error) {
1925f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1926f41ac2beSBill Paul 		return (ENOMEM);
1927f41ac2beSBill Paul 	}
1928f41ac2beSBill Paul 
1929f41ac2beSBill Paul 	/* Allocate DMA'able memory for TX ring */
1930f41ac2beSBill Paul 
1931f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
1932f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
1933f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
1934f41ac2beSBill Paul 	if (error)
1935f41ac2beSBill Paul 		return (ENOMEM);
1936f41ac2beSBill Paul 
1937f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1938f41ac2beSBill Paul 
1939f41ac2beSBill Paul 	/* Load the address of the TX ring */
1940f41ac2beSBill Paul 
1941f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1942f41ac2beSBill Paul 	ctx.sc = sc;
1943f41ac2beSBill Paul 
1944f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
1945f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
1946f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1947f41ac2beSBill Paul 
1948f41ac2beSBill Paul 	if (error)
1949f41ac2beSBill Paul 		return (ENOMEM);
1950f41ac2beSBill Paul 
1951f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
1952f41ac2beSBill Paul 
1953f41ac2beSBill Paul 	/* Create tag for status block */
1954f41ac2beSBill Paul 
1955f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1956f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1957f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
1958f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
1959f41ac2beSBill Paul 
1960f41ac2beSBill Paul 	if (error) {
1961f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1962f41ac2beSBill Paul 		return (ENOMEM);
1963f41ac2beSBill Paul 	}
1964f41ac2beSBill Paul 
1965f41ac2beSBill Paul 	/* Allocate DMA'able memory for status block */
1966f41ac2beSBill Paul 
1967f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
1968f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
1969f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
1970f41ac2beSBill Paul 	if (error)
1971f41ac2beSBill Paul 		return (ENOMEM);
1972f41ac2beSBill Paul 
1973f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1974f41ac2beSBill Paul 
1975f41ac2beSBill Paul 	/* Load the address of the status block */
1976f41ac2beSBill Paul 
1977f41ac2beSBill Paul 	ctx.sc = sc;
1978f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1979f41ac2beSBill Paul 
1980f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
1981f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
1982f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1983f41ac2beSBill Paul 
1984f41ac2beSBill Paul 	if (error)
1985f41ac2beSBill Paul 		return (ENOMEM);
1986f41ac2beSBill Paul 
1987f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
1988f41ac2beSBill Paul 
1989f41ac2beSBill Paul 	/* Create tag for statistics block */
1990f41ac2beSBill Paul 
1991f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1992f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1993f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
1994f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
1995f41ac2beSBill Paul 
1996f41ac2beSBill Paul 	if (error) {
1997f41ac2beSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1998f41ac2beSBill Paul 		return (ENOMEM);
1999f41ac2beSBill Paul 	}
2000f41ac2beSBill Paul 
2001f41ac2beSBill Paul 	/* Allocate DMA'able memory for statistics block */
2002f41ac2beSBill Paul 
2003f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2004f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2005f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2006f41ac2beSBill Paul 	if (error)
2007f41ac2beSBill Paul 		return (ENOMEM);
2008f41ac2beSBill Paul 
2009f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2010f41ac2beSBill Paul 
2011f41ac2beSBill Paul 	/* Load the address of the statstics block */
2012f41ac2beSBill Paul 
2013f41ac2beSBill Paul 	ctx.sc = sc;
2014f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2015f41ac2beSBill Paul 
2016f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2017f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2018f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2019f41ac2beSBill Paul 
2020f41ac2beSBill Paul 	if (error)
2021f41ac2beSBill Paul 		return (ENOMEM);
2022f41ac2beSBill Paul 
2023f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2024f41ac2beSBill Paul 
2025f41ac2beSBill Paul 	return(0);
2026f41ac2beSBill Paul }
2027f41ac2beSBill Paul 
202895d67482SBill Paul static int
202995d67482SBill Paul bge_attach(dev)
203095d67482SBill Paul 	device_t dev;
203195d67482SBill Paul {
203295d67482SBill Paul 	struct ifnet *ifp;
203395d67482SBill Paul 	struct bge_softc *sc;
2034a1d52896SBill Paul 	u_int32_t hwcfg = 0;
2035fc74a9f9SBrooks Davis 	u_int32_t mac_tmp = 0;
2036fc74a9f9SBrooks Davis 	u_char eaddr[6];
203795d67482SBill Paul 	int unit, error = 0, rid;
203895d67482SBill Paul 
203995d67482SBill Paul 	sc = device_get_softc(dev);
204095d67482SBill Paul 	unit = device_get_unit(dev);
204195d67482SBill Paul 	sc->bge_dev = dev;
204295d67482SBill Paul 	sc->bge_unit = unit;
204395d67482SBill Paul 
204495d67482SBill Paul 	/*
204595d67482SBill Paul 	 * Map control/status registers.
204695d67482SBill Paul 	 */
204795d67482SBill Paul 	pci_enable_busmaster(dev);
204895d67482SBill Paul 
204995d67482SBill Paul 	rid = BGE_PCI_BAR0;
20505f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
20515f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
205295d67482SBill Paul 
205395d67482SBill Paul 	if (sc->bge_res == NULL) {
205495d67482SBill Paul 		printf ("bge%d: couldn't map memory\n", unit);
205595d67482SBill Paul 		error = ENXIO;
205695d67482SBill Paul 		goto fail;
205795d67482SBill Paul 	}
205895d67482SBill Paul 
205995d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
206095d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
206195d67482SBill Paul 
206295d67482SBill Paul 	/* Allocate interrupt */
206395d67482SBill Paul 	rid = 0;
206495d67482SBill Paul 
20655f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
206695d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
206795d67482SBill Paul 
206895d67482SBill Paul 	if (sc->bge_irq == NULL) {
206995d67482SBill Paul 		printf("bge%d: couldn't map interrupt\n", unit);
207095d67482SBill Paul 		error = ENXIO;
207195d67482SBill Paul 		goto fail;
207295d67482SBill Paul 	}
207395d67482SBill Paul 
207495d67482SBill Paul 	sc->bge_unit = unit;
207595d67482SBill Paul 
20760f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
20770f9bd73bSSam Leffler 
2078e53d81eeSPaul Saab 	/* Save ASIC rev. */
2079e53d81eeSPaul Saab 
2080e53d81eeSPaul Saab 	sc->bge_chipid =
2081e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2082e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2083e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2084e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2085e53d81eeSPaul Saab 
2086e53d81eeSPaul Saab 	/*
2087560c1670SGleb Smirnoff 	 * Treat the 5714 and the 5752 like the 5750 until we have more info
2088419c028bSPaul Saab 	 * on this chip.
2089419c028bSPaul Saab 	 */
2090560c1670SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5714 ||
2091560c1670SGleb Smirnoff             sc->bge_asicrev == BGE_ASICREV_BCM5752)
2092419c028bSPaul Saab 		sc->bge_asicrev = BGE_ASICREV_BCM5750;
2093419c028bSPaul Saab 
2094419c028bSPaul Saab 	/*
2095e53d81eeSPaul Saab 	 * XXX: Broadcom Linux driver.  Not in specs or eratta.
2096e53d81eeSPaul Saab 	 * PCI-Express?
2097e53d81eeSPaul Saab 	 */
2098e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
2099e53d81eeSPaul Saab 		u_int32_t v;
2100e53d81eeSPaul Saab 
2101e53d81eeSPaul Saab 		v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4);
2102e53d81eeSPaul Saab 		if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) {
2103e53d81eeSPaul Saab 			v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
2104e53d81eeSPaul Saab 			if ((v & 0xff) == BGE_PCIE_CAPID)
2105e53d81eeSPaul Saab 				sc->bge_pcie = 1;
2106e53d81eeSPaul Saab 		}
2107e53d81eeSPaul Saab 	}
2108e53d81eeSPaul Saab 
210995d67482SBill Paul 	/* Try to reset the chip. */
211095d67482SBill Paul 	bge_reset(sc);
211195d67482SBill Paul 
211295d67482SBill Paul 	if (bge_chipinit(sc)) {
211395d67482SBill Paul 		printf("bge%d: chip initialization failed\n", sc->bge_unit);
211495d67482SBill Paul 		bge_release_resources(sc);
211595d67482SBill Paul 		error = ENXIO;
211695d67482SBill Paul 		goto fail;
211795d67482SBill Paul 	}
211895d67482SBill Paul 
211995d67482SBill Paul 	/*
212095d67482SBill Paul 	 * Get station address from the EEPROM.
212195d67482SBill Paul 	 */
2122fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2123fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2124fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2125fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2126fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2127fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2128fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2129fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2130fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2131fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
213295d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
213395d67482SBill Paul 		printf("bge%d: failed to read station address\n", unit);
213495d67482SBill Paul 		bge_release_resources(sc);
213595d67482SBill Paul 		error = ENXIO;
213695d67482SBill Paul 		goto fail;
213795d67482SBill Paul 	}
213895d67482SBill Paul 
2139f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
2140e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2141e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
2142f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2143f41ac2beSBill Paul 	else
2144f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2145f41ac2beSBill Paul 
2146f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2147f41ac2beSBill Paul 		printf ("bge%d: failed to allocate DMA resources\n",
2148f41ac2beSBill Paul 		    sc->bge_unit);
2149f41ac2beSBill Paul 		bge_release_resources(sc);
2150f41ac2beSBill Paul 		error = ENXIO;
2151f41ac2beSBill Paul 		goto fail;
2152f41ac2beSBill Paul 	}
2153f41ac2beSBill Paul 
215495d67482SBill Paul 	/* Set default tuneable values. */
215595d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
215695d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
215795d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
215895d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
215995d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
216095d67482SBill Paul 
216195d67482SBill Paul 	/* Set up ifnet structure */
2162fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2163fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2164fc74a9f9SBrooks Davis 		printf("bge%d: failed to if_alloc()\n", sc->bge_unit);
2165fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2166fc74a9f9SBrooks Davis 		error = ENXIO;
2167fc74a9f9SBrooks Davis 		goto fail;
2168fc74a9f9SBrooks Davis 	}
216995d67482SBill Paul 	ifp->if_softc = sc;
21709bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
217195d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
217295d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
217395d67482SBill Paul 	ifp->if_start = bge_start;
217495d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
217595d67482SBill Paul 	ifp->if_init = bge_init;
217695d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
21774d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
21784d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
21794d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
218095d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2181d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
21820434d1b8SBill Paul 	    IFCAP_VLAN_MTU;
218395d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
218475719184SGleb Smirnoff #ifdef DEVICE_POLLING
218575719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
218675719184SGleb Smirnoff #endif
218795d67482SBill Paul 
2188a1d52896SBill Paul         /*
2189d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2190d375e524SGleb Smirnoff 	 * to hardware bugs.
2191d375e524SGleb Smirnoff 	 */
2192d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2193d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2194d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2195d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2196d375e524SGleb Smirnoff 	}
2197d375e524SGleb Smirnoff 
2198d375e524SGleb Smirnoff 	/*
2199a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
220041abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
220141abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
220241abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
220341abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
220441abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
220541abcc1bSPaul Saab 	 * SK-9D41.
2206a1d52896SBill Paul 	 */
220741abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
220841abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
220941abcc1bSPaul Saab 	else {
2210f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2211f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2212f6789fbaSPyun YongHyeon 			printf("bge%d: failed to read EEPROM\n", unit);
2213f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2214f6789fbaSPyun YongHyeon 			error = ENXIO;
2215f6789fbaSPyun YongHyeon 			goto fail;
2216f6789fbaSPyun YongHyeon 		}
221741abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
221841abcc1bSPaul Saab 	}
221941abcc1bSPaul Saab 
222041abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2221a1d52896SBill Paul 		sc->bge_tbi = 1;
2222a1d52896SBill Paul 
222395d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
222495d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
222595d67482SBill Paul 		sc->bge_tbi = 1;
222695d67482SBill Paul 
222795d67482SBill Paul 	if (sc->bge_tbi) {
222895d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
222995d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
223095d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
223195d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
223295d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
223395d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
223495d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2235da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
223695d67482SBill Paul 	} else {
223795d67482SBill Paul 		/*
223895d67482SBill Paul 		 * Do transceiver setup.
223995d67482SBill Paul 		 */
224095d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
224195d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
224295d67482SBill Paul 			printf("bge%d: MII without any PHY!\n", sc->bge_unit);
224395d67482SBill Paul 			bge_release_resources(sc);
224495d67482SBill Paul 			error = ENXIO;
224595d67482SBill Paul 			goto fail;
224695d67482SBill Paul 		}
224795d67482SBill Paul 	}
224895d67482SBill Paul 
224995d67482SBill Paul 	/*
2250e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2251e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2252e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2253e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2254e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2255e255b776SJohn Polstra 	 * payloads by copying the received packets.
2256e255b776SJohn Polstra 	 */
2257e0ced696SPaul Saab 	switch (sc->bge_chipid) {
2258e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_A0:
2259e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B0:
2260e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B2:
2261e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B5:
2262e255b776SJohn Polstra 		/* If in PCI-X mode, work around the alignment bug. */
2263e255b776SJohn Polstra 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
2264e255b776SJohn Polstra 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2265e255b776SJohn Polstra 		    BGE_PCISTATE_PCI_BUSSPEED)
2266e255b776SJohn Polstra 			sc->bge_rx_alignment_bug = 1;
2267e255b776SJohn Polstra 		break;
2268e255b776SJohn Polstra 	}
2269e255b776SJohn Polstra 
2270e255b776SJohn Polstra 	/*
227195d67482SBill Paul 	 * Call MI attach routine.
227295d67482SBill Paul 	 */
2273fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
22740f9bd73bSSam Leffler 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
22750f9bd73bSSam Leffler 
22760f9bd73bSSam Leffler 	/*
22770f9bd73bSSam Leffler 	 * Hookup IRQ last.
22780f9bd73bSSam Leffler 	 */
22790f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
22800f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
22810f9bd73bSSam Leffler 
22820f9bd73bSSam Leffler 	if (error) {
2283fc74a9f9SBrooks Davis 		bge_detach(dev);
22840f9bd73bSSam Leffler 		printf("bge%d: couldn't set up irq\n", unit);
22850f9bd73bSSam Leffler 	}
228695d67482SBill Paul 
228795d67482SBill Paul fail:
228895d67482SBill Paul 	return(error);
228995d67482SBill Paul }
229095d67482SBill Paul 
229195d67482SBill Paul static int
229295d67482SBill Paul bge_detach(dev)
229395d67482SBill Paul 	device_t dev;
229495d67482SBill Paul {
229595d67482SBill Paul 	struct bge_softc *sc;
229695d67482SBill Paul 	struct ifnet *ifp;
229795d67482SBill Paul 
229895d67482SBill Paul 	sc = device_get_softc(dev);
2299fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
230095d67482SBill Paul 
230175719184SGleb Smirnoff #ifdef DEVICE_POLLING
230275719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
230375719184SGleb Smirnoff 		ether_poll_deregister(ifp);
230475719184SGleb Smirnoff #endif
230575719184SGleb Smirnoff 
23060f9bd73bSSam Leffler 	BGE_LOCK(sc);
230795d67482SBill Paul 	bge_stop(sc);
230895d67482SBill Paul 	bge_reset(sc);
23090f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
23100f9bd73bSSam Leffler 
23110f9bd73bSSam Leffler 	ether_ifdetach(ifp);
231295d67482SBill Paul 
231395d67482SBill Paul 	if (sc->bge_tbi) {
231495d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
231595d67482SBill Paul 	} else {
231695d67482SBill Paul 		bus_generic_detach(dev);
231795d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
231895d67482SBill Paul 	}
231995d67482SBill Paul 
232095d67482SBill Paul 	bge_release_resources(sc);
232195d67482SBill Paul 
232295d67482SBill Paul 	return(0);
232395d67482SBill Paul }
232495d67482SBill Paul 
232595d67482SBill Paul static void
232695d67482SBill Paul bge_release_resources(sc)
232795d67482SBill Paul 	struct bge_softc *sc;
232895d67482SBill Paul {
232995d67482SBill Paul 	device_t dev;
233095d67482SBill Paul 
233195d67482SBill Paul 	dev = sc->bge_dev;
233295d67482SBill Paul 
233395d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
233495d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
233595d67482SBill Paul 
233695d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
233795d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
233895d67482SBill Paul 
233995d67482SBill Paul 	if (sc->bge_intrhand != NULL)
234095d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
234195d67482SBill Paul 
234295d67482SBill Paul 	if (sc->bge_irq != NULL)
234395d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
234495d67482SBill Paul 
234595d67482SBill Paul 	if (sc->bge_res != NULL)
234695d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
234795d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
234895d67482SBill Paul 
2349ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2350ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2351ad61f896SRuslan Ermilov 
2352f41ac2beSBill Paul 	bge_dma_free(sc);
235395d67482SBill Paul 
23540f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
23550f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
23560f9bd73bSSam Leffler 
235795d67482SBill Paul 	return;
235895d67482SBill Paul }
235995d67482SBill Paul 
236095d67482SBill Paul static void
236195d67482SBill Paul bge_reset(sc)
236295d67482SBill Paul 	struct bge_softc *sc;
236395d67482SBill Paul {
236495d67482SBill Paul 	device_t dev;
2365e53d81eeSPaul Saab 	u_int32_t cachesize, command, pcistate, reset;
236695d67482SBill Paul 	int i, val = 0;
236795d67482SBill Paul 
236895d67482SBill Paul 	dev = sc->bge_dev;
236995d67482SBill Paul 
237095d67482SBill Paul 	/* Save some important PCI state. */
237195d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
237295d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
237395d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
237495d67482SBill Paul 
237595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
237695d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2377e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
237895d67482SBill Paul 
2379e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2380e53d81eeSPaul Saab 
2381e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2382e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2383e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2384e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2385e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2386e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2387e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2388e53d81eeSPaul Saab 			reset |= (1<<29);
2389e53d81eeSPaul Saab 		}
2390e53d81eeSPaul Saab 	}
2391e53d81eeSPaul Saab 
239295d67482SBill Paul 	/* Issue global reset */
2393e53d81eeSPaul Saab 	bge_writereg_ind(sc, BGE_MISC_CFG, reset);
239495d67482SBill Paul 
239595d67482SBill Paul 	DELAY(1000);
239695d67482SBill Paul 
2397e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2398e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2399e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2400e53d81eeSPaul Saab 			uint32_t v;
2401e53d81eeSPaul Saab 
2402e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2403e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2404e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2405e53d81eeSPaul Saab 		}
2406e53d81eeSPaul Saab 		/* Set PCIE max payload size and clear error status. */
2407e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2408e53d81eeSPaul Saab 	}
2409e53d81eeSPaul Saab 
241095d67482SBill Paul 	/* Reset some of the PCI state that got zapped by reset */
241195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
241295d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2413e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
241495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
241595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
241695d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
241795d67482SBill Paul 
2418a7b0c314SPaul Saab 	/* Enable memory arbiter. */
24195dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2420e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
2421a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2422a7b0c314SPaul Saab 
242395d67482SBill Paul 	/*
242495d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
242595d67482SBill Paul 	 * general communications memory at 0xB50.
242695d67482SBill Paul 	 */
242795d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
242895d67482SBill Paul 	/*
242995d67482SBill Paul 	 * Poll the value location we just wrote until
243095d67482SBill Paul 	 * we see the 1's complement of the magic number.
243195d67482SBill Paul 	 * This indicates that the firmware initialization
243295d67482SBill Paul 	 * is complete.
243395d67482SBill Paul 	 */
243495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
243595d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
243695d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
243795d67482SBill Paul 			break;
243895d67482SBill Paul 		DELAY(10);
243995d67482SBill Paul 	}
244095d67482SBill Paul 
244195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
244295d67482SBill Paul 		printf("bge%d: firmware handshake timed out\n", sc->bge_unit);
244395d67482SBill Paul 		return;
244495d67482SBill Paul 	}
244595d67482SBill Paul 
244695d67482SBill Paul 	/*
244795d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
244895d67482SBill Paul 	 * return to its original pre-reset state. This is a
244995d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
245095d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
245195d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
245295d67482SBill Paul 	 * results.
245395d67482SBill Paul 	 */
245495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
245595d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
245695d67482SBill Paul 			break;
245795d67482SBill Paul 		DELAY(10);
245895d67482SBill Paul 	}
245995d67482SBill Paul 
246095d67482SBill Paul 	/* Fix up byte swapping */
2461e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
246295d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
246395d67482SBill Paul 
246495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
246595d67482SBill Paul 
2466da3003f0SBill Paul 	/*
2467da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2468da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2469da3003f0SBill Paul 	 * to 1.2V.
2470da3003f0SBill Paul 	 */
2471da3003f0SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 && sc->bge_tbi) {
2472da3003f0SBill Paul 		uint32_t serdescfg;
2473da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2474da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2475da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2476da3003f0SBill Paul 	}
2477da3003f0SBill Paul 
2478e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2479e53d81eeSPaul Saab 	if (sc->bge_pcie && sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2480e53d81eeSPaul Saab 		uint32_t v;
2481e53d81eeSPaul Saab 
2482e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2483e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2484e53d81eeSPaul Saab 	}
248595d67482SBill Paul 	DELAY(10000);
248695d67482SBill Paul 
248795d67482SBill Paul 	return;
248895d67482SBill Paul }
248995d67482SBill Paul 
249095d67482SBill Paul /*
249195d67482SBill Paul  * Frame reception handling. This is called if there's a frame
249295d67482SBill Paul  * on the receive return list.
249395d67482SBill Paul  *
249495d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
24951be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
249695d67482SBill Paul  * 2) the frame is from the standard receive ring
249795d67482SBill Paul  */
249895d67482SBill Paul 
249995d67482SBill Paul static void
250095d67482SBill Paul bge_rxeof(sc)
250195d67482SBill Paul 	struct bge_softc *sc;
250295d67482SBill Paul {
250395d67482SBill Paul 	struct ifnet *ifp;
250495d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
250595d67482SBill Paul 
25060f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
25070f9bd73bSSam Leffler 
2508fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
250995d67482SBill Paul 
2510f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2511e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2512f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2513f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
25145dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2515e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2516f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2517f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2518f41ac2beSBill Paul 		    BUS_DMASYNC_POSTREAD);
2519f41ac2beSBill Paul 	}
2520f41ac2beSBill Paul 
252195d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2522f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
252395d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
252495d67482SBill Paul 		u_int32_t		rxidx;
252595d67482SBill Paul 		struct ether_header	*eh;
252695d67482SBill Paul 		struct mbuf		*m = NULL;
252795d67482SBill Paul 		u_int16_t		vlan_tag = 0;
252895d67482SBill Paul 		int			have_tag = 0;
252995d67482SBill Paul 
253075719184SGleb Smirnoff #ifdef DEVICE_POLLING
253175719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
253275719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
253375719184SGleb Smirnoff 				break;
253475719184SGleb Smirnoff 			sc->rxcycles--;
253575719184SGleb Smirnoff 		}
253675719184SGleb Smirnoff #endif
253775719184SGleb Smirnoff 
253895d67482SBill Paul 		cur_rx =
2539f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
254095d67482SBill Paul 
254195d67482SBill Paul 		rxidx = cur_rx->bge_idx;
25420434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
254395d67482SBill Paul 
254495d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
254595d67482SBill Paul 			have_tag = 1;
254695d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
254795d67482SBill Paul 		}
254895d67482SBill Paul 
254995d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
255095d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2551f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2552f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2553f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2554f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2555f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
255695d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
255795d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
255895d67482SBill Paul 			jumbocnt++;
255995d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
256095d67482SBill Paul 				ifp->if_ierrors++;
256195d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
256295d67482SBill Paul 				continue;
256395d67482SBill Paul 			}
256495d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
256595d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
256695d67482SBill Paul 				ifp->if_ierrors++;
256795d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
256895d67482SBill Paul 				continue;
256995d67482SBill Paul 			}
257095d67482SBill Paul 		} else {
257195d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2572f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2573f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2574f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2575f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2576f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
257795d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
257895d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
257995d67482SBill Paul 			stdcnt++;
258095d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
258195d67482SBill Paul 				ifp->if_ierrors++;
258295d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
258395d67482SBill Paul 				continue;
258495d67482SBill Paul 			}
258595d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
258695d67482SBill Paul 			    NULL) == ENOBUFS) {
258795d67482SBill Paul 				ifp->if_ierrors++;
258895d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
258995d67482SBill Paul 				continue;
259095d67482SBill Paul 			}
259195d67482SBill Paul 		}
259295d67482SBill Paul 
259395d67482SBill Paul 		ifp->if_ipackets++;
2594e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2595e255b776SJohn Polstra 		/*
2596e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2597e65bed95SPyun YongHyeon 		 * the payload is aligned.
2598e255b776SJohn Polstra 		 */
2599e255b776SJohn Polstra 		if (sc->bge_rx_alignment_bug) {
2600e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2601e255b776SJohn Polstra 			    cur_rx->bge_len);
2602e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2603e255b776SJohn Polstra 		}
2604e255b776SJohn Polstra #endif
260595d67482SBill Paul 		eh = mtod(m, struct ether_header *);
2606473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
260795d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
260895d67482SBill Paul 
2609b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
261095d67482SBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
261195d67482SBill Paul 			if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
261295d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2613d375e524SGleb Smirnoff 
2614d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2615d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
261695d67482SBill Paul 				m->m_pkthdr.csum_data =
261795d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
26180189c944SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
261995d67482SBill Paul 			}
262095d67482SBill Paul 		}
262195d67482SBill Paul 
262295d67482SBill Paul 		/*
2623673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2624673d9191SSam Leffler 		 * attach that information to the packet.
262595d67482SBill Paul 		 */
2626d147662cSGleb Smirnoff 		if (have_tag) {
2627d147662cSGleb Smirnoff 			VLAN_INPUT_TAG(ifp, m, vlan_tag);
2628d147662cSGleb Smirnoff 			if (m == NULL)
2629d147662cSGleb Smirnoff 				continue;
2630d147662cSGleb Smirnoff 		}
263195d67482SBill Paul 
26320f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2633673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
26340f9bd73bSSam Leffler 		BGE_LOCK(sc);
263595d67482SBill Paul 	}
263695d67482SBill Paul 
2637e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2638f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2639e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
26405dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2641e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2642e65bed95SPyun YongHyeon 		if (jumbocnt > 0)
2643f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2644f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_ring_map,
2645e65bed95SPyun YongHyeon 			    BUS_DMASYNC_PREWRITE);
2646f41ac2beSBill Paul 	}
2647f41ac2beSBill Paul 
264895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
264995d67482SBill Paul 	if (stdcnt)
265095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
265195d67482SBill Paul 	if (jumbocnt)
265295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
265395d67482SBill Paul 
265495d67482SBill Paul 	return;
265595d67482SBill Paul }
265695d67482SBill Paul 
265795d67482SBill Paul static void
265895d67482SBill Paul bge_txeof(sc)
265995d67482SBill Paul 	struct bge_softc *sc;
266095d67482SBill Paul {
266195d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
266295d67482SBill Paul 	struct ifnet *ifp;
266395d67482SBill Paul 
26640f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
26650f9bd73bSSam Leffler 
2666fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
266795d67482SBill Paul 
2668e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2669e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2670e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
267195d67482SBill Paul 	/*
267295d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
267395d67482SBill Paul 	 * frames that have been sent.
267495d67482SBill Paul 	 */
267595d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2676f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
267795d67482SBill Paul 		u_int32_t		idx = 0;
267895d67482SBill Paul 
267995d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2680f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
268195d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
268295d67482SBill Paul 			ifp->if_opackets++;
268395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2684e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2685e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2686e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2687f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2688f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2689e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2690e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
269195d67482SBill Paul 		}
269295d67482SBill Paul 		sc->bge_txcnt--;
269395d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
269495d67482SBill Paul 		ifp->if_timer = 0;
269595d67482SBill Paul 	}
269695d67482SBill Paul 
269795d67482SBill Paul 	if (cur_tx != NULL)
269813f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
269995d67482SBill Paul 
270095d67482SBill Paul 	return;
270195d67482SBill Paul }
270295d67482SBill Paul 
270375719184SGleb Smirnoff #ifdef DEVICE_POLLING
270475719184SGleb Smirnoff static void
270575719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
270675719184SGleb Smirnoff {
270775719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
270875719184SGleb Smirnoff 
270975719184SGleb Smirnoff 	BGE_LOCK(sc);
271075719184SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
271175719184SGleb Smirnoff 		bge_poll_locked(ifp, cmd, count);
271275719184SGleb Smirnoff 	BGE_UNLOCK(sc);
271375719184SGleb Smirnoff }
271475719184SGleb Smirnoff 
271575719184SGleb Smirnoff static void
271675719184SGleb Smirnoff bge_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
271775719184SGleb Smirnoff {
271875719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
271975719184SGleb Smirnoff 
272075719184SGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
272175719184SGleb Smirnoff 
272275719184SGleb Smirnoff 	sc->rxcycles = count;
272375719184SGleb Smirnoff 	bge_rxeof(sc);
272475719184SGleb Smirnoff 	bge_txeof(sc);
272575719184SGleb Smirnoff 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
272675719184SGleb Smirnoff 		bge_start_locked(ifp);
272775719184SGleb Smirnoff 
272875719184SGleb Smirnoff 	if (cmd == POLL_AND_CHECK_STATUS) {
2729dab5cd05SOleg Bulyzhin 		uint32_t statusword;
273075719184SGleb Smirnoff 
2731dab5cd05SOleg Bulyzhin 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2732e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2733dab5cd05SOleg Bulyzhin 
2734dab5cd05SOleg Bulyzhin 	    	statusword = atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
2735dab5cd05SOleg Bulyzhin 
2736dab5cd05SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
2737dab5cd05SOleg Bulyzhin 		    statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2738dab5cd05SOleg Bulyzhin 			bge_link_upd(sc);
2739dab5cd05SOleg Bulyzhin 
2740dab5cd05SOleg Bulyzhin 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2741e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
274275719184SGleb Smirnoff 	}
274375719184SGleb Smirnoff }
274475719184SGleb Smirnoff #endif /* DEVICE_POLLING */
274575719184SGleb Smirnoff 
274695d67482SBill Paul static void
274795d67482SBill Paul bge_intr(xsc)
274895d67482SBill Paul 	void *xsc;
274995d67482SBill Paul {
275095d67482SBill Paul 	struct bge_softc *sc;
275195d67482SBill Paul 	struct ifnet *ifp;
2752dab5cd05SOleg Bulyzhin 	uint32_t statusword;
275395d67482SBill Paul 
275495d67482SBill Paul 	sc = xsc;
2755f41ac2beSBill Paul 
27560f9bd73bSSam Leffler 	BGE_LOCK(sc);
27570f9bd73bSSam Leffler 
2758dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2759dab5cd05SOleg Bulyzhin 
276075719184SGleb Smirnoff #ifdef DEVICE_POLLING
276175719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
276275719184SGleb Smirnoff 		BGE_UNLOCK(sc);
276375719184SGleb Smirnoff 		return;
276475719184SGleb Smirnoff 	}
276575719184SGleb Smirnoff #endif
276675719184SGleb Smirnoff 
2767f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2768e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2769f41ac2beSBill Paul 
2770487a8c7eSPaul Saab 	statusword =
2771f41ac2beSBill Paul 	    atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
277295d67482SBill Paul 
277395d67482SBill Paul #ifdef notdef
277495d67482SBill Paul 	/* Avoid this for now -- checking this register is expensive. */
277595d67482SBill Paul 	/* Make sure this is really our interrupt. */
277695d67482SBill Paul 	if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
277795d67482SBill Paul 		return;
277895d67482SBill Paul #endif
277995d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
278095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
278195d67482SBill Paul 
2782dab5cd05SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
2783dab5cd05SOleg Bulyzhin 	    statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2784dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
278595d67482SBill Paul 
278613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
278795d67482SBill Paul 		/* Check RX return ring producer/consumer */
278895d67482SBill Paul 		bge_rxeof(sc);
278995d67482SBill Paul 
279095d67482SBill Paul 		/* Check TX ring producer/consumer */
279195d67482SBill Paul 		bge_txeof(sc);
279295d67482SBill Paul 	}
279395d67482SBill Paul 
279495d67482SBill Paul 	/* Re-enable interrupts. */
279595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
279695d67482SBill Paul 
279713f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
279813f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
27990f9bd73bSSam Leffler 		bge_start_locked(ifp);
28000f9bd73bSSam Leffler 
28010f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
280295d67482SBill Paul 
280395d67482SBill Paul 	return;
280495d67482SBill Paul }
280595d67482SBill Paul 
280695d67482SBill Paul static void
28070f9bd73bSSam Leffler bge_tick_locked(sc)
280895d67482SBill Paul 	struct bge_softc *sc;
28090f9bd73bSSam Leffler {
281095d67482SBill Paul 	struct mii_data *mii = NULL;
281195d67482SBill Paul 	struct ifnet *ifp;
281295d67482SBill Paul 
28130f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
281495d67482SBill Paul 
2815dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2816dab5cd05SOleg Bulyzhin 
2817e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2818e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
28190434d1b8SBill Paul 		bge_stats_update_regs(sc);
28200434d1b8SBill Paul 	else
282195d67482SBill Paul 		bge_stats_update(sc);
282295d67482SBill Paul 
282395d67482SBill Paul 	if (sc->bge_tbi) {
2824dab5cd05SOleg Bulyzhin 		if (!sc->bge_link) {
282595d67482SBill Paul 			if (CSR_READ_4(sc, BGE_MAC_STS) &
282695d67482SBill Paul 			    BGE_MACSTAT_TBI_PCS_SYNCHED) {
282795d67482SBill Paul 				sc->bge_link++;
2828da3003f0SBill Paul 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
2829da3003f0SBill Paul 					BGE_CLRBIT(sc, BGE_MAC_MODE,
2830da3003f0SBill Paul 					    BGE_MACMODE_TBI_SEND_CFGS);
283195d67482SBill Paul 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
2832649ce479SPoul-Henning Kamp 				if (bootverbose)
2833649ce479SPoul-Henning Kamp 					printf("bge%d: gigabit link up\n",
2834649ce479SPoul-Henning Kamp 					    sc->bge_unit);
28354d665c4dSDag-Erling Smørgrav 				if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
28360f9bd73bSSam Leffler 					bge_start_locked(ifp);
283795d67482SBill Paul 			}
283895d67482SBill Paul 		}
2839dab5cd05SOleg Bulyzhin 	}
2840dab5cd05SOleg Bulyzhin 	else {
284195d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
284295d67482SBill Paul 		mii_tick(mii);
284395d67482SBill Paul 
2844b2561871SJonathan Lemon 		if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE &&
284595d67482SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
284695d67482SBill Paul 			sc->bge_link++;
2847649ce479SPoul-Henning Kamp 			if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
2848649ce479SPoul-Henning Kamp 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)&&
2849649ce479SPoul-Henning Kamp 			    bootverbose)
2850649ce479SPoul-Henning Kamp 				printf("bge%d: gigabit link up\n", sc->bge_unit);
28514d665c4dSDag-Erling Smørgrav 			if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
28520f9bd73bSSam Leffler 				bge_start_locked(ifp);
285395d67482SBill Paul 		}
2854dab5cd05SOleg Bulyzhin 	}
285595d67482SBill Paul 
2856dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
285795d67482SBill Paul }
285895d67482SBill Paul 
285995d67482SBill Paul static void
28600f9bd73bSSam Leffler bge_tick(xsc)
28610f9bd73bSSam Leffler 	void *xsc;
28620f9bd73bSSam Leffler {
28630f9bd73bSSam Leffler 	struct bge_softc *sc;
28640f9bd73bSSam Leffler 
28650f9bd73bSSam Leffler 	sc = xsc;
28660f9bd73bSSam Leffler 
28670f9bd73bSSam Leffler 	BGE_LOCK(sc);
28680f9bd73bSSam Leffler 	bge_tick_locked(sc);
28690f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
28700f9bd73bSSam Leffler }
28710f9bd73bSSam Leffler 
28720f9bd73bSSam Leffler static void
28730434d1b8SBill Paul bge_stats_update_regs(sc)
28740434d1b8SBill Paul 	struct bge_softc *sc;
28750434d1b8SBill Paul {
28760434d1b8SBill Paul 	struct ifnet *ifp;
28770434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
28780434d1b8SBill Paul 	u_int32_t *s;
28790434d1b8SBill Paul 	int i;
28800434d1b8SBill Paul 
2881fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
28820434d1b8SBill Paul 
28830434d1b8SBill Paul 	s = (u_int32_t *)&stats;
28840434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
28850434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
28860434d1b8SBill Paul 		s++;
28870434d1b8SBill Paul 	}
28880434d1b8SBill Paul 
28890434d1b8SBill Paul 	ifp->if_collisions +=
28900434d1b8SBill Paul 	   (stats.dot3StatsSingleCollisionFrames +
28910434d1b8SBill Paul 	   stats.dot3StatsMultipleCollisionFrames +
28920434d1b8SBill Paul 	   stats.dot3StatsExcessiveCollisions +
28930434d1b8SBill Paul 	   stats.dot3StatsLateCollisions) -
28940434d1b8SBill Paul 	   ifp->if_collisions;
28950434d1b8SBill Paul 
28960434d1b8SBill Paul 	return;
28970434d1b8SBill Paul }
28980434d1b8SBill Paul 
28990434d1b8SBill Paul static void
290095d67482SBill Paul bge_stats_update(sc)
290195d67482SBill Paul 	struct bge_softc *sc;
290295d67482SBill Paul {
290395d67482SBill Paul 	struct ifnet *ifp;
2904e907febfSPyun YongHyeon 	bus_size_t stats;
290595d67482SBill Paul 
2906fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
290795d67482SBill Paul 
2908e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2909e907febfSPyun YongHyeon 
2910e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
2911e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
291295d67482SBill Paul 
291395d67482SBill Paul 	ifp->if_collisions +=
2914e907febfSPyun YongHyeon 	   (READ_STAT(sc, stats,
2915e907febfSPyun YongHyeon 		txstats.dot3StatsSingleCollisionFrames.bge_addr_lo) +
2916e907febfSPyun YongHyeon 	    READ_STAT(sc, stats,
2917e907febfSPyun YongHyeon 		txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo) +
2918e907febfSPyun YongHyeon 	    READ_STAT(sc, stats,
2919e907febfSPyun YongHyeon 		txstats.dot3StatsExcessiveCollisions.bge_addr_lo) +
2920e907febfSPyun YongHyeon 	    READ_STAT(sc, stats,
2921e907febfSPyun YongHyeon 		txstats.dot3StatsLateCollisions.bge_addr_lo)) -
292295d67482SBill Paul 	   ifp->if_collisions;
292395d67482SBill Paul 
2924e907febfSPyun YongHyeon #undef READ_STAT
2925e907febfSPyun YongHyeon 
292695d67482SBill Paul #ifdef notdef
292795d67482SBill Paul 	ifp->if_collisions +=
292895d67482SBill Paul 	   (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
292995d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
293095d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
293195d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
293295d67482SBill Paul 	   ifp->if_collisions;
293395d67482SBill Paul #endif
293495d67482SBill Paul 
293595d67482SBill Paul 	return;
293695d67482SBill Paul }
293795d67482SBill Paul 
293895d67482SBill Paul /*
2939d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
2940d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
2941d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
2942d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
2943d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
2944d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
2945d375e524SGleb Smirnoff  */
2946d375e524SGleb Smirnoff static __inline int
2947d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
2948d375e524SGleb Smirnoff {
2949d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
2950d375e524SGleb Smirnoff 	struct mbuf *last;
2951d375e524SGleb Smirnoff 
2952d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
2953d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
2954d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
2955d375e524SGleb Smirnoff 		last = m;
2956d375e524SGleb Smirnoff 	} else {
2957d375e524SGleb Smirnoff 		/*
2958d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
2959d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
2960d375e524SGleb Smirnoff 		 */
2961d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
2962d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
2963d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
2964d375e524SGleb Smirnoff 			struct mbuf *n;
2965d375e524SGleb Smirnoff 
2966d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
2967d375e524SGleb Smirnoff 			if (n == NULL)
2968d375e524SGleb Smirnoff 				return (ENOBUFS);
2969d375e524SGleb Smirnoff 			n->m_len = 0;
2970d375e524SGleb Smirnoff 			last->m_next = n;
2971d375e524SGleb Smirnoff 			last = n;
2972d375e524SGleb Smirnoff 		}
2973d375e524SGleb Smirnoff 	}
2974d375e524SGleb Smirnoff 
2975d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
2976d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
2977d375e524SGleb Smirnoff 	last->m_len += padlen;
2978d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
2979d375e524SGleb Smirnoff 
2980d375e524SGleb Smirnoff 	return (0);
2981d375e524SGleb Smirnoff }
2982d375e524SGleb Smirnoff 
2983d375e524SGleb Smirnoff /*
298495d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
298595d67482SBill Paul  * pointers to descriptors.
298695d67482SBill Paul  */
298795d67482SBill Paul static int
298895d67482SBill Paul bge_encap(sc, m_head, txidx)
298995d67482SBill Paul 	struct bge_softc *sc;
299095d67482SBill Paul 	struct mbuf *m_head;
29917e27542aSGleb Smirnoff 	uint32_t *txidx;
299295d67482SBill Paul {
29937e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
2994f41ac2beSBill Paul 	bus_dmamap_t		map;
29957e27542aSGleb Smirnoff 	struct bge_tx_bd	*d = NULL;
29967e27542aSGleb Smirnoff 	struct m_tag		*mtag;
29977e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
29987e27542aSGleb Smirnoff 	uint16_t		csum_flags = 0;
29997e27542aSGleb Smirnoff 	int			nsegs, i, error;
300095d67482SBill Paul 
300195d67482SBill Paul 	if (m_head->m_pkthdr.csum_flags) {
300295d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
300395d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3004d375e524SGleb Smirnoff 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
300595d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3006d375e524SGleb Smirnoff 			if (m_head->m_pkthdr.len < ETHER_MIN_NOPAD &&
3007d375e524SGleb Smirnoff 			    bge_cksum_pad(m_head) != 0)
3008d375e524SGleb Smirnoff 				return (ENOBUFS);
3009d375e524SGleb Smirnoff 		}
301095d67482SBill Paul 		if (m_head->m_flags & M_LASTFRAG)
301195d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
301295d67482SBill Paul 		else if (m_head->m_flags & M_FRAG)
301395d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
301495d67482SBill Paul 	}
301595d67482SBill Paul 
3016fc74a9f9SBrooks Davis 	mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m_head);
3017673d9191SSam Leffler 
30187e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
30197e27542aSGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map,
30207e27542aSGleb Smirnoff 	    m_head, segs, &nsegs, BUS_DMA_NOWAIT);
30217e27542aSGleb Smirnoff         if (error) {
30227e27542aSGleb Smirnoff 		if (error == EFBIG) {
30237e27542aSGleb Smirnoff 			struct mbuf *m0;
30247e27542aSGleb Smirnoff 
30257e27542aSGleb Smirnoff 			m0 = m_defrag(m_head, M_DONTWAIT);
30267e27542aSGleb Smirnoff 			if (m0 == NULL)
30277e27542aSGleb Smirnoff 				return (ENOBUFS);
30287e27542aSGleb Smirnoff 			m_head = m0;
30297e27542aSGleb Smirnoff 			error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag,
30307e27542aSGleb Smirnoff 			    map, m_head, segs, &nsegs, BUS_DMA_NOWAIT);
30317e27542aSGleb Smirnoff 		}
30327e27542aSGleb Smirnoff 		if (error)
30337e27542aSGleb Smirnoff 			return (error);
30347e27542aSGleb Smirnoff 	}
30357e27542aSGleb Smirnoff 
303695d67482SBill Paul 	/*
303795d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
303895d67482SBill Paul 	 * of the end of the ring.
303995d67482SBill Paul 	 */
30407e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
30417e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
304295d67482SBill Paul 		return (ENOBUFS);
30437e27542aSGleb Smirnoff 	}
30447e27542aSGleb Smirnoff 
3045e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3046e65bed95SPyun YongHyeon 
30477e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
30487e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
30497e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
30507e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
30517e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
30527e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
30537e27542aSGleb Smirnoff 		if (i == nsegs - 1)
30547e27542aSGleb Smirnoff 			break;
30557e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
30567e27542aSGleb Smirnoff 	}
30577e27542aSGleb Smirnoff 
30587e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
30597e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
30607e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
30617e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
30627e27542aSGleb Smirnoff 	if (mtag != NULL) {
30637e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
30647e27542aSGleb Smirnoff 		d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
30657e27542aSGleb Smirnoff 	} else
30667e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
3067f41ac2beSBill Paul 
3068f41ac2beSBill Paul 	/*
3069f41ac2beSBill Paul 	 * Insure that the map for this transmission
3070f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3071f41ac2beSBill Paul 	 * in this chain.
3072f41ac2beSBill Paul 	 */
30737e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
30747e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
30757e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m_head;
30767e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
307795d67482SBill Paul 
30787e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
30797e27542aSGleb Smirnoff 	*txidx = idx;
308095d67482SBill Paul 
308195d67482SBill Paul 	return (0);
308295d67482SBill Paul }
308395d67482SBill Paul 
308495d67482SBill Paul /*
308595d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
308695d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
308795d67482SBill Paul  */
308895d67482SBill Paul static void
30890f9bd73bSSam Leffler bge_start_locked(ifp)
309095d67482SBill Paul 	struct ifnet *ifp;
309195d67482SBill Paul {
309295d67482SBill Paul 	struct bge_softc *sc;
309395d67482SBill Paul 	struct mbuf *m_head = NULL;
309414bbd30fSGleb Smirnoff 	uint32_t prodidx;
3095303a718cSDag-Erling Smørgrav 	int count = 0;
309695d67482SBill Paul 
309795d67482SBill Paul 	sc = ifp->if_softc;
309895d67482SBill Paul 
3099dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
310095d67482SBill Paul 		return;
310195d67482SBill Paul 
310214bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
310395d67482SBill Paul 
310495d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
31054d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
310695d67482SBill Paul 		if (m_head == NULL)
310795d67482SBill Paul 			break;
310895d67482SBill Paul 
310995d67482SBill Paul 		/*
311095d67482SBill Paul 		 * XXX
3111b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3112b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3113b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3114b874fdd4SYaroslav Tykhiy 		 *
3115b874fdd4SYaroslav Tykhiy 		 * XXX
311695d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
311795d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
311895d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
311995d67482SBill Paul 		 * chain at once.
312095d67482SBill Paul 		 * (paranoia -- may not actually be needed)
312195d67482SBill Paul 		 */
312295d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
312395d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
312495d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
312595d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
31264d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
312713f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
312895d67482SBill Paul 				break;
312995d67482SBill Paul 			}
313095d67482SBill Paul 		}
313195d67482SBill Paul 
313295d67482SBill Paul 		/*
313395d67482SBill Paul 		 * Pack the data into the transmit ring. If we
313495d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
313595d67482SBill Paul 		 * for the NIC to drain the ring.
313695d67482SBill Paul 		 */
313795d67482SBill Paul 		if (bge_encap(sc, m_head, &prodidx)) {
31384d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
313913f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
314095d67482SBill Paul 			break;
314195d67482SBill Paul 		}
3142303a718cSDag-Erling Smørgrav 		++count;
314395d67482SBill Paul 
314495d67482SBill Paul 		/*
314595d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
314695d67482SBill Paul 		 * to him.
314795d67482SBill Paul 		 */
3148673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
314995d67482SBill Paul 	}
315095d67482SBill Paul 
3151303a718cSDag-Erling Smørgrav 	if (count == 0) {
3152303a718cSDag-Erling Smørgrav 		/* no packets were dequeued */
3153303a718cSDag-Erling Smørgrav 		return;
3154303a718cSDag-Erling Smørgrav 	}
3155303a718cSDag-Erling Smørgrav 
315695d67482SBill Paul 	/* Transmit */
315795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
31583927098fSPaul Saab 	/* 5700 b2 errata */
3159e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
31603927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
316195d67482SBill Paul 
316214bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
316314bbd30fSGleb Smirnoff 
316495d67482SBill Paul 	/*
316595d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
316695d67482SBill Paul 	 */
316795d67482SBill Paul 	ifp->if_timer = 5;
316895d67482SBill Paul 
316995d67482SBill Paul 	return;
317095d67482SBill Paul }
317195d67482SBill Paul 
31720f9bd73bSSam Leffler /*
31730f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
31740f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
31750f9bd73bSSam Leffler  */
317695d67482SBill Paul static void
31770f9bd73bSSam Leffler bge_start(ifp)
31780f9bd73bSSam Leffler 	struct ifnet *ifp;
317995d67482SBill Paul {
31800f9bd73bSSam Leffler 	struct bge_softc *sc;
31810f9bd73bSSam Leffler 
31820f9bd73bSSam Leffler 	sc = ifp->if_softc;
31830f9bd73bSSam Leffler 	BGE_LOCK(sc);
31840f9bd73bSSam Leffler 	bge_start_locked(ifp);
31850f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
31860f9bd73bSSam Leffler }
31870f9bd73bSSam Leffler 
31880f9bd73bSSam Leffler static void
31890f9bd73bSSam Leffler bge_init_locked(sc)
31900f9bd73bSSam Leffler 	struct bge_softc *sc;
31910f9bd73bSSam Leffler {
319295d67482SBill Paul 	struct ifnet *ifp;
319395d67482SBill Paul 	u_int16_t *m;
319495d67482SBill Paul 
31950f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
319695d67482SBill Paul 
3197fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
319895d67482SBill Paul 
319913f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
320095d67482SBill Paul 		return;
320195d67482SBill Paul 
320295d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
320395d67482SBill Paul 	bge_stop(sc);
320495d67482SBill Paul 	bge_reset(sc);
320595d67482SBill Paul 	bge_chipinit(sc);
320695d67482SBill Paul 
320795d67482SBill Paul 	/*
320895d67482SBill Paul 	 * Init the various state machines, ring
320995d67482SBill Paul 	 * control blocks and firmware.
321095d67482SBill Paul 	 */
321195d67482SBill Paul 	if (bge_blockinit(sc)) {
321295d67482SBill Paul 		printf("bge%d: initialization failure\n", sc->bge_unit);
321395d67482SBill Paul 		return;
321495d67482SBill Paul 	}
321595d67482SBill Paul 
3216fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
321795d67482SBill Paul 
321895d67482SBill Paul 	/* Specify MTU. */
321995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3220859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
322195d67482SBill Paul 
322295d67482SBill Paul 	/* Load our MAC address. */
32234a0d6638SRuslan Ermilov 	m = (u_int16_t *)IF_LLADDR(sc->bge_ifp);
322495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
322595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
322695d67482SBill Paul 
322795d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
322895d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
322995d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
323095d67482SBill Paul 	} else {
323195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
323295d67482SBill Paul 	}
323395d67482SBill Paul 
323495d67482SBill Paul 	/* Program multicast filter. */
323595d67482SBill Paul 	bge_setmulti(sc);
323695d67482SBill Paul 
323795d67482SBill Paul 	/* Init RX ring. */
323895d67482SBill Paul 	bge_init_rx_ring_std(sc);
323995d67482SBill Paul 
32400434d1b8SBill Paul 	/*
32410434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
32420434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
32430434d1b8SBill Paul 	 * entry of the ring.
32440434d1b8SBill Paul 	 */
32450434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
32460434d1b8SBill Paul 		u_int32_t		v, i;
32470434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
32480434d1b8SBill Paul 			DELAY(20);
32490434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
32500434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
32510434d1b8SBill Paul 				break;
32520434d1b8SBill Paul 		}
32530434d1b8SBill Paul 		if (i == 10)
32540434d1b8SBill Paul 			printf ("bge%d: 5705 A0 chip failed to load RX ring\n",
32550434d1b8SBill Paul 			    sc->bge_unit);
32560434d1b8SBill Paul 	}
32570434d1b8SBill Paul 
325895d67482SBill Paul 	/* Init jumbo RX ring. */
325995d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
326095d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
326195d67482SBill Paul 
326295d67482SBill Paul 	/* Init our RX return ring index */
326395d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
326495d67482SBill Paul 
326595d67482SBill Paul 	/* Init TX ring. */
326695d67482SBill Paul 	bge_init_tx_ring(sc);
326795d67482SBill Paul 
326895d67482SBill Paul 	/* Turn on transmitter */
326995d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
327095d67482SBill Paul 
327195d67482SBill Paul 	/* Turn on receiver */
327295d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
327395d67482SBill Paul 
327495d67482SBill Paul 	/* Tell firmware we're alive. */
327595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
327695d67482SBill Paul 
327775719184SGleb Smirnoff #ifdef DEVICE_POLLING
327875719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
327975719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
328075719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
328175719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
328275719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
328375719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
328475719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
328575719184SGleb Smirnoff 	} else
328675719184SGleb Smirnoff #endif
328775719184SGleb Smirnoff 
328895d67482SBill Paul 	/* Enable host interrupts. */
328975719184SGleb Smirnoff 	{
329095d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
329195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
329295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
329375719184SGleb Smirnoff 	}
329495d67482SBill Paul 
329595d67482SBill Paul 	bge_ifmedia_upd(ifp);
329695d67482SBill Paul 
329713f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
329813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
329995d67482SBill Paul 
33000f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
330195d67482SBill Paul 
33020f9bd73bSSam Leffler 	return;
33030f9bd73bSSam Leffler }
33040f9bd73bSSam Leffler 
33050f9bd73bSSam Leffler static void
33060f9bd73bSSam Leffler bge_init(xsc)
33070f9bd73bSSam Leffler 	void *xsc;
33080f9bd73bSSam Leffler {
33090f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
33100f9bd73bSSam Leffler 
33110f9bd73bSSam Leffler 	BGE_LOCK(sc);
33120f9bd73bSSam Leffler 	bge_init_locked(sc);
33130f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
331495d67482SBill Paul 
331595d67482SBill Paul 	return;
331695d67482SBill Paul }
331795d67482SBill Paul 
331895d67482SBill Paul /*
331995d67482SBill Paul  * Set media options.
332095d67482SBill Paul  */
332195d67482SBill Paul static int
332295d67482SBill Paul bge_ifmedia_upd(ifp)
332395d67482SBill Paul 	struct ifnet *ifp;
332495d67482SBill Paul {
332595d67482SBill Paul 	struct bge_softc *sc;
332695d67482SBill Paul 	struct mii_data *mii;
332795d67482SBill Paul 	struct ifmedia *ifm;
332895d67482SBill Paul 
332995d67482SBill Paul 	sc = ifp->if_softc;
333095d67482SBill Paul 	ifm = &sc->bge_ifmedia;
333195d67482SBill Paul 
333295d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
333395d67482SBill Paul 	if (sc->bge_tbi) {
333495d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
333595d67482SBill Paul 			return(EINVAL);
333695d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
333795d67482SBill Paul 		case IFM_AUTO:
3338ff50922bSDoug White #ifndef BGE_FAKE_AUTONEG
3339ff50922bSDoug White 			/*
3340ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3341ff50922bSDoug White 			 * mechanism for programming the autoneg
3342ff50922bSDoug White 			 * advertisement registers in TBI mode.
3343ff50922bSDoug White 			 */
3344ff50922bSDoug White 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3345ff50922bSDoug White 				uint32_t sgdig;
3346ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3347ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3348ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3349ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3350ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3351ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3352ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3353ff50922bSDoug White 				DELAY(5);
3354ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3355ff50922bSDoug White 			}
3356ff50922bSDoug White #endif
335795d67482SBill Paul 			break;
335895d67482SBill Paul 		case IFM_1000_SX:
335995d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
336095d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
336195d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
336295d67482SBill Paul 			} else {
336395d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
336495d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
336595d67482SBill Paul 			}
336695d67482SBill Paul 			break;
336795d67482SBill Paul 		default:
336895d67482SBill Paul 			return(EINVAL);
336995d67482SBill Paul 		}
337095d67482SBill Paul 		return(0);
337195d67482SBill Paul 	}
337295d67482SBill Paul 
337395d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
337495d67482SBill Paul 	sc->bge_link = 0;
337595d67482SBill Paul 	if (mii->mii_instance) {
337695d67482SBill Paul 		struct mii_softc *miisc;
337795d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
337895d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
337995d67482SBill Paul 			mii_phy_reset(miisc);
338095d67482SBill Paul 	}
338195d67482SBill Paul 	mii_mediachg(mii);
338295d67482SBill Paul 
338395d67482SBill Paul 	return(0);
338495d67482SBill Paul }
338595d67482SBill Paul 
338695d67482SBill Paul /*
338795d67482SBill Paul  * Report current media status.
338895d67482SBill Paul  */
338995d67482SBill Paul static void
339095d67482SBill Paul bge_ifmedia_sts(ifp, ifmr)
339195d67482SBill Paul 	struct ifnet *ifp;
339295d67482SBill Paul 	struct ifmediareq *ifmr;
339395d67482SBill Paul {
339495d67482SBill Paul 	struct bge_softc *sc;
339595d67482SBill Paul 	struct mii_data *mii;
339695d67482SBill Paul 
339795d67482SBill Paul 	sc = ifp->if_softc;
339895d67482SBill Paul 
339995d67482SBill Paul 	if (sc->bge_tbi) {
340095d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
340195d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
340295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
340395d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
340495d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
340595d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
340695d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
340795d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
340895d67482SBill Paul 		else
340995d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
341095d67482SBill Paul 		return;
341195d67482SBill Paul 	}
341295d67482SBill Paul 
341395d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
341495d67482SBill Paul 	mii_pollstat(mii);
341595d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
341695d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
341795d67482SBill Paul 
341895d67482SBill Paul 	return;
341995d67482SBill Paul }
342095d67482SBill Paul 
342195d67482SBill Paul static int
342295d67482SBill Paul bge_ioctl(ifp, command, data)
342395d67482SBill Paul 	struct ifnet *ifp;
342495d67482SBill Paul 	u_long command;
342595d67482SBill Paul 	caddr_t data;
342695d67482SBill Paul {
342795d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
342895d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
34290f9bd73bSSam Leffler 	int mask, error = 0;
343095d67482SBill Paul 	struct mii_data *mii;
343195d67482SBill Paul 
343295d67482SBill Paul 	switch(command) {
343395d67482SBill Paul 	case SIOCSIFMTU:
34340434d1b8SBill Paul 		/* Disallow jumbo frames on 5705. */
3435e53d81eeSPaul Saab 		if (((sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
3436e53d81eeSPaul Saab 		      sc->bge_asicrev == BGE_ASICREV_BCM5750) &&
34370434d1b8SBill Paul 		    ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU)
343895d67482SBill Paul 			error = EINVAL;
343995d67482SBill Paul 		else {
344095d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
344113f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
344295d67482SBill Paul 			bge_init(sc);
344395d67482SBill Paul 		}
344495d67482SBill Paul 		break;
344595d67482SBill Paul 	case SIOCSIFFLAGS:
34460f9bd73bSSam Leffler 		BGE_LOCK(sc);
344795d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
344895d67482SBill Paul 			/*
344995d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
345095d67482SBill Paul 			 * then just use the 'set promisc mode' command
345195d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
345295d67482SBill Paul 			 * a full re-init means reloading the firmware and
345395d67482SBill Paul 			 * waiting for it to start up, which may take a
345495d67482SBill Paul 			 * second or two.
345595d67482SBill Paul 			 */
345613f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
345795d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
345895d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
345995d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
346095d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
346113f4c340SRobert Watson 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
346295d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
346395d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
346495d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
346595d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
346695d67482SBill Paul 			} else
34670f9bd73bSSam Leffler 				bge_init_locked(sc);
346895d67482SBill Paul 		} else {
346913f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
347095d67482SBill Paul 				bge_stop(sc);
347195d67482SBill Paul 			}
347295d67482SBill Paul 		}
347395d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
34740f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
347595d67482SBill Paul 		error = 0;
347695d67482SBill Paul 		break;
347795d67482SBill Paul 	case SIOCADDMULTI:
347895d67482SBill Paul 	case SIOCDELMULTI:
347913f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
34800f9bd73bSSam Leffler 			BGE_LOCK(sc);
348195d67482SBill Paul 			bge_setmulti(sc);
34820f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
348395d67482SBill Paul 			error = 0;
348495d67482SBill Paul 		}
348595d67482SBill Paul 		break;
348695d67482SBill Paul 	case SIOCSIFMEDIA:
348795d67482SBill Paul 	case SIOCGIFMEDIA:
348895d67482SBill Paul 		if (sc->bge_tbi) {
348995d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
349095d67482SBill Paul 			    &sc->bge_ifmedia, command);
349195d67482SBill Paul 		} else {
349295d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
349395d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
349495d67482SBill Paul 			    &mii->mii_media, command);
349595d67482SBill Paul 		}
349695d67482SBill Paul 		break;
349795d67482SBill Paul 	case SIOCSIFCAP:
349895d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
349975719184SGleb Smirnoff #ifdef DEVICE_POLLING
350075719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
350175719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
350275719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
350375719184SGleb Smirnoff 				if (error)
350475719184SGleb Smirnoff 					return(error);
350575719184SGleb Smirnoff 				BGE_LOCK(sc);
350675719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
350775719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
350875719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
350975719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
351075719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
351175719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
351275719184SGleb Smirnoff 				BGE_UNLOCK(sc);
351375719184SGleb Smirnoff 			} else {
351475719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
351575719184SGleb Smirnoff 				/* Enable interrupt even in error case */
351675719184SGleb Smirnoff 				BGE_LOCK(sc);
351775719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
351875719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
351975719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
352075719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
352175719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
352275719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
352375719184SGleb Smirnoff 				BGE_UNLOCK(sc);
352475719184SGleb Smirnoff 			}
352575719184SGleb Smirnoff 		}
352675719184SGleb Smirnoff #endif
3527d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3528d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3529d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3530d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3531b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
353295d67482SBill Paul 			else
3533b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
353495d67482SBill Paul 		}
353595d67482SBill Paul 		break;
353695d67482SBill Paul 	default:
3537673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
353895d67482SBill Paul 		break;
353995d67482SBill Paul 	}
354095d67482SBill Paul 
354195d67482SBill Paul 	return(error);
354295d67482SBill Paul }
354395d67482SBill Paul 
354495d67482SBill Paul static void
354595d67482SBill Paul bge_watchdog(ifp)
354695d67482SBill Paul 	struct ifnet *ifp;
354795d67482SBill Paul {
354895d67482SBill Paul 	struct bge_softc *sc;
354995d67482SBill Paul 
355095d67482SBill Paul 	sc = ifp->if_softc;
355195d67482SBill Paul 
355295d67482SBill Paul 	printf("bge%d: watchdog timeout -- resetting\n", sc->bge_unit);
355395d67482SBill Paul 
355413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
355595d67482SBill Paul 	bge_init(sc);
355695d67482SBill Paul 
355795d67482SBill Paul 	ifp->if_oerrors++;
355895d67482SBill Paul 
355995d67482SBill Paul 	return;
356095d67482SBill Paul }
356195d67482SBill Paul 
356295d67482SBill Paul /*
356395d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
356495d67482SBill Paul  * RX and TX lists.
356595d67482SBill Paul  */
356695d67482SBill Paul static void
356795d67482SBill Paul bge_stop(sc)
356895d67482SBill Paul 	struct bge_softc *sc;
356995d67482SBill Paul {
357095d67482SBill Paul 	struct ifnet *ifp;
357195d67482SBill Paul 	struct ifmedia_entry *ifm;
357295d67482SBill Paul 	struct mii_data *mii = NULL;
357395d67482SBill Paul 	int mtmp, itmp;
357495d67482SBill Paul 
35750f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
35760f9bd73bSSam Leffler 
3577fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
357895d67482SBill Paul 
357995d67482SBill Paul 	if (!sc->bge_tbi)
358095d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
358195d67482SBill Paul 
35820f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
358395d67482SBill Paul 
358495d67482SBill Paul 	/*
358595d67482SBill Paul 	 * Disable all of the receiver blocks
358695d67482SBill Paul 	 */
358795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
358895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
358995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
35905dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3591e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
359295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
359395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
359495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
359595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
359695d67482SBill Paul 
359795d67482SBill Paul 	/*
359895d67482SBill Paul 	 * Disable all of the transmit blocks
359995d67482SBill Paul 	 */
360095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
360195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
360295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
360395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
360495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
36055dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3606e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
360795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
360895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
360995d67482SBill Paul 
361095d67482SBill Paul 	/*
361195d67482SBill Paul 	 * Shut down all of the memory managers and related
361295d67482SBill Paul 	 * state machines.
361395d67482SBill Paul 	 */
361495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
361595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
36165dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3617e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
361895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
361995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
362095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
36215dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3622e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
362395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
362495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
36250434d1b8SBill Paul 	}
362695d67482SBill Paul 
362795d67482SBill Paul 	/* Disable host interrupts. */
362895d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
362995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
363095d67482SBill Paul 
363195d67482SBill Paul 	/*
363295d67482SBill Paul 	 * Tell firmware we're shutting down.
363395d67482SBill Paul 	 */
363495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
363595d67482SBill Paul 
363695d67482SBill Paul 	/* Free the RX lists. */
363795d67482SBill Paul 	bge_free_rx_ring_std(sc);
363895d67482SBill Paul 
363995d67482SBill Paul 	/* Free jumbo RX list. */
36405dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3641e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
364295d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
364395d67482SBill Paul 
364495d67482SBill Paul 	/* Free TX buffers. */
364595d67482SBill Paul 	bge_free_tx_ring(sc);
364695d67482SBill Paul 
364795d67482SBill Paul 	/*
364895d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
364995d67482SBill Paul 	 * unchanged so that things will be put back to normal when
365095d67482SBill Paul 	 * we bring the interface back up.
365195d67482SBill Paul 	 */
365295d67482SBill Paul 	if (!sc->bge_tbi) {
365395d67482SBill Paul 		itmp = ifp->if_flags;
365495d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3655dcc34049SPawel Jakub Dawidek 		/*
3656dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3657dcc34049SPawel Jakub Dawidek 		 */
3658dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
365995d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
366095d67482SBill Paul 			mtmp = ifm->ifm_media;
366195d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
366295d67482SBill Paul 			mii_mediachg(mii);
366395d67482SBill Paul 			ifm->ifm_media = mtmp;
3664dcc34049SPawel Jakub Dawidek 		}
366595d67482SBill Paul 		ifp->if_flags = itmp;
366695d67482SBill Paul 	}
366795d67482SBill Paul 
366895d67482SBill Paul 	sc->bge_link = 0;
366995d67482SBill Paul 
367095d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
367195d67482SBill Paul 
367213f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
367395d67482SBill Paul 
367495d67482SBill Paul 	return;
367595d67482SBill Paul }
367695d67482SBill Paul 
367795d67482SBill Paul /*
367895d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
367995d67482SBill Paul  * get confused by errant DMAs when rebooting.
368095d67482SBill Paul  */
368195d67482SBill Paul static void
368295d67482SBill Paul bge_shutdown(dev)
368395d67482SBill Paul 	device_t dev;
368495d67482SBill Paul {
368595d67482SBill Paul 	struct bge_softc *sc;
368695d67482SBill Paul 
368795d67482SBill Paul 	sc = device_get_softc(dev);
368895d67482SBill Paul 
36890f9bd73bSSam Leffler 	BGE_LOCK(sc);
369095d67482SBill Paul 	bge_stop(sc);
369195d67482SBill Paul 	bge_reset(sc);
36920f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
369395d67482SBill Paul 
369495d67482SBill Paul 	return;
369595d67482SBill Paul }
369614afefa3SPawel Jakub Dawidek 
369714afefa3SPawel Jakub Dawidek static int
369814afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
369914afefa3SPawel Jakub Dawidek {
370014afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
370114afefa3SPawel Jakub Dawidek 
370214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
370314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
370414afefa3SPawel Jakub Dawidek 	bge_stop(sc);
370514afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
370614afefa3SPawel Jakub Dawidek 
370714afefa3SPawel Jakub Dawidek 	return (0);
370814afefa3SPawel Jakub Dawidek }
370914afefa3SPawel Jakub Dawidek 
371014afefa3SPawel Jakub Dawidek static int
371114afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
371214afefa3SPawel Jakub Dawidek {
371314afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
371414afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
371514afefa3SPawel Jakub Dawidek 
371614afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
371714afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
371814afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
371914afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
372014afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
372114afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
372214afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
372314afefa3SPawel Jakub Dawidek 	}
372414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
372514afefa3SPawel Jakub Dawidek 
372614afefa3SPawel Jakub Dawidek 	return (0);
372714afefa3SPawel Jakub Dawidek }
3728dab5cd05SOleg Bulyzhin 
3729dab5cd05SOleg Bulyzhin static void
3730dab5cd05SOleg Bulyzhin bge_link_upd(sc)
3731dab5cd05SOleg Bulyzhin 	struct bge_softc *sc;
3732dab5cd05SOleg Bulyzhin {
3733dab5cd05SOleg Bulyzhin 	uint32_t status;
3734dab5cd05SOleg Bulyzhin 
3735dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
3736dab5cd05SOleg Bulyzhin 	/*
3737dab5cd05SOleg Bulyzhin 	 * Process link state changes.
3738dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
3739dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
3740dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
3741dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
3742dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
3743dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
3744dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
3745dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
3746dab5cd05SOleg Bulyzhin 	 */
3747dab5cd05SOleg Bulyzhin 
3748dab5cd05SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700) {
3749dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
3750dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
3751dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
3752dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3753dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
3754dab5cd05SOleg Bulyzhin 			/* Clear the interrupt */
3755dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
3756dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
3757dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
3758dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
3759dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
3760dab5cd05SOleg Bulyzhin 		}
3761dab5cd05SOleg Bulyzhin 		return;
3762dab5cd05SOleg Bulyzhin 	}
3763dab5cd05SOleg Bulyzhin 
3764dab5cd05SOleg Bulyzhin 	/*
3765dab5cd05SOleg Bulyzhin 	 * Sometimes PCS encoding errors are detected in
3766dab5cd05SOleg Bulyzhin 	 * TBI mode (on fiber NICs), and for some reason
3767dab5cd05SOleg Bulyzhin 	 * the chip will signal them as link changes.
3768dab5cd05SOleg Bulyzhin 	 * If we get a link change event, but the 'PCS
3769dab5cd05SOleg Bulyzhin 	 * encoding error' bit in the MAC status register
3770dab5cd05SOleg Bulyzhin 	 * is set, don't bother doing a link check.
3771dab5cd05SOleg Bulyzhin 	 * This avoids spurious "gigabit link up" messages
3772dab5cd05SOleg Bulyzhin 	 * that sometimes appear on fiber NICs during
3773dab5cd05SOleg Bulyzhin 	 * periods of heavy traffic. (There should be no
3774dab5cd05SOleg Bulyzhin 	 * effect on copper NICs.)
3775dab5cd05SOleg Bulyzhin 	 */
37762778b70eSMarcel Moolenaar 	if (!sc->bge_tbi || ((status = CSR_READ_4(sc, BGE_MAC_STS)) &
37772778b70eSMarcel Moolenaar 	    (BGE_MACSTAT_PORT_DECODE_ERROR | BGE_MACSTAT_MI_COMPLETE)) == 0) {
3778dab5cd05SOleg Bulyzhin 		sc->bge_link = 0;
3779dab5cd05SOleg Bulyzhin 		callout_stop(&sc->bge_stat_ch);
3780dab5cd05SOleg Bulyzhin 		bge_tick_locked(sc);
3781dab5cd05SOleg Bulyzhin 	}
3782dab5cd05SOleg Bulyzhin 
3783dab5cd05SOleg Bulyzhin 	/* Clear the interrupt */
3784dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
3785dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
3786dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
3787dab5cd05SOleg Bulyzhin 
3788dab5cd05SOleg Bulyzhin 	/* Force flush the status block cached by PCI bridge */
3789dab5cd05SOleg Bulyzhin 	CSR_READ_4(sc, BGE_MBX_IRQ0_LO);
3790dab5cd05SOleg Bulyzhin }
3791dab5cd05SOleg Bulyzhin 
3792