xref: /freebsd/sys/dev/bge/if_bge.c (revision 1f313773f3f3d003b2ca77c1ff77fb76bf1fabd2)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
3895d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4195d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4295d67482SBill Paul  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
8295d67482SBill Paul 
8395d67482SBill Paul #include <net/if.h>
8495d67482SBill Paul #include <net/if_arp.h>
8595d67482SBill Paul #include <net/ethernet.h>
8695d67482SBill Paul #include <net/if_dl.h>
8795d67482SBill Paul #include <net/if_media.h>
8895d67482SBill Paul 
8995d67482SBill Paul #include <net/bpf.h>
9095d67482SBill Paul 
9195d67482SBill Paul #include <net/if_types.h>
9295d67482SBill Paul #include <net/if_vlan_var.h>
9395d67482SBill Paul 
9495d67482SBill Paul #include <netinet/in_systm.h>
9595d67482SBill Paul #include <netinet/in.h>
9695d67482SBill Paul #include <netinet/ip.h>
9795d67482SBill Paul 
9895d67482SBill Paul #include <machine/clock.h>      /* for DELAY */
9995d67482SBill Paul #include <machine/bus.h>
10095d67482SBill Paul #include <machine/resource.h>
10195d67482SBill Paul #include <sys/bus.h>
10295d67482SBill Paul #include <sys/rman.h>
10395d67482SBill Paul 
10495d67482SBill Paul #include <dev/mii/mii.h>
10595d67482SBill Paul #include <dev/mii/miivar.h>
1062d3ce713SDavid E. O'Brien #include "miidevs.h"
10795d67482SBill Paul #include <dev/mii/brgphyreg.h>
10895d67482SBill Paul 
1094fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1104fbd232cSWarner Losh #include <dev/pci/pcivar.h>
11195d67482SBill Paul 
11295d67482SBill Paul #include <dev/bge/if_bgereg.h>
11395d67482SBill Paul 
114ff50922bSDoug White #include "opt_bge.h"
115ff50922bSDoug White 
1165ddc5794SJohn Polstra #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
117d375e524SGleb Smirnoff #define ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
11895d67482SBill Paul 
119f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
120f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12195d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12295d67482SBill Paul 
1237b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12495d67482SBill Paul #include "miibus_if.h"
12595d67482SBill Paul 
12695d67482SBill Paul /*
12795d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
12895d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
12995d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13095d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13195d67482SBill Paul  */
132029e2ee3SJohn Polstra #define BGE_DEVDESC_MAX		64	/* Maximum device description length */
13395d67482SBill Paul 
13495d67482SBill Paul static struct bge_type bge_devs[] = {
13595d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5700,
13695d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
13795d67482SBill Paul 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5701,
13895d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
13995d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5700,
14095d67482SBill Paul 		"Broadcom BCM5700 Gigabit Ethernet" },
14195d67482SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5701,
14295d67482SBill Paul 		"Broadcom BCM5701 Gigabit Ethernet" },
1430434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702,
1440434d1b8SBill Paul 		"Broadcom BCM5702 Gigabit Ethernet" },
14501598b8dSMitsuru IWASAKI 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702X,
14601598b8dSMitsuru IWASAKI 		"Broadcom BCM5702X Gigabit Ethernet" },
1470434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703,
1480434d1b8SBill Paul 		"Broadcom BCM5703 Gigabit Ethernet" },
149b1265c1aSJohn Polstra 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703X,
150b1265c1aSJohn Polstra 		"Broadcom BCM5703X Gigabit Ethernet" },
1516ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704C,
1526ac6d2c8SPaul Saab 		"Broadcom BCM5704C Dual Gigabit Ethernet" },
1536ac6d2c8SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704S,
1546ac6d2c8SPaul Saab 		"Broadcom BCM5704S Dual Gigabit Ethernet" },
1550434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705,
1560434d1b8SBill Paul 		"Broadcom BCM5705 Gigabit Ethernet" },
157c001ccf2SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705K,
158c001ccf2SPaul Saab 		"Broadcom BCM5705K Gigabit Ethernet" },
1590434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M,
1600434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
1610434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M_ALT,
1620434d1b8SBill Paul 		"Broadcom BCM5705M Gigabit Ethernet" },
163419c028bSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5714C,
164419c028bSPaul Saab 		"Broadcom BCM5714C Gigabit Ethernet" },
16535ca8069SPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5721,
16635ca8069SPaul Saab 		"Broadcom BCM5721 Gigabit Ethernet" },
167e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5750,
168e53d81eeSPaul Saab 		"Broadcom BCM5750 Gigabit Ethernet" },
169e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5750M,
170e53d81eeSPaul Saab 		"Broadcom BCM5750M Gigabit Ethernet" },
171e53d81eeSPaul Saab 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5751,
172e53d81eeSPaul Saab 		"Broadcom BCM5751 Gigabit Ethernet" },
173d2014b30STai-hwa Liang 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5751M,
174d2014b30STai-hwa Liang 		"Broadcom BCM5751M Gigabit Ethernet" },
175560c1670SGleb Smirnoff 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5752,
176560c1670SGleb Smirnoff 		"Broadcom BCM5752 Gigabit Ethernet" },
1770434d1b8SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5782,
1780434d1b8SBill Paul 		"Broadcom BCM5782 Gigabit Ethernet" },
1799f71a4c2SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5788,
1809f71a4c2SBill Paul 		"Broadcom BCM5788 Gigabit Ethernet" },
181c3615d48SMike Silbersack 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5789,
182c3615d48SMike Silbersack 		"Broadcom BCM5789 Gigabit Ethernet" },
1835d99c641SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901,
1845d99c641SBill Paul 		"Broadcom BCM5901 Fast Ethernet" },
1855d99c641SBill Paul 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901A2,
1865d99c641SBill Paul 		"Broadcom BCM5901A2 Fast Ethernet" },
18795d67482SBill Paul 	{ SK_VENDORID, SK_DEVICEID_ALTIMA,
18895d67482SBill Paul 		"SysKonnect Gigabit Ethernet" },
189586d7c2eSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000,
190586d7c2eSJohn Polstra 		"Altima AC1000 Gigabit Ethernet" },
1912aae6624SBill Paul 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1002,
1922aae6624SBill Paul 		"Altima AC1002 Gigabit Ethernet" },
193470bd96aSJohn Polstra 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100,
194470bd96aSJohn Polstra 		"Altima AC9100 Gigabit Ethernet" },
19595d67482SBill Paul 	{ 0, 0, NULL }
19695d67482SBill Paul };
19795d67482SBill Paul 
198e51a25f8SAlfred Perlstein static int bge_probe		(device_t);
199e51a25f8SAlfred Perlstein static int bge_attach		(device_t);
200e51a25f8SAlfred Perlstein static int bge_detach		(device_t);
20114afefa3SPawel Jakub Dawidek static int bge_suspend		(device_t);
20214afefa3SPawel Jakub Dawidek static int bge_resume		(device_t);
20395d67482SBill Paul static void bge_release_resources
204e51a25f8SAlfred Perlstein 				(struct bge_softc *);
205f41ac2beSBill Paul static void bge_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
206f41ac2beSBill Paul static int bge_dma_alloc	(device_t);
207f41ac2beSBill Paul static void bge_dma_free	(struct bge_softc *);
208f41ac2beSBill Paul 
209e51a25f8SAlfred Perlstein static void bge_txeof		(struct bge_softc *);
210e51a25f8SAlfred Perlstein static void bge_rxeof		(struct bge_softc *);
21195d67482SBill Paul 
2120f9bd73bSSam Leffler static void bge_tick_locked	(struct bge_softc *);
213e51a25f8SAlfred Perlstein static void bge_tick		(void *);
214e51a25f8SAlfred Perlstein static void bge_stats_update	(struct bge_softc *);
2150434d1b8SBill Paul static void bge_stats_update_regs
2160434d1b8SBill Paul 				(struct bge_softc *);
217e51a25f8SAlfred Perlstein static int bge_encap		(struct bge_softc *, struct mbuf *,
218e51a25f8SAlfred Perlstein 					u_int32_t *);
21995d67482SBill Paul 
220e51a25f8SAlfred Perlstein static void bge_intr		(void *);
2210f9bd73bSSam Leffler static void bge_start_locked	(struct ifnet *);
222e51a25f8SAlfred Perlstein static void bge_start		(struct ifnet *);
223e51a25f8SAlfred Perlstein static int bge_ioctl		(struct ifnet *, u_long, caddr_t);
2240f9bd73bSSam Leffler static void bge_init_locked	(struct bge_softc *);
225e51a25f8SAlfred Perlstein static void bge_init		(void *);
226e51a25f8SAlfred Perlstein static void bge_stop		(struct bge_softc *);
227e51a25f8SAlfred Perlstein static void bge_watchdog		(struct ifnet *);
228e51a25f8SAlfred Perlstein static void bge_shutdown		(device_t);
229e51a25f8SAlfred Perlstein static int bge_ifmedia_upd	(struct ifnet *);
230e51a25f8SAlfred Perlstein static void bge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
23195d67482SBill Paul 
232e51a25f8SAlfred Perlstein static u_int8_t	bge_eeprom_getbyte	(struct bge_softc *, int, u_int8_t *);
233e51a25f8SAlfred Perlstein static int bge_read_eeprom	(struct bge_softc *, caddr_t, int, int);
23495d67482SBill Paul 
235e51a25f8SAlfred Perlstein static void bge_setmulti	(struct bge_softc *);
23695d67482SBill Paul 
237e51a25f8SAlfred Perlstein static int bge_newbuf_std	(struct bge_softc *, int, struct mbuf *);
238e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo	(struct bge_softc *, int, struct mbuf *);
239e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std	(struct bge_softc *);
240e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std	(struct bge_softc *);
241e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo	(struct bge_softc *);
242e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo	(struct bge_softc *);
243e51a25f8SAlfred Perlstein static void bge_free_tx_ring	(struct bge_softc *);
244e51a25f8SAlfred Perlstein static int bge_init_tx_ring	(struct bge_softc *);
24595d67482SBill Paul 
246e51a25f8SAlfred Perlstein static int bge_chipinit		(struct bge_softc *);
247e51a25f8SAlfred Perlstein static int bge_blockinit	(struct bge_softc *);
24895d67482SBill Paul 
2491b4a3b2fSPeter Wemm #ifdef notdef
250e51a25f8SAlfred Perlstein static u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
251e51a25f8SAlfred Perlstein static void bge_vpd_read_res	(struct bge_softc *, struct vpd_res *, int);
252e51a25f8SAlfred Perlstein static void bge_vpd_read	(struct bge_softc *);
2531b4a3b2fSPeter Wemm #endif
25495d67482SBill Paul 
25595d67482SBill Paul static u_int32_t bge_readmem_ind
256e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
257e51a25f8SAlfred Perlstein static void bge_writemem_ind	(struct bge_softc *, int, int);
25895d67482SBill Paul #ifdef notdef
25995d67482SBill Paul static u_int32_t bge_readreg_ind
260e51a25f8SAlfred Perlstein 				(struct bge_softc *, int);
26195d67482SBill Paul #endif
262e51a25f8SAlfred Perlstein static void bge_writereg_ind	(struct bge_softc *, int, int);
26395d67482SBill Paul 
264e51a25f8SAlfred Perlstein static int bge_miibus_readreg	(device_t, int, int);
265e51a25f8SAlfred Perlstein static int bge_miibus_writereg	(device_t, int, int, int);
266e51a25f8SAlfred Perlstein static void bge_miibus_statchg	(device_t);
26775719184SGleb Smirnoff #ifdef DEVICE_POLLING
26875719184SGleb Smirnoff static void bge_poll		(struct ifnet *ifp, enum poll_cmd cmd,
26975719184SGleb Smirnoff 				    int count);
27075719184SGleb Smirnoff static void bge_poll_locked	(struct ifnet *ifp, enum poll_cmd cmd,
27175719184SGleb Smirnoff 				    int count);
27275719184SGleb Smirnoff #endif
27395d67482SBill Paul 
274e51a25f8SAlfred Perlstein static void bge_reset		(struct bge_softc *);
275dab5cd05SOleg Bulyzhin static void bge_link_upd	(struct bge_softc *);
27695d67482SBill Paul 
27795d67482SBill Paul static device_method_t bge_methods[] = {
27895d67482SBill Paul 	/* Device interface */
27995d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
28095d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
28195d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
28295d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
28314afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
28414afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
28595d67482SBill Paul 
28695d67482SBill Paul 	/* bus interface */
28795d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
28895d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
28995d67482SBill Paul 
29095d67482SBill Paul 	/* MII interface */
29195d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
29295d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
29395d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
29495d67482SBill Paul 
29595d67482SBill Paul 	{ 0, 0 }
29695d67482SBill Paul };
29795d67482SBill Paul 
29895d67482SBill Paul static driver_t bge_driver = {
29995d67482SBill Paul 	"bge",
30095d67482SBill Paul 	bge_methods,
30195d67482SBill Paul 	sizeof(struct bge_softc)
30295d67482SBill Paul };
30395d67482SBill Paul 
30495d67482SBill Paul static devclass_t bge_devclass;
30595d67482SBill Paul 
306f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
30795d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
30895d67482SBill Paul 
30995d67482SBill Paul static u_int32_t
31095d67482SBill Paul bge_readmem_ind(sc, off)
31195d67482SBill Paul 	struct bge_softc *sc;
31295d67482SBill Paul 	int off;
31395d67482SBill Paul {
31495d67482SBill Paul 	device_t dev;
31595d67482SBill Paul 
31695d67482SBill Paul 	dev = sc->bge_dev;
31795d67482SBill Paul 
31895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
31995d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
32095d67482SBill Paul }
32195d67482SBill Paul 
32295d67482SBill Paul static void
32395d67482SBill Paul bge_writemem_ind(sc, off, val)
32495d67482SBill Paul 	struct bge_softc *sc;
32595d67482SBill Paul 	int off, val;
32695d67482SBill Paul {
32795d67482SBill Paul 	device_t dev;
32895d67482SBill Paul 
32995d67482SBill Paul 	dev = sc->bge_dev;
33095d67482SBill Paul 
33195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
33295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
33395d67482SBill Paul 
33495d67482SBill Paul 	return;
33595d67482SBill Paul }
33695d67482SBill Paul 
33795d67482SBill Paul #ifdef notdef
33895d67482SBill Paul static u_int32_t
33995d67482SBill Paul bge_readreg_ind(sc, off)
34095d67482SBill Paul 	struct bge_softc *sc;
34195d67482SBill Paul 	int off;
34295d67482SBill Paul {
34395d67482SBill Paul 	device_t dev;
34495d67482SBill Paul 
34595d67482SBill Paul 	dev = sc->bge_dev;
34695d67482SBill Paul 
34795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
34895d67482SBill Paul 	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
34995d67482SBill Paul }
35095d67482SBill Paul #endif
35195d67482SBill Paul 
35295d67482SBill Paul static void
35395d67482SBill Paul bge_writereg_ind(sc, off, val)
35495d67482SBill Paul 	struct bge_softc *sc;
35595d67482SBill Paul 	int off, val;
35695d67482SBill Paul {
35795d67482SBill Paul 	device_t dev;
35895d67482SBill Paul 
35995d67482SBill Paul 	dev = sc->bge_dev;
36095d67482SBill Paul 
36195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
36295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
36395d67482SBill Paul 
36495d67482SBill Paul 	return;
36595d67482SBill Paul }
36695d67482SBill Paul 
367f41ac2beSBill Paul /*
368f41ac2beSBill Paul  * Map a single buffer address.
369f41ac2beSBill Paul  */
370f41ac2beSBill Paul 
371f41ac2beSBill Paul static void
372f41ac2beSBill Paul bge_dma_map_addr(arg, segs, nseg, error)
373f41ac2beSBill Paul 	void *arg;
374f41ac2beSBill Paul 	bus_dma_segment_t *segs;
375f41ac2beSBill Paul 	int nseg;
376f41ac2beSBill Paul 	int error;
377f41ac2beSBill Paul {
378f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
379f41ac2beSBill Paul 
380f41ac2beSBill Paul 	if (error)
381f41ac2beSBill Paul 		return;
382f41ac2beSBill Paul 
383f41ac2beSBill Paul 	ctx = arg;
384f41ac2beSBill Paul 
385f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
386f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
387f41ac2beSBill Paul 		return;
388f41ac2beSBill Paul 	}
389f41ac2beSBill Paul 
390f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
391f41ac2beSBill Paul 
392f41ac2beSBill Paul 	return;
393f41ac2beSBill Paul }
394f41ac2beSBill Paul 
3951b4a3b2fSPeter Wemm #ifdef notdef
39695d67482SBill Paul static u_int8_t
39795d67482SBill Paul bge_vpd_readbyte(sc, addr)
39895d67482SBill Paul 	struct bge_softc *sc;
39995d67482SBill Paul 	int addr;
40095d67482SBill Paul {
40195d67482SBill Paul 	int i;
40295d67482SBill Paul 	device_t dev;
40395d67482SBill Paul 	u_int32_t val;
40495d67482SBill Paul 
40595d67482SBill Paul 	dev = sc->bge_dev;
40695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
40795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
40895d67482SBill Paul 		DELAY(10);
40995d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
41095d67482SBill Paul 			break;
41195d67482SBill Paul 	}
41295d67482SBill Paul 
41395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
414fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "VPD read timed out\n");
41595d67482SBill Paul 		return(0);
41695d67482SBill Paul 	}
41795d67482SBill Paul 
41895d67482SBill Paul 	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
41995d67482SBill Paul 
42095d67482SBill Paul 	return((val >> ((addr % 4) * 8)) & 0xFF);
42195d67482SBill Paul }
42295d67482SBill Paul 
42395d67482SBill Paul static void
42495d67482SBill Paul bge_vpd_read_res(sc, res, addr)
42595d67482SBill Paul 	struct bge_softc *sc;
42695d67482SBill Paul 	struct vpd_res *res;
42795d67482SBill Paul 	int addr;
42895d67482SBill Paul {
42995d67482SBill Paul 	int i;
43095d67482SBill Paul 	u_int8_t *ptr;
43195d67482SBill Paul 
43295d67482SBill Paul 	ptr = (u_int8_t *)res;
43395d67482SBill Paul 	for (i = 0; i < sizeof(struct vpd_res); i++)
43495d67482SBill Paul 		ptr[i] = bge_vpd_readbyte(sc, i + addr);
43595d67482SBill Paul 
43695d67482SBill Paul 	return;
43795d67482SBill Paul }
43895d67482SBill Paul 
43995d67482SBill Paul static void
44095d67482SBill Paul bge_vpd_read(sc)
44195d67482SBill Paul 	struct bge_softc *sc;
44295d67482SBill Paul {
44395d67482SBill Paul 	int pos = 0, i;
44495d67482SBill Paul 	struct vpd_res res;
44595d67482SBill Paul 
44695d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
44795d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
44895d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
44995d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
45095d67482SBill Paul 	sc->bge_vpd_prodname = NULL;
45195d67482SBill Paul 	sc->bge_vpd_readonly = NULL;
45295d67482SBill Paul 
45395d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
45495d67482SBill Paul 
45595d67482SBill Paul 	if (res.vr_id != VPD_RES_ID) {
456fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
457fe806fdaSPyun YongHyeon 		    "bad VPD resource id: expected %x got %x\n", VPD_RES_ID,
458fe806fdaSPyun YongHyeon 		    res.vr_id);
45995d67482SBill Paul 		return;
46095d67482SBill Paul 	}
46195d67482SBill Paul 
46295d67482SBill Paul 	pos += sizeof(res);
46395d67482SBill Paul 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
46495d67482SBill Paul 	for (i = 0; i < res.vr_len; i++)
46595d67482SBill Paul 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
46695d67482SBill Paul 	sc->bge_vpd_prodname[i] = '\0';
46795d67482SBill Paul 	pos += i;
46895d67482SBill Paul 
46995d67482SBill Paul 	bge_vpd_read_res(sc, &res, pos);
47095d67482SBill Paul 
47195d67482SBill Paul 	if (res.vr_id != VPD_RES_READ) {
472fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
473fe806fdaSPyun YongHyeon 		    "bad VPD resource id: expected %x got %x\n", VPD_RES_READ,
474fe806fdaSPyun YongHyeon 		    res.vr_id);
47595d67482SBill Paul 		return;
47695d67482SBill Paul 	}
47795d67482SBill Paul 
47895d67482SBill Paul 	pos += sizeof(res);
47995d67482SBill Paul 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
48095d67482SBill Paul 	for (i = 0; i < res.vr_len + 1; i++)
48195d67482SBill Paul 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
48295d67482SBill Paul 
48395d67482SBill Paul 	return;
48495d67482SBill Paul }
4851b4a3b2fSPeter Wemm #endif
48695d67482SBill Paul 
48795d67482SBill Paul /*
48895d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
48995d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
49095d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
49195d67482SBill Paul  * access method.
49295d67482SBill Paul  */
49395d67482SBill Paul static u_int8_t
49495d67482SBill Paul bge_eeprom_getbyte(sc, addr, dest)
49595d67482SBill Paul 	struct bge_softc *sc;
49695d67482SBill Paul 	int addr;
49795d67482SBill Paul 	u_int8_t *dest;
49895d67482SBill Paul {
49995d67482SBill Paul 	int i;
50095d67482SBill Paul 	u_int32_t byte = 0;
50195d67482SBill Paul 
50295d67482SBill Paul 	/*
50395d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
50495d67482SBill Paul 	 * having to use the bitbang method.
50595d67482SBill Paul 	 */
50695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
50795d67482SBill Paul 
50895d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
50995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
51095d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
51195d67482SBill Paul 	DELAY(20);
51295d67482SBill Paul 
51395d67482SBill Paul 	/* Issue the read EEPROM command. */
51495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
51595d67482SBill Paul 
51695d67482SBill Paul 	/* Wait for completion */
51795d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
51895d67482SBill Paul 		DELAY(10);
51995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
52095d67482SBill Paul 			break;
52195d67482SBill Paul 	}
52295d67482SBill Paul 
52395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
524fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
525f6789fbaSPyun YongHyeon 		return(1);
52695d67482SBill Paul 	}
52795d67482SBill Paul 
52895d67482SBill Paul 	/* Get result. */
52995d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
53095d67482SBill Paul 
53195d67482SBill Paul 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
53295d67482SBill Paul 
53395d67482SBill Paul 	return(0);
53495d67482SBill Paul }
53595d67482SBill Paul 
53695d67482SBill Paul /*
53795d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
53895d67482SBill Paul  */
53995d67482SBill Paul static int
54095d67482SBill Paul bge_read_eeprom(sc, dest, off, cnt)
54195d67482SBill Paul 	struct bge_softc *sc;
54295d67482SBill Paul 	caddr_t dest;
54395d67482SBill Paul 	int off;
54495d67482SBill Paul 	int cnt;
54595d67482SBill Paul {
54695d67482SBill Paul 	int err = 0, i;
54795d67482SBill Paul 	u_int8_t byte = 0;
54895d67482SBill Paul 
54995d67482SBill Paul 	for (i = 0; i < cnt; i++) {
55095d67482SBill Paul 		err = bge_eeprom_getbyte(sc, off + i, &byte);
55195d67482SBill Paul 		if (err)
55295d67482SBill Paul 			break;
55395d67482SBill Paul 		*(dest + i) = byte;
55495d67482SBill Paul 	}
55595d67482SBill Paul 
55695d67482SBill Paul 	return(err ? 1 : 0);
55795d67482SBill Paul }
55895d67482SBill Paul 
55995d67482SBill Paul static int
56095d67482SBill Paul bge_miibus_readreg(dev, phy, reg)
56195d67482SBill Paul 	device_t dev;
56295d67482SBill Paul 	int phy, reg;
56395d67482SBill Paul {
56495d67482SBill Paul 	struct bge_softc *sc;
56537ceeb4dSPaul Saab 	u_int32_t val, autopoll;
56695d67482SBill Paul 	int i;
56795d67482SBill Paul 
56895d67482SBill Paul 	sc = device_get_softc(dev);
56995d67482SBill Paul 
5700434d1b8SBill Paul 	/*
5710434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5720434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5730434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5740434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5750434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5760434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5770434d1b8SBill Paul 	 * special-cased.
5780434d1b8SBill Paul 	 */
579b1265c1aSJohn Polstra 	if (phy != 1)
58098b28ee5SBill Paul 		return(0);
58198b28ee5SBill Paul 
58237ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
58337ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
58437ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
58537ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
58637ceeb4dSPaul Saab 		DELAY(40);
58737ceeb4dSPaul Saab 	}
58837ceeb4dSPaul Saab 
58995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
59095d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
59195d67482SBill Paul 
59295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
59395d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
59495d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
59595d67482SBill Paul 			break;
59695d67482SBill Paul 	}
59795d67482SBill Paul 
59895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
599fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
60037ceeb4dSPaul Saab 		val = 0;
60137ceeb4dSPaul Saab 		goto done;
60295d67482SBill Paul 	}
60395d67482SBill Paul 
60495d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
60595d67482SBill Paul 
60637ceeb4dSPaul Saab done:
60737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
60837ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
60937ceeb4dSPaul Saab 		DELAY(40);
61037ceeb4dSPaul Saab 	}
61137ceeb4dSPaul Saab 
61295d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
61395d67482SBill Paul 		return(0);
61495d67482SBill Paul 
61595d67482SBill Paul 	return(val & 0xFFFF);
61695d67482SBill Paul }
61795d67482SBill Paul 
61895d67482SBill Paul static int
61995d67482SBill Paul bge_miibus_writereg(dev, phy, reg, val)
62095d67482SBill Paul 	device_t dev;
62195d67482SBill Paul 	int phy, reg, val;
62295d67482SBill Paul {
62395d67482SBill Paul 	struct bge_softc *sc;
62437ceeb4dSPaul Saab 	u_int32_t autopoll;
62595d67482SBill Paul 	int i;
62695d67482SBill Paul 
62795d67482SBill Paul 	sc = device_get_softc(dev);
62895d67482SBill Paul 
62937ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
63037ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
63137ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
63237ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
63337ceeb4dSPaul Saab 		DELAY(40);
63437ceeb4dSPaul Saab 	}
63537ceeb4dSPaul Saab 
63695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
63795d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
63895d67482SBill Paul 
63995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
64095d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
64195d67482SBill Paul 			break;
64295d67482SBill Paul 	}
64395d67482SBill Paul 
64437ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
64537ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
64637ceeb4dSPaul Saab 		DELAY(40);
64737ceeb4dSPaul Saab 	}
64837ceeb4dSPaul Saab 
64995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
650fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
65195d67482SBill Paul 		return(0);
65295d67482SBill Paul 	}
65395d67482SBill Paul 
65495d67482SBill Paul 	return(0);
65595d67482SBill Paul }
65695d67482SBill Paul 
65795d67482SBill Paul static void
65895d67482SBill Paul bge_miibus_statchg(dev)
65995d67482SBill Paul 	device_t dev;
66095d67482SBill Paul {
66195d67482SBill Paul 	struct bge_softc *sc;
66295d67482SBill Paul 	struct mii_data *mii;
66395d67482SBill Paul 
66495d67482SBill Paul 	sc = device_get_softc(dev);
66595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
66695d67482SBill Paul 
66795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
668b418ad5cSPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
66995d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
67095d67482SBill Paul 	} else {
67195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
67295d67482SBill Paul 	}
67395d67482SBill Paul 
67495d67482SBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
67595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
67695d67482SBill Paul 	} else {
67795d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
67895d67482SBill Paul 	}
67995d67482SBill Paul 
68095d67482SBill Paul 	return;
68195d67482SBill Paul }
68295d67482SBill Paul 
68395d67482SBill Paul /*
68495d67482SBill Paul  * Intialize a standard receive ring descriptor.
68595d67482SBill Paul  */
68695d67482SBill Paul static int
68795d67482SBill Paul bge_newbuf_std(sc, i, m)
68895d67482SBill Paul 	struct bge_softc	*sc;
68995d67482SBill Paul 	int			i;
69095d67482SBill Paul 	struct mbuf		*m;
69195d67482SBill Paul {
69295d67482SBill Paul 	struct mbuf		*m_new = NULL;
69395d67482SBill Paul 	struct bge_rx_bd	*r;
694f41ac2beSBill Paul 	struct bge_dmamap_arg	ctx;
695f41ac2beSBill Paul 	int			error;
69695d67482SBill Paul 
69795d67482SBill Paul 	if (m == NULL) {
698a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
69995d67482SBill Paul 		if (m_new == NULL) {
70095d67482SBill Paul 			return(ENOBUFS);
70195d67482SBill Paul 		}
70295d67482SBill Paul 
703a163d034SWarner Losh 		MCLGET(m_new, M_DONTWAIT);
70495d67482SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
70595d67482SBill Paul 			m_freem(m_new);
70695d67482SBill Paul 			return(ENOBUFS);
70795d67482SBill Paul 		}
70895d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70995d67482SBill Paul 	} else {
71095d67482SBill Paul 		m_new = m;
71195d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
71295d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
71395d67482SBill Paul 	}
71495d67482SBill Paul 
715e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
71695d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
71795d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
718f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
719f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
720f41ac2beSBill Paul 	ctx.sc = sc;
721f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
722f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
723f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
724f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
725f7cea149SGleb Smirnoff 		if (m == NULL) {
726f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
727f41ac2beSBill Paul 			m_freem(m_new);
728f7cea149SGleb Smirnoff 		}
729f41ac2beSBill Paul 		return(ENOMEM);
730f41ac2beSBill Paul 	}
731e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
732e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
733e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
734e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
735e907febfSPyun YongHyeon 	r->bge_idx = i;
736f41ac2beSBill Paul 
737f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
738f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
739f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
74095d67482SBill Paul 
74195d67482SBill Paul 	return(0);
74295d67482SBill Paul }
74395d67482SBill Paul 
74495d67482SBill Paul /*
74595d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
74695d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
74795d67482SBill Paul  */
74895d67482SBill Paul static int
74995d67482SBill Paul bge_newbuf_jumbo(sc, i, m)
75095d67482SBill Paul 	struct bge_softc *sc;
75195d67482SBill Paul 	int i;
75295d67482SBill Paul 	struct mbuf *m;
75395d67482SBill Paul {
7541be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
7551be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
75695d67482SBill Paul 	struct mbuf *m_new = NULL;
7571be6acb7SGleb Smirnoff 	int nsegs;
758f41ac2beSBill Paul 	int error;
75995d67482SBill Paul 
76095d67482SBill Paul 	if (m == NULL) {
761a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
7621be6acb7SGleb Smirnoff 		if (m_new == NULL)
76395d67482SBill Paul 			return(ENOBUFS);
76495d67482SBill Paul 
7651be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7661be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
76795d67482SBill Paul 			m_freem(m_new);
76895d67482SBill Paul 			return(ENOBUFS);
76995d67482SBill Paul 		}
7701be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
77195d67482SBill Paul 	} else {
77295d67482SBill Paul 		m_new = m;
7731be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
77495d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
77595d67482SBill Paul 	}
77695d67482SBill Paul 
777e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
77895d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7791be6acb7SGleb Smirnoff 
7801be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7811be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7821be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7831be6acb7SGleb Smirnoff 	if (error) {
7841be6acb7SGleb Smirnoff 		if (m == NULL)
785f41ac2beSBill Paul 			m_freem(m_new);
7861be6acb7SGleb Smirnoff 		return(error);
787f7cea149SGleb Smirnoff 	}
7881be6acb7SGleb Smirnoff 	KASSERT(nsegs == BGE_NSEG_JUMBO, ("%s: %d segments", __func__, nsegs));
7891be6acb7SGleb Smirnoff 
7901be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
7911be6acb7SGleb Smirnoff 
7921be6acb7SGleb Smirnoff 	/*
7931be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
7941be6acb7SGleb Smirnoff 	 */
7951be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
796e907febfSPyun YongHyeon 	r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
797e907febfSPyun YongHyeon 	r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
798e907febfSPyun YongHyeon 	r->bge_len0 = segs[0].ds_len;
799e907febfSPyun YongHyeon 	r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
800e907febfSPyun YongHyeon 	r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
801e907febfSPyun YongHyeon 	r->bge_len1 = segs[1].ds_len;
802e907febfSPyun YongHyeon 	r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
803e907febfSPyun YongHyeon 	r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
804e907febfSPyun YongHyeon 	r->bge_len2 = segs[2].ds_len;
805e907febfSPyun YongHyeon 	r->bge_len3 = 0;
806e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING|BGE_RXBDFLAG_END;
807e907febfSPyun YongHyeon 	r->bge_idx = i;
808f41ac2beSBill Paul 
809f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
810f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
811f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
81295d67482SBill Paul 
81395d67482SBill Paul 	return (0);
81495d67482SBill Paul }
81595d67482SBill Paul 
81695d67482SBill Paul /*
81795d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
81895d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
81995d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
82095d67482SBill Paul  * the NIC.
82195d67482SBill Paul  */
82295d67482SBill Paul static int
82395d67482SBill Paul bge_init_rx_ring_std(sc)
82495d67482SBill Paul 	struct bge_softc *sc;
82595d67482SBill Paul {
82695d67482SBill Paul 	int i;
82795d67482SBill Paul 
82895d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
82995d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
83095d67482SBill Paul 			return(ENOBUFS);
83195d67482SBill Paul 	};
83295d67482SBill Paul 
833f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
834f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
835f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
836f41ac2beSBill Paul 
83795d67482SBill Paul 	sc->bge_std = i - 1;
83895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
83995d67482SBill Paul 
84095d67482SBill Paul 	return(0);
84195d67482SBill Paul }
84295d67482SBill Paul 
84395d67482SBill Paul static void
84495d67482SBill Paul bge_free_rx_ring_std(sc)
84595d67482SBill Paul 	struct bge_softc *sc;
84695d67482SBill Paul {
84795d67482SBill Paul 	int i;
84895d67482SBill Paul 
84995d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
85095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
851e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
852e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
853e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
854f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
855f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
856e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
857e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
85895d67482SBill Paul 		}
859f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
86095d67482SBill Paul 		    sizeof(struct bge_rx_bd));
86195d67482SBill Paul 	}
86295d67482SBill Paul 
86395d67482SBill Paul 	return;
86495d67482SBill Paul }
86595d67482SBill Paul 
86695d67482SBill Paul static int
86795d67482SBill Paul bge_init_rx_ring_jumbo(sc)
86895d67482SBill Paul 	struct bge_softc *sc;
86995d67482SBill Paul {
87095d67482SBill Paul 	struct bge_rcb *rcb;
8711be6acb7SGleb Smirnoff 	int i;
87295d67482SBill Paul 
87395d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
87495d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
87595d67482SBill Paul 			return(ENOBUFS);
87695d67482SBill Paul 	};
87795d67482SBill Paul 
878f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
879f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
880f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
881f41ac2beSBill Paul 
88295d67482SBill Paul 	sc->bge_jumbo = i - 1;
88395d67482SBill Paul 
884f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8851be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8861be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
88767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
88895d67482SBill Paul 
88995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
89095d67482SBill Paul 
89195d67482SBill Paul 	return(0);
89295d67482SBill Paul }
89395d67482SBill Paul 
89495d67482SBill Paul static void
89595d67482SBill Paul bge_free_rx_ring_jumbo(sc)
89695d67482SBill Paul 	struct bge_softc *sc;
89795d67482SBill Paul {
89895d67482SBill Paul 	int i;
89995d67482SBill Paul 
90095d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
90195d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
902e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
903e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
904e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
905f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
906f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
907e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
908e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
90995d67482SBill Paul 		}
910f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
9111be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
91295d67482SBill Paul 	}
91395d67482SBill Paul 
91495d67482SBill Paul 	return;
91595d67482SBill Paul }
91695d67482SBill Paul 
91795d67482SBill Paul static void
91895d67482SBill Paul bge_free_tx_ring(sc)
91995d67482SBill Paul 	struct bge_softc *sc;
92095d67482SBill Paul {
92195d67482SBill Paul 	int i;
92295d67482SBill Paul 
923f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
92495d67482SBill Paul 		return;
92595d67482SBill Paul 
92695d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
92795d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
928e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
929e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
930e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
931f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
932f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
933e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
934e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
93595d67482SBill Paul 		}
936f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
93795d67482SBill Paul 		    sizeof(struct bge_tx_bd));
93895d67482SBill Paul 	}
93995d67482SBill Paul 
94095d67482SBill Paul 	return;
94195d67482SBill Paul }
94295d67482SBill Paul 
94395d67482SBill Paul static int
94495d67482SBill Paul bge_init_tx_ring(sc)
94595d67482SBill Paul 	struct bge_softc *sc;
94695d67482SBill Paul {
94795d67482SBill Paul 	sc->bge_txcnt = 0;
94895d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9493927098fSPaul Saab 
95014bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
95114bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
95214bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
95314bbd30fSGleb Smirnoff 
9543927098fSPaul Saab 	/* 5700 b2 errata */
955e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
95614bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
9573927098fSPaul Saab 
95814bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
9593927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
9603927098fSPaul Saab 	/* 5700 b2 errata */
961e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
96295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
96395d67482SBill Paul 
96495d67482SBill Paul 	return(0);
96595d67482SBill Paul }
96695d67482SBill Paul 
96795d67482SBill Paul static void
96895d67482SBill Paul bge_setmulti(sc)
96995d67482SBill Paul 	struct bge_softc *sc;
97095d67482SBill Paul {
97195d67482SBill Paul 	struct ifnet *ifp;
97295d67482SBill Paul 	struct ifmultiaddr *ifma;
97395d67482SBill Paul 	u_int32_t hashes[4] = { 0, 0, 0, 0 };
97495d67482SBill Paul 	int h, i;
97595d67482SBill Paul 
9760f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9770f9bd73bSSam Leffler 
978fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
97995d67482SBill Paul 
98095d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
98195d67482SBill Paul 		for (i = 0; i < 4; i++)
98295d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
98395d67482SBill Paul 		return;
98495d67482SBill Paul 	}
98595d67482SBill Paul 
98695d67482SBill Paul 	/* First, zot all the existing filters. */
98795d67482SBill Paul 	for (i = 0; i < 4; i++)
98895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
98995d67482SBill Paul 
99095d67482SBill Paul 	/* Now program new ones. */
99113b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
99295d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
99395d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
99495d67482SBill Paul 			continue;
9950e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
9960e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
99795d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
99895d67482SBill Paul 	}
99913b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
100095d67482SBill Paul 
100195d67482SBill Paul 	for (i = 0; i < 4; i++)
100295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
100395d67482SBill Paul 
100495d67482SBill Paul 	return;
100595d67482SBill Paul }
100695d67482SBill Paul 
100795d67482SBill Paul /*
100895d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
100995d67482SBill Paul  * self-test results.
101095d67482SBill Paul  */
101195d67482SBill Paul static int
101295d67482SBill Paul bge_chipinit(sc)
101395d67482SBill Paul 	struct bge_softc *sc;
101495d67482SBill Paul {
101595d67482SBill Paul 	int			i;
10165cba12d3SPaul Saab 	u_int32_t		dma_rw_ctl;
101795d67482SBill Paul 
1018e907febfSPyun YongHyeon 	/* Set endian type before we access any non-PCI registers. */
1019e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
102095d67482SBill Paul 
102195d67482SBill Paul 	/*
102295d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
102395d67482SBill Paul 	 * self-tests passed.
102495d67482SBill Paul 	 */
102595d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1026fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
102795d67482SBill Paul 		return(ENODEV);
102895d67482SBill Paul 	}
102995d67482SBill Paul 
103095d67482SBill Paul 	/* Clear the MAC control register */
103195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
103295d67482SBill Paul 
103395d67482SBill Paul 	/*
103495d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
103595d67482SBill Paul 	 * internal memory.
103695d67482SBill Paul 	 */
103795d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
103895d67482SBill Paul 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
103995d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
104095d67482SBill Paul 
104195d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
104295d67482SBill Paul 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
104395d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
104495d67482SBill Paul 
104595d67482SBill Paul 	/* Set up the PCI DMA control register. */
1046e53d81eeSPaul Saab 	if (sc->bge_pcie) {
1047e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1048e53d81eeSPaul Saab 		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1049e53d81eeSPaul Saab 		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1050e53d81eeSPaul Saab 	} else if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
10518287860eSJohn Polstra 	    BGE_PCISTATE_PCI_BUSMODE) {
10528287860eSJohn Polstra 		/* Conventional PCI bus */
10535cba12d3SPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10545cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10555cba12d3SPaul Saab 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10565cba12d3SPaul Saab 		    (0x0F);
10578287860eSJohn Polstra 	} else {
10588287860eSJohn Polstra 		/* PCI-X bus */
10595cba12d3SPaul Saab 		/*
10605cba12d3SPaul Saab 		 * The 5704 uses a different encoding of read/write
10615cba12d3SPaul Saab 		 * watermarks.
10625cba12d3SPaul Saab 		 */
1063e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
10645cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10655cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10665cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
10675cba12d3SPaul Saab 		else
10685cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10695cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10705cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10715cba12d3SPaul Saab 			    (0x0F);
10725cba12d3SPaul Saab 
10735cba12d3SPaul Saab 		/*
10745cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
10755cba12d3SPaul Saab 		 * for hardware bugs.
10765cba12d3SPaul Saab 		 */
1077e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1078e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
10795cba12d3SPaul Saab 			u_int32_t tmp;
10805cba12d3SPaul Saab 
10815cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
10825cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
10835cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
10848287860eSJohn Polstra 		}
10855cba12d3SPaul Saab 	}
10865cba12d3SPaul Saab 
1087e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
10880434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
1089e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1090e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
10915cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
10925cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
109395d67482SBill Paul 
109495d67482SBill Paul 	/*
109595d67482SBill Paul 	 * Set up general mode register.
109695d67482SBill Paul 	 */
1097e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
109895d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1099e446dc86SPaul Saab 	    BGE_MODECTL_TX_NO_PHDR_CSUM|BGE_MODECTL_RX_NO_PHDR_CSUM);
110095d67482SBill Paul 
110195d67482SBill Paul 	/*
1102ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1103ea13bdd5SJohn Polstra 	 * properly by these devices.
110495d67482SBill Paul 	 */
1105ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
110695d67482SBill Paul 
110795d67482SBill Paul #ifdef __brokenalpha__
110895d67482SBill Paul 	/*
110995d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
111095d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
111195d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
111295d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
111395d67482SBill Paul 	 */
111462f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
111562f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
111695d67482SBill Paul #endif
111795d67482SBill Paul 
111895d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
111995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
112095d67482SBill Paul 
112195d67482SBill Paul 	return(0);
112295d67482SBill Paul }
112395d67482SBill Paul 
112495d67482SBill Paul static int
112595d67482SBill Paul bge_blockinit(sc)
112695d67482SBill Paul 	struct bge_softc *sc;
112795d67482SBill Paul {
112895d67482SBill Paul 	struct bge_rcb *rcb;
1129e907febfSPyun YongHyeon 	bus_size_t vrcb;
1130e907febfSPyun YongHyeon 	bge_hostaddr taddr;
113195d67482SBill Paul 	int i;
113295d67482SBill Paul 
113395d67482SBill Paul 	/*
113495d67482SBill Paul 	 * Initialize the memory window pointer register so that
113595d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
113695d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
113795d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
113895d67482SBill Paul 	 */
113995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
114095d67482SBill Paul 
1141822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1142822f63fcSBill Paul 
11435dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1144e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
114595d67482SBill Paul 		/* Configure mbuf memory pool */
114695d67482SBill Paul 		if (sc->bge_extram) {
11470434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11480434d1b8SBill Paul 			    BGE_EXT_SSRAM);
1149822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1150822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1151822f63fcSBill Paul 			else
115295d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
115395d67482SBill Paul 		} else {
11540434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11550434d1b8SBill Paul 			    BGE_BUFFPOOL_1);
1156822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1157822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1158822f63fcSBill Paul 			else
115995d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
116095d67482SBill Paul 		}
116195d67482SBill Paul 
116295d67482SBill Paul 		/* Configure DMA resource pool */
11630434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
11640434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
116595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
11660434d1b8SBill Paul 	}
116795d67482SBill Paul 
116895d67482SBill Paul 	/* Configure mbuf pool watermarks */
1169e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1170e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750) {
11710434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11720434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
11730434d1b8SBill Paul 	} else {
1174fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1175fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
11760434d1b8SBill Paul 	}
1177fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
117895d67482SBill Paul 
117995d67482SBill Paul 	/* Configure DMA resource watermarks */
118095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
118195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
118295d67482SBill Paul 
118395d67482SBill Paul 	/* Enable buffer manager */
11845dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1185e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
118695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
118795d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
118895d67482SBill Paul 
118995d67482SBill Paul 		/* Poll for buffer manager start indication */
119095d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
119195d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
119295d67482SBill Paul 				break;
119395d67482SBill Paul 			DELAY(10);
119495d67482SBill Paul 		}
119595d67482SBill Paul 
119695d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1197fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1198fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
119995d67482SBill Paul 			return(ENXIO);
120095d67482SBill Paul 		}
12010434d1b8SBill Paul 	}
120295d67482SBill Paul 
120395d67482SBill Paul 	/* Enable flow-through queues */
120495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
120595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
120695d67482SBill Paul 
120795d67482SBill Paul 	/* Wait until queue initialization is complete */
120895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
120995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
121095d67482SBill Paul 			break;
121195d67482SBill Paul 		DELAY(10);
121295d67482SBill Paul 	}
121395d67482SBill Paul 
121495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1215fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
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) {
1387fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1388fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
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);
15151f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
15161f313773SOleg Bulyzhin 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B1)
1517a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1518a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1519a1d52896SBill Paul 	}
152095d67482SBill Paul 
15211f313773SOleg Bulyzhin 	/*
15221f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
15231f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
15241f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
15251f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
15261f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
15271f313773SOleg Bulyzhin 	 */
15281f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15291f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15301f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
15311f313773SOleg Bulyzhin 
153295d67482SBill Paul 	/* Enable link state change attentions. */
153395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
153495d67482SBill Paul 
153595d67482SBill Paul 	return(0);
153695d67482SBill Paul }
153795d67482SBill Paul 
153895d67482SBill Paul /*
153995d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
154095d67482SBill Paul  * against our list and return its name if we find a match. Note
154195d67482SBill Paul  * that since the Broadcom controller contains VPD support, we
154295d67482SBill Paul  * can get the device name string from the controller itself instead
154395d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
154495d67482SBill Paul  * we'll always announce the right product name.
154595d67482SBill Paul  */
154695d67482SBill Paul static int
154795d67482SBill Paul bge_probe(dev)
154895d67482SBill Paul 	device_t dev;
154995d67482SBill Paul {
155095d67482SBill Paul 	struct bge_type *t;
155195d67482SBill Paul 	struct bge_softc *sc;
1552029e2ee3SJohn Polstra 	char *descbuf;
155395d67482SBill Paul 
155495d67482SBill Paul 	t = bge_devs;
155595d67482SBill Paul 
155695d67482SBill Paul 	sc = device_get_softc(dev);
155795d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
155895d67482SBill Paul 	sc->bge_dev = dev;
155995d67482SBill Paul 
156095d67482SBill Paul 	while(t->bge_name != NULL) {
156195d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
156295d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
156395d67482SBill Paul #ifdef notdef
156495d67482SBill Paul 			bge_vpd_read(sc);
156595d67482SBill Paul 			device_set_desc(dev, sc->bge_vpd_prodname);
156695d67482SBill Paul #endif
1567029e2ee3SJohn Polstra 			descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
1568029e2ee3SJohn Polstra 			if (descbuf == NULL)
1569029e2ee3SJohn Polstra 				return(ENOMEM);
1570029e2ee3SJohn Polstra 			snprintf(descbuf, BGE_DEVDESC_MAX,
1571029e2ee3SJohn Polstra 			    "%s, ASIC rev. %#04x", t->bge_name,
1572029e2ee3SJohn Polstra 			    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16);
1573029e2ee3SJohn Polstra 			device_set_desc_copy(dev, descbuf);
15746d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
15756d2a9bd6SDoug Ambrisko 				sc->bge_no_3_led = 1;
1576029e2ee3SJohn Polstra 			free(descbuf, M_TEMP);
157795d67482SBill Paul 			return(0);
157895d67482SBill Paul 		}
157995d67482SBill Paul 		t++;
158095d67482SBill Paul 	}
158195d67482SBill Paul 
158295d67482SBill Paul 	return(ENXIO);
158395d67482SBill Paul }
158495d67482SBill Paul 
1585f41ac2beSBill Paul static void
1586f41ac2beSBill Paul bge_dma_free(sc)
1587f41ac2beSBill Paul 	struct bge_softc *sc;
1588f41ac2beSBill Paul {
1589f41ac2beSBill Paul 	int i;
1590f41ac2beSBill Paul 
1591f41ac2beSBill Paul 
1592f41ac2beSBill Paul 	/* Destroy DMA maps for RX buffers */
1593f41ac2beSBill Paul 
1594f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1595f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1596f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1597f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1598f41ac2beSBill Paul 	}
1599f41ac2beSBill Paul 
1600f41ac2beSBill Paul 	/* Destroy DMA maps for jumbo RX buffers */
1601f41ac2beSBill Paul 
1602f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1603f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1604f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1605f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1606f41ac2beSBill Paul 	}
1607f41ac2beSBill Paul 
1608f41ac2beSBill Paul 	/* Destroy DMA maps for TX buffers */
1609f41ac2beSBill Paul 
1610f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1611f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1612f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1613f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1614f41ac2beSBill Paul 	}
1615f41ac2beSBill Paul 
1616f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1617f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1618f41ac2beSBill Paul 
1619f41ac2beSBill Paul 
1620f41ac2beSBill Paul 	/* Destroy standard RX ring */
1621f41ac2beSBill Paul 
1622e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1623e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1624e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1625e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1626f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1627f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1628f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1629f41ac2beSBill Paul 
1630f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1631f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1632f41ac2beSBill Paul 
1633f41ac2beSBill Paul 	/* Destroy jumbo RX ring */
1634f41ac2beSBill Paul 
1635e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1636e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1637e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1638e65bed95SPyun YongHyeon 
1639e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1640e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1641f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1642f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1643f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1644f41ac2beSBill Paul 
1645f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1646f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1647f41ac2beSBill Paul 
1648f41ac2beSBill Paul 	/* Destroy RX return ring */
1649f41ac2beSBill Paul 
1650e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1651e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1652e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1653e65bed95SPyun YongHyeon 
1654e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1655e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1656f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1657f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1658f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1659f41ac2beSBill Paul 
1660f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1661f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1662f41ac2beSBill Paul 
1663f41ac2beSBill Paul 	/* Destroy TX ring */
1664f41ac2beSBill Paul 
1665e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1666e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1667e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1668e65bed95SPyun YongHyeon 
1669e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1670f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1671f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1672f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1673f41ac2beSBill Paul 
1674f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1675f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1676f41ac2beSBill Paul 
1677f41ac2beSBill Paul 	/* Destroy status block */
1678f41ac2beSBill Paul 
1679e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1680e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1681e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1682e65bed95SPyun YongHyeon 
1683e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1684f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1685f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1686f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1687f41ac2beSBill Paul 
1688f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1689f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1690f41ac2beSBill Paul 
1691f41ac2beSBill Paul 	/* Destroy statistics block */
1692f41ac2beSBill Paul 
1693e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1694e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1695e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1696e65bed95SPyun YongHyeon 
1697e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1698f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1699f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1700f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1701f41ac2beSBill Paul 
1702f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1703f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1704f41ac2beSBill Paul 
1705f41ac2beSBill Paul 	/* Destroy the parent tag */
1706f41ac2beSBill Paul 
1707f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1708f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1709f41ac2beSBill Paul 
1710f41ac2beSBill Paul 	return;
1711f41ac2beSBill Paul }
1712f41ac2beSBill Paul 
1713f41ac2beSBill Paul static int
1714f41ac2beSBill Paul bge_dma_alloc(dev)
1715f41ac2beSBill Paul 	device_t dev;
1716f41ac2beSBill Paul {
1717f41ac2beSBill Paul 	struct bge_softc *sc;
17181be6acb7SGleb Smirnoff 	int i, error;
1719f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
1720f41ac2beSBill Paul 
1721f41ac2beSBill Paul 	sc = device_get_softc(dev);
1722f41ac2beSBill Paul 
1723f41ac2beSBill Paul 	/*
1724f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1725f41ac2beSBill Paul 	 */
1726f41ac2beSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1727f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1728f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
17292f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1730f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1731f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1732f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
17338a40c10eSScott Long 			0,			/* flags */
1734f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1735f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1736f41ac2beSBill Paul 
1737e65bed95SPyun YongHyeon 	if (error != 0) {
1738fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1739fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1740e65bed95SPyun YongHyeon 		return (ENOMEM);
1741e65bed95SPyun YongHyeon 	}
1742e65bed95SPyun YongHyeon 
1743f41ac2beSBill Paul 	/*
1744f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1745f41ac2beSBill Paul 	 */
17468a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1747f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
17481be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
17491be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1750f41ac2beSBill Paul 
1751f41ac2beSBill Paul 	if (error) {
1752fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1753f41ac2beSBill Paul 		return (ENOMEM);
1754f41ac2beSBill Paul 	}
1755f41ac2beSBill Paul 
1756f41ac2beSBill Paul 	/* Create DMA maps for RX buffers */
1757f41ac2beSBill Paul 
1758f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1759f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1760f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1761f41ac2beSBill Paul 		if (error) {
1762fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1763fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1764f41ac2beSBill Paul 			return(ENOMEM);
1765f41ac2beSBill Paul 		}
1766f41ac2beSBill Paul 	}
1767f41ac2beSBill Paul 
1768f41ac2beSBill Paul 	/* Create DMA maps for TX buffers */
1769f41ac2beSBill Paul 
1770f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1771f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1772f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1773f41ac2beSBill Paul 		if (error) {
1774fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1775fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1776f41ac2beSBill Paul 			return(ENOMEM);
1777f41ac2beSBill Paul 		}
1778f41ac2beSBill Paul 	}
1779f41ac2beSBill Paul 
1780f41ac2beSBill Paul 	/* Create tag for standard RX ring */
1781f41ac2beSBill Paul 
1782f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1783f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1784f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1785f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1786f41ac2beSBill Paul 
1787f41ac2beSBill Paul 	if (error) {
1788fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1789f41ac2beSBill Paul 		return (ENOMEM);
1790f41ac2beSBill Paul 	}
1791f41ac2beSBill Paul 
1792f41ac2beSBill Paul 	/* Allocate DMA'able memory for standard RX ring */
1793f41ac2beSBill Paul 
1794f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1795f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1796f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1797f41ac2beSBill Paul 	if (error)
1798f41ac2beSBill Paul 		return (ENOMEM);
1799f41ac2beSBill Paul 
1800f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1801f41ac2beSBill Paul 
1802f41ac2beSBill Paul 	/* Load the address of the standard RX ring */
1803f41ac2beSBill Paul 
1804f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1805f41ac2beSBill Paul 	ctx.sc = sc;
1806f41ac2beSBill Paul 
1807f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1808f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1809f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1810f41ac2beSBill Paul 
1811f41ac2beSBill Paul 	if (error)
1812f41ac2beSBill Paul 		return (ENOMEM);
1813f41ac2beSBill Paul 
1814f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1815f41ac2beSBill Paul 
18165dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1817e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1818f41ac2beSBill Paul 
1819f41ac2beSBill Paul 		/*
1820f41ac2beSBill Paul 		 * Create tag for jumbo mbufs.
1821f41ac2beSBill Paul 		 * This is really a bit of a kludge. We allocate a special
1822f41ac2beSBill Paul 		 * jumbo buffer pool which (thanks to the way our DMA
1823f41ac2beSBill Paul 		 * memory allocation works) will consist of contiguous
1824f41ac2beSBill Paul 		 * pages. This means that even though a jumbo buffer might
1825f41ac2beSBill Paul 		 * be larger than a page size, we don't really need to
1826f41ac2beSBill Paul 		 * map it into more than one DMA segment. However, the
1827f41ac2beSBill Paul 		 * default mbuf tag will result in multi-segment mappings,
1828f41ac2beSBill Paul 		 * so we have to create a special jumbo mbuf tag that
1829f41ac2beSBill Paul 		 * lets us get away with mapping the jumbo buffers as
1830f41ac2beSBill Paul 		 * a single segment. I think eventually the driver should
1831f41ac2beSBill Paul 		 * be changed so that it uses ordinary mbufs and cluster
1832f41ac2beSBill Paul 		 * buffers, i.e. jumbo frames can span multiple DMA
1833f41ac2beSBill Paul 		 * descriptors. But that's a project for another day.
1834f41ac2beSBill Paul 		 */
1835f41ac2beSBill Paul 
1836f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
18378a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
18381be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
18391be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1840f41ac2beSBill Paul 
1841f41ac2beSBill Paul 		if (error) {
1842fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1843fe806fdaSPyun YongHyeon 			    "could not allocate dma tag\n");
1844f41ac2beSBill Paul 			return (ENOMEM);
1845f41ac2beSBill Paul 		}
1846f41ac2beSBill Paul 
1847f41ac2beSBill Paul 		/* Create tag for jumbo RX ring */
1848f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1849f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1850f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1851f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1852f41ac2beSBill Paul 
1853f41ac2beSBill Paul 		if (error) {
1854fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1855fe806fdaSPyun YongHyeon 			    "could not allocate dma tag\n");
1856f41ac2beSBill Paul 			return (ENOMEM);
1857f41ac2beSBill Paul 		}
1858f41ac2beSBill Paul 
1859f41ac2beSBill Paul 		/* Allocate DMA'able memory for jumbo RX ring */
1860f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
18611be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
18621be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1863f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1864f41ac2beSBill Paul 		if (error)
1865f41ac2beSBill Paul 			return (ENOMEM);
1866f41ac2beSBill Paul 
1867f41ac2beSBill Paul 		/* Load the address of the jumbo RX ring */
1868f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1869f41ac2beSBill Paul 		ctx.sc = sc;
1870f41ac2beSBill Paul 
1871f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1872f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1873f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1874f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1875f41ac2beSBill Paul 
1876f41ac2beSBill Paul 		if (error)
1877f41ac2beSBill Paul 			return (ENOMEM);
1878f41ac2beSBill Paul 
1879f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1880f41ac2beSBill Paul 
1881f41ac2beSBill Paul 		/* Create DMA maps for jumbo RX buffers */
1882f41ac2beSBill Paul 
1883f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1884f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1885f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1886f41ac2beSBill Paul 			if (error) {
1887fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
1888f41ac2beSBill Paul 				    "can't create DMA map for RX\n");
1889f41ac2beSBill Paul 				return(ENOMEM);
1890f41ac2beSBill Paul 			}
1891f41ac2beSBill Paul 		}
1892f41ac2beSBill Paul 
1893f41ac2beSBill Paul 	}
1894f41ac2beSBill Paul 
1895f41ac2beSBill Paul 	/* Create tag for RX return ring */
1896f41ac2beSBill Paul 
1897f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1898f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1899f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1900f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1901f41ac2beSBill Paul 
1902f41ac2beSBill Paul 	if (error) {
1903fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1904f41ac2beSBill Paul 		return (ENOMEM);
1905f41ac2beSBill Paul 	}
1906f41ac2beSBill Paul 
1907f41ac2beSBill Paul 	/* Allocate DMA'able memory for RX return ring */
1908f41ac2beSBill Paul 
1909f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1910f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1911f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1912f41ac2beSBill Paul 	if (error)
1913f41ac2beSBill Paul 		return (ENOMEM);
1914f41ac2beSBill Paul 
1915f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1916f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1917f41ac2beSBill Paul 
1918f41ac2beSBill Paul 	/* Load the address of the RX return ring */
1919f41ac2beSBill Paul 
1920f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1921f41ac2beSBill Paul 	ctx.sc = sc;
1922f41ac2beSBill Paul 
1923f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
1924f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
1925f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
1926f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1927f41ac2beSBill Paul 
1928f41ac2beSBill Paul 	if (error)
1929f41ac2beSBill Paul 		return (ENOMEM);
1930f41ac2beSBill Paul 
1931f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
1932f41ac2beSBill Paul 
1933f41ac2beSBill Paul 	/* Create tag for TX ring */
1934f41ac2beSBill Paul 
1935f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1936f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1937f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
1938f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
1939f41ac2beSBill Paul 
1940f41ac2beSBill Paul 	if (error) {
1941fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1942f41ac2beSBill Paul 		return (ENOMEM);
1943f41ac2beSBill Paul 	}
1944f41ac2beSBill Paul 
1945f41ac2beSBill Paul 	/* Allocate DMA'able memory for TX ring */
1946f41ac2beSBill Paul 
1947f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
1948f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
1949f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
1950f41ac2beSBill Paul 	if (error)
1951f41ac2beSBill Paul 		return (ENOMEM);
1952f41ac2beSBill Paul 
1953f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1954f41ac2beSBill Paul 
1955f41ac2beSBill Paul 	/* Load the address of the TX ring */
1956f41ac2beSBill Paul 
1957f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1958f41ac2beSBill Paul 	ctx.sc = sc;
1959f41ac2beSBill Paul 
1960f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
1961f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
1962f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1963f41ac2beSBill Paul 
1964f41ac2beSBill Paul 	if (error)
1965f41ac2beSBill Paul 		return (ENOMEM);
1966f41ac2beSBill Paul 
1967f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
1968f41ac2beSBill Paul 
1969f41ac2beSBill Paul 	/* Create tag for status block */
1970f41ac2beSBill Paul 
1971f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1972f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1973f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
1974f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
1975f41ac2beSBill Paul 
1976f41ac2beSBill Paul 	if (error) {
1977fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1978f41ac2beSBill Paul 		return (ENOMEM);
1979f41ac2beSBill Paul 	}
1980f41ac2beSBill Paul 
1981f41ac2beSBill Paul 	/* Allocate DMA'able memory for status block */
1982f41ac2beSBill Paul 
1983f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
1984f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
1985f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
1986f41ac2beSBill Paul 	if (error)
1987f41ac2beSBill Paul 		return (ENOMEM);
1988f41ac2beSBill Paul 
1989f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1990f41ac2beSBill Paul 
1991f41ac2beSBill Paul 	/* Load the address of the status block */
1992f41ac2beSBill Paul 
1993f41ac2beSBill Paul 	ctx.sc = sc;
1994f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1995f41ac2beSBill Paul 
1996f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
1997f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
1998f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1999f41ac2beSBill Paul 
2000f41ac2beSBill Paul 	if (error)
2001f41ac2beSBill Paul 		return (ENOMEM);
2002f41ac2beSBill Paul 
2003f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2004f41ac2beSBill Paul 
2005f41ac2beSBill Paul 	/* Create tag for statistics block */
2006f41ac2beSBill Paul 
2007f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2008f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2009f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2010f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2011f41ac2beSBill Paul 
2012f41ac2beSBill Paul 	if (error) {
2013fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2014f41ac2beSBill Paul 		return (ENOMEM);
2015f41ac2beSBill Paul 	}
2016f41ac2beSBill Paul 
2017f41ac2beSBill Paul 	/* Allocate DMA'able memory for statistics block */
2018f41ac2beSBill Paul 
2019f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2020f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2021f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2022f41ac2beSBill Paul 	if (error)
2023f41ac2beSBill Paul 		return (ENOMEM);
2024f41ac2beSBill Paul 
2025f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2026f41ac2beSBill Paul 
2027f41ac2beSBill Paul 	/* Load the address of the statstics block */
2028f41ac2beSBill Paul 
2029f41ac2beSBill Paul 	ctx.sc = sc;
2030f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2031f41ac2beSBill Paul 
2032f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2033f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2034f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2035f41ac2beSBill Paul 
2036f41ac2beSBill Paul 	if (error)
2037f41ac2beSBill Paul 		return (ENOMEM);
2038f41ac2beSBill Paul 
2039f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2040f41ac2beSBill Paul 
2041f41ac2beSBill Paul 	return(0);
2042f41ac2beSBill Paul }
2043f41ac2beSBill Paul 
204495d67482SBill Paul static int
204595d67482SBill Paul bge_attach(dev)
204695d67482SBill Paul 	device_t dev;
204795d67482SBill Paul {
204895d67482SBill Paul 	struct ifnet *ifp;
204995d67482SBill Paul 	struct bge_softc *sc;
2050a1d52896SBill Paul 	u_int32_t hwcfg = 0;
2051fc74a9f9SBrooks Davis 	u_int32_t mac_tmp = 0;
2052fc74a9f9SBrooks Davis 	u_char eaddr[6];
2053fe806fdaSPyun YongHyeon 	int error = 0, rid;
205495d67482SBill Paul 
205595d67482SBill Paul 	sc = device_get_softc(dev);
205695d67482SBill Paul 	sc->bge_dev = dev;
205795d67482SBill Paul 
205895d67482SBill Paul 	/*
205995d67482SBill Paul 	 * Map control/status registers.
206095d67482SBill Paul 	 */
206195d67482SBill Paul 	pci_enable_busmaster(dev);
206295d67482SBill Paul 
206395d67482SBill Paul 	rid = BGE_PCI_BAR0;
20645f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
20655f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
206695d67482SBill Paul 
206795d67482SBill Paul 	if (sc->bge_res == NULL) {
2068fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
206995d67482SBill Paul 		error = ENXIO;
207095d67482SBill Paul 		goto fail;
207195d67482SBill Paul 	}
207295d67482SBill Paul 
207395d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
207495d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
207595d67482SBill Paul 
207695d67482SBill Paul 	/* Allocate interrupt */
207795d67482SBill Paul 	rid = 0;
207895d67482SBill Paul 
20795f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
208095d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
208195d67482SBill Paul 
208295d67482SBill Paul 	if (sc->bge_irq == NULL) {
2083fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
208495d67482SBill Paul 		error = ENXIO;
208595d67482SBill Paul 		goto fail;
208695d67482SBill Paul 	}
208795d67482SBill Paul 
20880f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
20890f9bd73bSSam Leffler 
2090e53d81eeSPaul Saab 	/* Save ASIC rev. */
2091e53d81eeSPaul Saab 
2092e53d81eeSPaul Saab 	sc->bge_chipid =
2093e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2094e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2095e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2096e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2097e53d81eeSPaul Saab 
2098e53d81eeSPaul Saab 	/*
2099560c1670SGleb Smirnoff 	 * Treat the 5714 and the 5752 like the 5750 until we have more info
2100419c028bSPaul Saab 	 * on this chip.
2101419c028bSPaul Saab 	 */
2102560c1670SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5714 ||
2103560c1670SGleb Smirnoff             sc->bge_asicrev == BGE_ASICREV_BCM5752)
2104419c028bSPaul Saab 		sc->bge_asicrev = BGE_ASICREV_BCM5750;
2105419c028bSPaul Saab 
2106419c028bSPaul Saab 	/*
2107e53d81eeSPaul Saab 	 * XXX: Broadcom Linux driver.  Not in specs or eratta.
2108e53d81eeSPaul Saab 	 * PCI-Express?
2109e53d81eeSPaul Saab 	 */
2110e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
2111e53d81eeSPaul Saab 		u_int32_t v;
2112e53d81eeSPaul Saab 
2113e53d81eeSPaul Saab 		v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4);
2114e53d81eeSPaul Saab 		if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) {
2115e53d81eeSPaul Saab 			v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
2116e53d81eeSPaul Saab 			if ((v & 0xff) == BGE_PCIE_CAPID)
2117e53d81eeSPaul Saab 				sc->bge_pcie = 1;
2118e53d81eeSPaul Saab 		}
2119e53d81eeSPaul Saab 	}
2120e53d81eeSPaul Saab 
212195d67482SBill Paul 	/* Try to reset the chip. */
212295d67482SBill Paul 	bge_reset(sc);
212395d67482SBill Paul 
212495d67482SBill Paul 	if (bge_chipinit(sc)) {
2125fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
212695d67482SBill Paul 		bge_release_resources(sc);
212795d67482SBill Paul 		error = ENXIO;
212895d67482SBill Paul 		goto fail;
212995d67482SBill Paul 	}
213095d67482SBill Paul 
213195d67482SBill Paul 	/*
213295d67482SBill Paul 	 * Get station address from the EEPROM.
213395d67482SBill Paul 	 */
2134fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2135fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2136fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2137fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2138fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2139fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2140fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2141fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2142fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2143fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
214495d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2145fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
214695d67482SBill Paul 		bge_release_resources(sc);
214795d67482SBill Paul 		error = ENXIO;
214895d67482SBill Paul 		goto fail;
214995d67482SBill Paul 	}
215095d67482SBill Paul 
2151f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
2152e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2153e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
2154f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2155f41ac2beSBill Paul 	else
2156f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2157f41ac2beSBill Paul 
2158f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2159fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2160fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2161f41ac2beSBill Paul 		bge_release_resources(sc);
2162f41ac2beSBill Paul 		error = ENXIO;
2163f41ac2beSBill Paul 		goto fail;
2164f41ac2beSBill Paul 	}
2165f41ac2beSBill Paul 
216695d67482SBill Paul 	/* Set default tuneable values. */
216795d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
216895d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
216995d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
217095d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
217195d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
217295d67482SBill Paul 
217395d67482SBill Paul 	/* Set up ifnet structure */
2174fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2175fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2176fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2177fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2178fc74a9f9SBrooks Davis 		error = ENXIO;
2179fc74a9f9SBrooks Davis 		goto fail;
2180fc74a9f9SBrooks Davis 	}
218195d67482SBill Paul 	ifp->if_softc = sc;
21829bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
218395d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
218495d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
218595d67482SBill Paul 	ifp->if_start = bge_start;
218695d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
218795d67482SBill Paul 	ifp->if_init = bge_init;
218895d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
21894d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
21904d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
21914d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
219295d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2193d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
21940434d1b8SBill Paul 	    IFCAP_VLAN_MTU;
219595d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
219675719184SGleb Smirnoff #ifdef DEVICE_POLLING
219775719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
219875719184SGleb Smirnoff #endif
219995d67482SBill Paul 
2200a1d52896SBill Paul         /*
2201d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2202d375e524SGleb Smirnoff 	 * to hardware bugs.
2203d375e524SGleb Smirnoff 	 */
2204d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2205d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2206d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2207d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2208d375e524SGleb Smirnoff 	}
2209d375e524SGleb Smirnoff 
2210d375e524SGleb Smirnoff 	/*
2211a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
221241abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
221341abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
221441abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
221541abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
221641abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
221741abcc1bSPaul Saab 	 * SK-9D41.
2218a1d52896SBill Paul 	 */
221941abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
222041abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
222141abcc1bSPaul Saab 	else {
2222f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2223f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2224fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2225f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2226f6789fbaSPyun YongHyeon 			error = ENXIO;
2227f6789fbaSPyun YongHyeon 			goto fail;
2228f6789fbaSPyun YongHyeon 		}
222941abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
223041abcc1bSPaul Saab 	}
223141abcc1bSPaul Saab 
223241abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2233a1d52896SBill Paul 		sc->bge_tbi = 1;
2234a1d52896SBill Paul 
223595d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
223695d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
223795d67482SBill Paul 		sc->bge_tbi = 1;
223895d67482SBill Paul 
223995d67482SBill Paul 	if (sc->bge_tbi) {
224095d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
224195d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
224295d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
224395d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
224495d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
224595d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
224695d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2247da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
224895d67482SBill Paul 	} else {
224995d67482SBill Paul 		/*
225095d67482SBill Paul 		 * Do transceiver setup.
225195d67482SBill Paul 		 */
225295d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
225395d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
2254fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
225595d67482SBill Paul 			bge_release_resources(sc);
225695d67482SBill Paul 			error = ENXIO;
225795d67482SBill Paul 			goto fail;
225895d67482SBill Paul 		}
225995d67482SBill Paul 	}
226095d67482SBill Paul 
226195d67482SBill Paul 	/*
2262e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2263e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2264e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2265e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2266e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2267e255b776SJohn Polstra 	 * payloads by copying the received packets.
2268e255b776SJohn Polstra 	 */
2269e0ced696SPaul Saab 	switch (sc->bge_chipid) {
2270e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_A0:
2271e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B0:
2272e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B2:
2273e0ced696SPaul Saab 	case BGE_CHIPID_BCM5701_B5:
2274e255b776SJohn Polstra 		/* If in PCI-X mode, work around the alignment bug. */
2275e255b776SJohn Polstra 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
2276e255b776SJohn Polstra 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2277e255b776SJohn Polstra 		    BGE_PCISTATE_PCI_BUSSPEED)
2278e255b776SJohn Polstra 			sc->bge_rx_alignment_bug = 1;
2279e255b776SJohn Polstra 		break;
2280e255b776SJohn Polstra 	}
2281e255b776SJohn Polstra 
2282e255b776SJohn Polstra 	/*
228395d67482SBill Paul 	 * Call MI attach routine.
228495d67482SBill Paul 	 */
2285fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
22860f9bd73bSSam Leffler 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
22870f9bd73bSSam Leffler 
22880f9bd73bSSam Leffler 	/*
22890f9bd73bSSam Leffler 	 * Hookup IRQ last.
22900f9bd73bSSam Leffler 	 */
22910f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
22920f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
22930f9bd73bSSam Leffler 
22940f9bd73bSSam Leffler 	if (error) {
2295fc74a9f9SBrooks Davis 		bge_detach(dev);
2296fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
22970f9bd73bSSam Leffler 	}
229895d67482SBill Paul 
229995d67482SBill Paul fail:
230095d67482SBill Paul 	return(error);
230195d67482SBill Paul }
230295d67482SBill Paul 
230395d67482SBill Paul static int
230495d67482SBill Paul bge_detach(dev)
230595d67482SBill Paul 	device_t dev;
230695d67482SBill Paul {
230795d67482SBill Paul 	struct bge_softc *sc;
230895d67482SBill Paul 	struct ifnet *ifp;
230995d67482SBill Paul 
231095d67482SBill Paul 	sc = device_get_softc(dev);
2311fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
231295d67482SBill Paul 
231375719184SGleb Smirnoff #ifdef DEVICE_POLLING
231475719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
231575719184SGleb Smirnoff 		ether_poll_deregister(ifp);
231675719184SGleb Smirnoff #endif
231775719184SGleb Smirnoff 
23180f9bd73bSSam Leffler 	BGE_LOCK(sc);
231995d67482SBill Paul 	bge_stop(sc);
232095d67482SBill Paul 	bge_reset(sc);
23210f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
23220f9bd73bSSam Leffler 
23230f9bd73bSSam Leffler 	ether_ifdetach(ifp);
232495d67482SBill Paul 
232595d67482SBill Paul 	if (sc->bge_tbi) {
232695d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
232795d67482SBill Paul 	} else {
232895d67482SBill Paul 		bus_generic_detach(dev);
232995d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
233095d67482SBill Paul 	}
233195d67482SBill Paul 
233295d67482SBill Paul 	bge_release_resources(sc);
233395d67482SBill Paul 
233495d67482SBill Paul 	return(0);
233595d67482SBill Paul }
233695d67482SBill Paul 
233795d67482SBill Paul static void
233895d67482SBill Paul bge_release_resources(sc)
233995d67482SBill Paul 	struct bge_softc *sc;
234095d67482SBill Paul {
234195d67482SBill Paul 	device_t dev;
234295d67482SBill Paul 
234395d67482SBill Paul 	dev = sc->bge_dev;
234495d67482SBill Paul 
234595d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
234695d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
234795d67482SBill Paul 
234895d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
234995d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
235095d67482SBill Paul 
235195d67482SBill Paul 	if (sc->bge_intrhand != NULL)
235295d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
235395d67482SBill Paul 
235495d67482SBill Paul 	if (sc->bge_irq != NULL)
235595d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
235695d67482SBill Paul 
235795d67482SBill Paul 	if (sc->bge_res != NULL)
235895d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
235995d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
236095d67482SBill Paul 
2361ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2362ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2363ad61f896SRuslan Ermilov 
2364f41ac2beSBill Paul 	bge_dma_free(sc);
236595d67482SBill Paul 
23660f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
23670f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
23680f9bd73bSSam Leffler 
236995d67482SBill Paul 	return;
237095d67482SBill Paul }
237195d67482SBill Paul 
237295d67482SBill Paul static void
237395d67482SBill Paul bge_reset(sc)
237495d67482SBill Paul 	struct bge_softc *sc;
237595d67482SBill Paul {
237695d67482SBill Paul 	device_t dev;
2377e53d81eeSPaul Saab 	u_int32_t cachesize, command, pcistate, reset;
237895d67482SBill Paul 	int i, val = 0;
237995d67482SBill Paul 
238095d67482SBill Paul 	dev = sc->bge_dev;
238195d67482SBill Paul 
238295d67482SBill Paul 	/* Save some important PCI state. */
238395d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
238495d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
238595d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
238695d67482SBill Paul 
238795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
238895d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2389e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
239095d67482SBill Paul 
2391e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2392e53d81eeSPaul Saab 
2393e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2394e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2395e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2396e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2397e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2398e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2399e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2400e53d81eeSPaul Saab 			reset |= (1<<29);
2401e53d81eeSPaul Saab 		}
2402e53d81eeSPaul Saab 	}
2403e53d81eeSPaul Saab 
240495d67482SBill Paul 	/* Issue global reset */
2405e53d81eeSPaul Saab 	bge_writereg_ind(sc, BGE_MISC_CFG, reset);
240695d67482SBill Paul 
240795d67482SBill Paul 	DELAY(1000);
240895d67482SBill Paul 
2409e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2410e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2411e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2412e53d81eeSPaul Saab 			uint32_t v;
2413e53d81eeSPaul Saab 
2414e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2415e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2416e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2417e53d81eeSPaul Saab 		}
2418e53d81eeSPaul Saab 		/* Set PCIE max payload size and clear error status. */
2419e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2420e53d81eeSPaul Saab 	}
2421e53d81eeSPaul Saab 
242295d67482SBill Paul 	/* Reset some of the PCI state that got zapped by reset */
242395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
242495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2425e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
242695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
242795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
242895d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
242995d67482SBill Paul 
2430a7b0c314SPaul Saab 	/* Enable memory arbiter. */
24315dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2432e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
2433a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2434a7b0c314SPaul Saab 
243595d67482SBill Paul 	/*
243695d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
243795d67482SBill Paul 	 * general communications memory at 0xB50.
243895d67482SBill Paul 	 */
243995d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
244095d67482SBill Paul 	/*
244195d67482SBill Paul 	 * Poll the value location we just wrote until
244295d67482SBill Paul 	 * we see the 1's complement of the magic number.
244395d67482SBill Paul 	 * This indicates that the firmware initialization
244495d67482SBill Paul 	 * is complete.
244595d67482SBill Paul 	 */
244695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
244795d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
244895d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
244995d67482SBill Paul 			break;
245095d67482SBill Paul 		DELAY(10);
245195d67482SBill Paul 	}
245295d67482SBill Paul 
245395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2454fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "firmware handshake timed out\n");
245595d67482SBill Paul 		return;
245695d67482SBill Paul 	}
245795d67482SBill Paul 
245895d67482SBill Paul 	/*
245995d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
246095d67482SBill Paul 	 * return to its original pre-reset state. This is a
246195d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
246295d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
246395d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
246495d67482SBill Paul 	 * results.
246595d67482SBill Paul 	 */
246695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
246795d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
246895d67482SBill Paul 			break;
246995d67482SBill Paul 		DELAY(10);
247095d67482SBill Paul 	}
247195d67482SBill Paul 
247295d67482SBill Paul 	/* Fix up byte swapping */
2473e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
247495d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
247595d67482SBill Paul 
247695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
247795d67482SBill Paul 
2478da3003f0SBill Paul 	/*
2479da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2480da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2481da3003f0SBill Paul 	 * to 1.2V.
2482da3003f0SBill Paul 	 */
2483da3003f0SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 && sc->bge_tbi) {
2484da3003f0SBill Paul 		uint32_t serdescfg;
2485da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2486da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2487da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2488da3003f0SBill Paul 	}
2489da3003f0SBill Paul 
2490e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2491e53d81eeSPaul Saab 	if (sc->bge_pcie && sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2492e53d81eeSPaul Saab 		uint32_t v;
2493e53d81eeSPaul Saab 
2494e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2495e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2496e53d81eeSPaul Saab 	}
249795d67482SBill Paul 	DELAY(10000);
249895d67482SBill Paul 
249995d67482SBill Paul 	return;
250095d67482SBill Paul }
250195d67482SBill Paul 
250295d67482SBill Paul /*
250395d67482SBill Paul  * Frame reception handling. This is called if there's a frame
250495d67482SBill Paul  * on the receive return list.
250595d67482SBill Paul  *
250695d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
25071be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
250895d67482SBill Paul  * 2) the frame is from the standard receive ring
250995d67482SBill Paul  */
251095d67482SBill Paul 
251195d67482SBill Paul static void
251295d67482SBill Paul bge_rxeof(sc)
251395d67482SBill Paul 	struct bge_softc *sc;
251495d67482SBill Paul {
251595d67482SBill Paul 	struct ifnet *ifp;
251695d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
251795d67482SBill Paul 
25180f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
25190f9bd73bSSam Leffler 
2520fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
252195d67482SBill Paul 
2522f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2523e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2524f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2525f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
25265dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2527e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2528f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2529f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2530f41ac2beSBill Paul 		    BUS_DMASYNC_POSTREAD);
2531f41ac2beSBill Paul 	}
2532f41ac2beSBill Paul 
253395d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2534f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
253595d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
253695d67482SBill Paul 		u_int32_t		rxidx;
253795d67482SBill Paul 		struct ether_header	*eh;
253895d67482SBill Paul 		struct mbuf		*m = NULL;
253995d67482SBill Paul 		u_int16_t		vlan_tag = 0;
254095d67482SBill Paul 		int			have_tag = 0;
254195d67482SBill Paul 
254275719184SGleb Smirnoff #ifdef DEVICE_POLLING
254375719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
254475719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
254575719184SGleb Smirnoff 				break;
254675719184SGleb Smirnoff 			sc->rxcycles--;
254775719184SGleb Smirnoff 		}
254875719184SGleb Smirnoff #endif
254975719184SGleb Smirnoff 
255095d67482SBill Paul 		cur_rx =
2551f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
255295d67482SBill Paul 
255395d67482SBill Paul 		rxidx = cur_rx->bge_idx;
25540434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
255595d67482SBill Paul 
255695d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
255795d67482SBill Paul 			have_tag = 1;
255895d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
255995d67482SBill Paul 		}
256095d67482SBill Paul 
256195d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
256295d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2563f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2564f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2565f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2566f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2567f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
256895d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
256995d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
257095d67482SBill Paul 			jumbocnt++;
257195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
257295d67482SBill Paul 				ifp->if_ierrors++;
257395d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
257495d67482SBill Paul 				continue;
257595d67482SBill Paul 			}
257695d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
257795d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
257895d67482SBill Paul 				ifp->if_ierrors++;
257995d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
258095d67482SBill Paul 				continue;
258195d67482SBill Paul 			}
258295d67482SBill Paul 		} else {
258395d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2584f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2585f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2586f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2587f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2588f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
258995d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
259095d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
259195d67482SBill Paul 			stdcnt++;
259295d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
259395d67482SBill Paul 				ifp->if_ierrors++;
259495d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
259595d67482SBill Paul 				continue;
259695d67482SBill Paul 			}
259795d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
259895d67482SBill Paul 			    NULL) == ENOBUFS) {
259995d67482SBill Paul 				ifp->if_ierrors++;
260095d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
260195d67482SBill Paul 				continue;
260295d67482SBill Paul 			}
260395d67482SBill Paul 		}
260495d67482SBill Paul 
260595d67482SBill Paul 		ifp->if_ipackets++;
2606e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2607e255b776SJohn Polstra 		/*
2608e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2609e65bed95SPyun YongHyeon 		 * the payload is aligned.
2610e255b776SJohn Polstra 		 */
2611e255b776SJohn Polstra 		if (sc->bge_rx_alignment_bug) {
2612e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2613e255b776SJohn Polstra 			    cur_rx->bge_len);
2614e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2615e255b776SJohn Polstra 		}
2616e255b776SJohn Polstra #endif
261795d67482SBill Paul 		eh = mtod(m, struct ether_header *);
2618473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
261995d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
262095d67482SBill Paul 
2621b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
262295d67482SBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
262395d67482SBill Paul 			if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
262495d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2625d375e524SGleb Smirnoff 
2626d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2627d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
262895d67482SBill Paul 				m->m_pkthdr.csum_data =
262995d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
26300189c944SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
263195d67482SBill Paul 			}
263295d67482SBill Paul 		}
263395d67482SBill Paul 
263495d67482SBill Paul 		/*
2635673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2636673d9191SSam Leffler 		 * attach that information to the packet.
263795d67482SBill Paul 		 */
2638d147662cSGleb Smirnoff 		if (have_tag) {
2639d147662cSGleb Smirnoff 			VLAN_INPUT_TAG(ifp, m, vlan_tag);
2640d147662cSGleb Smirnoff 			if (m == NULL)
2641d147662cSGleb Smirnoff 				continue;
2642d147662cSGleb Smirnoff 		}
264395d67482SBill Paul 
26440f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2645673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
26460f9bd73bSSam Leffler 		BGE_LOCK(sc);
264795d67482SBill Paul 	}
264895d67482SBill Paul 
2649e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2650f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2651e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
26525dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2653e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2654e65bed95SPyun YongHyeon 		if (jumbocnt > 0)
2655f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2656f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_ring_map,
2657e65bed95SPyun YongHyeon 			    BUS_DMASYNC_PREWRITE);
2658f41ac2beSBill Paul 	}
2659f41ac2beSBill Paul 
266095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
266195d67482SBill Paul 	if (stdcnt)
266295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
266395d67482SBill Paul 	if (jumbocnt)
266495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
266595d67482SBill Paul 
266695d67482SBill Paul 	return;
266795d67482SBill Paul }
266895d67482SBill Paul 
266995d67482SBill Paul static void
267095d67482SBill Paul bge_txeof(sc)
267195d67482SBill Paul 	struct bge_softc *sc;
267295d67482SBill Paul {
267395d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
267495d67482SBill Paul 	struct ifnet *ifp;
267595d67482SBill Paul 
26760f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
26770f9bd73bSSam Leffler 
2678fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
267995d67482SBill Paul 
2680e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2681e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2682e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
268395d67482SBill Paul 	/*
268495d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
268595d67482SBill Paul 	 * frames that have been sent.
268695d67482SBill Paul 	 */
268795d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2688f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
268995d67482SBill Paul 		u_int32_t		idx = 0;
269095d67482SBill Paul 
269195d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2692f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
269395d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
269495d67482SBill Paul 			ifp->if_opackets++;
269595d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2696e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2697e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2698e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2699f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2700f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2701e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2702e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
270395d67482SBill Paul 		}
270495d67482SBill Paul 		sc->bge_txcnt--;
270595d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
270695d67482SBill Paul 		ifp->if_timer = 0;
270795d67482SBill Paul 	}
270895d67482SBill Paul 
270995d67482SBill Paul 	if (cur_tx != NULL)
271013f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
271195d67482SBill Paul 
271295d67482SBill Paul 	return;
271395d67482SBill Paul }
271495d67482SBill Paul 
271575719184SGleb Smirnoff #ifdef DEVICE_POLLING
271675719184SGleb Smirnoff static void
271775719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
271875719184SGleb Smirnoff {
271975719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
272075719184SGleb Smirnoff 
272175719184SGleb Smirnoff 	BGE_LOCK(sc);
272275719184SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
272375719184SGleb Smirnoff 		bge_poll_locked(ifp, cmd, count);
272475719184SGleb Smirnoff 	BGE_UNLOCK(sc);
272575719184SGleb Smirnoff }
272675719184SGleb Smirnoff 
272775719184SGleb Smirnoff static void
272875719184SGleb Smirnoff bge_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
272975719184SGleb Smirnoff {
273075719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
273175719184SGleb Smirnoff 
273275719184SGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
273375719184SGleb Smirnoff 
273475719184SGleb Smirnoff 	sc->rxcycles = count;
273575719184SGleb Smirnoff 	bge_rxeof(sc);
273675719184SGleb Smirnoff 	bge_txeof(sc);
273775719184SGleb Smirnoff 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
273875719184SGleb Smirnoff 		bge_start_locked(ifp);
273975719184SGleb Smirnoff 
274075719184SGleb Smirnoff 	if (cmd == POLL_AND_CHECK_STATUS) {
2741dab5cd05SOleg Bulyzhin 		uint32_t statusword;
274275719184SGleb Smirnoff 
2743dab5cd05SOleg Bulyzhin 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2744e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2745dab5cd05SOleg Bulyzhin 
2746dab5cd05SOleg Bulyzhin 	    	statusword = atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
2747dab5cd05SOleg Bulyzhin 
27481f313773SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
27491f313773SOleg Bulyzhin 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) ||
2750dab5cd05SOleg Bulyzhin 		    statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2751dab5cd05SOleg Bulyzhin 			bge_link_upd(sc);
2752dab5cd05SOleg Bulyzhin 
2753dab5cd05SOleg Bulyzhin 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2754e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
275575719184SGleb Smirnoff 	}
275675719184SGleb Smirnoff }
275775719184SGleb Smirnoff #endif /* DEVICE_POLLING */
275875719184SGleb Smirnoff 
275995d67482SBill Paul static void
276095d67482SBill Paul bge_intr(xsc)
276195d67482SBill Paul 	void *xsc;
276295d67482SBill Paul {
276395d67482SBill Paul 	struct bge_softc *sc;
276495d67482SBill Paul 	struct ifnet *ifp;
2765dab5cd05SOleg Bulyzhin 	uint32_t statusword;
276695d67482SBill Paul 
276795d67482SBill Paul 	sc = xsc;
2768f41ac2beSBill Paul 
27690f9bd73bSSam Leffler 	BGE_LOCK(sc);
27700f9bd73bSSam Leffler 
2771dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2772dab5cd05SOleg Bulyzhin 
277375719184SGleb Smirnoff #ifdef DEVICE_POLLING
277475719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
277575719184SGleb Smirnoff 		BGE_UNLOCK(sc);
277675719184SGleb Smirnoff 		return;
277775719184SGleb Smirnoff 	}
277875719184SGleb Smirnoff #endif
277975719184SGleb Smirnoff 
2780f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2781e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2782f41ac2beSBill Paul 
2783487a8c7eSPaul Saab 	statusword =
2784f41ac2beSBill Paul 	    atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
278595d67482SBill Paul 
278695d67482SBill Paul #ifdef notdef
278795d67482SBill Paul 	/* Avoid this for now -- checking this register is expensive. */
278895d67482SBill Paul 	/* Make sure this is really our interrupt. */
278995d67482SBill Paul 	if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
279095d67482SBill Paul 		return;
279195d67482SBill Paul #endif
279295d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
279395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
279495d67482SBill Paul 
27951f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
27961f313773SOleg Bulyzhin 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) ||
2797dab5cd05SOleg Bulyzhin 	    statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2798dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
279995d67482SBill Paul 
280013f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
280195d67482SBill Paul 		/* Check RX return ring producer/consumer */
280295d67482SBill Paul 		bge_rxeof(sc);
280395d67482SBill Paul 
280495d67482SBill Paul 		/* Check TX ring producer/consumer */
280595d67482SBill Paul 		bge_txeof(sc);
280695d67482SBill Paul 	}
280795d67482SBill Paul 
280895d67482SBill Paul 	/* Re-enable interrupts. */
280995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
281095d67482SBill Paul 
281113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
281213f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
28130f9bd73bSSam Leffler 		bge_start_locked(ifp);
28140f9bd73bSSam Leffler 
28150f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
281695d67482SBill Paul 
281795d67482SBill Paul 	return;
281895d67482SBill Paul }
281995d67482SBill Paul 
282095d67482SBill Paul static void
28210f9bd73bSSam Leffler bge_tick_locked(sc)
282295d67482SBill Paul 	struct bge_softc *sc;
28230f9bd73bSSam Leffler {
282495d67482SBill Paul 	struct mii_data *mii = NULL;
282595d67482SBill Paul 	struct ifnet *ifp;
282695d67482SBill Paul 
28270f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
282895d67482SBill Paul 
2829dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2830dab5cd05SOleg Bulyzhin 
2831e53d81eeSPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2832e53d81eeSPaul Saab 	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
28330434d1b8SBill Paul 		bge_stats_update_regs(sc);
28340434d1b8SBill Paul 	else
283595d67482SBill Paul 		bge_stats_update(sc);
283695d67482SBill Paul 
28371f313773SOleg Bulyzhin 	if (!sc->bge_tbi) {
283895d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
283995d67482SBill Paul 		mii_tick(mii);
2840dab5cd05SOleg Bulyzhin 	}
284195d67482SBill Paul 
2842dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
284395d67482SBill Paul }
284495d67482SBill Paul 
284595d67482SBill Paul static void
28460f9bd73bSSam Leffler bge_tick(xsc)
28470f9bd73bSSam Leffler 	void *xsc;
28480f9bd73bSSam Leffler {
28490f9bd73bSSam Leffler 	struct bge_softc *sc;
28500f9bd73bSSam Leffler 
28510f9bd73bSSam Leffler 	sc = xsc;
28520f9bd73bSSam Leffler 
28530f9bd73bSSam Leffler 	BGE_LOCK(sc);
28540f9bd73bSSam Leffler 	bge_tick_locked(sc);
28550f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
28560f9bd73bSSam Leffler }
28570f9bd73bSSam Leffler 
28580f9bd73bSSam Leffler static void
28590434d1b8SBill Paul bge_stats_update_regs(sc)
28600434d1b8SBill Paul 	struct bge_softc *sc;
28610434d1b8SBill Paul {
28620434d1b8SBill Paul 	struct ifnet *ifp;
28630434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
28640434d1b8SBill Paul 	u_int32_t *s;
28650434d1b8SBill Paul 	int i;
28660434d1b8SBill Paul 
2867fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
28680434d1b8SBill Paul 
28690434d1b8SBill Paul 	s = (u_int32_t *)&stats;
28700434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
28710434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
28720434d1b8SBill Paul 		s++;
28730434d1b8SBill Paul 	}
28740434d1b8SBill Paul 
28750434d1b8SBill Paul 	ifp->if_collisions +=
28760434d1b8SBill Paul 	   (stats.dot3StatsSingleCollisionFrames +
28770434d1b8SBill Paul 	   stats.dot3StatsMultipleCollisionFrames +
28780434d1b8SBill Paul 	   stats.dot3StatsExcessiveCollisions +
28790434d1b8SBill Paul 	   stats.dot3StatsLateCollisions) -
28800434d1b8SBill Paul 	   ifp->if_collisions;
28810434d1b8SBill Paul 
28820434d1b8SBill Paul 	return;
28830434d1b8SBill Paul }
28840434d1b8SBill Paul 
28850434d1b8SBill Paul static void
288695d67482SBill Paul bge_stats_update(sc)
288795d67482SBill Paul 	struct bge_softc *sc;
288895d67482SBill Paul {
288995d67482SBill Paul 	struct ifnet *ifp;
2890e907febfSPyun YongHyeon 	bus_size_t stats;
289195d67482SBill Paul 
2892fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
289395d67482SBill Paul 
2894e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2895e907febfSPyun YongHyeon 
2896e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
2897e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
289895d67482SBill Paul 
289995d67482SBill Paul 	ifp->if_collisions +=
2900e907febfSPyun YongHyeon 	   (READ_STAT(sc, stats,
2901e907febfSPyun YongHyeon 		txstats.dot3StatsSingleCollisionFrames.bge_addr_lo) +
2902e907febfSPyun YongHyeon 	    READ_STAT(sc, stats,
2903e907febfSPyun YongHyeon 		txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo) +
2904e907febfSPyun YongHyeon 	    READ_STAT(sc, stats,
2905e907febfSPyun YongHyeon 		txstats.dot3StatsExcessiveCollisions.bge_addr_lo) +
2906e907febfSPyun YongHyeon 	    READ_STAT(sc, stats,
2907e907febfSPyun YongHyeon 		txstats.dot3StatsLateCollisions.bge_addr_lo)) -
290895d67482SBill Paul 	   ifp->if_collisions;
290995d67482SBill Paul 
2910e907febfSPyun YongHyeon #undef READ_STAT
2911e907febfSPyun YongHyeon 
291295d67482SBill Paul #ifdef notdef
291395d67482SBill Paul 	ifp->if_collisions +=
291495d67482SBill Paul 	   (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
291595d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
291695d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
291795d67482SBill Paul 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
291895d67482SBill Paul 	   ifp->if_collisions;
291995d67482SBill Paul #endif
292095d67482SBill Paul 
292195d67482SBill Paul 	return;
292295d67482SBill Paul }
292395d67482SBill Paul 
292495d67482SBill Paul /*
2925d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
2926d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
2927d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
2928d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
2929d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
2930d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
2931d375e524SGleb Smirnoff  */
2932d375e524SGleb Smirnoff static __inline int
2933d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
2934d375e524SGleb Smirnoff {
2935d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
2936d375e524SGleb Smirnoff 	struct mbuf *last;
2937d375e524SGleb Smirnoff 
2938d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
2939d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
2940d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
2941d375e524SGleb Smirnoff 		last = m;
2942d375e524SGleb Smirnoff 	} else {
2943d375e524SGleb Smirnoff 		/*
2944d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
2945d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
2946d375e524SGleb Smirnoff 		 */
2947d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
2948d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
2949d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
2950d375e524SGleb Smirnoff 			struct mbuf *n;
2951d375e524SGleb Smirnoff 
2952d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
2953d375e524SGleb Smirnoff 			if (n == NULL)
2954d375e524SGleb Smirnoff 				return (ENOBUFS);
2955d375e524SGleb Smirnoff 			n->m_len = 0;
2956d375e524SGleb Smirnoff 			last->m_next = n;
2957d375e524SGleb Smirnoff 			last = n;
2958d375e524SGleb Smirnoff 		}
2959d375e524SGleb Smirnoff 	}
2960d375e524SGleb Smirnoff 
2961d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
2962d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
2963d375e524SGleb Smirnoff 	last->m_len += padlen;
2964d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
2965d375e524SGleb Smirnoff 
2966d375e524SGleb Smirnoff 	return (0);
2967d375e524SGleb Smirnoff }
2968d375e524SGleb Smirnoff 
2969d375e524SGleb Smirnoff /*
297095d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
297195d67482SBill Paul  * pointers to descriptors.
297295d67482SBill Paul  */
297395d67482SBill Paul static int
297495d67482SBill Paul bge_encap(sc, m_head, txidx)
297595d67482SBill Paul 	struct bge_softc *sc;
297695d67482SBill Paul 	struct mbuf *m_head;
29777e27542aSGleb Smirnoff 	uint32_t *txidx;
297895d67482SBill Paul {
29797e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
2980f41ac2beSBill Paul 	bus_dmamap_t		map;
29817e27542aSGleb Smirnoff 	struct bge_tx_bd	*d = NULL;
29827e27542aSGleb Smirnoff 	struct m_tag		*mtag;
29837e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
29847e27542aSGleb Smirnoff 	uint16_t		csum_flags = 0;
29857e27542aSGleb Smirnoff 	int			nsegs, i, error;
298695d67482SBill Paul 
298795d67482SBill Paul 	if (m_head->m_pkthdr.csum_flags) {
298895d67482SBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
298995d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
2990d375e524SGleb Smirnoff 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
299195d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
2992d375e524SGleb Smirnoff 			if (m_head->m_pkthdr.len < ETHER_MIN_NOPAD &&
2993d375e524SGleb Smirnoff 			    bge_cksum_pad(m_head) != 0)
2994d375e524SGleb Smirnoff 				return (ENOBUFS);
2995d375e524SGleb Smirnoff 		}
299695d67482SBill Paul 		if (m_head->m_flags & M_LASTFRAG)
299795d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
299895d67482SBill Paul 		else if (m_head->m_flags & M_FRAG)
299995d67482SBill Paul 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
300095d67482SBill Paul 	}
300195d67482SBill Paul 
3002fc74a9f9SBrooks Davis 	mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m_head);
3003673d9191SSam Leffler 
30047e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
30057e27542aSGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map,
30067e27542aSGleb Smirnoff 	    m_head, segs, &nsegs, BUS_DMA_NOWAIT);
30077e27542aSGleb Smirnoff         if (error) {
30087e27542aSGleb Smirnoff 		if (error == EFBIG) {
30097e27542aSGleb Smirnoff 			struct mbuf *m0;
30107e27542aSGleb Smirnoff 
30117e27542aSGleb Smirnoff 			m0 = m_defrag(m_head, M_DONTWAIT);
30127e27542aSGleb Smirnoff 			if (m0 == NULL)
30137e27542aSGleb Smirnoff 				return (ENOBUFS);
30147e27542aSGleb Smirnoff 			m_head = m0;
30157e27542aSGleb Smirnoff 			error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag,
30167e27542aSGleb Smirnoff 			    map, m_head, segs, &nsegs, BUS_DMA_NOWAIT);
30177e27542aSGleb Smirnoff 		}
30187e27542aSGleb Smirnoff 		if (error)
30197e27542aSGleb Smirnoff 			return (error);
30207e27542aSGleb Smirnoff 	}
30217e27542aSGleb Smirnoff 
302295d67482SBill Paul 	/*
302395d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
302495d67482SBill Paul 	 * of the end of the ring.
302595d67482SBill Paul 	 */
30267e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
30277e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
302895d67482SBill Paul 		return (ENOBUFS);
30297e27542aSGleb Smirnoff 	}
30307e27542aSGleb Smirnoff 
3031e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3032e65bed95SPyun YongHyeon 
30337e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
30347e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
30357e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
30367e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
30377e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
30387e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
30397e27542aSGleb Smirnoff 		if (i == nsegs - 1)
30407e27542aSGleb Smirnoff 			break;
30417e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
30427e27542aSGleb Smirnoff 	}
30437e27542aSGleb Smirnoff 
30447e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
30457e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
30467e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
30477e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
30487e27542aSGleb Smirnoff 	if (mtag != NULL) {
30497e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
30507e27542aSGleb Smirnoff 		d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
30517e27542aSGleb Smirnoff 	} else
30527e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
3053f41ac2beSBill Paul 
3054f41ac2beSBill Paul 	/*
3055f41ac2beSBill Paul 	 * Insure that the map for this transmission
3056f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3057f41ac2beSBill Paul 	 * in this chain.
3058f41ac2beSBill Paul 	 */
30597e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
30607e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
30617e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m_head;
30627e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
306395d67482SBill Paul 
30647e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
30657e27542aSGleb Smirnoff 	*txidx = idx;
306695d67482SBill Paul 
306795d67482SBill Paul 	return (0);
306895d67482SBill Paul }
306995d67482SBill Paul 
307095d67482SBill Paul /*
307195d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
307295d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
307395d67482SBill Paul  */
307495d67482SBill Paul static void
30750f9bd73bSSam Leffler bge_start_locked(ifp)
307695d67482SBill Paul 	struct ifnet *ifp;
307795d67482SBill Paul {
307895d67482SBill Paul 	struct bge_softc *sc;
307995d67482SBill Paul 	struct mbuf *m_head = NULL;
308014bbd30fSGleb Smirnoff 	uint32_t prodidx;
3081303a718cSDag-Erling Smørgrav 	int count = 0;
308295d67482SBill Paul 
308395d67482SBill Paul 	sc = ifp->if_softc;
308495d67482SBill Paul 
3085dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
308695d67482SBill Paul 		return;
308795d67482SBill Paul 
308814bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
308995d67482SBill Paul 
309095d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
30914d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
309295d67482SBill Paul 		if (m_head == NULL)
309395d67482SBill Paul 			break;
309495d67482SBill Paul 
309595d67482SBill Paul 		/*
309695d67482SBill Paul 		 * XXX
3097b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3098b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3099b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3100b874fdd4SYaroslav Tykhiy 		 *
3101b874fdd4SYaroslav Tykhiy 		 * XXX
310295d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
310395d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
310495d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
310595d67482SBill Paul 		 * chain at once.
310695d67482SBill Paul 		 * (paranoia -- may not actually be needed)
310795d67482SBill Paul 		 */
310895d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
310995d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
311095d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
311195d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
31124d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
311313f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
311495d67482SBill Paul 				break;
311595d67482SBill Paul 			}
311695d67482SBill Paul 		}
311795d67482SBill Paul 
311895d67482SBill Paul 		/*
311995d67482SBill Paul 		 * Pack the data into the transmit ring. If we
312095d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
312195d67482SBill Paul 		 * for the NIC to drain the ring.
312295d67482SBill Paul 		 */
312395d67482SBill Paul 		if (bge_encap(sc, m_head, &prodidx)) {
31244d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
312513f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
312695d67482SBill Paul 			break;
312795d67482SBill Paul 		}
3128303a718cSDag-Erling Smørgrav 		++count;
312995d67482SBill Paul 
313095d67482SBill Paul 		/*
313195d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
313295d67482SBill Paul 		 * to him.
313395d67482SBill Paul 		 */
3134673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
313595d67482SBill Paul 	}
313695d67482SBill Paul 
3137303a718cSDag-Erling Smørgrav 	if (count == 0) {
3138303a718cSDag-Erling Smørgrav 		/* no packets were dequeued */
3139303a718cSDag-Erling Smørgrav 		return;
3140303a718cSDag-Erling Smørgrav 	}
3141303a718cSDag-Erling Smørgrav 
314295d67482SBill Paul 	/* Transmit */
314395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
31443927098fSPaul Saab 	/* 5700 b2 errata */
3145e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
31463927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
314795d67482SBill Paul 
314814bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
314914bbd30fSGleb Smirnoff 
315095d67482SBill Paul 	/*
315195d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
315295d67482SBill Paul 	 */
315395d67482SBill Paul 	ifp->if_timer = 5;
315495d67482SBill Paul 
315595d67482SBill Paul 	return;
315695d67482SBill Paul }
315795d67482SBill Paul 
31580f9bd73bSSam Leffler /*
31590f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
31600f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
31610f9bd73bSSam Leffler  */
316295d67482SBill Paul static void
31630f9bd73bSSam Leffler bge_start(ifp)
31640f9bd73bSSam Leffler 	struct ifnet *ifp;
316595d67482SBill Paul {
31660f9bd73bSSam Leffler 	struct bge_softc *sc;
31670f9bd73bSSam Leffler 
31680f9bd73bSSam Leffler 	sc = ifp->if_softc;
31690f9bd73bSSam Leffler 	BGE_LOCK(sc);
31700f9bd73bSSam Leffler 	bge_start_locked(ifp);
31710f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
31720f9bd73bSSam Leffler }
31730f9bd73bSSam Leffler 
31740f9bd73bSSam Leffler static void
31750f9bd73bSSam Leffler bge_init_locked(sc)
31760f9bd73bSSam Leffler 	struct bge_softc *sc;
31770f9bd73bSSam Leffler {
317895d67482SBill Paul 	struct ifnet *ifp;
317995d67482SBill Paul 	u_int16_t *m;
318095d67482SBill Paul 
31810f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
318295d67482SBill Paul 
3183fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
318495d67482SBill Paul 
318513f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
318695d67482SBill Paul 		return;
318795d67482SBill Paul 
318895d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
318995d67482SBill Paul 	bge_stop(sc);
319095d67482SBill Paul 	bge_reset(sc);
319195d67482SBill Paul 	bge_chipinit(sc);
319295d67482SBill Paul 
319395d67482SBill Paul 	/*
319495d67482SBill Paul 	 * Init the various state machines, ring
319595d67482SBill Paul 	 * control blocks and firmware.
319695d67482SBill Paul 	 */
319795d67482SBill Paul 	if (bge_blockinit(sc)) {
3198fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
319995d67482SBill Paul 		return;
320095d67482SBill Paul 	}
320195d67482SBill Paul 
3202fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
320395d67482SBill Paul 
320495d67482SBill Paul 	/* Specify MTU. */
320595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3206859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
320795d67482SBill Paul 
320895d67482SBill Paul 	/* Load our MAC address. */
32094a0d6638SRuslan Ermilov 	m = (u_int16_t *)IF_LLADDR(sc->bge_ifp);
321095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
321195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
321295d67482SBill Paul 
321395d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
321495d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
321595d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
321695d67482SBill Paul 	} else {
321795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
321895d67482SBill Paul 	}
321995d67482SBill Paul 
322095d67482SBill Paul 	/* Program multicast filter. */
322195d67482SBill Paul 	bge_setmulti(sc);
322295d67482SBill Paul 
322395d67482SBill Paul 	/* Init RX ring. */
322495d67482SBill Paul 	bge_init_rx_ring_std(sc);
322595d67482SBill Paul 
32260434d1b8SBill Paul 	/*
32270434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
32280434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
32290434d1b8SBill Paul 	 * entry of the ring.
32300434d1b8SBill Paul 	 */
32310434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
32320434d1b8SBill Paul 		u_int32_t		v, i;
32330434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
32340434d1b8SBill Paul 			DELAY(20);
32350434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
32360434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
32370434d1b8SBill Paul 				break;
32380434d1b8SBill Paul 		}
32390434d1b8SBill Paul 		if (i == 10)
3240fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3241fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
32420434d1b8SBill Paul 	}
32430434d1b8SBill Paul 
324495d67482SBill Paul 	/* Init jumbo RX ring. */
324595d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
324695d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
324795d67482SBill Paul 
324895d67482SBill Paul 	/* Init our RX return ring index */
324995d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
325095d67482SBill Paul 
325195d67482SBill Paul 	/* Init TX ring. */
325295d67482SBill Paul 	bge_init_tx_ring(sc);
325395d67482SBill Paul 
325495d67482SBill Paul 	/* Turn on transmitter */
325595d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
325695d67482SBill Paul 
325795d67482SBill Paul 	/* Turn on receiver */
325895d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
325995d67482SBill Paul 
326095d67482SBill Paul 	/* Tell firmware we're alive. */
326195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
326295d67482SBill Paul 
326375719184SGleb Smirnoff #ifdef DEVICE_POLLING
326475719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
326575719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
326675719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
326775719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
326875719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
326975719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
327075719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
327175719184SGleb Smirnoff 	} else
327275719184SGleb Smirnoff #endif
327375719184SGleb Smirnoff 
327495d67482SBill Paul 	/* Enable host interrupts. */
327575719184SGleb Smirnoff 	{
327695d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
327795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
327895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
327975719184SGleb Smirnoff 	}
328095d67482SBill Paul 
328195d67482SBill Paul 	bge_ifmedia_upd(ifp);
328295d67482SBill Paul 
328313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
328413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
328595d67482SBill Paul 
32860f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
32870f9bd73bSSam Leffler }
32880f9bd73bSSam Leffler 
32890f9bd73bSSam Leffler static void
32900f9bd73bSSam Leffler bge_init(xsc)
32910f9bd73bSSam Leffler 	void *xsc;
32920f9bd73bSSam Leffler {
32930f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
32940f9bd73bSSam Leffler 
32950f9bd73bSSam Leffler 	BGE_LOCK(sc);
32960f9bd73bSSam Leffler 	bge_init_locked(sc);
32970f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
329895d67482SBill Paul 
329995d67482SBill Paul 	return;
330095d67482SBill Paul }
330195d67482SBill Paul 
330295d67482SBill Paul /*
330395d67482SBill Paul  * Set media options.
330495d67482SBill Paul  */
330595d67482SBill Paul static int
330695d67482SBill Paul bge_ifmedia_upd(ifp)
330795d67482SBill Paul 	struct ifnet *ifp;
330895d67482SBill Paul {
330995d67482SBill Paul 	struct bge_softc *sc;
331095d67482SBill Paul 	struct mii_data *mii;
331195d67482SBill Paul 	struct ifmedia *ifm;
331295d67482SBill Paul 
331395d67482SBill Paul 	sc = ifp->if_softc;
331495d67482SBill Paul 	ifm = &sc->bge_ifmedia;
331595d67482SBill Paul 
331695d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
331795d67482SBill Paul 	if (sc->bge_tbi) {
331895d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
331995d67482SBill Paul 			return(EINVAL);
332095d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
332195d67482SBill Paul 		case IFM_AUTO:
3322ff50922bSDoug White #ifndef BGE_FAKE_AUTONEG
3323ff50922bSDoug White 			/*
3324ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3325ff50922bSDoug White 			 * mechanism for programming the autoneg
3326ff50922bSDoug White 			 * advertisement registers in TBI mode.
3327ff50922bSDoug White 			 */
3328ff50922bSDoug White 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3329ff50922bSDoug White 				uint32_t sgdig;
3330ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3331ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3332ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3333ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3334ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3335ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3336ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3337ff50922bSDoug White 				DELAY(5);
3338ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3339ff50922bSDoug White 			}
3340ff50922bSDoug White #endif
334195d67482SBill Paul 			break;
334295d67482SBill Paul 		case IFM_1000_SX:
334395d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
334495d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
334595d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
334695d67482SBill Paul 			} else {
334795d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
334895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
334995d67482SBill Paul 			}
335095d67482SBill Paul 			break;
335195d67482SBill Paul 		default:
335295d67482SBill Paul 			return(EINVAL);
335395d67482SBill Paul 		}
335495d67482SBill Paul 		return(0);
335595d67482SBill Paul 	}
335695d67482SBill Paul 
335795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
335895d67482SBill Paul 	if (mii->mii_instance) {
335995d67482SBill Paul 		struct mii_softc *miisc;
336095d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
336195d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
336295d67482SBill Paul 			mii_phy_reset(miisc);
336395d67482SBill Paul 	}
336495d67482SBill Paul 	mii_mediachg(mii);
336595d67482SBill Paul 
336695d67482SBill Paul 	return(0);
336795d67482SBill Paul }
336895d67482SBill Paul 
336995d67482SBill Paul /*
337095d67482SBill Paul  * Report current media status.
337195d67482SBill Paul  */
337295d67482SBill Paul static void
337395d67482SBill Paul bge_ifmedia_sts(ifp, ifmr)
337495d67482SBill Paul 	struct ifnet *ifp;
337595d67482SBill Paul 	struct ifmediareq *ifmr;
337695d67482SBill Paul {
337795d67482SBill Paul 	struct bge_softc *sc;
337895d67482SBill Paul 	struct mii_data *mii;
337995d67482SBill Paul 
338095d67482SBill Paul 	sc = ifp->if_softc;
338195d67482SBill Paul 
338295d67482SBill Paul 	if (sc->bge_tbi) {
338395d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
338495d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
338595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
338695d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
338795d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
338895d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
338995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
339095d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
339195d67482SBill Paul 		else
339295d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
339395d67482SBill Paul 		return;
339495d67482SBill Paul 	}
339595d67482SBill Paul 
339695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
339795d67482SBill Paul 	mii_pollstat(mii);
339895d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
339995d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
340095d67482SBill Paul 
340195d67482SBill Paul 	return;
340295d67482SBill Paul }
340395d67482SBill Paul 
340495d67482SBill Paul static int
340595d67482SBill Paul bge_ioctl(ifp, command, data)
340695d67482SBill Paul 	struct ifnet *ifp;
340795d67482SBill Paul 	u_long command;
340895d67482SBill Paul 	caddr_t data;
340995d67482SBill Paul {
341095d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
341195d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
34120f9bd73bSSam Leffler 	int mask, error = 0;
341395d67482SBill Paul 	struct mii_data *mii;
341495d67482SBill Paul 
341595d67482SBill Paul 	switch(command) {
341695d67482SBill Paul 	case SIOCSIFMTU:
34170434d1b8SBill Paul 		/* Disallow jumbo frames on 5705. */
3418e53d81eeSPaul Saab 		if (((sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
3419e53d81eeSPaul Saab 		      sc->bge_asicrev == BGE_ASICREV_BCM5750) &&
34200434d1b8SBill Paul 		    ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU)
342195d67482SBill Paul 			error = EINVAL;
342295d67482SBill Paul 		else {
342395d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
342413f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
342595d67482SBill Paul 			bge_init(sc);
342695d67482SBill Paul 		}
342795d67482SBill Paul 		break;
342895d67482SBill Paul 	case SIOCSIFFLAGS:
34290f9bd73bSSam Leffler 		BGE_LOCK(sc);
343095d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
343195d67482SBill Paul 			/*
343295d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
343395d67482SBill Paul 			 * then just use the 'set promisc mode' command
343495d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
343595d67482SBill Paul 			 * a full re-init means reloading the firmware and
343695d67482SBill Paul 			 * waiting for it to start up, which may take a
343795d67482SBill Paul 			 * second or two.
343895d67482SBill Paul 			 */
343913f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
344095d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
344195d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
344295d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
344395d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
344413f4c340SRobert Watson 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
344595d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
344695d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
344795d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
344895d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
344995d67482SBill Paul 			} else
34500f9bd73bSSam Leffler 				bge_init_locked(sc);
345195d67482SBill Paul 		} else {
345213f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
345395d67482SBill Paul 				bge_stop(sc);
345495d67482SBill Paul 			}
345595d67482SBill Paul 		}
345695d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
34570f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
345895d67482SBill Paul 		error = 0;
345995d67482SBill Paul 		break;
346095d67482SBill Paul 	case SIOCADDMULTI:
346195d67482SBill Paul 	case SIOCDELMULTI:
346213f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
34630f9bd73bSSam Leffler 			BGE_LOCK(sc);
346495d67482SBill Paul 			bge_setmulti(sc);
34650f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
346695d67482SBill Paul 			error = 0;
346795d67482SBill Paul 		}
346895d67482SBill Paul 		break;
346995d67482SBill Paul 	case SIOCSIFMEDIA:
347095d67482SBill Paul 	case SIOCGIFMEDIA:
347195d67482SBill Paul 		if (sc->bge_tbi) {
347295d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
347395d67482SBill Paul 			    &sc->bge_ifmedia, command);
347495d67482SBill Paul 		} else {
347595d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
347695d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
347795d67482SBill Paul 			    &mii->mii_media, command);
347895d67482SBill Paul 		}
347995d67482SBill Paul 		break;
348095d67482SBill Paul 	case SIOCSIFCAP:
348195d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
348275719184SGleb Smirnoff #ifdef DEVICE_POLLING
348375719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
348475719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
348575719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
348675719184SGleb Smirnoff 				if (error)
348775719184SGleb Smirnoff 					return(error);
348875719184SGleb Smirnoff 				BGE_LOCK(sc);
348975719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
349075719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
349175719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
349275719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
349375719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
349475719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
349575719184SGleb Smirnoff 				BGE_UNLOCK(sc);
349675719184SGleb Smirnoff 			} else {
349775719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
349875719184SGleb Smirnoff 				/* Enable interrupt even in error case */
349975719184SGleb Smirnoff 				BGE_LOCK(sc);
350075719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
350175719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
350275719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
350375719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
350475719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
350575719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
350675719184SGleb Smirnoff 				BGE_UNLOCK(sc);
350775719184SGleb Smirnoff 			}
350875719184SGleb Smirnoff 		}
350975719184SGleb Smirnoff #endif
3510d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3511d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3512d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3513d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3514b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
351595d67482SBill Paul 			else
3516b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
351795d67482SBill Paul 		}
351895d67482SBill Paul 		break;
351995d67482SBill Paul 	default:
3520673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
352195d67482SBill Paul 		break;
352295d67482SBill Paul 	}
352395d67482SBill Paul 
352495d67482SBill Paul 	return(error);
352595d67482SBill Paul }
352695d67482SBill Paul 
352795d67482SBill Paul static void
352895d67482SBill Paul bge_watchdog(ifp)
352995d67482SBill Paul 	struct ifnet *ifp;
353095d67482SBill Paul {
353195d67482SBill Paul 	struct bge_softc *sc;
353295d67482SBill Paul 
353395d67482SBill Paul 	sc = ifp->if_softc;
353495d67482SBill Paul 
3535fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
353695d67482SBill Paul 
353713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
353895d67482SBill Paul 	bge_init(sc);
353995d67482SBill Paul 
354095d67482SBill Paul 	ifp->if_oerrors++;
354195d67482SBill Paul 
354295d67482SBill Paul 	return;
354395d67482SBill Paul }
354495d67482SBill Paul 
354595d67482SBill Paul /*
354695d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
354795d67482SBill Paul  * RX and TX lists.
354895d67482SBill Paul  */
354995d67482SBill Paul static void
355095d67482SBill Paul bge_stop(sc)
355195d67482SBill Paul 	struct bge_softc *sc;
355295d67482SBill Paul {
355395d67482SBill Paul 	struct ifnet *ifp;
355495d67482SBill Paul 	struct ifmedia_entry *ifm;
355595d67482SBill Paul 	struct mii_data *mii = NULL;
355695d67482SBill Paul 	int mtmp, itmp;
355795d67482SBill Paul 
35580f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
35590f9bd73bSSam Leffler 
3560fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
356195d67482SBill Paul 
356295d67482SBill Paul 	if (!sc->bge_tbi)
356395d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
356495d67482SBill Paul 
35650f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
356695d67482SBill Paul 
356795d67482SBill Paul 	/*
356895d67482SBill Paul 	 * Disable all of the receiver blocks
356995d67482SBill Paul 	 */
357095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
357195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
357295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
35735dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3574e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
357595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
357695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
357795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
357895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
357995d67482SBill Paul 
358095d67482SBill Paul 	/*
358195d67482SBill Paul 	 * Disable all of the transmit blocks
358295d67482SBill Paul 	 */
358395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
358495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
358595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
358695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
358795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
35885dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3589e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
359095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
359195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
359295d67482SBill Paul 
359395d67482SBill Paul 	/*
359495d67482SBill Paul 	 * Shut down all of the memory managers and related
359595d67482SBill Paul 	 * state machines.
359695d67482SBill Paul 	 */
359795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
359895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
35995dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3600e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
360195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
360295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
360395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
36045dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3605e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
360695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
360795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
36080434d1b8SBill Paul 	}
360995d67482SBill Paul 
361095d67482SBill Paul 	/* Disable host interrupts. */
361195d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
361295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
361395d67482SBill Paul 
361495d67482SBill Paul 	/*
361595d67482SBill Paul 	 * Tell firmware we're shutting down.
361695d67482SBill Paul 	 */
361795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
361895d67482SBill Paul 
361995d67482SBill Paul 	/* Free the RX lists. */
362095d67482SBill Paul 	bge_free_rx_ring_std(sc);
362195d67482SBill Paul 
362295d67482SBill Paul 	/* Free jumbo RX list. */
36235dc9afc5SPaul Saab 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3624e53d81eeSPaul Saab 	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
362595d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
362695d67482SBill Paul 
362795d67482SBill Paul 	/* Free TX buffers. */
362895d67482SBill Paul 	bge_free_tx_ring(sc);
362995d67482SBill Paul 
363095d67482SBill Paul 	/*
363195d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
363295d67482SBill Paul 	 * unchanged so that things will be put back to normal when
363395d67482SBill Paul 	 * we bring the interface back up.
363495d67482SBill Paul 	 */
363595d67482SBill Paul 	if (!sc->bge_tbi) {
363695d67482SBill Paul 		itmp = ifp->if_flags;
363795d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3638dcc34049SPawel Jakub Dawidek 		/*
3639dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3640dcc34049SPawel Jakub Dawidek 		 */
3641dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
364295d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
364395d67482SBill Paul 			mtmp = ifm->ifm_media;
364495d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
364595d67482SBill Paul 			mii_mediachg(mii);
364695d67482SBill Paul 			ifm->ifm_media = mtmp;
3647dcc34049SPawel Jakub Dawidek 		}
364895d67482SBill Paul 		ifp->if_flags = itmp;
364995d67482SBill Paul 	}
365095d67482SBill Paul 
365195d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
365295d67482SBill Paul 
365313f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
365495d67482SBill Paul 
365595d67482SBill Paul 	return;
365695d67482SBill Paul }
365795d67482SBill Paul 
365895d67482SBill Paul /*
365995d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
366095d67482SBill Paul  * get confused by errant DMAs when rebooting.
366195d67482SBill Paul  */
366295d67482SBill Paul static void
366395d67482SBill Paul bge_shutdown(dev)
366495d67482SBill Paul 	device_t dev;
366595d67482SBill Paul {
366695d67482SBill Paul 	struct bge_softc *sc;
366795d67482SBill Paul 
366895d67482SBill Paul 	sc = device_get_softc(dev);
366995d67482SBill Paul 
36700f9bd73bSSam Leffler 	BGE_LOCK(sc);
367195d67482SBill Paul 	bge_stop(sc);
367295d67482SBill Paul 	bge_reset(sc);
36730f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
367495d67482SBill Paul 
367595d67482SBill Paul 	return;
367695d67482SBill Paul }
367714afefa3SPawel Jakub Dawidek 
367814afefa3SPawel Jakub Dawidek static int
367914afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
368014afefa3SPawel Jakub Dawidek {
368114afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
368214afefa3SPawel Jakub Dawidek 
368314afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
368414afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
368514afefa3SPawel Jakub Dawidek 	bge_stop(sc);
368614afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
368714afefa3SPawel Jakub Dawidek 
368814afefa3SPawel Jakub Dawidek 	return (0);
368914afefa3SPawel Jakub Dawidek }
369014afefa3SPawel Jakub Dawidek 
369114afefa3SPawel Jakub Dawidek static int
369214afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
369314afefa3SPawel Jakub Dawidek {
369414afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
369514afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
369614afefa3SPawel Jakub Dawidek 
369714afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
369814afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
369914afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
370014afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
370114afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
370214afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
370314afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
370414afefa3SPawel Jakub Dawidek 	}
370514afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
370614afefa3SPawel Jakub Dawidek 
370714afefa3SPawel Jakub Dawidek 	return (0);
370814afefa3SPawel Jakub Dawidek }
3709dab5cd05SOleg Bulyzhin 
3710dab5cd05SOleg Bulyzhin static void
3711dab5cd05SOleg Bulyzhin bge_link_upd(sc)
3712dab5cd05SOleg Bulyzhin 	struct bge_softc *sc;
3713dab5cd05SOleg Bulyzhin {
37141f313773SOleg Bulyzhin 	struct mii_data *mii;
37151f313773SOleg Bulyzhin 	uint32_t link, status;
3716dab5cd05SOleg Bulyzhin 
3717dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
37181f313773SOleg Bulyzhin 
3719dab5cd05SOleg Bulyzhin 	/*
3720dab5cd05SOleg Bulyzhin 	 * Process link state changes.
3721dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
3722dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
3723dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
3724dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
3725dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
3726dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
3727dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
3728dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
37291f313773SOleg Bulyzhin 	 *
37301f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
37311f313773SOleg Bulyzhin 	 * BGE_CHIPID_BCM5700_B1 can be used for others BCM5700 revisions.
3732dab5cd05SOleg Bulyzhin 	 */
3733dab5cd05SOleg Bulyzhin 
37341f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
37351f313773SOleg Bulyzhin 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B1) {
3736dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
3737dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
3738dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3739dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
37401f313773SOleg Bulyzhin 
37411f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
37421f313773SOleg Bulyzhin 			if (!sc->bge_link &&
37431f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
37441f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
37451f313773SOleg Bulyzhin 				sc->bge_link++;
37461f313773SOleg Bulyzhin 				if (bootverbose)
37471f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
37481f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
37491f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
37501f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
37511f313773SOleg Bulyzhin 				sc->bge_link = 0;
37521f313773SOleg Bulyzhin 				if (bootverbose)
37531f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
37541f313773SOleg Bulyzhin 			}
37551f313773SOleg Bulyzhin 
3756dab5cd05SOleg Bulyzhin 			/* Clear the interrupt */
3757dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
3758dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
3759dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
3760dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
3761dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
3762dab5cd05SOleg Bulyzhin 		}
3763dab5cd05SOleg Bulyzhin 		return;
3764dab5cd05SOleg Bulyzhin 	}
3765dab5cd05SOleg Bulyzhin 
37661f313773SOleg Bulyzhin 	if (sc->bge_tbi) {
3767dab5cd05SOleg Bulyzhin 		/*
3768dab5cd05SOleg Bulyzhin 		 * Sometimes PCS encoding errors are detected in
3769dab5cd05SOleg Bulyzhin 		 * TBI mode (on fiber NICs), and for some reason
3770dab5cd05SOleg Bulyzhin 		 * the chip will signal them as link changes.
3771dab5cd05SOleg Bulyzhin 		 * If we get a link change event, but the 'PCS
3772dab5cd05SOleg Bulyzhin 		 * encoding error' bit in the MAC status register
3773dab5cd05SOleg Bulyzhin 		 * is set, don't bother doing a link check.
37741f313773SOleg Bulyzhin 		 * This avoids spurious "link UP" messages
3775dab5cd05SOleg Bulyzhin 		 * that sometimes appear on fiber NICs during
3776dab5cd05SOleg Bulyzhin 		 * periods of heavy traffic. (There should be no
3777dab5cd05SOleg Bulyzhin 		 * effect on copper NICs.)
3778dab5cd05SOleg Bulyzhin 		 */
37791f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
37801f313773SOleg Bulyzhin 		if (!(status & (BGE_MACSTAT_PORT_DECODE_ERROR|
37811f313773SOleg Bulyzhin 		    BGE_MACSTAT_MI_COMPLETE))) {
37821f313773SOleg Bulyzhin 			if (!sc->bge_link &&
37831f313773SOleg Bulyzhin 			    (status & BGE_MACSTAT_TBI_PCS_SYNCHED)) {
37841f313773SOleg Bulyzhin 				sc->bge_link++;
37851f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
37861f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
37871f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
37881f313773SOleg Bulyzhin 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
37891f313773SOleg Bulyzhin 				if (bootverbose)
37901f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
37911f313773SOleg Bulyzhin 			} else if (sc->bge_link) {
3792dab5cd05SOleg Bulyzhin 				sc->bge_link = 0;
37931f313773SOleg Bulyzhin 				if (bootverbose)
37941f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
37951f313773SOleg Bulyzhin 			}
37961f313773SOleg Bulyzhin 		}
37971f313773SOleg Bulyzhin 	} else {
37981f313773SOleg Bulyzhin 		/*
37991f313773SOleg Bulyzhin 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
38001f313773SOleg Bulyzhin 		 * in status word always set. Workaround this bug by reading
38011f313773SOleg Bulyzhin 		 * PHY link status directly.
38021f313773SOleg Bulyzhin 		 */
38031f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
38041f313773SOleg Bulyzhin 
38051f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
38061f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
3807dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3808dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
38091f313773SOleg Bulyzhin 
38101f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
38111f313773SOleg Bulyzhin 			if (!sc->bge_link &&
38121f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
38131f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
38141f313773SOleg Bulyzhin 				sc->bge_link++;
38151f313773SOleg Bulyzhin 				if (bootverbose)
38161f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
38171f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
38181f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
38191f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
38201f313773SOleg Bulyzhin 				sc->bge_link = 0;
38211f313773SOleg Bulyzhin 				if (bootverbose)
38221f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
38231f313773SOleg Bulyzhin 			}
38241f313773SOleg Bulyzhin 		}
3825dab5cd05SOleg Bulyzhin 	}
3826dab5cd05SOleg Bulyzhin 
3827dab5cd05SOleg Bulyzhin 	/* Clear the interrupt */
3828dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
3829dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
3830dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
3831dab5cd05SOleg Bulyzhin }
3832