xref: /freebsd/sys/dev/bge/if_bge.c (revision 2246e8c6e88f2c52f6368aef6a87ada62c1e565c)
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
4222a4ecedSMarius Strobl  * MAC chips. The BCM5700, sometimes referred 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>
82f1a7e6d5SScott Long #include <sys/sysctl.h>
83dfe0df9aSPyun YongHyeon #include <sys/taskqueue.h>
8495d67482SBill Paul 
8595d67482SBill Paul #include <net/if.h>
8695d67482SBill Paul #include <net/if_arp.h>
8795d67482SBill Paul #include <net/ethernet.h>
8895d67482SBill Paul #include <net/if_dl.h>
8995d67482SBill Paul #include <net/if_media.h>
9095d67482SBill Paul 
9195d67482SBill Paul #include <net/bpf.h>
9295d67482SBill Paul 
9395d67482SBill Paul #include <net/if_types.h>
9495d67482SBill Paul #include <net/if_vlan_var.h>
9595d67482SBill Paul 
9695d67482SBill Paul #include <netinet/in_systm.h>
9795d67482SBill Paul #include <netinet/in.h>
9895d67482SBill Paul #include <netinet/ip.h>
99ca3f1187SPyun YongHyeon #include <netinet/tcp.h>
10095d67482SBill Paul 
10195d67482SBill Paul #include <machine/bus.h>
10295d67482SBill Paul #include <machine/resource.h>
10395d67482SBill Paul #include <sys/bus.h>
10495d67482SBill Paul #include <sys/rman.h>
10595d67482SBill Paul 
10695d67482SBill Paul #include <dev/mii/mii.h>
10795d67482SBill Paul #include <dev/mii/miivar.h>
1082d3ce713SDavid E. O'Brien #include "miidevs.h"
10995d67482SBill Paul #include <dev/mii/brgphyreg.h>
11095d67482SBill Paul 
11108013fd3SMarius Strobl #ifdef __sparc64__
11208013fd3SMarius Strobl #include <dev/ofw/ofw_bus.h>
11308013fd3SMarius Strobl #include <dev/ofw/openfirm.h>
11408013fd3SMarius Strobl #include <machine/ofw_machdep.h>
11508013fd3SMarius Strobl #include <machine/ver.h>
11608013fd3SMarius Strobl #endif
11708013fd3SMarius Strobl 
1184fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1194fbd232cSWarner Losh #include <dev/pci/pcivar.h>
12095d67482SBill Paul 
12195d67482SBill Paul #include <dev/bge/if_bgereg.h>
12295d67482SBill Paul 
12335f945cdSPyun YongHyeon #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP)
124d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12595d67482SBill Paul 
126f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
127f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12895d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12995d67482SBill Paul 
1307b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
13195d67482SBill Paul #include "miibus_if.h"
13295d67482SBill Paul 
13395d67482SBill Paul /*
13495d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13595d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13695d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13795d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13895d67482SBill Paul  */
139852c67f9SMarius Strobl static const struct bge_type {
1404c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1414c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
142978f2704SMarius Strobl } const bge_devs[] = {
1434c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1444c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
14595d67482SBill Paul 
1464c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1474c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1484c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1494c0da0ffSGleb Smirnoff 
1504c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1514c0da0ffSGleb Smirnoff 
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1721108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5717 },
1731108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5718 },
174bbe2ca75SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5719 },
1754c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1764c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
177effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
178a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5723 },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1834c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1844c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1854c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1899e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1909e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1919e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1929e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
193f7d1b2ebSXin LI 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5756 },
194a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761 },
195a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761E },
196a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761S },
197a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761SE },
198a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5764 },
1994c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
2004c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
2014c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
2024c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
203a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5784 },
204a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785F },
205a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785G },
2069e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
2079e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
208a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787F },
2099e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
2104c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
2114c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
2124c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
2134c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
2144c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
21538cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
21638cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
217a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57760 },
218b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57761 },
219b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57765 },
220a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
221b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57781 },
222b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57785 },
223a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
224a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
225b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57791 },
226b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57795 },
2274c0da0ffSGleb Smirnoff 
2284c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2294c0da0ffSGleb Smirnoff 
2304c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2314c0da0ffSGleb Smirnoff 
232a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
233a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
234a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PP250450 },
235a5779553SStanislav Sedov 
2364c0da0ffSGleb Smirnoff 	{ 0, 0 }
23795d67482SBill Paul };
23895d67482SBill Paul 
2394c0da0ffSGleb Smirnoff static const struct bge_vendor {
2404c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2414c0da0ffSGleb Smirnoff 	const char	*v_name;
242978f2704SMarius Strobl } const bge_vendors[] = {
2434c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2444c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2454c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2464c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2474c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2484c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
249a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2504c0da0ffSGleb Smirnoff 
2514c0da0ffSGleb Smirnoff 	{ 0, NULL }
2524c0da0ffSGleb Smirnoff };
2534c0da0ffSGleb Smirnoff 
2544c0da0ffSGleb Smirnoff static const struct bge_revision {
2554c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2564c0da0ffSGleb Smirnoff 	const char	*br_name;
257978f2704SMarius Strobl } const bge_revisions[] = {
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2594c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2604c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2614c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2624c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2654c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2674c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2684c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2694c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2704c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2714c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2724c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2749e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2764c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2794c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2824c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2844c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2854c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2884c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2894c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2904c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
29142787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2924c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2934c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2944c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2954c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2964c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2974c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2984c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2994c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
3000c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
3011108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_A0,	"BCM5717 A0" },
3021108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_B0,	"BCM5717 B0" },
303bbe2ca75SPyun YongHyeon 	{ BGE_CHIPID_BCM5719_A0,	"BCM5719 A0" },
30450515680SPyun YongHyeon 	{ BGE_CHIPID_BCM5720_A0,	"BCM5720 A0" },
3050c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
3060c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
3070c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
308bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
309a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
310a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
311a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
312a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
31381179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3146f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
3156f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3166f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
31738cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
31838cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
319b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_A0,	"BCM57765 A0" },
320b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_B0,	"BCM57765 B0" },
321a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
322a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3234c0da0ffSGleb Smirnoff 
3244c0da0ffSGleb Smirnoff 	{ 0, NULL }
3254c0da0ffSGleb Smirnoff };
3264c0da0ffSGleb Smirnoff 
3274c0da0ffSGleb Smirnoff /*
3284c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3294c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3304c0da0ffSGleb Smirnoff  */
331978f2704SMarius Strobl static const struct bge_revision const bge_majorrevs[] = {
3329e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3339e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3349e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3359e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3369e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3379e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3389e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3399e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3409e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3419e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3429e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
343a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
344a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
345a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
34681179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3476f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
34838cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
349b4a256acSPyun YongHyeon 	{ BGE_ASICREV_BCM57765,		"unknown BCM57765" },
350a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3511108273aSPyun YongHyeon 	{ BGE_ASICREV_BCM5717,		"unknown BCM5717" },
352bbe2ca75SPyun YongHyeon 	{ BGE_ASICREV_BCM5719,		"unknown BCM5719" },
35350515680SPyun YongHyeon 	{ BGE_ASICREV_BCM5720,		"unknown BCM5720" },
3544c0da0ffSGleb Smirnoff 
3554c0da0ffSGleb Smirnoff 	{ 0, NULL }
3564c0da0ffSGleb Smirnoff };
3574c0da0ffSGleb Smirnoff 
3580c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3590c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3600c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3610c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3620c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
363a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3641108273aSPyun YongHyeon #define	BGE_IS_5717_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5717_PLUS)
3654c0da0ffSGleb Smirnoff 
3664c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3674c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
36838cc658fSJohn Baldwin 
36938cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
37038cc658fSJohn Baldwin 
371e51a25f8SAlfred Perlstein static int bge_probe(device_t);
372e51a25f8SAlfred Perlstein static int bge_attach(device_t);
373e51a25f8SAlfred Perlstein static int bge_detach(device_t);
37414afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
37514afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3763f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
377f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3785b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
379f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
3805b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
3815b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
382f41ac2beSBill Paul 
383ea9c3a30SPyun YongHyeon static void bge_devinfo(struct bge_softc *);
384062af0b0SPyun YongHyeon static int bge_mbox_reorder(struct bge_softc *);
385062af0b0SPyun YongHyeon 
3865fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
38738cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
38838cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
38938cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
39038cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
39138cc658fSJohn Baldwin 
392b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
3931108273aSPyun YongHyeon static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
394dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
39595d67482SBill Paul 
3968cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
397e51a25f8SAlfred Perlstein static void bge_tick(void *);
3982280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
399e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
4003f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
401d598b626SPyun YongHyeon static struct mbuf *bge_check_short_dma(struct mbuf *);
4022e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
4031108273aSPyun YongHyeon     uint16_t *, uint16_t *);
404676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
40595d67482SBill Paul 
406e51a25f8SAlfred Perlstein static void bge_intr(void *);
407dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
408dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
4090f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
410e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
411e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
4120f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
413e51a25f8SAlfred Perlstein static void bge_init(void *);
4145a147ba6SPyun YongHyeon static void bge_stop_block(struct bge_softc *, bus_size_t, uint32_t);
415e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
416b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
417b6c974e8SWarner Losh static int bge_shutdown(device_t);
41867d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
419e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
420e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
42195d67482SBill Paul 
42238cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
42338cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
42438cc658fSJohn Baldwin 
4253f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
426e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
42795d67482SBill Paul 
4283e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
429e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
430cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
43195d67482SBill Paul 
432e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
433e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
434943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
435943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
436e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
437e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
438e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
439e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
440e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
441e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
44295d67482SBill Paul 
443e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
444e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
44550515680SPyun YongHyeon static uint32_t bge_dma_swap_options(struct bge_softc *);
44695d67482SBill Paul 
4475fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4483f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
449e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
45038cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
45195d67482SBill Paul #ifdef notdef
4523f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
45395d67482SBill Paul #endif
4549ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
455e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
45695d67482SBill Paul 
457e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
458e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
459e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
46075719184SGleb Smirnoff #ifdef DEVICE_POLLING
4611abcdbd1SAttilio Rao static int bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
46275719184SGleb Smirnoff #endif
46395d67482SBill Paul 
4648cb1383cSDoug Ambrisko #define	BGE_RESET_START 1
4658cb1383cSDoug Ambrisko #define	BGE_RESET_STOP  2
4668cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4678cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4688cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
469797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4708cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
471dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
47295d67482SBill Paul 
4736f8718a3SScott Long /*
4746f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4756f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4766f8718a3SScott Long  * traps on certain architectures.
4776f8718a3SScott Long  */
4786f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4796f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4806f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
4816f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
4826f8718a3SScott Long #endif
4836f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
4842280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
4852280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
4862280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
4872280c16bSPyun YongHyeon     struct sysctl_oid_list *);
488763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
4896f8718a3SScott Long 
49095d67482SBill Paul static device_method_t bge_methods[] = {
49195d67482SBill Paul 	/* Device interface */
49295d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
49395d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
49495d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
49595d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
49614afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
49714afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
49895d67482SBill Paul 
49995d67482SBill Paul 	/* MII interface */
50095d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
50195d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
50295d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
50395d67482SBill Paul 
5044b7ec270SMarius Strobl 	DEVMETHOD_END
50595d67482SBill Paul };
50695d67482SBill Paul 
50795d67482SBill Paul static driver_t bge_driver = {
50895d67482SBill Paul 	"bge",
50995d67482SBill Paul 	bge_methods,
51095d67482SBill Paul 	sizeof(struct bge_softc)
51195d67482SBill Paul };
51295d67482SBill Paul 
51395d67482SBill Paul static devclass_t bge_devclass;
51495d67482SBill Paul 
515f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
51695d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
51795d67482SBill Paul 
518f1a7e6d5SScott Long static int bge_allow_asf = 1;
519f1a7e6d5SScott Long 
520f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
521f1a7e6d5SScott Long 
5226472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
523f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
524f1a7e6d5SScott Long 	"Allow ASF mode if available");
525c4529f41SMichael Reifenberger 
52608013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
52708013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
52808013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
52908013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
53008013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
53108013fd3SMarius Strobl 
53208013fd3SMarius Strobl static int
5335fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
53408013fd3SMarius Strobl {
53508013fd3SMarius Strobl #ifdef __sparc64__
53608013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
53708013fd3SMarius Strobl 	device_t dev;
53808013fd3SMarius Strobl 	uint32_t subvendor;
53908013fd3SMarius Strobl 
54008013fd3SMarius Strobl 	dev = sc->bge_dev;
54108013fd3SMarius Strobl 
54208013fd3SMarius Strobl 	/*
54308013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
54408013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
54508013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
54608013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
54708013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
54808013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
54908013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
55008013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
55108013fd3SMarius Strobl 	 * there.
55208013fd3SMarius Strobl 	 */
55308013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
55408013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5552d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
55608013fd3SMarius Strobl 		return (0);
55708013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
55808013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
55908013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
56008013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
56108013fd3SMarius Strobl 			return (0);
56208013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
56308013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
56408013fd3SMarius Strobl 			return (0);
56508013fd3SMarius Strobl 	}
56608013fd3SMarius Strobl #endif
56708013fd3SMarius Strobl 	return (1);
56808013fd3SMarius Strobl }
56908013fd3SMarius Strobl 
5703f74909aSGleb Smirnoff static uint32_t
5713f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
57295d67482SBill Paul {
57395d67482SBill Paul 	device_t dev;
5746f8718a3SScott Long 	uint32_t val;
57595d67482SBill Paul 
576a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
577a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
578a4431ebaSPyun YongHyeon 		return (0);
579a4431ebaSPyun YongHyeon 
58095d67482SBill Paul 	dev = sc->bge_dev;
58195d67482SBill Paul 
58295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5836f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5846f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5856f8718a3SScott Long 	return (val);
58695d67482SBill Paul }
58795d67482SBill Paul 
58895d67482SBill Paul static void
5893f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
59095d67482SBill Paul {
59195d67482SBill Paul 	device_t dev;
59295d67482SBill Paul 
593a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
594a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
595a4431ebaSPyun YongHyeon 		return;
596a4431ebaSPyun YongHyeon 
59795d67482SBill Paul 	dev = sc->bge_dev;
59895d67482SBill Paul 
59995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
60095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
6016f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
60295d67482SBill Paul }
60395d67482SBill Paul 
60495d67482SBill Paul #ifdef notdef
6053f74909aSGleb Smirnoff static uint32_t
6063f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
60795d67482SBill Paul {
60895d67482SBill Paul 	device_t dev;
60995d67482SBill Paul 
61095d67482SBill Paul 	dev = sc->bge_dev;
61195d67482SBill Paul 
61295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
61395d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
61495d67482SBill Paul }
61595d67482SBill Paul #endif
61695d67482SBill Paul 
61795d67482SBill Paul static void
6183f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
61995d67482SBill Paul {
62095d67482SBill Paul 	device_t dev;
62195d67482SBill Paul 
62295d67482SBill Paul 	dev = sc->bge_dev;
62395d67482SBill Paul 
62495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
62595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
62695d67482SBill Paul }
62795d67482SBill Paul 
6286f8718a3SScott Long static void
6296f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6306f8718a3SScott Long {
6316f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6326f8718a3SScott Long }
6336f8718a3SScott Long 
63438cc658fSJohn Baldwin static void
63538cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
63638cc658fSJohn Baldwin {
63738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
63838cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
63938cc658fSJohn Baldwin 
64038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
641062af0b0SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
642062af0b0SPyun YongHyeon 		CSR_READ_4(sc, off);
64338cc658fSJohn Baldwin }
64438cc658fSJohn Baldwin 
645f41ac2beSBill Paul /*
646f41ac2beSBill Paul  * Map a single buffer address.
647f41ac2beSBill Paul  */
648f41ac2beSBill Paul 
649f41ac2beSBill Paul static void
6503f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
651f41ac2beSBill Paul {
652f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
653f41ac2beSBill Paul 
654f41ac2beSBill Paul 	if (error)
655f41ac2beSBill Paul 		return;
656f41ac2beSBill Paul 
6575b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
6585b610048SPyun YongHyeon 
659f41ac2beSBill Paul 	ctx = arg;
660f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
661f41ac2beSBill Paul }
662f41ac2beSBill Paul 
66338cc658fSJohn Baldwin static uint8_t
66438cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
66538cc658fSJohn Baldwin {
66638cc658fSJohn Baldwin 	uint32_t access, byte = 0;
66738cc658fSJohn Baldwin 	int i;
66838cc658fSJohn Baldwin 
66938cc658fSJohn Baldwin 	/* Lock. */
67038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
67138cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
67238cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
67338cc658fSJohn Baldwin 			break;
67438cc658fSJohn Baldwin 		DELAY(20);
67538cc658fSJohn Baldwin 	}
67638cc658fSJohn Baldwin 	if (i == 8000)
67738cc658fSJohn Baldwin 		return (1);
67838cc658fSJohn Baldwin 
67938cc658fSJohn Baldwin 	/* Enable access. */
68038cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
68138cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
68238cc658fSJohn Baldwin 
68338cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
68438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
68538cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
68638cc658fSJohn Baldwin 		DELAY(10);
68738cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
68838cc658fSJohn Baldwin 			DELAY(10);
68938cc658fSJohn Baldwin 			break;
69038cc658fSJohn Baldwin 		}
69138cc658fSJohn Baldwin 	}
69238cc658fSJohn Baldwin 
69338cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
69438cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
69538cc658fSJohn Baldwin 		return (1);
69638cc658fSJohn Baldwin 	}
69738cc658fSJohn Baldwin 
69838cc658fSJohn Baldwin 	/* Get result. */
69938cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
70038cc658fSJohn Baldwin 
70138cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
70238cc658fSJohn Baldwin 
70338cc658fSJohn Baldwin 	/* Disable access. */
70438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
70538cc658fSJohn Baldwin 
70638cc658fSJohn Baldwin 	/* Unlock. */
70738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
70838cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
70938cc658fSJohn Baldwin 
71038cc658fSJohn Baldwin 	return (0);
71138cc658fSJohn Baldwin }
71238cc658fSJohn Baldwin 
71338cc658fSJohn Baldwin /*
71438cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
71538cc658fSJohn Baldwin  */
71638cc658fSJohn Baldwin static int
71738cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
71838cc658fSJohn Baldwin {
71938cc658fSJohn Baldwin 	int err = 0, i;
72038cc658fSJohn Baldwin 	uint8_t byte = 0;
72138cc658fSJohn Baldwin 
72238cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
72338cc658fSJohn Baldwin 		return (1);
72438cc658fSJohn Baldwin 
72538cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
72638cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
72738cc658fSJohn Baldwin 		if (err)
72838cc658fSJohn Baldwin 			break;
72938cc658fSJohn Baldwin 		*(dest + i) = byte;
73038cc658fSJohn Baldwin 	}
73138cc658fSJohn Baldwin 
73238cc658fSJohn Baldwin 	return (err ? 1 : 0);
73338cc658fSJohn Baldwin }
73438cc658fSJohn Baldwin 
73595d67482SBill Paul /*
73695d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
73795d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
73895d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
73995d67482SBill Paul  * access method.
74095d67482SBill Paul  */
7413f74909aSGleb Smirnoff static uint8_t
7423f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
74395d67482SBill Paul {
74495d67482SBill Paul 	int i;
7453f74909aSGleb Smirnoff 	uint32_t byte = 0;
74695d67482SBill Paul 
74795d67482SBill Paul 	/*
74895d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
74995d67482SBill Paul 	 * having to use the bitbang method.
75095d67482SBill Paul 	 */
75195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
75295d67482SBill Paul 
75395d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
75495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
75595d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
75695d67482SBill Paul 	DELAY(20);
75795d67482SBill Paul 
75895d67482SBill Paul 	/* Issue the read EEPROM command. */
75995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
76095d67482SBill Paul 
76195d67482SBill Paul 	/* Wait for completion */
76295d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
76395d67482SBill Paul 		DELAY(10);
76495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
76595d67482SBill Paul 			break;
76695d67482SBill Paul 	}
76795d67482SBill Paul 
768d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
769fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
770f6789fbaSPyun YongHyeon 		return (1);
77195d67482SBill Paul 	}
77295d67482SBill Paul 
77395d67482SBill Paul 	/* Get result. */
77495d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
77595d67482SBill Paul 
7760c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
77795d67482SBill Paul 
77895d67482SBill Paul 	return (0);
77995d67482SBill Paul }
78095d67482SBill Paul 
78195d67482SBill Paul /*
78295d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
78395d67482SBill Paul  */
78495d67482SBill Paul static int
7853f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
78695d67482SBill Paul {
7873f74909aSGleb Smirnoff 	int i, error = 0;
7883f74909aSGleb Smirnoff 	uint8_t byte = 0;
78995d67482SBill Paul 
79095d67482SBill Paul 	for (i = 0; i < cnt; i++) {
7913f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
7923f74909aSGleb Smirnoff 		if (error)
79395d67482SBill Paul 			break;
79495d67482SBill Paul 		*(dest + i) = byte;
79595d67482SBill Paul 	}
79695d67482SBill Paul 
7973f74909aSGleb Smirnoff 	return (error ? 1 : 0);
79895d67482SBill Paul }
79995d67482SBill Paul 
80095d67482SBill Paul static int
8013f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
80295d67482SBill Paul {
80395d67482SBill Paul 	struct bge_softc *sc;
804a813ed78SPyun YongHyeon 	uint32_t val;
80595d67482SBill Paul 	int i;
80695d67482SBill Paul 
80795d67482SBill Paul 	sc = device_get_softc(dev);
80895d67482SBill Paul 
809a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
810a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
811a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
812a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
813a813ed78SPyun YongHyeon 		DELAY(80);
81437ceeb4dSPaul Saab 	}
81537ceeb4dSPaul Saab 
81695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
81795d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
81895d67482SBill Paul 
819a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
82095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
821d5d23857SJung-uk Kim 		DELAY(10);
82295d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
823a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
824a813ed78SPyun YongHyeon 			DELAY(5);
825a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
82695d67482SBill Paul 			break;
82795d67482SBill Paul 		}
828a813ed78SPyun YongHyeon 	}
82995d67482SBill Paul 
83095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
8315fea260fSMarius Strobl 		device_printf(sc->bge_dev,
8325fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
8335fea260fSMarius Strobl 		    phy, reg, val);
83437ceeb4dSPaul Saab 		val = 0;
83595d67482SBill Paul 	}
83695d67482SBill Paul 
837a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
838a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
839a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
840a813ed78SPyun YongHyeon 		DELAY(80);
84137ceeb4dSPaul Saab 	}
84237ceeb4dSPaul Saab 
84395d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
84495d67482SBill Paul 		return (0);
84595d67482SBill Paul 
8460c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
84795d67482SBill Paul }
84895d67482SBill Paul 
84995d67482SBill Paul static int
8503f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
85195d67482SBill Paul {
85295d67482SBill Paul 	struct bge_softc *sc;
85395d67482SBill Paul 	int i;
85495d67482SBill Paul 
85595d67482SBill Paul 	sc = device_get_softc(dev);
85695d67482SBill Paul 
85738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
85838cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
85938cc658fSJohn Baldwin 		return (0);
86038cc658fSJohn Baldwin 
861a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
862a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
863a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
864a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
865a813ed78SPyun YongHyeon 		DELAY(80);
86637ceeb4dSPaul Saab 	}
86737ceeb4dSPaul Saab 
86895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
86995d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
87095d67482SBill Paul 
87195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
872d5d23857SJung-uk Kim 		DELAY(10);
87338cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
87438cc658fSJohn Baldwin 			DELAY(5);
87538cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
87695d67482SBill Paul 			break;
877d5d23857SJung-uk Kim 		}
87838cc658fSJohn Baldwin 	}
879d5d23857SJung-uk Kim 
880a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
881a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
882a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
883a813ed78SPyun YongHyeon 		DELAY(80);
884a813ed78SPyun YongHyeon 	}
885a813ed78SPyun YongHyeon 
886a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
88738cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
888*2246e8c6SPyun YongHyeon 		    "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
88938cc658fSJohn Baldwin 		    phy, reg, val);
89037ceeb4dSPaul Saab 
89195d67482SBill Paul 	return (0);
89295d67482SBill Paul }
89395d67482SBill Paul 
89495d67482SBill Paul static void
8953f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
89695d67482SBill Paul {
89795d67482SBill Paul 	struct bge_softc *sc;
89895d67482SBill Paul 	struct mii_data *mii;
899a0a03d1eSPyun YongHyeon 	uint32_t mac_mode, rx_mode, tx_mode;
900e4146b95SPyun YongHyeon 
90195d67482SBill Paul 	sc = device_get_softc(dev);
902e4146b95SPyun YongHyeon 	if ((sc->bge_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
903e4146b95SPyun YongHyeon 		return;
90495d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
90595d67482SBill Paul 
906d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
907d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
908d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
909d4f5240aSPyun YongHyeon 		case IFM_10_T:
910d4f5240aSPyun YongHyeon 		case IFM_100_TX:
911d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
912d4f5240aSPyun YongHyeon 			break;
913d4f5240aSPyun YongHyeon 		case IFM_1000_T:
914d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
915d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
916d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
917d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
918d4f5240aSPyun YongHyeon 			else
919d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
920d4f5240aSPyun YongHyeon 			break;
921d4f5240aSPyun YongHyeon 		default:
922d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
923d4f5240aSPyun YongHyeon 			break;
924d4f5240aSPyun YongHyeon 		}
925d4f5240aSPyun YongHyeon 	} else
926d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
927d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
928d4f5240aSPyun YongHyeon 		return;
929a0a03d1eSPyun YongHyeon 
930a0a03d1eSPyun YongHyeon 	/*
931a0a03d1eSPyun YongHyeon 	 * APE firmware touches these registers to keep the MAC
932a0a03d1eSPyun YongHyeon 	 * connected to the outside world.  Try to keep the
933a0a03d1eSPyun YongHyeon 	 * accesses atomic.
934a0a03d1eSPyun YongHyeon 	 */
935a0a03d1eSPyun YongHyeon 
936a0a03d1eSPyun YongHyeon 	/* Set the port mode (MII/GMII) to match the link speed. */
937a0a03d1eSPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
938a0a03d1eSPyun YongHyeon 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
939a0a03d1eSPyun YongHyeon 	tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
940a0a03d1eSPyun YongHyeon 	rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
941a0a03d1eSPyun YongHyeon 
942ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
943ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
944a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_GMII;
9453f74909aSGleb Smirnoff 	else
946a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_MII;
94795d67482SBill Paul 
948a0a03d1eSPyun YongHyeon 	/* Set MAC flow control behavior to match link flow control settings. */
949a0a03d1eSPyun YongHyeon 	tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
950a0a03d1eSPyun YongHyeon 	rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
9516854be25SPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0) {
952a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
953a0a03d1eSPyun YongHyeon 			tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
954a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
955a0a03d1eSPyun YongHyeon 			rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
956a0a03d1eSPyun YongHyeon 	} else
957a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
958a0a03d1eSPyun YongHyeon 
959a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
9609b80ffe7SPyun YongHyeon 	DELAY(40);
961a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
962a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
96395d67482SBill Paul }
96495d67482SBill Paul 
96595d67482SBill Paul /*
96695d67482SBill Paul  * Intialize a standard receive ring descriptor.
96795d67482SBill Paul  */
96895d67482SBill Paul static int
969943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
97095d67482SBill Paul {
971943787f3SPyun YongHyeon 	struct mbuf *m;
97295d67482SBill Paul 	struct bge_rx_bd *r;
973a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
974943787f3SPyun YongHyeon 	bus_dmamap_t map;
975a23634a1SPyun YongHyeon 	int error, nsegs;
97695d67482SBill Paul 
977f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
978f5459d4cSPyun YongHyeon 	    (sc->bge_ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
979f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
980f5459d4cSPyun YongHyeon 		m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
981f5459d4cSPyun YongHyeon 		if (m == NULL)
982f5459d4cSPyun YongHyeon 			return (ENOBUFS);
983f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
984f5459d4cSPyun YongHyeon 	} else {
985943787f3SPyun YongHyeon 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
986943787f3SPyun YongHyeon 		if (m == NULL)
98795d67482SBill Paul 			return (ENOBUFS);
988943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
989f5459d4cSPyun YongHyeon 	}
990652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
991943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
992943787f3SPyun YongHyeon 
9930ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
994943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
995a23634a1SPyun YongHyeon 	if (error != 0) {
996943787f3SPyun YongHyeon 		m_freem(m);
997a23634a1SPyun YongHyeon 		return (error);
998f41ac2beSBill Paul 	}
999943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1000943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1001943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1002943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1003943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
1004943787f3SPyun YongHyeon 	}
1005943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
1006943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1007943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
1008943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
1009e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1010943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1011a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1012a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1013e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1014a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1015e907febfSPyun YongHyeon 	r->bge_idx = i;
1016f41ac2beSBill Paul 
10170ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1018943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
101995d67482SBill Paul 
102095d67482SBill Paul 	return (0);
102195d67482SBill Paul }
102295d67482SBill Paul 
102395d67482SBill Paul /*
102495d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
102595d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
102695d67482SBill Paul  */
102795d67482SBill Paul static int
1028943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
102995d67482SBill Paul {
10301be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1031943787f3SPyun YongHyeon 	bus_dmamap_t map;
10321be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1033943787f3SPyun YongHyeon 	struct mbuf *m;
1034943787f3SPyun YongHyeon 	int error, nsegs;
103595d67482SBill Paul 
1036943787f3SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1037943787f3SPyun YongHyeon 	if (m == NULL)
103895d67482SBill Paul 		return (ENOBUFS);
103995d67482SBill Paul 
1040943787f3SPyun YongHyeon 	m_cljget(m, M_DONTWAIT, MJUM9BYTES);
1041943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
1042943787f3SPyun YongHyeon 		m_freem(m);
104395d67482SBill Paul 		return (ENOBUFS);
104495d67482SBill Paul 	}
1045943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1046652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1047943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
10481be6acb7SGleb Smirnoff 
10491be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1050943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1051943787f3SPyun YongHyeon 	if (error != 0) {
1052943787f3SPyun YongHyeon 		m_freem(m);
10531be6acb7SGleb Smirnoff 		return (error);
1054f7cea149SGleb Smirnoff 	}
10551be6acb7SGleb Smirnoff 
1056aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1057943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1058943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1059943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1060943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1061943787f3SPyun YongHyeon 	}
1062943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1063943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1064943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1065943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1066943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1067e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1068e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1069e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1070e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1071e0b7b101SPyun YongHyeon 
10721be6acb7SGleb Smirnoff 	/*
10731be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
10741be6acb7SGleb Smirnoff 	 */
1075943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
10764e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
10774e7ba1abSGleb Smirnoff 	r->bge_idx = i;
10784e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
10794e7ba1abSGleb Smirnoff 	switch (nsegs) {
10804e7ba1abSGleb Smirnoff 	case 4:
10814e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
10824e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
10834e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1084e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
10854e7ba1abSGleb Smirnoff 	case 3:
1086e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1087e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1088e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1089e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
10904e7ba1abSGleb Smirnoff 	case 2:
10914e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
10924e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
10934e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1094e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
10954e7ba1abSGleb Smirnoff 	case 1:
10964e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
10974e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
10984e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1099e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
11004e7ba1abSGleb Smirnoff 		break;
11014e7ba1abSGleb Smirnoff 	default:
11024e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
11034e7ba1abSGleb Smirnoff 	}
1104f41ac2beSBill Paul 
1105a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1106943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
110795d67482SBill Paul 
110895d67482SBill Paul 	return (0);
110995d67482SBill Paul }
111095d67482SBill Paul 
111195d67482SBill Paul static int
11123f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
111395d67482SBill Paul {
11143ee5d7daSPyun YongHyeon 	int error, i;
111595d67482SBill Paul 
1116e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
111703e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1118e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1119943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
11203ee5d7daSPyun YongHyeon 			return (error);
112103e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
11221888f324SPyun YongHyeon 	}
112395d67482SBill Paul 
1124f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1125d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1126f41ac2beSBill Paul 
1127e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1128e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
112995d67482SBill Paul 
113095d67482SBill Paul 	return (0);
113195d67482SBill Paul }
113295d67482SBill Paul 
113395d67482SBill Paul static void
11343f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
113595d67482SBill Paul {
113695d67482SBill Paul 	int i;
113795d67482SBill Paul 
113895d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
113995d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
11400ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1141e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1142e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
11430ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1144f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1145e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1146e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
114795d67482SBill Paul 		}
1148f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
114995d67482SBill Paul 		    sizeof(struct bge_rx_bd));
115095d67482SBill Paul 	}
115195d67482SBill Paul }
115295d67482SBill Paul 
115395d67482SBill Paul static int
11543f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
115595d67482SBill Paul {
115695d67482SBill Paul 	struct bge_rcb *rcb;
11573ee5d7daSPyun YongHyeon 	int error, i;
115895d67482SBill Paul 
1159e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
116003e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
116195d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1162943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
11633ee5d7daSPyun YongHyeon 			return (error);
116403e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
11651888f324SPyun YongHyeon 	}
116695d67482SBill Paul 
1167f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1168d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1169f41ac2beSBill Paul 
1170e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
117195d67482SBill Paul 
11728a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1173f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
11748a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
11758a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
117667111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
117795d67482SBill Paul 
1178e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
117995d67482SBill Paul 
118095d67482SBill Paul 	return (0);
118195d67482SBill Paul }
118295d67482SBill Paul 
118395d67482SBill Paul static void
11843f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
118595d67482SBill Paul {
118695d67482SBill Paul 	int i;
118795d67482SBill Paul 
118895d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
118995d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1190e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1191e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1192e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1193f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1194f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1195e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1196e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
119795d67482SBill Paul 		}
1198f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
11991be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
120095d67482SBill Paul 	}
120195d67482SBill Paul }
120295d67482SBill Paul 
120395d67482SBill Paul static void
12043f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
120595d67482SBill Paul {
120695d67482SBill Paul 	int i;
120795d67482SBill Paul 
1208f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
120995d67482SBill Paul 		return;
121095d67482SBill Paul 
121195d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
121295d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
12130ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1214e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1215e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
12160ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1217f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1218e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1219e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
122095d67482SBill Paul 		}
1221f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
122295d67482SBill Paul 		    sizeof(struct bge_tx_bd));
122395d67482SBill Paul 	}
122495d67482SBill Paul }
122595d67482SBill Paul 
122695d67482SBill Paul static int
12273f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
122895d67482SBill Paul {
122995d67482SBill Paul 	sc->bge_txcnt = 0;
123095d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
12313927098fSPaul Saab 
1232e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1233e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
12345c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1235e6bf277eSPyun YongHyeon 
123614bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
123714bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
123838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
123914bbd30fSGleb Smirnoff 
12403927098fSPaul Saab 	/* 5700 b2 errata */
1241e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
124238cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
12433927098fSPaul Saab 
124414bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
124538cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
12463927098fSPaul Saab 	/* 5700 b2 errata */
1247e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
124838cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
124995d67482SBill Paul 
125095d67482SBill Paul 	return (0);
125195d67482SBill Paul }
125295d67482SBill Paul 
125395d67482SBill Paul static void
12543e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
12553e9b1bcaSJung-uk Kim {
12563e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
12573e9b1bcaSJung-uk Kim 
12583e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
12593e9b1bcaSJung-uk Kim 
12603e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
12613e9b1bcaSJung-uk Kim 
126245ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
12633e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
126445ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12653e9b1bcaSJung-uk Kim 	else
126645ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12673e9b1bcaSJung-uk Kim }
12683e9b1bcaSJung-uk Kim 
12693e9b1bcaSJung-uk Kim static void
12703f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
127195d67482SBill Paul {
127295d67482SBill Paul 	struct ifnet *ifp;
127395d67482SBill Paul 	struct ifmultiaddr *ifma;
12743f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
127595d67482SBill Paul 	int h, i;
127695d67482SBill Paul 
12770f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
12780f9bd73bSSam Leffler 
1279fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
128095d67482SBill Paul 
128195d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
128295d67482SBill Paul 		for (i = 0; i < 4; i++)
12830c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
128495d67482SBill Paul 		return;
128595d67482SBill Paul 	}
128695d67482SBill Paul 
128795d67482SBill Paul 	/* First, zot all the existing filters. */
128895d67482SBill Paul 	for (i = 0; i < 4; i++)
128995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
129095d67482SBill Paul 
129195d67482SBill Paul 	/* Now program new ones. */
1292eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
129395d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
129495d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
129595d67482SBill Paul 			continue;
12960e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
12970c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
12980c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
129995d67482SBill Paul 	}
1300eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
130195d67482SBill Paul 
130295d67482SBill Paul 	for (i = 0; i < 4; i++)
130395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
130495d67482SBill Paul }
130595d67482SBill Paul 
13068cb1383cSDoug Ambrisko static void
1307cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1308cb2eacc7SYaroslav Tykhiy {
1309cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1310cb2eacc7SYaroslav Tykhiy 
1311cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1312cb2eacc7SYaroslav Tykhiy 
1313cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1314cb2eacc7SYaroslav Tykhiy 
1315cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1316cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1317cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1318cb2eacc7SYaroslav Tykhiy 	else
1319cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1320cb2eacc7SYaroslav Tykhiy }
1321cb2eacc7SYaroslav Tykhiy 
1322cb2eacc7SYaroslav Tykhiy static void
1323797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
13248cb1383cSDoug Ambrisko {
1325797ab05eSPyun YongHyeon 
13268cb1383cSDoug Ambrisko 	/*
13278cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
13288cb1383cSDoug Ambrisko 	 */
13298cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1330888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
13318cb1383cSDoug Ambrisko 
13328cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
13338cb1383cSDoug Ambrisko 		switch (type) {
13348cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1335224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1336224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
13378cb1383cSDoug Ambrisko 			break;
13388cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
1339224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1340224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
13418cb1383cSDoug Ambrisko 			break;
13428cb1383cSDoug Ambrisko 		}
13438cb1383cSDoug Ambrisko 	}
13448cb1383cSDoug Ambrisko }
13458cb1383cSDoug Ambrisko 
13468cb1383cSDoug Ambrisko static void
1347797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
13488cb1383cSDoug Ambrisko {
1349797ab05eSPyun YongHyeon 
13508cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
13518cb1383cSDoug Ambrisko 		switch (type) {
13528cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1353224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1354224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
13558cb1383cSDoug Ambrisko 			/* START DONE */
13568cb1383cSDoug Ambrisko 			break;
13578cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
1358224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1359224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
13608cb1383cSDoug Ambrisko 			break;
13618cb1383cSDoug Ambrisko 		}
13628cb1383cSDoug Ambrisko 	}
13638cb1383cSDoug Ambrisko }
13648cb1383cSDoug Ambrisko 
13658cb1383cSDoug Ambrisko static void
1366797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
13678cb1383cSDoug Ambrisko {
1368797ab05eSPyun YongHyeon 
13698cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13708cb1383cSDoug Ambrisko 		switch (type) {
13718cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1372224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1373224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
13748cb1383cSDoug Ambrisko 			break;
13758cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
1376224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1377224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
13788cb1383cSDoug Ambrisko 			break;
13798cb1383cSDoug Ambrisko 		}
13808cb1383cSDoug Ambrisko 	}
13818cb1383cSDoug Ambrisko }
13828cb1383cSDoug Ambrisko 
1383797ab05eSPyun YongHyeon static void
1384797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
13858cb1383cSDoug Ambrisko {
13868cb1383cSDoug Ambrisko 	int i;
13878cb1383cSDoug Ambrisko 
13888cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13893c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
13903fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
13919931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
13928cb1383cSDoug Ambrisko 
13938cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
13949931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
13959931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
13968cb1383cSDoug Ambrisko 				break;
13978cb1383cSDoug Ambrisko 			DELAY(10);
13988cb1383cSDoug Ambrisko 		}
13998cb1383cSDoug Ambrisko 	}
14008cb1383cSDoug Ambrisko }
14018cb1383cSDoug Ambrisko 
140250515680SPyun YongHyeon static uint32_t
140350515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
140450515680SPyun YongHyeon {
140550515680SPyun YongHyeon 	uint32_t dma_options;
140650515680SPyun YongHyeon 
140750515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
140850515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
140950515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
141050515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
141150515680SPyun YongHyeon #endif
141250515680SPyun YongHyeon 	if ((sc)->bge_asicrev == BGE_ASICREV_BCM5720)
141350515680SPyun YongHyeon 		dma_options |= BGE_MODECTL_BYTESWAP_B2HRX_DATA |
141450515680SPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA | BGE_MODECTL_B2HRX_ENABLE |
141550515680SPyun YongHyeon 		    BGE_MODECTL_HTX2B_ENABLE;
141650515680SPyun YongHyeon 
141750515680SPyun YongHyeon 	return (dma_options);
141850515680SPyun YongHyeon }
141950515680SPyun YongHyeon 
142095d67482SBill Paul /*
1421c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
142295d67482SBill Paul  */
142395d67482SBill Paul static int
14243f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
142595d67482SBill Paul {
142650515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1427fbc374afSPyun YongHyeon 	uint16_t val;
142895d67482SBill Paul 	int i;
142995d67482SBill Paul 
14308cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
14311108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
14321108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
14331108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
14341108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
143595d67482SBill Paul 
143695d67482SBill Paul 	/* Clear the MAC control register */
143795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
14389b80ffe7SPyun YongHyeon 	DELAY(40);
143995d67482SBill Paul 
144095d67482SBill Paul 	/*
144195d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
144295d67482SBill Paul 	 * internal memory.
144395d67482SBill Paul 	 */
144495d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
14453f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
144695d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
144795d67482SBill Paul 
144895d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
14493f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
145095d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
145195d67482SBill Paul 
1452fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1453fbc374afSPyun YongHyeon 		/*
1454d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1455fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1456fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1457fbc374afSPyun YongHyeon 		 */
1458fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1459fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1460fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1461fbc374afSPyun YongHyeon 	}
1462fbc374afSPyun YongHyeon 
1463186f842bSJung-uk Kim 	/*
1464186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1465186f842bSJung-uk Kim 	 */
1466186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1467186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1468652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
146948630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
147048630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
147148630d79SPyun YongHyeon 		else
1472186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1473652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
14744c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1475186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1476186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1477186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1478186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1479186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1480186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1481cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1482cbb2b2feSPyun YongHyeon 			/*
1483cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1484cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1485cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1486cbb2b2feSPyun YongHyeon 			 * register.
1487cbb2b2feSPyun YongHyeon 			 */
1488cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1489cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1490186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1491186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1492186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1493186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1494186f842bSJung-uk Kim 		} else {
1495186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1496186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1497186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
14980c8aa4eaSJung-uk Kim 			    0x0F;
1499186f842bSJung-uk Kim 		}
1500e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1501e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
15023f74909aSGleb Smirnoff 			uint32_t tmp;
15035cba12d3SPaul Saab 
1504186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
15050c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1506186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1507186f842bSJung-uk Kim 				dma_rw_ctl |=
1508186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
15095cba12d3SPaul Saab 
1510186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1511186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1512186f842bSJung-uk Kim 		}
1513186f842bSJung-uk Kim 	} else {
1514186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1515186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1516186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1517186f842bSJung-uk Kim 
1518186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1519186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1520186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1521186f842bSJung-uk Kim 	}
1522186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1523186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1524186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1525186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1526e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1527186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
15285cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1529b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
15301108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1531b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1532b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1533bbe2ca75SPyun YongHyeon 		/*
1534bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1535bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1536bbe2ca75SPyun YongHyeon 		 * disabled.
1537bbe2ca75SPyun YongHyeon 		 */
1538bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
1539bbe2ca75SPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM57765)
1540bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1541b4a256acSPyun YongHyeon 	}
15425cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
154395d67482SBill Paul 
154495d67482SBill Paul 	/*
154595d67482SBill Paul 	 * Set up general mode register.
154695d67482SBill Paul 	 */
154750515680SPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc) | BGE_MODECTL_MAC_ATTN_INTR |
154850515680SPyun YongHyeon 	    BGE_MODECTL_HOST_SEND_BDS | BGE_MODECTL_TX_NO_PHDR_CSUM;
154995d67482SBill Paul 
155095d67482SBill Paul 	/*
155190447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
155290447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
155390447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
155490447aadSMarius Strobl 	 * certain bridges.
155590447aadSMarius Strobl 	 */
155690447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
155790447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
155850515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
155990447aadSMarius Strobl 
156090447aadSMarius Strobl 	/*
15618cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
15628cb1383cSDoug Ambrisko 	 */
15638cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
156450515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
156550515680SPyun YongHyeon 
156650515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
15678cb1383cSDoug Ambrisko 
15688cb1383cSDoug Ambrisko 	/*
1569ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1570c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1571c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
157295d67482SBill Paul 	 */
1573c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1574c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
157595d67482SBill Paul 
157695d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
15770c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
157895d67482SBill Paul 
157938cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
158038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
158138cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
158238cc658fSJohn Baldwin 
158338cc658fSJohn Baldwin 		/* Put PHY into ready state */
158438cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
158538cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
158638cc658fSJohn Baldwin 		DELAY(40);
158738cc658fSJohn Baldwin 	}
158838cc658fSJohn Baldwin 
158995d67482SBill Paul 	return (0);
159095d67482SBill Paul }
159195d67482SBill Paul 
159295d67482SBill Paul static int
15933f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
159495d67482SBill Paul {
159595d67482SBill Paul 	struct bge_rcb *rcb;
1596e907febfSPyun YongHyeon 	bus_size_t vrcb;
1597e907febfSPyun YongHyeon 	bge_hostaddr taddr;
1598bbe2ca75SPyun YongHyeon 	uint32_t dmactl, val;
15998a315a6dSPyun YongHyeon 	int i, limit;
160095d67482SBill Paul 
160195d67482SBill Paul 	/*
160295d67482SBill Paul 	 * Initialize the memory window pointer register so that
160395d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
160495d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
160595d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
160695d67482SBill Paul 	 */
160795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
160895d67482SBill Paul 
1609822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1610822f63fcSBill Paul 
16117ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
161295d67482SBill Paul 		/* Configure mbuf memory pool */
16130dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1614822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1615822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1616822f63fcSBill Paul 		else
161795d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
161895d67482SBill Paul 
161995d67482SBill Paul 		/* Configure DMA resource pool */
16200434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
16210434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
162295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
16230434d1b8SBill Paul 	}
162495d67482SBill Paul 
162595d67482SBill Paul 	/* Configure mbuf pool watermarks */
162650515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
16271108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
16281108273aSPyun YongHyeon 		if (sc->bge_ifp->if_mtu > ETHERMTU) {
16291108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
16301108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
16311108273aSPyun YongHyeon 		} else {
16321108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
16331108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
16341108273aSPyun YongHyeon 		}
16351108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
1636fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1637fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1638fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
163938cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
164038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
164138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
164238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
164338cc658fSJohn Baldwin 	} else {
164438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
164538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
164638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
164738cc658fSJohn Baldwin 	}
164895d67482SBill Paul 
164995d67482SBill Paul 	/* Configure DMA resource watermarks */
165095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
165195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
165295d67482SBill Paul 
165395d67482SBill Paul 	/* Enable buffer manager */
1654bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1655bbe2ca75SPyun YongHyeon 	/*
1656bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
1657bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
1658bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
1659bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
1660bbe2ca75SPyun YongHyeon 	 */
1661bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
1662bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1663bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
166495d67482SBill Paul 
166595d67482SBill Paul 	/* Poll for buffer manager start indication */
166695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1667d5d23857SJung-uk Kim 		DELAY(10);
16680c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
166995d67482SBill Paul 			break;
167095d67482SBill Paul 	}
167195d67482SBill Paul 
167295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
16735a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
167495d67482SBill Paul 		return (ENXIO);
167595d67482SBill Paul 	}
167695d67482SBill Paul 
167795d67482SBill Paul 	/* Enable flow-through queues */
16780c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
167995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
168095d67482SBill Paul 
168195d67482SBill Paul 	/* Wait until queue initialization is complete */
168295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1683d5d23857SJung-uk Kim 		DELAY(10);
168495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
168595d67482SBill Paul 			break;
168695d67482SBill Paul 	}
168795d67482SBill Paul 
168895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1689fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
169095d67482SBill Paul 		return (ENXIO);
169195d67482SBill Paul 	}
169295d67482SBill Paul 
16938a315a6dSPyun YongHyeon 	/*
16948a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
16958a315a6dSPyun YongHyeon 	 *
16968a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
16978a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
16988a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
16998a315a6dSPyun YongHyeon 	 *
17008a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
17018a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
17028a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
17038a315a6dSPyun YongHyeon 	 *   to the controller.
17048a315a6dSPyun YongHyeon 	 *
17058a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
17068a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
17078a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
17088a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
17098a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
17108a315a6dSPyun YongHyeon 	 *   be disabled.
17118a315a6dSPyun YongHyeon 	 *
17128a315a6dSPyun YongHyeon 	 * Receive Return Ring
17138a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
17148a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
17158a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
17168a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
17178a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
17188a315a6dSPyun YongHyeon 	 *
17198a315a6dSPyun YongHyeon 	 * Send Ring
17208a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
17218a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
17228a315a6dSPyun YongHyeon 	 */
17238a315a6dSPyun YongHyeon 
17248a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
1725f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1726f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1727f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1728f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1729f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1730f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1731f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
17321108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
17331108273aSPyun YongHyeon 		/*
17341108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
17351108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
17361108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
17371108273aSPyun YongHyeon 		 * Bit 0     : Reserved
17381108273aSPyun YongHyeon 		 */
17391108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
17401108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
17411108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
17428a315a6dSPyun YongHyeon 		/*
17438a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
17448a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
17458a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
17468a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
17478a315a6dSPyun YongHyeon 		 */
17480434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
17498a315a6dSPyun YongHyeon 	} else {
17508a315a6dSPyun YongHyeon 		/*
17518a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
17528a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
17538a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
17548a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
17558a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
17568a315a6dSPyun YongHyeon 		 */
17570434d1b8SBill Paul 		rcb->bge_maxlen_flags =
17580434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
17598a315a6dSPyun YongHyeon 	}
1760bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
176150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
176250515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
17631108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
17641108273aSPyun YongHyeon 	else
176595d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
17668a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
17670c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
17680c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
176967111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
177067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
177195d67482SBill Paul 
17728a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
17738a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
17748a315a6dSPyun YongHyeon 
177595d67482SBill Paul 	/*
17768a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
17778a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
17788a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
177995d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
178095d67482SBill Paul 	 * high enough to require it).
178195d67482SBill Paul 	 */
17824c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1783f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
17848a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
1785f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1786f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1787f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1788f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1789f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1790f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1791f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
17921be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
17931be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
1794bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
179550515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
179650515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
17971108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
17981108273aSPyun YongHyeon 		else
179995d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
180067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
180167111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
180267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
180367111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
18048a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
18050434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
18060434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
180767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
18088a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
18098a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
18108a315a6dSPyun YongHyeon 	}
181195d67482SBill Paul 
18128a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
18135e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
1814f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
181567111612SJohn Polstra 		rcb->bge_maxlen_flags =
181667111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
18170434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
18180434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
18198a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
18208a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
18210434d1b8SBill Paul 	}
182295d67482SBill Paul 
1823ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
1824ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
1825427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
1826427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
1827427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
18288d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
18298d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
1830ca4f8986SPyun YongHyeon 	}
183195d67482SBill Paul 	/*
18328a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
18338a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
18348a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
18358a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
18368a315a6dSPyun YongHyeon 	 *
183795d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
183895d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
183995d67482SBill Paul 	 * each ring.
18409ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
18419ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
18429ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
18439ba784dbSScott Long 	 * are reports that it might not need to be so strict.
184438cc658fSJohn Baldwin 	 *
184538cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
184638cc658fSJohn Baldwin 	 * well.
184795d67482SBill Paul 	 */
18485345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
18496f8718a3SScott Long 		val = 8;
18506f8718a3SScott Long 	else
18516f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
18526f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
18532a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
18542a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
18552a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
18561108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18571108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
18581108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
18591108273aSPyun YongHyeon 	}
186095d67482SBill Paul 
186195d67482SBill Paul 	/*
18628a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
18638a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
18648a315a6dSPyun YongHyeon 	 * located in NIC memory.
186595d67482SBill Paul 	 */
18668a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
18678a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
18688a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
18698a315a6dSPyun YongHyeon 	else
18708a315a6dSPyun YongHyeon 		limit = 1;
1871e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
18728a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1873e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1874e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1875e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1876e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
187795d67482SBill Paul 	}
187895d67482SBill Paul 
18798a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
1880e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1881e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1882e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1883e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1884bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
188550515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
188650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
18871108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
18881108273aSPyun YongHyeon 	else
1889e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1890e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1891e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1892e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
189395d67482SBill Paul 
18948a315a6dSPyun YongHyeon 	/*
18958a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
18968a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
18978a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
18988a315a6dSPyun YongHyeon 	 */
1899bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
190050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
190150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
19021108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
19031108273aSPyun YongHyeon 		limit = 16;
19041108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
19058a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
1906b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
1907b4a256acSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57765)
19088a315a6dSPyun YongHyeon 		limit = 4;
19098a315a6dSPyun YongHyeon 	else
19108a315a6dSPyun YongHyeon 		limit = 1;
19118a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
1912e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
19138a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1914e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1915e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1916e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
19178a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
1918e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
191938cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
19203f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1921e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
192295d67482SBill Paul 	}
192395d67482SBill Paul 
192495d67482SBill Paul 	/*
19258a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
19268a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
19278a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
192895d67482SBill Paul 	 */
1929e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1930e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1931e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1932e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
19338a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1934e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1935e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
193695d67482SBill Paul 
193795d67482SBill Paul 	/* Set random backoff seed for TX */
193895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
19394a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
19404a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
19414a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
194295d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
194395d67482SBill Paul 
194495d67482SBill Paul 	/* Set inter-packet gap */
194550515680SPyun YongHyeon 	val = 0x2620;
194650515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
194750515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
194850515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
194950515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
195095d67482SBill Paul 
195195d67482SBill Paul 	/*
195295d67482SBill Paul 	 * Specify which ring to use for packets that don't match
195395d67482SBill Paul 	 * any RX rules.
195495d67482SBill Paul 	 */
195595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
195695d67482SBill Paul 
195795d67482SBill Paul 	/*
195895d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
195995d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
196095d67482SBill Paul 	 */
196195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
196295d67482SBill Paul 
196395d67482SBill Paul 	/* Inialize RX list placement stats mask. */
19640c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
196595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
196695d67482SBill Paul 
196795d67482SBill Paul 	/* Disable host coalescing until we get it set up */
196895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
196995d67482SBill Paul 
197095d67482SBill Paul 	/* Poll to make sure it's shut down. */
197195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1972d5d23857SJung-uk Kim 		DELAY(10);
197395d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
197495d67482SBill Paul 			break;
197595d67482SBill Paul 	}
197695d67482SBill Paul 
197795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1978fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1979fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
198095d67482SBill Paul 		return (ENXIO);
198195d67482SBill Paul 	}
198295d67482SBill Paul 
198395d67482SBill Paul 	/* Set up host coalescing defaults */
198495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
198595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
198695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
198795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
19887ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
198995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
199095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
19910434d1b8SBill Paul 	}
1992b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1993b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
199495d67482SBill Paul 
199595d67482SBill Paul 	/* Set up address of statistics block */
19967ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1997f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1998f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
199995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2000f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
20010434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
200295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
20030434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
20040434d1b8SBill Paul 	}
20050434d1b8SBill Paul 
20060434d1b8SBill Paul 	/* Set up address of status block */
2007f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2008f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
200995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2010f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
201195d67482SBill Paul 
201230f57f61SPyun YongHyeon 	/* Set up status block size. */
201330f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2014864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
201530f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2016864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2017864104feSPyun YongHyeon 	} else {
201830f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2019864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2020864104feSPyun YongHyeon 	}
2021864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2022864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2023864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
202430f57f61SPyun YongHyeon 
202595d67482SBill Paul 	/* Turn on host coalescing state machine */
202630f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
202795d67482SBill Paul 
202895d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
202995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
203095d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
203195d67482SBill Paul 
203295d67482SBill Paul 	/* Turn on RX list placement state machine */
203395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
203495d67482SBill Paul 
203595d67482SBill Paul 	/* Turn on RX list selector state machine. */
20367ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
203795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
203895d67482SBill Paul 
2039*2246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2040ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2041ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2042ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2043ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2044ea3b4127SPyun YongHyeon 
2045ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2046ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2047ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2048ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2049ea3b4127SPyun YongHyeon 	else
2050ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2051ea3b4127SPyun YongHyeon 
2052ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
20539b80ffe7SPyun YongHyeon 	DELAY(40);
205495d67482SBill Paul 
205595d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
205695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
205795d67482SBill Paul 
205895d67482SBill Paul #ifdef notdef
205995d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
206095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
206195d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
206295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
206395d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
206495d67482SBill Paul #endif
206595d67482SBill Paul 
206695d67482SBill Paul 	/* Turn on DMA completion state machine */
20677ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
206895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
206995d67482SBill Paul 
20706f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
20716f8718a3SScott Long 
20726f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2073a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
20743889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
20756f8718a3SScott Long 
20767aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
20777aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
20787aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
20797aa4b937SPyun YongHyeon 
208095d67482SBill Paul 	/* Turn on write DMA state machine */
20816f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
20824f09c4c7SMarius Strobl 	DELAY(40);
208395d67482SBill Paul 
208495d67482SBill Paul 	/* Turn on read DMA state machine */
20854f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
20861108273aSPyun YongHyeon 
20871108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
20881108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
20891108273aSPyun YongHyeon 
2090a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2091a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2092a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2093a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2094a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2095a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
20964f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
20974f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
20981108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2099ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
21001108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
21011108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
210255a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
210355a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
210455a24a05SPyun YongHyeon 	}
210550515680SPyun YongHyeon 
2106e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
210750515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
210850515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2109e3215f76SPyun YongHyeon 		/*
2110e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2111e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2112e3215f76SPyun YongHyeon 		 */
2113e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2114e3215f76SPyun YongHyeon 	}
211550515680SPyun YongHyeon 
2116d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2117d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2118d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
21191108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
21201108273aSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc)) {
2121bbe2ca75SPyun YongHyeon 		dmactl = CSR_READ_4(sc, BGE_RDMA_RSRVCTRL);
2122bbe2ca75SPyun YongHyeon 		/*
2123bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2124bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2125bbe2ca75SPyun YongHyeon 		 */
2126f7add34cSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
2127f7add34cSPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
2128bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2129bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2130bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2131bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2132bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2133bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2134bbe2ca75SPyun YongHyeon 		}
2135d255f2a9SPyun YongHyeon 		/*
2136d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2137d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2138d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2139d255f2a9SPyun YongHyeon 		 */
2140bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL, dmactl |
2141d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2142d255f2a9SPyun YongHyeon 	}
2143bbe2ca75SPyun YongHyeon 
2144e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2145bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2146bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2147bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2148bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2149e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2150e3215f76SPyun YongHyeon 		/*
2151e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2152e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2153e3215f76SPyun YongHyeon 		 */
2154e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2155e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2156e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2157e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2158bbe2ca75SPyun YongHyeon 	}
2159bbe2ca75SPyun YongHyeon 
21604f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
21614f09c4c7SMarius Strobl 	DELAY(40);
216295d67482SBill Paul 
216395d67482SBill Paul 	/* Turn on RX data completion state machine */
216495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
216595d67482SBill Paul 
216695d67482SBill Paul 	/* Turn on RX BD initiator state machine */
216795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
216895d67482SBill Paul 
216995d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
217095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
217195d67482SBill Paul 
217295d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
21737ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
217495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
217595d67482SBill Paul 
217695d67482SBill Paul 	/* Turn on send BD completion state machine */
217795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
217895d67482SBill Paul 
217995d67482SBill Paul 	/* Turn on send data completion state machine */
2180a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2181a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2182a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2183a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
218495d67482SBill Paul 
218595d67482SBill Paul 	/* Turn on send data initiator state machine */
21861108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
21871108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
21881108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2189ca3f1187SPyun YongHyeon 	else
219095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
219195d67482SBill Paul 
219295d67482SBill Paul 	/* Turn on send BD initiator state machine */
219395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
219495d67482SBill Paul 
219595d67482SBill Paul 	/* Turn on send BD selector state machine */
219695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
219795d67482SBill Paul 
21980c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
219995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
220095d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
220195d67482SBill Paul 
220295d67482SBill Paul 	/* ack/clear link change events */
220395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
22040434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
22050434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2206f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
220795d67482SBill Paul 
22086ede2cfaSPyun YongHyeon 	/*
22096ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
22106ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
22116ede2cfaSPyun YongHyeon 	 */
2212652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
221395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2214a1d52896SBill Paul 	} else {
22157ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
22167ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
22177ed3f0f0SPyun YongHyeon 			DELAY(80);
22187ed3f0f0SPyun YongHyeon 		}
22191f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
22204c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2221a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2222a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2223a1d52896SBill Paul 	}
222495d67482SBill Paul 
22251f313773SOleg Bulyzhin 	/*
22261f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
22271f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
22281f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
22291f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
22301f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
22311f313773SOleg Bulyzhin 	 */
22321f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
22331f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
22341f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
22351f313773SOleg Bulyzhin 
223695d67482SBill Paul 	/* Enable link state change attentions. */
223795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
223895d67482SBill Paul 
223995d67482SBill Paul 	return (0);
224095d67482SBill Paul }
224195d67482SBill Paul 
22424c0da0ffSGleb Smirnoff const struct bge_revision *
22434c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
22444c0da0ffSGleb Smirnoff {
22454c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
22464c0da0ffSGleb Smirnoff 
22474c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
22484c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
22494c0da0ffSGleb Smirnoff 			return (br);
22504c0da0ffSGleb Smirnoff 	}
22514c0da0ffSGleb Smirnoff 
22524c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
22534c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
22544c0da0ffSGleb Smirnoff 			return (br);
22554c0da0ffSGleb Smirnoff 	}
22564c0da0ffSGleb Smirnoff 
22574c0da0ffSGleb Smirnoff 	return (NULL);
22584c0da0ffSGleb Smirnoff }
22594c0da0ffSGleb Smirnoff 
22604c0da0ffSGleb Smirnoff const struct bge_vendor *
22614c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
22624c0da0ffSGleb Smirnoff {
22634c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
22644c0da0ffSGleb Smirnoff 
22654c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
22664c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
22674c0da0ffSGleb Smirnoff 			return (v);
22684c0da0ffSGleb Smirnoff 
22694c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
22704c0da0ffSGleb Smirnoff 	return (NULL);
22714c0da0ffSGleb Smirnoff }
22724c0da0ffSGleb Smirnoff 
227395d67482SBill Paul /*
227495d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
22754c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
22764c0da0ffSGleb Smirnoff  *
22774c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
22787c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
22797c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
22807c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
22817c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
228295d67482SBill Paul  */
228395d67482SBill Paul static int
22843f74909aSGleb Smirnoff bge_probe(device_t dev)
228595d67482SBill Paul {
2286978f2704SMarius Strobl 	char buf[96];
2287978f2704SMarius Strobl 	char model[64];
2288978f2704SMarius Strobl 	const struct bge_revision *br;
2289978f2704SMarius Strobl 	const char *pname;
22904c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
2291978f2704SMarius Strobl 	const struct bge_type *t = bge_devs;
2292978f2704SMarius Strobl 	const struct bge_vendor *v;
2293978f2704SMarius Strobl 	uint32_t id;
2294978f2704SMarius Strobl 	uint16_t did, vid;
229595d67482SBill Paul 
229695d67482SBill Paul 	sc->bge_dev = dev;
22977c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
22987c929cf9SJung-uk Kim 	did = pci_get_device(dev);
22994c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
23007c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2301a5779553SStanislav Sedov 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2302a5779553SStanislav Sedov 			    BGE_PCIMISCCTL_ASICREV_SHIFT;
23031108273aSPyun YongHyeon 			if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
23041108273aSPyun YongHyeon 				/*
23051108273aSPyun YongHyeon 				 * Find the ASCI revision.  Different chips
23061108273aSPyun YongHyeon 				 * use different registers.
23071108273aSPyun YongHyeon 				 */
23081108273aSPyun YongHyeon 				switch (pci_get_device(dev)) {
23091108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5717:
23101108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5718:
2311bbe2ca75SPyun YongHyeon 				case BCOM_DEVICEID_BCM5719:
231250515680SPyun YongHyeon 				case BCOM_DEVICEID_BCM5720:
23131108273aSPyun YongHyeon 					id = pci_read_config(dev,
23141108273aSPyun YongHyeon 					    BGE_PCI_GEN2_PRODID_ASICREV, 4);
23151108273aSPyun YongHyeon 					break;
2316b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57761:
2317b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57765:
2318b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57781:
2319b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57785:
2320b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57791:
2321b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57795:
2322b4a256acSPyun YongHyeon 					id = pci_read_config(dev,
2323b4a256acSPyun YongHyeon 					    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2324b4a256acSPyun YongHyeon 					break;
23251108273aSPyun YongHyeon 				default:
2326a5779553SStanislav Sedov 					id = pci_read_config(dev,
2327a5779553SStanislav Sedov 					    BGE_PCI_PRODID_ASICREV, 4);
23281108273aSPyun YongHyeon 				}
23291108273aSPyun YongHyeon 			}
23304c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
23317c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
2332852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2333852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
23344e35d186SJung-uk Kim 				snprintf(model, 64, "%s", pname);
23354e35d186SJung-uk Kim 			else
2336978f2704SMarius Strobl 				snprintf(model, 64, "%s %s", v->v_name,
23377c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
23387c929cf9SJung-uk Kim 				    "NetXtreme Ethernet Controller");
2339a5779553SStanislav Sedov 			snprintf(buf, 96, "%s, %sASIC rev. %#08x", model,
2340a5779553SStanislav Sedov 			    br != NULL ? "" : "unknown ", id);
23414c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
234295d67482SBill Paul 			return (0);
234395d67482SBill Paul 		}
234495d67482SBill Paul 		t++;
234595d67482SBill Paul 	}
234695d67482SBill Paul 
234795d67482SBill Paul 	return (ENXIO);
234895d67482SBill Paul }
234995d67482SBill Paul 
2350f41ac2beSBill Paul static void
23513f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2352f41ac2beSBill Paul {
2353f41ac2beSBill Paul 	int i;
2354f41ac2beSBill Paul 
23553f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2356f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2357f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
23580ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2359f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2360f41ac2beSBill Paul 	}
2361943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2362943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2363943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2364f41ac2beSBill Paul 
23653f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2366f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2367f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2368f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2369f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2370f41ac2beSBill Paul 	}
2371943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2372943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2373943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2374f41ac2beSBill Paul 
23753f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2376f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2377f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
23780ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2379f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2380f41ac2beSBill Paul 	}
2381f41ac2beSBill Paul 
23820ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
23830ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2384c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2385c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
23860ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
23870ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2388f41ac2beSBill Paul 
23893f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2390e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2391e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2392e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2393e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2394f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2395f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2396f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2397f41ac2beSBill Paul 
2398f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2399f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2400f41ac2beSBill Paul 
24013f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2402e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2403e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2404e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2405e65bed95SPyun YongHyeon 
2406e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2407e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2408f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2409f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2410f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2411f41ac2beSBill Paul 
2412f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2413f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2414f41ac2beSBill Paul 
24153f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2416e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2417e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2418e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2419e65bed95SPyun YongHyeon 
2420e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2421e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2422f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2423f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2424f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2425f41ac2beSBill Paul 
2426f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2427f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2428f41ac2beSBill Paul 
24293f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2430e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2431e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2432e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2433e65bed95SPyun YongHyeon 
2434e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2435f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2436f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2437f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2438f41ac2beSBill Paul 
2439f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2440f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2441f41ac2beSBill Paul 
24423f74909aSGleb Smirnoff 	/* Destroy status block. */
2443e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2444e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2445e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2446e65bed95SPyun YongHyeon 
2447e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2448f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2449f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2450f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2451f41ac2beSBill Paul 
2452f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2453f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2454f41ac2beSBill Paul 
24553f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2456e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2457e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2458e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2459e65bed95SPyun YongHyeon 
2460e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2461f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2462f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2463f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2464f41ac2beSBill Paul 
2465f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2466f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2467f41ac2beSBill Paul 
24685b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
24695b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
24705b610048SPyun YongHyeon 
24713f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2472f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2473f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2474f41ac2beSBill Paul }
2475f41ac2beSBill Paul 
2476f41ac2beSBill Paul static int
24775b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
24785b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
24795b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2480f41ac2beSBill Paul {
24813f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
24825b610048SPyun YongHyeon 	int error;
2483f41ac2beSBill Paul 
24845b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2485fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
24865b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
24875b610048SPyun YongHyeon 	if (error != 0) {
24885b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
24895b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
24905b610048SPyun YongHyeon 		return (ENOMEM);
24915b610048SPyun YongHyeon 	}
24925b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
24935b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
24945b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
24955b610048SPyun YongHyeon 	if (error != 0) {
24965b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
24975b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
24985b610048SPyun YongHyeon 		return (ENOMEM);
24995b610048SPyun YongHyeon 	}
25005b610048SPyun YongHyeon 	/* Load the address of the ring. */
25015b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
25025b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
25035b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
25045b610048SPyun YongHyeon 	if (error != 0) {
25055b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
25065b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
25075b610048SPyun YongHyeon 		return (ENOMEM);
25085b610048SPyun YongHyeon 	}
25095b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
25105b610048SPyun YongHyeon 	return (0);
25115b610048SPyun YongHyeon }
25125b610048SPyun YongHyeon 
25135b610048SPyun YongHyeon static int
25145b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
25155b610048SPyun YongHyeon {
25165b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2517fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
25185b610048SPyun YongHyeon 	int i, error;
2519f41ac2beSBill Paul 
2520f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2521f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2522f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2523f41ac2beSBill Paul 	/*
2524f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2525f41ac2beSBill Paul 	 */
25264eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2527f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
25284eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
25294eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2530e65bed95SPyun YongHyeon 	if (error != 0) {
2531fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2532fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2533e65bed95SPyun YongHyeon 		return (ENOMEM);
2534e65bed95SPyun YongHyeon 	}
2535e65bed95SPyun YongHyeon 
25365b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
25375b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
25385b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
25395b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
25405b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
25415b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
25425b610048SPyun YongHyeon 	if (error)
25435b610048SPyun YongHyeon 		return (error);
25445b610048SPyun YongHyeon 
25455b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
25465b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
25475b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
25485b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
25495b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
25505b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
25515b610048SPyun YongHyeon 	if (error)
25525b610048SPyun YongHyeon 		return (error);
25535b610048SPyun YongHyeon 
25545b610048SPyun YongHyeon 	/* Create tag for TX ring. */
25555b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
25565b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
25575b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
25585b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
25595b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
25605b610048SPyun YongHyeon 	if (error)
25615b610048SPyun YongHyeon 		return (error);
25625b610048SPyun YongHyeon 
2563f41ac2beSBill Paul 	/*
25645b610048SPyun YongHyeon 	 * Create tag for status block.
25655b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
25665b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
25675b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
25685b610048SPyun YongHyeon 	 * of configured number of ring.
2569f41ac2beSBill Paul 	 */
25705b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
25715b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
25725b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
25735b610048SPyun YongHyeon 	else
25745b610048SPyun YongHyeon 		sbsz = 32;
25755b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
25765b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
25775b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
25785b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
25795b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
25805b610048SPyun YongHyeon 	if (error)
25815b610048SPyun YongHyeon 		return (error);
25825b610048SPyun YongHyeon 
258312c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
258412c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
258512c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
258612c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
258712c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
258812c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
258912c65daeSPyun YongHyeon 	if (error)
259012c65daeSPyun YongHyeon 		return (error);
259112c65daeSPyun YongHyeon 
25925b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
25935b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
25945b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
25955b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
25965b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
25975b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
25985b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
25995b610048SPyun YongHyeon 		if (error)
26005b610048SPyun YongHyeon 			return (error);
26015b610048SPyun YongHyeon 	}
26025b610048SPyun YongHyeon 
26035b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
2604d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
2605d2ffe15aSPyun YongHyeon 		/*
2606d2ffe15aSPyun YongHyeon 		 * XXX
2607d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
2608d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
2609062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
2610062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
2611d2ffe15aSPyun YongHyeon 		 */
2612062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
2613d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
2614d2ffe15aSPyun YongHyeon 	}
2615fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
2616fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
2617fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
2618fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
26195b610048SPyun YongHyeon 	if (error != 0) {
26205b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
26215b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
26225b610048SPyun YongHyeon 		return (ENOMEM);
26235b610048SPyun YongHyeon 	}
26245b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
26251108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2626ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
2627ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
2628ca3f1187SPyun YongHyeon 	} else {
2629ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
2630ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
2631ca3f1187SPyun YongHyeon 	}
26325b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
2633ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2634ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
2635ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
2636f41ac2beSBill Paul 
2637f41ac2beSBill Paul 	if (error) {
26380ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
26390ac56796SPyun YongHyeon 		return (ENOMEM);
26400ac56796SPyun YongHyeon 	}
26410ac56796SPyun YongHyeon 
26425b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
2643f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
2644f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
2645f5459d4cSPyun YongHyeon 	else
2646f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
26475b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
2648f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
2649f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
26500ac56796SPyun YongHyeon 
26510ac56796SPyun YongHyeon 	if (error) {
26520ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
2653f41ac2beSBill Paul 		return (ENOMEM);
2654f41ac2beSBill Paul 	}
2655f41ac2beSBill Paul 
26563f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
2657943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2658943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
2659943787f3SPyun YongHyeon 	if (error) {
2660943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
2661943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
2662943787f3SPyun YongHyeon 		return (ENOMEM);
2663943787f3SPyun YongHyeon 	}
2664f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
26650ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2666f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
2667f41ac2beSBill Paul 		if (error) {
2668fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
2669fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
2670f41ac2beSBill Paul 			return (ENOMEM);
2671f41ac2beSBill Paul 		}
2672f41ac2beSBill Paul 	}
2673f41ac2beSBill Paul 
26743f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
2675f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
26760ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
2677f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
2678f41ac2beSBill Paul 		if (error) {
2679fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
26800ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
2681f41ac2beSBill Paul 			return (ENOMEM);
2682f41ac2beSBill Paul 		}
2683f41ac2beSBill Paul 	}
2684f41ac2beSBill Paul 
26855b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
26864c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
26875b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
26888a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
26891be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
26901be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
2691f41ac2beSBill Paul 		if (error) {
2692fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
26933f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
2694f41ac2beSBill Paul 			return (ENOMEM);
2695f41ac2beSBill Paul 		}
26963f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
2697943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2698943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
2699943787f3SPyun YongHyeon 		if (error) {
2700943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
27011b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
2702943787f3SPyun YongHyeon 			return (ENOMEM);
2703943787f3SPyun YongHyeon 		}
2704f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2705f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2706f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2707f41ac2beSBill Paul 			if (error) {
2708fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
27093f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
2710f41ac2beSBill Paul 				return (ENOMEM);
2711f41ac2beSBill Paul 			}
2712f41ac2beSBill Paul 		}
2713f41ac2beSBill Paul 	}
2714f41ac2beSBill Paul 
2715f41ac2beSBill Paul 	return (0);
2716f41ac2beSBill Paul }
2717f41ac2beSBill Paul 
2718bf6ef57aSJohn Polstra /*
2719bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2720bf6ef57aSJohn Polstra  */
2721bf6ef57aSJohn Polstra static int
2722bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2723bf6ef57aSJohn Polstra {
2724bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
272555aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
2726bf6ef57aSJohn Polstra 
272755aaf894SMarius Strobl 	d = pci_get_domain(dev);
2728bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2729bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2730bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2731bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
273255aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2733bf6ef57aSJohn Polstra 			return (1);
2734bf6ef57aSJohn Polstra 	return (0);
2735bf6ef57aSJohn Polstra }
2736bf6ef57aSJohn Polstra 
2737bf6ef57aSJohn Polstra /*
2738bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2739bf6ef57aSJohn Polstra  */
2740bf6ef57aSJohn Polstra static int
2741bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2742bf6ef57aSJohn Polstra {
2743bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2744bf6ef57aSJohn Polstra 
2745d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
27465c952e8dSPyun YongHyeon 		return (0);
27475c952e8dSPyun YongHyeon 
27481108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
27491108273aSPyun YongHyeon #ifdef DEVICE_POLLING
27501108273aSPyun YongHyeon 	return (0);
27511108273aSPyun YongHyeon #endif
2752bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2753a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
2754bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2755bf6ef57aSJohn Polstra 		/*
2756a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
2757a8376f70SMarius Strobl 		 * configured in single-port mode.
2758bf6ef57aSJohn Polstra 		 */
2759bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2760bf6ef57aSJohn Polstra 			can_use_msi = 1;
2761bf6ef57aSJohn Polstra 		break;
2762bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2763bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2764bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2765bf6ef57aSJohn Polstra 			can_use_msi = 1;
2766bf6ef57aSJohn Polstra 		break;
2767a8376f70SMarius Strobl 	default:
2768a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
2769bf6ef57aSJohn Polstra 			can_use_msi = 1;
2770bf6ef57aSJohn Polstra 	}
2771bf6ef57aSJohn Polstra 	return (can_use_msi);
2772bf6ef57aSJohn Polstra }
2773bf6ef57aSJohn Polstra 
277495d67482SBill Paul static int
2775062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
2776062af0b0SPyun YongHyeon {
2777062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
2778062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
2779062af0b0SPyun YongHyeon 		const uint16_t vendor;
2780062af0b0SPyun YongHyeon 		const uint16_t device;
2781062af0b0SPyun YongHyeon 		const char *desc;
2782062af0b0SPyun YongHyeon 	} const mbox_reorder_lists[] = {
2783062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
2784062af0b0SPyun YongHyeon 	};
2785062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
2786062af0b0SPyun YongHyeon 	device_t bus, dev;
278747f4a4dcSMarius Strobl 	int i;
2788062af0b0SPyun YongHyeon 
2789062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
2790062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
2791062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
2792062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
2793062af0b0SPyun YongHyeon 	for (;;) {
2794062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
2795062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
2796062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
2797062af0b0SPyun YongHyeon 			break;
279847f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
2799062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
2800062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
2801062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
2802062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
2803062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
2804062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
2805062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
2806062af0b0SPyun YongHyeon 				return (1);
2807062af0b0SPyun YongHyeon 			}
2808062af0b0SPyun YongHyeon 		}
2809062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
2810062af0b0SPyun YongHyeon 			break;
2811062af0b0SPyun YongHyeon 	}
2812062af0b0SPyun YongHyeon 	return (0);
2813062af0b0SPyun YongHyeon }
2814062af0b0SPyun YongHyeon 
2815ea9c3a30SPyun YongHyeon static void
2816ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
2817ea9c3a30SPyun YongHyeon {
2818ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
2819ea9c3a30SPyun YongHyeon 
2820ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
2821ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
2822ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
2823ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
2824ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
2825ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
2826ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
2827ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
2828ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
2829ea9c3a30SPyun YongHyeon 			clk = 133;
2830ea9c3a30SPyun YongHyeon 		else {
2831ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
2832ea9c3a30SPyun YongHyeon 			switch (clk) {
2833ea9c3a30SPyun YongHyeon 			case 0:
2834ea9c3a30SPyun YongHyeon 				clk = 33;
2835ea9c3a30SPyun YongHyeon 				break;
2836ea9c3a30SPyun YongHyeon 			case 2:
2837ea9c3a30SPyun YongHyeon 				clk = 50;
2838ea9c3a30SPyun YongHyeon 				break;
2839ea9c3a30SPyun YongHyeon 			case 4:
2840ea9c3a30SPyun YongHyeon 				clk = 66;
2841ea9c3a30SPyun YongHyeon 				break;
2842ea9c3a30SPyun YongHyeon 			case 6:
2843ea9c3a30SPyun YongHyeon 				clk = 100;
2844ea9c3a30SPyun YongHyeon 				break;
2845ea9c3a30SPyun YongHyeon 			case 7:
2846ea9c3a30SPyun YongHyeon 				clk = 133;
2847ea9c3a30SPyun YongHyeon 				break;
2848ea9c3a30SPyun YongHyeon 			}
2849ea9c3a30SPyun YongHyeon 		}
2850ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
2851ea9c3a30SPyun YongHyeon 	} else {
2852ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
2853ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
2854ea9c3a30SPyun YongHyeon 		else
2855ea9c3a30SPyun YongHyeon 			printf("PCI ");
2856ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
2857ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
2858ea9c3a30SPyun YongHyeon 			clk = 66;
2859ea9c3a30SPyun YongHyeon 		else
2860ea9c3a30SPyun YongHyeon 			clk = 33;
2861ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
2862ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
2863ea9c3a30SPyun YongHyeon 		else
2864ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
2865ea9c3a30SPyun YongHyeon 	}
2866ea9c3a30SPyun YongHyeon }
2867ea9c3a30SPyun YongHyeon 
2868062af0b0SPyun YongHyeon static int
28693f74909aSGleb Smirnoff bge_attach(device_t dev)
287095d67482SBill Paul {
287195d67482SBill Paul 	struct ifnet *ifp;
287295d67482SBill Paul 	struct bge_softc *sc;
28734f0794ffSBjoern A. Zeeb 	uint32_t hwcfg = 0, misccfg;
287408013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
2875fb772a6cSMarius Strobl 	int capmask, error, f, msicount, phy_addr, reg, rid, trys;
287695d67482SBill Paul 
287795d67482SBill Paul 	sc = device_get_softc(dev);
287895d67482SBill Paul 	sc->bge_dev = dev;
287995d67482SBill Paul 
2880e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2881dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
2882e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
2883dfe0df9aSPyun YongHyeon 
288495d67482SBill Paul 	/*
288595d67482SBill Paul 	 * Map control/status registers.
288695d67482SBill Paul 	 */
288795d67482SBill Paul 	pci_enable_busmaster(dev);
288895d67482SBill Paul 
2889736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
28905f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
289144f8f2fcSMarius Strobl 	    RF_ACTIVE);
289295d67482SBill Paul 
289395d67482SBill Paul 	if (sc->bge_res == NULL) {
2894fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
289595d67482SBill Paul 		error = ENXIO;
289695d67482SBill Paul 		goto fail;
289795d67482SBill Paul 	}
289895d67482SBill Paul 
28994f09c4c7SMarius Strobl 	/* Save various chip information. */
2900e53d81eeSPaul Saab 	sc->bge_chipid =
2901a5779553SStanislav Sedov 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2902a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
29031108273aSPyun YongHyeon 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG) {
29041108273aSPyun YongHyeon 		/*
29051108273aSPyun YongHyeon 		 * Find the ASCI revision.  Different chips use different
29061108273aSPyun YongHyeon 		 * registers.
29071108273aSPyun YongHyeon 		 */
29081108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
29091108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
29101108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2911bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
291250515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
29131108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
29141108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
29151108273aSPyun YongHyeon 			break;
2916b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2917b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2918b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
2919b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
2920b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2921b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2922b4a256acSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
2923b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2924b4a256acSPyun YongHyeon 			break;
29251108273aSPyun YongHyeon 		default:
29261108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
29271108273aSPyun YongHyeon 			    BGE_PCI_PRODID_ASICREV, 4);
29281108273aSPyun YongHyeon 		}
29291108273aSPyun YongHyeon 	}
2930e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2931e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2932e53d81eeSPaul Saab 
2933a813ed78SPyun YongHyeon 	/* Set default PHY address. */
29348e5d93dbSMarius Strobl 	phy_addr = 1;
29351108273aSPyun YongHyeon 	 /*
29361108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
29371108273aSPyun YongHyeon 	  *
29381108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
29391108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
29401108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
29411108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
29421108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
2943bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
294450515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
29451108273aSPyun YongHyeon 	  *
29461108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
29471108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
29481108273aSPyun YongHyeon 	  */
2949bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
295050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
295150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
29521108273aSPyun YongHyeon 		f = pci_get_function(dev);
29531108273aSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5717_A0) {
29541108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
29551108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
29561108273aSPyun YongHyeon 				phy_addr = f + 8;
29571108273aSPyun YongHyeon 			else
29581108273aSPyun YongHyeon 				phy_addr = f + 1;
2959bbe2ca75SPyun YongHyeon 		} else {
29601108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
29611108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
29621108273aSPyun YongHyeon 				phy_addr = f + 8;
29631108273aSPyun YongHyeon 			else
29641108273aSPyun YongHyeon 				phy_addr = f + 1;
29651108273aSPyun YongHyeon 		}
29661108273aSPyun YongHyeon 	}
2967a813ed78SPyun YongHyeon 
296886543395SJung-uk Kim 	/*
296938cc658fSJohn Baldwin 	 * Don't enable Ethernet@WireSpeed for the 5700, 5906, or the
297086543395SJung-uk Kim 	 * 5705 A0 and A1 chips.
297186543395SJung-uk Kim 	 */
2972cb777a07SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
2973cb777a07SPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
2974cb777a07SPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
2975cb777a07SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)) ||
2976cb777a07SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5906)
2977cb777a07SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
297886543395SJung-uk Kim 
29795fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
29805fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
298108013fd3SMarius Strobl 
29820dae9719SJung-uk Kim 	/* Save chipset family. */
29830dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
29841108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
2985bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
298650515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
2987b4a256acSPyun YongHyeon 	case BGE_ASICREV_BCM57765:
29881108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
29891108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
2990b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
2991bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
2992bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
2993bbe2ca75SPyun YongHyeon 			/* Jumbo frame on BCM5719 A0 does not work. */
2994463a7e27SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_JUMBO;
2995bbe2ca75SPyun YongHyeon 		}
29961108273aSPyun YongHyeon 		break;
2997a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
2998a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
2999a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3000a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3001a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3002a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3003a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3004a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3005a5779553SStanislav Sedov 		break;
30060dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
30070dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
30080dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
30090dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
30107ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
30110dae9719SJung-uk Kim 		break;
30120dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
30130dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
30140dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3015f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
30169fe569d8SXin LI 		/* FALLTHROUGH */
30170dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
30180dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
301938cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
30200dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
30219fe569d8SXin LI 		/* FALLTHROUGH */
30220dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
30230dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
30240dae9719SJung-uk Kim 		break;
30250dae9719SJung-uk Kim 	}
30260dae9719SJung-uk Kim 
3027749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3028749a5269SMarius Strobl 	bge_add_sysctls(sc);
3029749a5269SMarius Strobl 
3030757402fbSPyun YongHyeon 	/* Set various PHY bug flags. */
30311ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
30321ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
3033757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
30345ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
30355ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
3036757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
30375ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
3038757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
30394150ce6fSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
3040757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
3041eea8956aSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
3042eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
30431108273aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
3044bbe2ca75SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5719 &&
304550515680SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5720 &&
3046eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3047b4a256acSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57765 &&
3048eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780) {
30495ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
3050a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3051a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
30524fcf220bSJohn Baldwin 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
3053f7d1b2ebSXin LI 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
3054f7d1b2ebSXin LI 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
3055757402fbSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
3056eea8956aSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
3057eea8956aSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
3058eea8956aSPyun YongHyeon 		} else
3059757402fbSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
30605ee49a3aSJung-uk Kim 	}
30615ee49a3aSJung-uk Kim 
3062a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
30631108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
30641108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3065a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3066a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3067a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3068a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3069a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3070a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3071a813ed78SPyun YongHyeon 	else
3072a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
30737ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
30747ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
30757ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3076a813ed78SPyun YongHyeon 
3077f681b29aSPyun YongHyeon 	/*
3078d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3079f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3080f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3081f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3082f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3083f681b29aSPyun YongHyeon 	 */
3084f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
30854f0794ffSBjoern A. Zeeb 
3086d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3087d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3088d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3089d9820cd8SPyun YongHyeon 
3090a7fcfcf3SPyun YongHyeon 	/*
3091a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3092a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3093a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3094a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3095a7fcfcf3SPyun YongHyeon 	 */
3096a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3097a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3098a7fcfcf3SPyun YongHyeon 
3099ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3100fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
31014f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
31024f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
31034f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
310484ac96f8SPyun YongHyeon 	}
31054f0794ffSBjoern A. Zeeb 
3106fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3107fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3108fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3109fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3110fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3111fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3112fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3113fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3114fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3115fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3116fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3117fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3118fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3119fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3120fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3121fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3122fb772a6cSMarius Strobl 	}
3123fb772a6cSMarius Strobl 
3124e53d81eeSPaul Saab 	/*
3125ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3126ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3127ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3128ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3129ca3f1187SPyun YongHyeon 	 * known bug which can't handle TSO if ethernet header + IP/TCP
3130ca3f1187SPyun YongHyeon 	 * header is greater than 80 bytes. The workaround for the TSO
3131ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3132ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3133ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3134ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3135ca3f1187SPyun YongHyeon 	 */
31361108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
31371108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
31381108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3139bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3140bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3141bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3142bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3143bbe2ca75SPyun YongHyeon 		}
31441108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
31454f4a16e1SPyun YongHyeon 		/*
31464f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
31474f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3148be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
31494f4a16e1SPyun YongHyeon 		 */
31504f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3151be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3152be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3153ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
31544f4a16e1SPyun YongHyeon 	}
3155ca3f1187SPyun YongHyeon 
3156ca3f1187SPyun YongHyeon 	/*
31576f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3158e53d81eeSPaul Saab 	 */
31593b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
31604c0da0ffSGleb Smirnoff 		/*
31616f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
31626f8718a3SScott Long 		 * must be a PCI Express device.
31636f8718a3SScott Long 		 */
31646f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
31650aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
316648630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
316748630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
316848630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
316948630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
317050515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
317150515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
317248630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
317348630d79SPyun YongHyeon 		else
317448630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
317548630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
31766f8718a3SScott Long 	} else {
31776f8718a3SScott Long 		/*
31786f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
31796f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
31804c0da0ffSGleb Smirnoff 		 */
31813b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
31820aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
318390447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
31844c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3185652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
31866f8718a3SScott Long 	}
31874c0da0ffSGleb Smirnoff 
3188bf6ef57aSJohn Polstra 	/*
3189fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3190fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3191fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3192fd4d32feSPyun YongHyeon 	 */
3193fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3194fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3195fd4d32feSPyun YongHyeon 	/*
3196062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3197062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3198062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3199062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3200062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3201062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3202062af0b0SPyun YongHyeon 	 */
3203062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3204062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3205062af0b0SPyun YongHyeon 	/*
3206bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3207bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3208bf6ef57aSJohn Polstra 	 * normal operation.
3209bf6ef57aSJohn Polstra 	 */
32100aaf1057SPyun YongHyeon 	rid = 0;
32113b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
32120aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3213bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
3214bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
3215bf6ef57aSJohn Polstra 			if (msicount > 1)
3216bf6ef57aSJohn Polstra 				msicount = 1;
3217bf6ef57aSJohn Polstra 		} else
3218bf6ef57aSJohn Polstra 			msicount = 0;
3219bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
3220bf6ef57aSJohn Polstra 			rid = 1;
3221bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
32220aaf1057SPyun YongHyeon 		}
32230aaf1057SPyun YongHyeon 	}
3224bf6ef57aSJohn Polstra 
32251108273aSPyun YongHyeon 	/*
32261108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
32271108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
32281108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
32291108273aSPyun YongHyeon 	 */
32301108273aSPyun YongHyeon #ifndef DEVICE_POLLING
32311108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
32321108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
32331108273aSPyun YongHyeon #endif
32341108273aSPyun YongHyeon 
3235bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3236bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
3237bf6ef57aSJohn Polstra 
3238bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3239bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3240bf6ef57aSJohn Polstra 		error = ENXIO;
3241bf6ef57aSJohn Polstra 		goto fail;
3242bf6ef57aSJohn Polstra 	}
3243bf6ef57aSJohn Polstra 
3244ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
32454f09c4c7SMarius Strobl 
324695d67482SBill Paul 	/* Try to reset the chip. */
32478cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
32488cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
32498cb1383cSDoug Ambrisko 		error = ENXIO;
32508cb1383cSDoug Ambrisko 		goto fail;
32518cb1383cSDoug Ambrisko 	}
32528cb1383cSDoug Ambrisko 
32538cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3254888b47f0SPyun YongHyeon 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3255888b47f0SPyun YongHyeon 	    BGE_SRAM_DATA_SIG_MAGIC)) {
3256888b47f0SPyun YongHyeon 		if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG)
32578cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
32588cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
32598cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
3260d67eba2fSPyun YongHyeon 			if (BGE_IS_575X_PLUS(sc))
32618cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
32628cb1383cSDoug Ambrisko 		}
32638cb1383cSDoug Ambrisko 	}
32648cb1383cSDoug Ambrisko 
32658cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
32668cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
32678cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
32688cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
32698cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
32708cb1383cSDoug Ambrisko 		error = ENXIO;
32718cb1383cSDoug Ambrisko 		goto fail;
32728cb1383cSDoug Ambrisko 	}
32738cb1383cSDoug Ambrisko 
32748cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
32758cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
327695d67482SBill Paul 
327795d67482SBill Paul 	if (bge_chipinit(sc)) {
3278fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
327995d67482SBill Paul 		error = ENXIO;
328095d67482SBill Paul 		goto fail;
328195d67482SBill Paul 	}
328295d67482SBill Paul 
328338cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
328438cc658fSJohn Baldwin 	if (error) {
328508013fd3SMarius Strobl 		device_printf(sc->bge_dev,
328608013fd3SMarius Strobl 		    "failed to read station address\n");
328795d67482SBill Paul 		error = ENXIO;
328895d67482SBill Paul 		goto fail;
328995d67482SBill Paul 	}
329095d67482SBill Paul 
3291f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
32921108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
32931108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
32941108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3295f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3296f41ac2beSBill Paul 	else
3297f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3298f41ac2beSBill Paul 
32995b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3300fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3301fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3302f41ac2beSBill Paul 		error = ENXIO;
3303f41ac2beSBill Paul 		goto fail;
3304f41ac2beSBill Paul 	}
3305f41ac2beSBill Paul 
330695d67482SBill Paul 	/* Set default tuneable values. */
330795d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
330895d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
330995d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
33106f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
33116f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
331295d67482SBill Paul 
331335f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
331435f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
331535f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
331635f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
331735f945cdSPyun YongHyeon 
331895d67482SBill Paul 	/* Set up ifnet structure */
3319fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3320fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3321fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3322fc74a9f9SBrooks Davis 		error = ENXIO;
3323fc74a9f9SBrooks Davis 		goto fail;
3324fc74a9f9SBrooks Davis 	}
332595d67482SBill Paul 	ifp->if_softc = sc;
33269bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
332795d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
332895d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
332995d67482SBill Paul 	ifp->if_start = bge_start;
333095d67482SBill Paul 	ifp->if_init = bge_init;
33314d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
33324d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
33334d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
333435f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
3335d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
33364e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
33371108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3338ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
333904bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
3340ca3f1187SPyun YongHyeon 	}
33414e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
33424e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
33434e35d186SJung-uk Kim #endif
334495d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
334575719184SGleb Smirnoff #ifdef DEVICE_POLLING
334675719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
334775719184SGleb Smirnoff #endif
334895d67482SBill Paul 
3349a1d52896SBill Paul 	/*
3350d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3351d375e524SGleb Smirnoff 	 * to hardware bugs.
3352d375e524SGleb Smirnoff 	 */
3353d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3354d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
33554d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
3356d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
3357d375e524SGleb Smirnoff 	}
3358d375e524SGleb Smirnoff 
3359d375e524SGleb Smirnoff 	/*
3360a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
336141abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
336241abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
336341abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
336441abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
336541abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
336641abcc1bSPaul Saab 	 * SK-9D41.
3367a1d52896SBill Paul 	 */
3368888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3369888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
33705fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
33715fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3372f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3373f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3374fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3375f6789fbaSPyun YongHyeon 			error = ENXIO;
3376f6789fbaSPyun YongHyeon 			goto fail;
3377f6789fbaSPyun YongHyeon 		}
337841abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
337941abcc1bSPaul Saab 	}
338041abcc1bSPaul Saab 
338195d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3382ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3383ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
3384ea3b4127SPyun YongHyeon 		if (BGE_IS_5714_FAMILY(sc))
3385ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
3386ea3b4127SPyun YongHyeon 		else
3387652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3388ea3b4127SPyun YongHyeon 	}
338995d67482SBill Paul 
3390652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
33910c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
33920c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
33930c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
33946098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
33956098821cSJung-uk Kim 		    0, NULL);
339695d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
339795d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3398da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
339995d67482SBill Paul 	} else {
340095d67482SBill Paul 		/*
34018cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
34028cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
34038cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
34048cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
34058cb1383cSDoug Ambrisko 		 * the PHY.
340695d67482SBill Paul 		 */
34074012d104SMarius Strobl 		trys = 0;
34088cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
34098cb1383cSDoug Ambrisko again:
34108cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
34118cb1383cSDoug Ambrisko 
3412fb772a6cSMarius Strobl 		error = mii_attach(dev, &sc->bge_miibus, ifp, bge_ifmedia_upd,
3413fb772a6cSMarius Strobl 		    bge_ifmedia_sts, capmask, phy_addr, MII_OFFSET_ANY,
3414fb772a6cSMarius Strobl 		    MIIF_DOPAUSE);
34158e5d93dbSMarius Strobl 		if (error != 0) {
34168cb1383cSDoug Ambrisko 			if (trys++ < 4) {
34178cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
34184e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
34194e35d186SJung-uk Kim 				    BMCR_RESET);
34208cb1383cSDoug Ambrisko 				goto again;
34218cb1383cSDoug Ambrisko 			}
34228e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
342395d67482SBill Paul 			goto fail;
342495d67482SBill Paul 		}
34258cb1383cSDoug Ambrisko 
34268cb1383cSDoug Ambrisko 		/*
34278cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
34288cb1383cSDoug Ambrisko 		 */
34298cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
34308cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
343195d67482SBill Paul 	}
343295d67482SBill Paul 
343395d67482SBill Paul 	/*
3434e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3435e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3436e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3437e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3438e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3439e255b776SJohn Polstra 	 * payloads by copying the received packets.
3440e255b776SJohn Polstra 	 */
3441652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3442652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3443652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3444e255b776SJohn Polstra 
3445e255b776SJohn Polstra 	/*
344695d67482SBill Paul 	 * Call MI attach routine.
344795d67482SBill Paul 	 */
3448fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
34490f9bd73bSSam Leffler 
345061ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
345161ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
345261ccb9daSPyun YongHyeon 
34530f9bd73bSSam Leffler 	/*
34540f9bd73bSSam Leffler 	 * Hookup IRQ last.
34550f9bd73bSSam Leffler 	 */
3456dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3457dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
34587e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
34597e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3460dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3461dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3462dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3463dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3464dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3465e010b055SPyun YongHyeon 			error = ENOMEM;
3466dfe0df9aSPyun YongHyeon 			goto fail;
3467dfe0df9aSPyun YongHyeon 		}
3468dfe0df9aSPyun YongHyeon 		taskqueue_start_threads(&sc->bge_tq, 1, PI_NET, "%s taskq",
3469dfe0df9aSPyun YongHyeon 		    device_get_nameunit(sc->bge_dev));
3470dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3471dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3472dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3473dfe0df9aSPyun YongHyeon 	} else
3474dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3475dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3476dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
34770f9bd73bSSam Leffler 
34780f9bd73bSSam Leffler 	if (error) {
3479e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3480fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
34810f9bd73bSSam Leffler 	}
348295d67482SBill Paul 
348395d67482SBill Paul fail:
3484e010b055SPyun YongHyeon 	if (error)
3485e010b055SPyun YongHyeon 		bge_detach(dev);
348695d67482SBill Paul 	return (error);
348795d67482SBill Paul }
348895d67482SBill Paul 
348995d67482SBill Paul static int
34903f74909aSGleb Smirnoff bge_detach(device_t dev)
349195d67482SBill Paul {
349295d67482SBill Paul 	struct bge_softc *sc;
349395d67482SBill Paul 	struct ifnet *ifp;
349495d67482SBill Paul 
349595d67482SBill Paul 	sc = device_get_softc(dev);
3496fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
349795d67482SBill Paul 
349875719184SGleb Smirnoff #ifdef DEVICE_POLLING
349975719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
350075719184SGleb Smirnoff 		ether_poll_deregister(ifp);
350175719184SGleb Smirnoff #endif
350275719184SGleb Smirnoff 
3503e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3504e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
35050f9bd73bSSam Leffler 		BGE_LOCK(sc);
350695d67482SBill Paul 		bge_stop(sc);
35070f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
35085dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3509e010b055SPyun YongHyeon 	}
35105dda8085SOleg Bulyzhin 
3511dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3512dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
351395d67482SBill Paul 
3514652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
351595d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
351695d67482SBill Paul 	} else {
351795d67482SBill Paul 		bus_generic_detach(dev);
351895d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
351995d67482SBill Paul 	}
352095d67482SBill Paul 
352195d67482SBill Paul 	bge_release_resources(sc);
352295d67482SBill Paul 
352395d67482SBill Paul 	return (0);
352495d67482SBill Paul }
352595d67482SBill Paul 
352695d67482SBill Paul static void
35273f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
352895d67482SBill Paul {
352995d67482SBill Paul 	device_t dev;
353095d67482SBill Paul 
353195d67482SBill Paul 	dev = sc->bge_dev;
353295d67482SBill Paul 
3533dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3534dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3535dfe0df9aSPyun YongHyeon 
353695d67482SBill Paul 	if (sc->bge_intrhand != NULL)
353795d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
353895d67482SBill Paul 
353995d67482SBill Paul 	if (sc->bge_irq != NULL)
3540724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3541724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3542724bd939SJohn Polstra 
3543724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3544724bd939SJohn Polstra 		pci_release_msi(dev);
354595d67482SBill Paul 
354695d67482SBill Paul 	if (sc->bge_res != NULL)
354795d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3548736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
354995d67482SBill Paul 
3550ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3551ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3552ad61f896SRuslan Ermilov 
3553f41ac2beSBill Paul 	bge_dma_free(sc);
355495d67482SBill Paul 
35550f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
35560f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
355795d67482SBill Paul }
355895d67482SBill Paul 
35598cb1383cSDoug Ambrisko static int
35603f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
356195d67482SBill Paul {
356295d67482SBill Paul 	device_t dev;
35635fea260fSMarius Strobl 	uint32_t cachesize, command, pcistate, reset, val;
35646f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
35650aaf1057SPyun YongHyeon 	uint16_t devctl;
35665fea260fSMarius Strobl 	int i;
356795d67482SBill Paul 
356895d67482SBill Paul 	dev = sc->bge_dev;
356995d67482SBill Paul 
357038cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
357138cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
35726f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
35736f8718a3SScott Long 			write_op = bge_writemem_direct;
35746f8718a3SScott Long 		else
35756f8718a3SScott Long 			write_op = bge_writemem_ind;
35769ba784dbSScott Long 	} else
35776f8718a3SScott Long 		write_op = bge_writereg_ind;
35786f8718a3SScott Long 
357995d67482SBill Paul 	/* Save some important PCI state. */
358095d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
358195d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
358295d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
358395d67482SBill Paul 
358495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
358595d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3586e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
358795d67482SBill Paul 
35886f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
35896f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
3590a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
35916f8718a3SScott Long 		if (bootverbose)
3592333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
35936f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
35946f8718a3SScott Long 	}
35956f8718a3SScott Long 
35966f8718a3SScott Long 	/*
35976f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
35986f8718a3SScott Long 	 * When firmware finishes its initialization it will
3599888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
36006f8718a3SScott Long 	 */
3601888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
36026f8718a3SScott Long 
36030c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
3604e53d81eeSPaul Saab 
3605e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3606652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
36070c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
36080c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
3609e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
3610e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
36110c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
36120c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
3613e53d81eeSPaul Saab 		}
3614e53d81eeSPaul Saab 	}
3615e53d81eeSPaul Saab 
361621c9e407SDavid Christensen 	/*
36176f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
36186f8718a3SScott Long 	 * powered up in D0 uninitialized.
36196f8718a3SScott Long 	 */
36205512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
36215512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
3622caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
36236f8718a3SScott Long 
362495d67482SBill Paul 	/* Issue global reset */
36256f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
362695d67482SBill Paul 
362738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
36285fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
362938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
36305fea260fSMarius Strobl 		    val | BGE_VCPU_STATUS_DRV_RESET);
36315fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
363238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
36335fea260fSMarius Strobl 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
363438cc658fSJohn Baldwin 	}
363538cc658fSJohn Baldwin 
363695d67482SBill Paul 	DELAY(1000);
363795d67482SBill Paul 
3638e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3639652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
3640e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
3641e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
36425fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
36435fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
3644e53d81eeSPaul Saab 		}
36450aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
3646389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
36470aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
3648389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
3649389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
3650389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
36510aaf1057SPyun YongHyeon 		    devctl, 2);
365248630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
36530aaf1057SPyun YongHyeon 		/* Clear error status. */
3654389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
3655389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
3656389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
3657389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
3658e53d81eeSPaul Saab 	}
3659e53d81eeSPaul Saab 
36603f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
366195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
366295d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3663e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
366495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
366595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
36660c8aa4eaSJung-uk Kim 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
3667cbb2b2feSPyun YongHyeon 	/*
3668cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
3669fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
3670cbb2b2feSPyun YongHyeon 	 * read stale status block.
3671cbb2b2feSPyun YongHyeon 	 */
3672cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
3673cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
3674cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
3675cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
3676cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
3677cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
3678cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3679cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3680cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
3681cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
3682cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3683cbb2b2feSPyun YongHyeon 		}
3684cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
3685cbb2b2feSPyun YongHyeon 		    devctl, 2);
3686cbb2b2feSPyun YongHyeon 	}
368722a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
36884c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
3689bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
3690bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
36910aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
36920aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
36930aaf1057SPyun YongHyeon 			pci_write_config(dev,
36940aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
3695bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
3696bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
3697bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
3698bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
3699bf6ef57aSJohn Polstra 		}
37004c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
37014c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
37024c0da0ffSGleb Smirnoff 	} else
3703a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3704a7b0c314SPaul Saab 
370538cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
370638cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
370738cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
370838cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
370938cc658fSJohn Baldwin 				break;
371038cc658fSJohn Baldwin 			DELAY(100);
371138cc658fSJohn Baldwin 		}
371238cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
3713333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
371438cc658fSJohn Baldwin 			return (1);
371538cc658fSJohn Baldwin 		}
371638cc658fSJohn Baldwin 	} else {
371795d67482SBill Paul 		/*
37186f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
371908013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
37205fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
37215fea260fSMarius Strobl 		 * address is fitted though.
372295d67482SBill Paul 		 */
372395d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
3724d5d23857SJung-uk Kim 			DELAY(10);
3725888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
3726888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
372795d67482SBill Paul 				break;
372895d67482SBill Paul 		}
372995d67482SBill Paul 
37305fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
3731333704a3SPyun YongHyeon 			device_printf(dev,
3732333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
3733333704a3SPyun YongHyeon 			    val);
3734b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
3735b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
3736b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
373738cc658fSJohn Baldwin 	}
373895d67482SBill Paul 
373995d67482SBill Paul 	/*
374095d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
374195d67482SBill Paul 	 * return to its original pre-reset state. This is a
374295d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
374395d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
374495d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
374595d67482SBill Paul 	 * results.
374695d67482SBill Paul 	 */
374795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
374895d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
374995d67482SBill Paul 			break;
375095d67482SBill Paul 		DELAY(10);
375195d67482SBill Paul 	}
375295d67482SBill Paul 
37533f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
375450515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
375595d67482SBill Paul 
37568cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
37578cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
37588cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
37598cb1383cSDoug Ambrisko 
376095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
37619b80ffe7SPyun YongHyeon 	DELAY(40);
376295d67482SBill Paul 
3763da3003f0SBill Paul 	/*
3764da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
3765da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
3766da3003f0SBill Paul 	 * to 1.2V.
3767da3003f0SBill Paul 	 */
3768652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
3769652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
37705fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
37715fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
37725fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
3773da3003f0SBill Paul 	}
3774da3003f0SBill Paul 
3775e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3776652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
3777b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
3778a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
3779a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
3780a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
37815fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
37825fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
3783e53d81eeSPaul Saab 	}
378495d67482SBill Paul 	DELAY(10000);
37858cb1383cSDoug Ambrisko 
378650515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
378750515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
378850515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
378950515680SPyun YongHyeon 
37908cb1383cSDoug Ambrisko 	return (0);
379195d67482SBill Paul }
379295d67482SBill Paul 
3793e0b7b101SPyun YongHyeon static __inline void
3794e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
3795e0b7b101SPyun YongHyeon {
3796e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
3797e0b7b101SPyun YongHyeon 
3798e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
3799e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
3800e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
3801e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3802e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3803e0b7b101SPyun YongHyeon }
3804e0b7b101SPyun YongHyeon 
3805e0b7b101SPyun YongHyeon static __inline void
3806e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
3807e0b7b101SPyun YongHyeon {
3808e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
3809e0b7b101SPyun YongHyeon 
3810e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
3811e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
3812e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
3813e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
3814e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
3815e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
3816e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3817e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3818e0b7b101SPyun YongHyeon }
3819e0b7b101SPyun YongHyeon 
382095d67482SBill Paul /*
382195d67482SBill Paul  * Frame reception handling. This is called if there's a frame
382295d67482SBill Paul  * on the receive return list.
382395d67482SBill Paul  *
382495d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
38251be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
382695d67482SBill Paul  * 2) the frame is from the standard receive ring
382795d67482SBill Paul  */
382895d67482SBill Paul 
38291abcdbd1SAttilio Rao static int
3830dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
383195d67482SBill Paul {
383295d67482SBill Paul 	struct ifnet *ifp;
38331abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
3834b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
383595d67482SBill Paul 
38367f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
38370f9bd73bSSam Leffler 
38383f74909aSGleb Smirnoff 	/* Nothing to do. */
38397f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
38401abcdbd1SAttilio Rao 		return (rx_npkts);
3841cfcb5025SOleg Bulyzhin 
3842fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
384395d67482SBill Paul 
3844f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
3845e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
3846f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
384715eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
3848f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
3849f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
3850c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
3851f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
385215eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
3853f41ac2beSBill Paul 
38547f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
385595d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
38563f74909aSGleb Smirnoff 		uint32_t		rxidx;
385795d67482SBill Paul 		struct mbuf		*m = NULL;
38583f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
385995d67482SBill Paul 		int			have_tag = 0;
386095d67482SBill Paul 
386175719184SGleb Smirnoff #ifdef DEVICE_POLLING
386275719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
386375719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
386475719184SGleb Smirnoff 				break;
386575719184SGleb Smirnoff 			sc->rxcycles--;
386675719184SGleb Smirnoff 		}
386775719184SGleb Smirnoff #endif
386875719184SGleb Smirnoff 
38697f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
387095d67482SBill Paul 
387195d67482SBill Paul 		rxidx = cur_rx->bge_idx;
38727f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
387395d67482SBill Paul 
3874cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
3875cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
387695d67482SBill Paul 			have_tag = 1;
387795d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
387895d67482SBill Paul 		}
387995d67482SBill Paul 
388095d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
388195d67482SBill Paul 			jumbocnt++;
3882943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
388395d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3884e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
388595d67482SBill Paul 				continue;
388695d67482SBill Paul 			}
3887943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
3888e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
3889943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
389095d67482SBill Paul 				continue;
389195d67482SBill Paul 			}
389203e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
389395d67482SBill Paul 		} else {
389495d67482SBill Paul 			stdcnt++;
3895e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
389695d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3897e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
389895d67482SBill Paul 				continue;
389995d67482SBill Paul 			}
3900943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
3901e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
3902943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
390395d67482SBill Paul 				continue;
390495d67482SBill Paul 			}
390503e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
390695d67482SBill Paul 		}
390795d67482SBill Paul 
390895d67482SBill Paul 		ifp->if_ipackets++;
3909e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3910e255b776SJohn Polstra 		/*
3911e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
3912e65bed95SPyun YongHyeon 		 * the payload is aligned.
3913e255b776SJohn Polstra 		 */
3914652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
3915e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
3916e255b776SJohn Polstra 			    cur_rx->bge_len);
3917e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
3918e255b776SJohn Polstra 		}
3919e255b776SJohn Polstra #endif
3920473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
392195d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
392295d67482SBill Paul 
39231108273aSPyun YongHyeon 		if (ifp->if_capenable & IFCAP_RXCSUM)
39241108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
392595d67482SBill Paul 
392695d67482SBill Paul 		/*
3927673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
3928673d9191SSam Leffler 		 * attach that information to the packet.
392995d67482SBill Paul 		 */
3930d147662cSGleb Smirnoff 		if (have_tag) {
393178ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
393278ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
3933d147662cSGleb Smirnoff 		}
393495d67482SBill Paul 
3935dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
39360f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
3937673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
39380f9bd73bSSam Leffler 			BGE_LOCK(sc);
3939dfe0df9aSPyun YongHyeon 		} else
3940dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
3941d4da719cSAttilio Rao 		rx_npkts++;
394225e13e68SXin LI 
394325e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
39448cf7d13dSAttilio Rao 			return (rx_npkts);
394595d67482SBill Paul 	}
394695d67482SBill Paul 
394715eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
394815eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
3949e65bed95SPyun YongHyeon 	if (stdcnt > 0)
3950f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3951e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
39524c0da0ffSGleb Smirnoff 
3953c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
3954f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
39554c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3956f41ac2beSBill Paul 
39577f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
395838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
395995d67482SBill Paul 	if (stdcnt)
3960767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
3961767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
396295d67482SBill Paul 	if (jumbocnt)
3963767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
3964767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
3965f5a034f9SPyun YongHyeon #ifdef notyet
3966f5a034f9SPyun YongHyeon 	/*
3967f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
3968f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
3969f5a034f9SPyun YongHyeon 	 */
3970f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
3971f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3972f5a034f9SPyun YongHyeon #endif
39731abcdbd1SAttilio Rao 	return (rx_npkts);
397495d67482SBill Paul }
397595d67482SBill Paul 
397695d67482SBill Paul static void
39771108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
39781108273aSPyun YongHyeon {
39791108273aSPyun YongHyeon 
39801108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
39811108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
39821108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
39831108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
39841108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
39851108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
39861108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
39871108273aSPyun YongHyeon 			}
39881108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
39891108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
39901108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
39911108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
39921108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
39931108273aSPyun YongHyeon 			}
39941108273aSPyun YongHyeon 		}
39951108273aSPyun YongHyeon 	} else {
39961108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
39971108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
39981108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
39991108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
40001108273aSPyun YongHyeon 		}
40011108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
40021108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
40031108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
40041108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
40051108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
40061108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
40071108273aSPyun YongHyeon 		}
40081108273aSPyun YongHyeon 	}
40091108273aSPyun YongHyeon }
40101108273aSPyun YongHyeon 
40111108273aSPyun YongHyeon static void
4012b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
401395d67482SBill Paul {
401495a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
401595d67482SBill Paul 	struct ifnet *ifp;
401695d67482SBill Paul 
40170f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
40180f9bd73bSSam Leffler 
40193f74909aSGleb Smirnoff 	/* Nothing to do. */
4020b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4021cfcb5025SOleg Bulyzhin 		return;
4022cfcb5025SOleg Bulyzhin 
4023fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
402495d67482SBill Paul 
4025e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
40265c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
402795d67482SBill Paul 	/*
402895d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
402995d67482SBill Paul 	 * frames that have been sent.
403095d67482SBill Paul 	 */
4031b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
403295a0a340SPyun YongHyeon 		uint32_t		idx;
403395d67482SBill Paul 
403495d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4035f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
403695d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
403795d67482SBill Paul 			ifp->if_opackets++;
403895d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
40390ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4040e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4041e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
40420ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4043f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4044e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4045e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
404695d67482SBill Paul 		}
404795d67482SBill Paul 		sc->bge_txcnt--;
404895d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
404995d67482SBill Paul 	}
405095d67482SBill Paul 
405113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
40525b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
40535b01e77cSBruce Evans 		sc->bge_timer = 0;
405495d67482SBill Paul }
405595d67482SBill Paul 
405675719184SGleb Smirnoff #ifdef DEVICE_POLLING
40571abcdbd1SAttilio Rao static int
405875719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
405975719184SGleb Smirnoff {
406075719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
4061b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4062366454f2SOleg Bulyzhin 	uint32_t statusword;
40631abcdbd1SAttilio Rao 	int rx_npkts = 0;
406475719184SGleb Smirnoff 
40653f74909aSGleb Smirnoff 	BGE_LOCK(sc);
40663f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
40673f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
40681abcdbd1SAttilio Rao 		return (rx_npkts);
40693f74909aSGleb Smirnoff 	}
407075719184SGleb Smirnoff 
4071dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4072b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4073b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4074*2246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4075b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4076b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4077dab5cd05SOleg Bulyzhin 
4078175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
4079*2246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4080175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4081dab5cd05SOleg Bulyzhin 
4082dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4083b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4084b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4085366454f2SOleg Bulyzhin 
40860c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4087366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4088366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4089366454f2SOleg Bulyzhin 
4090366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4091366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
40924c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4093652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4094366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4095366454f2SOleg Bulyzhin 
4096366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4097dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
409825e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
409925e13e68SXin LI 		BGE_UNLOCK(sc);
41008cf7d13dSAttilio Rao 		return (rx_npkts);
410125e13e68SXin LI 	}
4102b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4103366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4104366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
41053f74909aSGleb Smirnoff 
41063f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
41071abcdbd1SAttilio Rao 	return (rx_npkts);
410875719184SGleb Smirnoff }
410975719184SGleb Smirnoff #endif /* DEVICE_POLLING */
411075719184SGleb Smirnoff 
4111dfe0df9aSPyun YongHyeon static int
4112dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4113dfe0df9aSPyun YongHyeon {
4114dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4115dfe0df9aSPyun YongHyeon 
4116dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4117dfe0df9aSPyun YongHyeon 	/*
4118dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4119dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4120dfe0df9aSPyun YongHyeon 	 */
4121dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4122dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4123dfe0df9aSPyun YongHyeon }
4124dfe0df9aSPyun YongHyeon 
4125dfe0df9aSPyun YongHyeon static void
4126dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4127dfe0df9aSPyun YongHyeon {
4128dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4129dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
41301108273aSPyun YongHyeon 	uint32_t status, status_tag;
4131dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4132dfe0df9aSPyun YongHyeon 
4133dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4134dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4135dfe0df9aSPyun YongHyeon 
413666151edfSPyun YongHyeon 	BGE_LOCK(sc);
413766151edfSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
413866151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4139dfe0df9aSPyun YongHyeon 		return;
414066151edfSPyun YongHyeon 	}
4141dfe0df9aSPyun YongHyeon 
4142dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4143dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4144dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4145dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4146dfe0df9aSPyun YongHyeon 
4147*2246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4148dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4149dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4150dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
41511108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
4152*2246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4153dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4154dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4155dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4156dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
41571108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
41581108273aSPyun YongHyeon 		status_tag = 0;
415966151edfSPyun YongHyeon 
416066151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
416166151edfSPyun YongHyeon 		bge_link_upd(sc);
416266151edfSPyun YongHyeon 
4163dfe0df9aSPyun YongHyeon 	/* Let controller work. */
41641108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4165dfe0df9aSPyun YongHyeon 
416666151edfSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
416766151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4168dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
416966151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4170dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
417166151edfSPyun YongHyeon 		BGE_LOCK(sc);
4172dfe0df9aSPyun YongHyeon 	}
4173dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4174dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4175dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4176dfe0df9aSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4177dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4178dfe0df9aSPyun YongHyeon 	}
417966151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4180dfe0df9aSPyun YongHyeon }
4181dfe0df9aSPyun YongHyeon 
418295d67482SBill Paul static void
41833f74909aSGleb Smirnoff bge_intr(void *xsc)
418495d67482SBill Paul {
418595d67482SBill Paul 	struct bge_softc *sc;
418695d67482SBill Paul 	struct ifnet *ifp;
4187dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4188b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
418995d67482SBill Paul 
419095d67482SBill Paul 	sc = xsc;
4191f41ac2beSBill Paul 
41920f9bd73bSSam Leffler 	BGE_LOCK(sc);
41930f9bd73bSSam Leffler 
4194dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4195dab5cd05SOleg Bulyzhin 
419675719184SGleb Smirnoff #ifdef DEVICE_POLLING
419775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
419875719184SGleb Smirnoff 		BGE_UNLOCK(sc);
419975719184SGleb Smirnoff 		return;
420075719184SGleb Smirnoff 	}
420175719184SGleb Smirnoff #endif
420275719184SGleb Smirnoff 
4203f30cbfc6SScott Long 	/*
4204b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4205b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4206b848e032SBruce Evans 	 * our current organization this just gives complications and
4207b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4208b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4209b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4210b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4211b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4212b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4213b848e032SBruce Evans 	 *
4214b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4215b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4216b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4217b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4218b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4219b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4220b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4221b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4222b848e032SBruce Evans 	 */
422338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4224b848e032SBruce Evans 
4225f584dfd1SPyun YongHyeon 	/*
4226f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4227f584dfd1SPyun YongHyeon 	 */
4228f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4229f584dfd1SPyun YongHyeon 
4230f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4231f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4232f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4233f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4234f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4235f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4236f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4237f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4238f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4239f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4240f584dfd1SPyun YongHyeon 
42411f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
42424c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4243f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4244dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
424595d67482SBill Paul 
424613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
42473f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4248dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
424925e13e68SXin LI 	}
425095d67482SBill Paul 
425125e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
42523f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4253b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
425495d67482SBill Paul 	}
425595d67482SBill Paul 
425613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
425713f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
42580f9bd73bSSam Leffler 		bge_start_locked(ifp);
42590f9bd73bSSam Leffler 
42600f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
426195d67482SBill Paul }
426295d67482SBill Paul 
426395d67482SBill Paul static void
42648cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
42658cb1383cSDoug Ambrisko {
42668cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
42678cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
42688cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
42698cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
42708cb1383cSDoug Ambrisko 		else {
4271899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4272888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
42733c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4274888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4275941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4276941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
42773fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
42789931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
42799931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
42808cb1383cSDoug Ambrisko 		}
42818cb1383cSDoug Ambrisko 	}
42828cb1383cSDoug Ambrisko }
42838cb1383cSDoug Ambrisko 
42848cb1383cSDoug Ambrisko static void
4285b74e67fbSGleb Smirnoff bge_tick(void *xsc)
42860f9bd73bSSam Leffler {
4287b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
428895d67482SBill Paul 	struct mii_data *mii = NULL;
428995d67482SBill Paul 
42900f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
429195d67482SBill Paul 
42925dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
42935dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
42945dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
42955dda8085SOleg Bulyzhin 		return;
42965dda8085SOleg Bulyzhin 
42977ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
42980434d1b8SBill Paul 		bge_stats_update_regs(sc);
42990434d1b8SBill Paul 	else
430095d67482SBill Paul 		bge_stats_update(sc);
430195d67482SBill Paul 
4302652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
430395d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
430482b67c01SOleg Bulyzhin 		/*
430582b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
430682b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
430782b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
430882b67c01SOleg Bulyzhin 		 */
430982b67c01SOleg Bulyzhin 		if (!sc->bge_link)
431095d67482SBill Paul 			mii_tick(mii);
43117b97099dSOleg Bulyzhin 	} else {
43127b97099dSOleg Bulyzhin 		/*
43137b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
43147b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
43157b97099dSOleg Bulyzhin 		 * and trigger interrupt.
43167b97099dSOleg Bulyzhin 		 */
43177b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
43183f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
43197b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
43207b97099dSOleg Bulyzhin #endif
43217b97099dSOleg Bulyzhin 		{
43227b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
43234f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
43244f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
43257b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
43264f0794ffSBjoern A. Zeeb 		else
43274f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
43287b97099dSOleg Bulyzhin 		}
4329dab5cd05SOleg Bulyzhin 	}
433095d67482SBill Paul 
43318cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4332b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
43338cb1383cSDoug Ambrisko 
4334dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
433595d67482SBill Paul }
433695d67482SBill Paul 
433795d67482SBill Paul static void
43383f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
43390434d1b8SBill Paul {
43403f74909aSGleb Smirnoff 	struct ifnet *ifp;
43412280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
43420434d1b8SBill Paul 
4343fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
43442280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
43450434d1b8SBill Paul 
43462280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
43472280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
43482280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
43492280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
43502280c16bSPyun YongHyeon 	stats->outXonSent +=
43512280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
43522280c16bSPyun YongHyeon 	stats->outXoffSent +=
43532280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
43542280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
43552280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
43562280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
43572280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
43582280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
43592280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
43602280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
43612280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
43622280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
43632280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
43642280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
43652280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
43662280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
43672280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
43682280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
43692280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
43702280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
43712280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
43727e6e2507SJung-uk Kim 
43732280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
43742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
43752280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
43762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
43772280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
43782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
43792280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
43802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
43812280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
43822280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
43832280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
43842280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
43852280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
43862280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
43872280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
43882280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
43892280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
43902280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
43912280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
43922280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
43932280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
43942280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
43952280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
43962280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
43972280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
43982280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
43992280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
44002280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
44012280c16bSPyun YongHyeon 
44022280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
44032280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
44042280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
44052280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
44062280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
44072280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
44082280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
44092280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4410f78094a5SPyun YongHyeon 	/*
4411f78094a5SPyun YongHyeon 	 * XXX
4412f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4413f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4414f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4415f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4416f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4417f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4418f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4419f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4420f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4421f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4422f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4423f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4424f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4425f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4426f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4427f78094a5SPyun YongHyeon 	 * silicon bug.
4428f78094a5SPyun YongHyeon 	 */
4429f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4430f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4431f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
44322280c16bSPyun YongHyeon 		stats->InputDiscards +=
44332280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
44342280c16bSPyun YongHyeon 	stats->InputErrors +=
44352280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
44362280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
44372280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
44382280c16bSPyun YongHyeon 
44392280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
44402280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
44412280c16bSPyun YongHyeon 	    stats->InputErrors);
44422280c16bSPyun YongHyeon }
44432280c16bSPyun YongHyeon 
44442280c16bSPyun YongHyeon static void
44452280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
44462280c16bSPyun YongHyeon {
44472280c16bSPyun YongHyeon 
44482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
44492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
44502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
44512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
44522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
44532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
44542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
44552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
44562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
44572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
44582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
44592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
44602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
44612280c16bSPyun YongHyeon 
44622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
44632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
44642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
44652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
44662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
44672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
44682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
44692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
44702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
44712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
44722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
44732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
44742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
44752280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
44762280c16bSPyun YongHyeon 
44772280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
44782280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
44792280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
44802280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
44812280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
44822280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
44832280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
44840434d1b8SBill Paul }
44850434d1b8SBill Paul 
44860434d1b8SBill Paul static void
44873f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
448895d67482SBill Paul {
448995d67482SBill Paul 	struct ifnet *ifp;
4490e907febfSPyun YongHyeon 	bus_size_t stats;
44917e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
449295d67482SBill Paul 
4493fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
449495d67482SBill Paul 
4495e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4496e907febfSPyun YongHyeon 
4497e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4498e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
449995d67482SBill Paul 
45008634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
45016b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
45026fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
45036fb34dd2SOleg Bulyzhin 
450437ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
450537ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_nobds);
450637ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
450737ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
450837ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_inerrs);
450937ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
45106fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
45116b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
45126fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
45136fb34dd2SOleg Bulyzhin 
45146fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
45156b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
45166fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
451795d67482SBill Paul 
4518e907febfSPyun YongHyeon #undef	READ_STAT
451995d67482SBill Paul }
452095d67482SBill Paul 
452195d67482SBill Paul /*
4522d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
4523d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
4524d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
4525d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
4526d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
4527d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
4528d375e524SGleb Smirnoff  */
4529d375e524SGleb Smirnoff static __inline int
4530d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
4531d375e524SGleb Smirnoff {
4532d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
4533d375e524SGleb Smirnoff 	struct mbuf *last;
4534d375e524SGleb Smirnoff 
4535d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
4536d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
4537d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
4538d375e524SGleb Smirnoff 		last = m;
4539d375e524SGleb Smirnoff 	} else {
4540d375e524SGleb Smirnoff 		/*
4541d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
4542d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
4543d375e524SGleb Smirnoff 		 */
4544d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
4545d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
4546d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
4547d375e524SGleb Smirnoff 			struct mbuf *n;
4548d375e524SGleb Smirnoff 
4549d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
4550d375e524SGleb Smirnoff 			if (n == NULL)
4551d375e524SGleb Smirnoff 				return (ENOBUFS);
4552d375e524SGleb Smirnoff 			n->m_len = 0;
4553d375e524SGleb Smirnoff 			last->m_next = n;
4554d375e524SGleb Smirnoff 			last = n;
4555d375e524SGleb Smirnoff 		}
4556d375e524SGleb Smirnoff 	}
4557d375e524SGleb Smirnoff 
4558d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
4559d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
4560d375e524SGleb Smirnoff 	last->m_len += padlen;
4561d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
4562d375e524SGleb Smirnoff 
4563d375e524SGleb Smirnoff 	return (0);
4564d375e524SGleb Smirnoff }
4565d375e524SGleb Smirnoff 
4566ca3f1187SPyun YongHyeon static struct mbuf *
4567d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
4568d598b626SPyun YongHyeon {
4569d598b626SPyun YongHyeon 	struct mbuf *n;
4570d598b626SPyun YongHyeon 	int found;
4571d598b626SPyun YongHyeon 
4572d598b626SPyun YongHyeon 	/*
4573d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
4574d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
4575d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
4576d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
4577d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
4578d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
4579d598b626SPyun YongHyeon 	 */
4580d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
4581d598b626SPyun YongHyeon 		if (n->m_len < 8) {
4582d598b626SPyun YongHyeon 			found++;
4583d598b626SPyun YongHyeon 			if (found > 1)
4584d598b626SPyun YongHyeon 				break;
4585d598b626SPyun YongHyeon 			continue;
4586d598b626SPyun YongHyeon 		}
4587d598b626SPyun YongHyeon 		found = 0;
4588d598b626SPyun YongHyeon 	}
4589d598b626SPyun YongHyeon 
4590d598b626SPyun YongHyeon 	if (found > 1) {
4591d598b626SPyun YongHyeon 		n = m_defrag(m, M_DONTWAIT);
4592d598b626SPyun YongHyeon 		if (n == NULL)
4593d598b626SPyun YongHyeon 			m_freem(m);
4594d598b626SPyun YongHyeon 	} else
4595d598b626SPyun YongHyeon 		n = m;
4596d598b626SPyun YongHyeon 	return (n);
4597d598b626SPyun YongHyeon }
4598d598b626SPyun YongHyeon 
4599d598b626SPyun YongHyeon static struct mbuf *
46001108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
46011108273aSPyun YongHyeon     uint16_t *flags)
4602ca3f1187SPyun YongHyeon {
4603ca3f1187SPyun YongHyeon 	struct ip *ip;
4604ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
4605ca3f1187SPyun YongHyeon 	struct mbuf *n;
4606ca3f1187SPyun YongHyeon 	uint16_t hlen;
46075b355c4fSPyun YongHyeon 	uint32_t poff;
4608ca3f1187SPyun YongHyeon 
4609ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
4610ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
4611ca3f1187SPyun YongHyeon 		n = m_dup(m, M_DONTWAIT);
4612ca3f1187SPyun YongHyeon 		m_freem(m);
4613ca3f1187SPyun YongHyeon 		if (n == NULL)
4614ca3f1187SPyun YongHyeon 			return (NULL);
4615ca3f1187SPyun YongHyeon 		m = n;
4616ca3f1187SPyun YongHyeon 	}
46175b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
4618ca3f1187SPyun YongHyeon 	if (m == NULL)
4619ca3f1187SPyun YongHyeon 		return (NULL);
46205b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
46215b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
4622ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
4623ca3f1187SPyun YongHyeon 	if (m == NULL)
4624ca3f1187SPyun YongHyeon 		return (NULL);
4625ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
46265b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
4627ca3f1187SPyun YongHyeon 	if (m == NULL)
4628ca3f1187SPyun YongHyeon 		return (NULL);
4629ca3f1187SPyun YongHyeon 	/*
4630ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
4631ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
4632ca3f1187SPyun YongHyeon 	 */
4633ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
463496486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
4635ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
4636ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
4637ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
463896486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
4639ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
4640ca3f1187SPyun YongHyeon 	/*
4641ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
4642ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
4643ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
46441108273aSPyun YongHyeon 	 * we only support hardware based TSO.
4645ca3f1187SPyun YongHyeon 	 */
46461108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
4647ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
46481108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
46491108273aSPyun YongHyeon 		/*
46501108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
46511108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
46521108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
46531108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
46541108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
46551108273aSPyun YongHyeon 		 * frames are supported.
46561108273aSPyun YongHyeon 		 */
46571108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
46581108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
46591108273aSPyun YongHyeon 	} else {
46601108273aSPyun YongHyeon 		/*
46611108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
46621108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
46631108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
46641108273aSPyun YongHyeon 		 * supported.
46651108273aSPyun YongHyeon 		 */
4666ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
46671108273aSPyun YongHyeon 	}
4668ca3f1187SPyun YongHyeon 	return (m);
4669ca3f1187SPyun YongHyeon }
4670ca3f1187SPyun YongHyeon 
4671d375e524SGleb Smirnoff /*
467295d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
467395d67482SBill Paul  * pointers to descriptors.
467495d67482SBill Paul  */
467595d67482SBill Paul static int
4676676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
467795d67482SBill Paul {
46787e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
4679f41ac2beSBill Paul 	bus_dmamap_t		map;
4680676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
4681676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
46827e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
4683ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
46847e27542aSGleb Smirnoff 	int			nsegs, i, error;
468595d67482SBill Paul 
46866909dc43SGleb Smirnoff 	csum_flags = 0;
4687ca3f1187SPyun YongHyeon 	mss = 0;
4688ca3f1187SPyun YongHyeon 	vlan_tag = 0;
4689d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
4690d598b626SPyun YongHyeon 	    m->m_next != NULL) {
4691d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
4692d598b626SPyun YongHyeon 		if (*m_head == NULL)
4693d598b626SPyun YongHyeon 			return (ENOBUFS);
4694d598b626SPyun YongHyeon 		m = *m_head;
4695d598b626SPyun YongHyeon 	}
4696ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
46971108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
4698ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
4699ca3f1187SPyun YongHyeon 			return (ENOBUFS);
4700ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
4701ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
470235f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
47036909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
47046909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
47056909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
47066909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
47076909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
47086909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
47096909dc43SGleb Smirnoff 				m_freem(m);
47106909dc43SGleb Smirnoff 				*m_head = NULL;
47116909dc43SGleb Smirnoff 				return (error);
47126909dc43SGleb Smirnoff 			}
47136909dc43SGleb Smirnoff 		}
47146909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
47156909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
47166909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
47176909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
47186909dc43SGleb Smirnoff 	}
47196909dc43SGleb Smirnoff 
47201108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
47211108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
47221108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
47231108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
47241108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
4725beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
4726d94f2b85SPyun YongHyeon 			/*
4727d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
4728d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
4729d94f2b85SPyun YongHyeon 			 * DMA read operation.
4730d94f2b85SPyun YongHyeon 			 */
4731beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
4732d94f2b85SPyun YongHyeon 				m = m_defrag(m, M_DONTWAIT);
4733d94f2b85SPyun YongHyeon 			else
47341108273aSPyun YongHyeon 				m = m_collapse(m, M_DONTWAIT,
47351108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
4736261f04d6SPyun YongHyeon 			if (m == NULL)
4737261f04d6SPyun YongHyeon 				m = *m_head;
4738d94f2b85SPyun YongHyeon 			*m_head = m;
4739d94f2b85SPyun YongHyeon 		}
47401108273aSPyun YongHyeon 	}
4741d94f2b85SPyun YongHyeon 
47427e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
47430ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
4744676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
47457e27542aSGleb Smirnoff 	if (error == EFBIG) {
47464eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
4747676ad2c9SGleb Smirnoff 		if (m == NULL) {
4748676ad2c9SGleb Smirnoff 			m_freem(*m_head);
4749676ad2c9SGleb Smirnoff 			*m_head = NULL;
47507e27542aSGleb Smirnoff 			return (ENOBUFS);
47517e27542aSGleb Smirnoff 		}
4752676ad2c9SGleb Smirnoff 		*m_head = m;
47530ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
47540ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
4755676ad2c9SGleb Smirnoff 		if (error) {
4756676ad2c9SGleb Smirnoff 			m_freem(m);
4757676ad2c9SGleb Smirnoff 			*m_head = NULL;
47587e27542aSGleb Smirnoff 			return (error);
47597e27542aSGleb Smirnoff 		}
4760676ad2c9SGleb Smirnoff 	} else if (error != 0)
4761676ad2c9SGleb Smirnoff 		return (error);
47627e27542aSGleb Smirnoff 
4763167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
4764167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
47650ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
476695d67482SBill Paul 		return (ENOBUFS);
47677e27542aSGleb Smirnoff 	}
47687e27542aSGleb Smirnoff 
47690ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
4770e65bed95SPyun YongHyeon 
4771ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
4772ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
4773ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
4774ca3f1187SPyun YongHyeon 	}
47757e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
47767e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
47777e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
47787e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
47797e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
47807e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
4781ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
4782ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
47837e27542aSGleb Smirnoff 		if (i == nsegs - 1)
47847e27542aSGleb Smirnoff 			break;
47857e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
47867e27542aSGleb Smirnoff 	}
47877e27542aSGleb Smirnoff 
47887e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
47897e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
4790676ad2c9SGleb Smirnoff 
4791f41ac2beSBill Paul 	/*
4792f41ac2beSBill Paul 	 * Insure that the map for this transmission
4793f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
4794f41ac2beSBill Paul 	 * in this chain.
4795f41ac2beSBill Paul 	 */
47967e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
47977e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
4798676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
47997e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
480095d67482SBill Paul 
48017e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
48027e27542aSGleb Smirnoff 	*txidx = idx;
480395d67482SBill Paul 
480495d67482SBill Paul 	return (0);
480595d67482SBill Paul }
480695d67482SBill Paul 
480795d67482SBill Paul /*
480895d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
480995d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
481095d67482SBill Paul  */
481195d67482SBill Paul static void
48123f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
481395d67482SBill Paul {
481495d67482SBill Paul 	struct bge_softc *sc;
4815167fdb62SPyun YongHyeon 	struct mbuf *m_head;
481614bbd30fSGleb Smirnoff 	uint32_t prodidx;
4817167fdb62SPyun YongHyeon 	int count;
481895d67482SBill Paul 
481995d67482SBill Paul 	sc = ifp->if_softc;
4820167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
482195d67482SBill Paul 
4822167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
4823167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4824167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
482595d67482SBill Paul 		return;
482695d67482SBill Paul 
482714bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
482895d67482SBill Paul 
4829167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
4830167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
4831167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4832167fdb62SPyun YongHyeon 			break;
4833167fdb62SPyun YongHyeon 		}
48344d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
483595d67482SBill Paul 		if (m_head == NULL)
483695d67482SBill Paul 			break;
483795d67482SBill Paul 
483895d67482SBill Paul 		/*
483995d67482SBill Paul 		 * XXX
4840b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
4841b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
4842b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
4843b874fdd4SYaroslav Tykhiy 		 *
4844b874fdd4SYaroslav Tykhiy 		 * XXX
484595d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
484695d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
484795d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
484895d67482SBill Paul 		 * chain at once.
484995d67482SBill Paul 		 * (paranoia -- may not actually be needed)
485095d67482SBill Paul 		 */
485195d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
485295d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
485395d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
485495d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
48554d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
485613f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
485795d67482SBill Paul 				break;
485895d67482SBill Paul 			}
485995d67482SBill Paul 		}
486095d67482SBill Paul 
486195d67482SBill Paul 		/*
486295d67482SBill Paul 		 * Pack the data into the transmit ring. If we
486395d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
486495d67482SBill Paul 		 * for the NIC to drain the ring.
486595d67482SBill Paul 		 */
4866676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
4867676ad2c9SGleb Smirnoff 			if (m_head == NULL)
4868676ad2c9SGleb Smirnoff 				break;
48694d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
487013f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
487195d67482SBill Paul 			break;
487295d67482SBill Paul 		}
4873303a718cSDag-Erling Smørgrav 		++count;
487495d67482SBill Paul 
487595d67482SBill Paul 		/*
487695d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
487795d67482SBill Paul 		 * to him.
487895d67482SBill Paul 		 */
48794e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
488045ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
48814e35d186SJung-uk Kim #else
48824e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
48834e35d186SJung-uk Kim #endif
488495d67482SBill Paul 	}
488595d67482SBill Paul 
4886167fdb62SPyun YongHyeon 	if (count > 0) {
4887aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
48885c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
48893f74909aSGleb Smirnoff 		/* Transmit. */
489038cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
48913927098fSPaul Saab 		/* 5700 b2 errata */
4892e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
489338cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
489495d67482SBill Paul 
489514bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
489614bbd30fSGleb Smirnoff 
489795d67482SBill Paul 		/*
489895d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
489995d67482SBill Paul 		 */
4900b74e67fbSGleb Smirnoff 		sc->bge_timer = 5;
490195d67482SBill Paul 	}
4902167fdb62SPyun YongHyeon }
490395d67482SBill Paul 
49040f9bd73bSSam Leffler /*
49050f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
49060f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
49070f9bd73bSSam Leffler  */
490895d67482SBill Paul static void
49093f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
491095d67482SBill Paul {
49110f9bd73bSSam Leffler 	struct bge_softc *sc;
49120f9bd73bSSam Leffler 
49130f9bd73bSSam Leffler 	sc = ifp->if_softc;
49140f9bd73bSSam Leffler 	BGE_LOCK(sc);
49150f9bd73bSSam Leffler 	bge_start_locked(ifp);
49160f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
49170f9bd73bSSam Leffler }
49180f9bd73bSSam Leffler 
49190f9bd73bSSam Leffler static void
49203f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
49210f9bd73bSSam Leffler {
492295d67482SBill Paul 	struct ifnet *ifp;
49233f74909aSGleb Smirnoff 	uint16_t *m;
4924f6a65488SPyun YongHyeon 	uint32_t mode;
492595d67482SBill Paul 
49260f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
492795d67482SBill Paul 
4928fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
492995d67482SBill Paul 
493013f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
493195d67482SBill Paul 		return;
493295d67482SBill Paul 
493395d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
493495d67482SBill Paul 	bge_stop(sc);
49358cb1383cSDoug Ambrisko 
49368cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
49378cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
493895d67482SBill Paul 	bge_reset(sc);
49398cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
49408cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
49418cb1383cSDoug Ambrisko 
494295d67482SBill Paul 	bge_chipinit(sc);
494395d67482SBill Paul 
494495d67482SBill Paul 	/*
494595d67482SBill Paul 	 * Init the various state machines, ring
494695d67482SBill Paul 	 * control blocks and firmware.
494795d67482SBill Paul 	 */
494895d67482SBill Paul 	if (bge_blockinit(sc)) {
4949fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
495095d67482SBill Paul 		return;
495195d67482SBill Paul 	}
495295d67482SBill Paul 
4953fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
495495d67482SBill Paul 
495595d67482SBill Paul 	/* Specify MTU. */
495695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
4957cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
4958cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
495995d67482SBill Paul 
496095d67482SBill Paul 	/* Load our MAC address. */
49613f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
496295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
496395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
496495d67482SBill Paul 
49653e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
49663e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
496795d67482SBill Paul 
496895d67482SBill Paul 	/* Program multicast filter. */
496995d67482SBill Paul 	bge_setmulti(sc);
497095d67482SBill Paul 
4971cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
4972cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
4973cb2eacc7SYaroslav Tykhiy 
497435f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
497535f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
497635f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
497735f945cdSPyun YongHyeon 	else
497835f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
497935f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
498035f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
498135f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
498235f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
498335f945cdSPyun YongHyeon 	}
498435f945cdSPyun YongHyeon 
498595d67482SBill Paul 	/* Init RX ring. */
49863ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
49873ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
49883ee5d7daSPyun YongHyeon 		bge_stop(sc);
49893ee5d7daSPyun YongHyeon 		return;
49903ee5d7daSPyun YongHyeon 	}
499195d67482SBill Paul 
49920434d1b8SBill Paul 	/*
49930434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
49940434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
49950434d1b8SBill Paul 	 * entry of the ring.
49960434d1b8SBill Paul 	 */
49970434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
49983f74909aSGleb Smirnoff 		uint32_t		v, i;
49990434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
50000434d1b8SBill Paul 			DELAY(20);
50010434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
50020434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
50030434d1b8SBill Paul 				break;
50040434d1b8SBill Paul 		}
50050434d1b8SBill Paul 		if (i == 10)
5006fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5007fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
50080434d1b8SBill Paul 	}
50090434d1b8SBill Paul 
501095d67482SBill Paul 	/* Init jumbo RX ring. */
5011f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5012f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
5013c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
50143ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5015333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5016b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
50173ee5d7daSPyun YongHyeon 			bge_stop(sc);
50183ee5d7daSPyun YongHyeon 			return;
50193ee5d7daSPyun YongHyeon 		}
50203ee5d7daSPyun YongHyeon 	}
502195d67482SBill Paul 
50223f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
502395d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
502495d67482SBill Paul 
50257e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
50267e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
50277e6e2507SJung-uk Kim 
502895d67482SBill Paul 	/* Init TX ring. */
502995d67482SBill Paul 	bge_init_tx_ring(sc);
503095d67482SBill Paul 
5031f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5032f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5033f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5034f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
503550515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
503650515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
503750515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
503850515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
503950515680SPyun YongHyeon 	}
50403f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5041f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5042a6e66cd2SPyun YongHyeon 	DELAY(100);
504395d67482SBill Paul 
50443f74909aSGleb Smirnoff 	/* Turn on receiver. */
504595d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
5046a6e66cd2SPyun YongHyeon 	DELAY(10);
504795d67482SBill Paul 
5048dedcdf57SPyun YongHyeon 	/*
5049dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5050dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5051dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5052dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5053dedcdf57SPyun YongHyeon 	 */
5054b4a256acSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765)
5055b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5056b4a256acSPyun YongHyeon 	else
5057dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5058dedcdf57SPyun YongHyeon 
50592280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
50602280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
50612280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
50622280c16bSPyun YongHyeon 
506395d67482SBill Paul 	/* Tell firmware we're alive. */
506495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
506595d67482SBill Paul 
506675719184SGleb Smirnoff #ifdef DEVICE_POLLING
506775719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
506875719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
506975719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
507075719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
507138cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
507275719184SGleb Smirnoff 	} else
507375719184SGleb Smirnoff #endif
507475719184SGleb Smirnoff 
507595d67482SBill Paul 	/* Enable host interrupts. */
507675719184SGleb Smirnoff 	{
507795d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
507895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
507938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
508075719184SGleb Smirnoff 	}
508195d67482SBill Paul 
508213f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
508313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
508495d67482SBill Paul 
5085e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5086e4146b95SPyun YongHyeon 
50870f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
50880f9bd73bSSam Leffler }
50890f9bd73bSSam Leffler 
50900f9bd73bSSam Leffler static void
50913f74909aSGleb Smirnoff bge_init(void *xsc)
50920f9bd73bSSam Leffler {
50930f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
50940f9bd73bSSam Leffler 
50950f9bd73bSSam Leffler 	BGE_LOCK(sc);
50960f9bd73bSSam Leffler 	bge_init_locked(sc);
50970f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
509895d67482SBill Paul }
509995d67482SBill Paul 
510095d67482SBill Paul /*
510195d67482SBill Paul  * Set media options.
510295d67482SBill Paul  */
510395d67482SBill Paul static int
51043f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
510595d67482SBill Paul {
510667d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
510767d5e043SOleg Bulyzhin 	int res;
510867d5e043SOleg Bulyzhin 
510967d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
511067d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
511167d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
511267d5e043SOleg Bulyzhin 
511367d5e043SOleg Bulyzhin 	return (res);
511467d5e043SOleg Bulyzhin }
511567d5e043SOleg Bulyzhin 
511667d5e043SOleg Bulyzhin static int
511767d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
511867d5e043SOleg Bulyzhin {
511967d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
512095d67482SBill Paul 	struct mii_data *mii;
51214f09c4c7SMarius Strobl 	struct mii_softc *miisc;
512295d67482SBill Paul 	struct ifmedia *ifm;
512395d67482SBill Paul 
512467d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
512567d5e043SOleg Bulyzhin 
512695d67482SBill Paul 	ifm = &sc->bge_ifmedia;
512795d67482SBill Paul 
512895d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5129652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
513095d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
513195d67482SBill Paul 			return (EINVAL);
513295d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
513395d67482SBill Paul 		case IFM_AUTO:
5134ff50922bSDoug White 			/*
5135ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5136ff50922bSDoug White 			 * mechanism for programming the autoneg
5137ff50922bSDoug White 			 * advertisement registers in TBI mode.
5138ff50922bSDoug White 			 */
51390f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5140ff50922bSDoug White 				uint32_t sgdig;
51410f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
51420f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5143ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5144ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5145ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5146ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5147ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5148ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5149ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5150ff50922bSDoug White 					DELAY(5);
5151ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5152ff50922bSDoug White 				}
51530f89fde2SJung-uk Kim 			}
515495d67482SBill Paul 			break;
515595d67482SBill Paul 		case IFM_1000_SX:
515695d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
515795d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
515895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
515995d67482SBill Paul 			} else {
516095d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
516195d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
516295d67482SBill Paul 			}
51639b80ffe7SPyun YongHyeon 			DELAY(40);
516495d67482SBill Paul 			break;
516595d67482SBill Paul 		default:
516695d67482SBill Paul 			return (EINVAL);
516795d67482SBill Paul 		}
516895d67482SBill Paul 		return (0);
516995d67482SBill Paul 	}
517095d67482SBill Paul 
51711493e883SOleg Bulyzhin 	sc->bge_link_evt++;
517295d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
51734f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
51743fcb7a53SMarius Strobl 		PHY_RESET(miisc);
517595d67482SBill Paul 	mii_mediachg(mii);
517695d67482SBill Paul 
5177902827f6SBjoern A. Zeeb 	/*
5178902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5179902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5180902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5181902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5182902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5183902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5184902827f6SBjoern A. Zeeb 	 * get an RX intr.
5185902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5186902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5187902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5188902827f6SBjoern A. Zeeb 	 */
51894f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
51904f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5191902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
51924f0794ffSBjoern A. Zeeb 	else
519363ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5194902827f6SBjoern A. Zeeb 
519595d67482SBill Paul 	return (0);
519695d67482SBill Paul }
519795d67482SBill Paul 
519895d67482SBill Paul /*
519995d67482SBill Paul  * Report current media status.
520095d67482SBill Paul  */
520195d67482SBill Paul static void
52023f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
520395d67482SBill Paul {
520467d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
520595d67482SBill Paul 	struct mii_data *mii;
520695d67482SBill Paul 
520767d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
520895d67482SBill Paul 
5209652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
521095d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
521195d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
521295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
521395d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
521495d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
52154c0da0ffSGleb Smirnoff 		else {
52164c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
521767d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
52184c0da0ffSGleb Smirnoff 			return;
52194c0da0ffSGleb Smirnoff 		}
522095d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
522195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
522295d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
522395d67482SBill Paul 		else
522495d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
522567d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
522695d67482SBill Paul 		return;
522795d67482SBill Paul 	}
522895d67482SBill Paul 
522995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
523095d67482SBill Paul 	mii_pollstat(mii);
523195d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
523295d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
523367d5e043SOleg Bulyzhin 
523467d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
523595d67482SBill Paul }
523695d67482SBill Paul 
523795d67482SBill Paul static int
52383f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
523995d67482SBill Paul {
524095d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
524195d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
524295d67482SBill Paul 	struct mii_data *mii;
5243f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
524495d67482SBill Paul 
524595d67482SBill Paul 	switch (command) {
524695d67482SBill Paul 	case SIOCSIFMTU:
5247f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5248f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
52494c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5250f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
525195d67482SBill Paul 				error = EINVAL;
5252f5459d4cSPyun YongHyeon 				break;
5253f5459d4cSPyun YongHyeon 			}
5254f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5255f5459d4cSPyun YongHyeon 			error = EINVAL;
5256f5459d4cSPyun YongHyeon 			break;
5257f5459d4cSPyun YongHyeon 		}
5258f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5259f5459d4cSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
526095d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
52613a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
526213f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
52633a429c8fSPyun YongHyeon 				bge_init_locked(sc);
526495d67482SBill Paul 			}
52653a429c8fSPyun YongHyeon 		}
52663a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
526795d67482SBill Paul 		break;
526895d67482SBill Paul 	case SIOCSIFFLAGS:
52690f9bd73bSSam Leffler 		BGE_LOCK(sc);
527095d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
527195d67482SBill Paul 			/*
527295d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
527395d67482SBill Paul 			 * then just use the 'set promisc mode' command
527495d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
527595d67482SBill Paul 			 * a full re-init means reloading the firmware and
527695d67482SBill Paul 			 * waiting for it to start up, which may take a
5277d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
527895d67482SBill Paul 			 */
5279f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5280f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
52813e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
52823e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5283f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5284d183af7fSRuslan Ermilov 					bge_setmulti(sc);
528595d67482SBill Paul 			} else
52860f9bd73bSSam Leffler 				bge_init_locked(sc);
528795d67482SBill Paul 		} else {
528813f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
528995d67482SBill Paul 				bge_stop(sc);
529095d67482SBill Paul 			}
529195d67482SBill Paul 		}
529295d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
52930f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
529495d67482SBill Paul 		error = 0;
529595d67482SBill Paul 		break;
529695d67482SBill Paul 	case SIOCADDMULTI:
529795d67482SBill Paul 	case SIOCDELMULTI:
529813f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
52990f9bd73bSSam Leffler 			BGE_LOCK(sc);
530095d67482SBill Paul 			bge_setmulti(sc);
53010f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
530295d67482SBill Paul 			error = 0;
530395d67482SBill Paul 		}
530495d67482SBill Paul 		break;
530595d67482SBill Paul 	case SIOCSIFMEDIA:
530695d67482SBill Paul 	case SIOCGIFMEDIA:
5307652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
530895d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
530995d67482SBill Paul 			    &sc->bge_ifmedia, command);
531095d67482SBill Paul 		} else {
531195d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
531295d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
531395d67482SBill Paul 			    &mii->mii_media, command);
531495d67482SBill Paul 		}
531595d67482SBill Paul 		break;
531695d67482SBill Paul 	case SIOCSIFCAP:
531795d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
531875719184SGleb Smirnoff #ifdef DEVICE_POLLING
531975719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
532075719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
532175719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
532275719184SGleb Smirnoff 				if (error)
532375719184SGleb Smirnoff 					return (error);
532475719184SGleb Smirnoff 				BGE_LOCK(sc);
532575719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
532675719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
532738cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
532875719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
532975719184SGleb Smirnoff 				BGE_UNLOCK(sc);
533075719184SGleb Smirnoff 			} else {
533175719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
533275719184SGleb Smirnoff 				/* Enable interrupt even in error case */
533375719184SGleb Smirnoff 				BGE_LOCK(sc);
533475719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
533575719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
533638cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
533775719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
533875719184SGleb Smirnoff 				BGE_UNLOCK(sc);
533975719184SGleb Smirnoff 			}
534075719184SGleb Smirnoff 		}
534175719184SGleb Smirnoff #endif
5342d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5343d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
5344d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
5345d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
534635f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
534795d67482SBill Paul 			else
534835f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
534995d67482SBill Paul 		}
5350cb2eacc7SYaroslav Tykhiy 
5351d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5352d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
5353d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
5354d8b57f98SPyun YongHyeon 
5355ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5356ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
5357ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
5358ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
5359ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
5360ca3f1187SPyun YongHyeon 			else
5361ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
5362ca3f1187SPyun YongHyeon 		}
5363ca3f1187SPyun YongHyeon 
5364cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5365cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
5366cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5367cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5368cb2eacc7SYaroslav Tykhiy 		}
5369cb2eacc7SYaroslav Tykhiy 
537004bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
537104bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
537204bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
537304bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
537404bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
5375cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
537604bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
537704bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
5378cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5379cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5380cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
538104bde852SPyun YongHyeon 		}
5382cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5383cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
5384cb2eacc7SYaroslav Tykhiy #endif
538595d67482SBill Paul 		break;
538695d67482SBill Paul 	default:
5387673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
538895d67482SBill Paul 		break;
538995d67482SBill Paul 	}
539095d67482SBill Paul 
539195d67482SBill Paul 	return (error);
539295d67482SBill Paul }
539395d67482SBill Paul 
539495d67482SBill Paul static void
5395b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
539695d67482SBill Paul {
5397b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
539895d67482SBill Paul 
5399b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5400b74e67fbSGleb Smirnoff 
5401b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5402b74e67fbSGleb Smirnoff 		return;
5403b74e67fbSGleb Smirnoff 
5404b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
540595d67482SBill Paul 
5406fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
540795d67482SBill Paul 
540813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5409426742bfSGleb Smirnoff 	bge_init_locked(sc);
541095d67482SBill Paul 
541195d67482SBill Paul 	ifp->if_oerrors++;
541295d67482SBill Paul }
541395d67482SBill Paul 
54145a147ba6SPyun YongHyeon static void
54155a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
54165a147ba6SPyun YongHyeon {
54175a147ba6SPyun YongHyeon 	int i;
54185a147ba6SPyun YongHyeon 
54195a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
54205a147ba6SPyun YongHyeon 
54215a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
54225a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
54235a147ba6SPyun YongHyeon 			return;
54245a147ba6SPyun YongHyeon 		DELAY(100);
54255a147ba6SPyun YongHyeon         }
54265a147ba6SPyun YongHyeon }
54275a147ba6SPyun YongHyeon 
542895d67482SBill Paul /*
542995d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
543095d67482SBill Paul  * RX and TX lists.
543195d67482SBill Paul  */
543295d67482SBill Paul static void
54333f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
543495d67482SBill Paul {
543595d67482SBill Paul 	struct ifnet *ifp;
543695d67482SBill Paul 
54370f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
54380f9bd73bSSam Leffler 
5439fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
544095d67482SBill Paul 
54410f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
544295d67482SBill Paul 
544344b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
544444b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
544544b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
544644b63691SBjoern A. Zeeb 
544744b63691SBjoern A. Zeeb 	/*
544844b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
544944b63691SBjoern A. Zeeb 	 */
545044b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
545144b63691SBjoern A. Zeeb 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
545244b63691SBjoern A. Zeeb 
545395d67482SBill Paul 	/*
54543f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
545595d67482SBill Paul 	 */
54565a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
54575a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
54585a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
54595a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
54605a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
54615a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
54625a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
54635a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
546495d67482SBill Paul 
546595d67482SBill Paul 	/*
54663f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
546795d67482SBill Paul 	 */
54685a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
54695a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
54705a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
54715a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
54725a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
54735a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
54745a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
54755a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
547695d67482SBill Paul 
547795d67482SBill Paul 	/*
547895d67482SBill Paul 	 * Shut down all of the memory managers and related
547995d67482SBill Paul 	 * state machines.
548095d67482SBill Paul 	 */
54815a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
54825a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
54835a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
54845a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
54855a147ba6SPyun YongHyeon 
54860c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
548795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
54887ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
548995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
549095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
54910434d1b8SBill Paul 	}
54922280c16bSPyun YongHyeon 	/* Update MAC statistics. */
54932280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
54942280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
549595d67482SBill Paul 
54968cb1383cSDoug Ambrisko 	bge_reset(sc);
54978cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
54988cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
54998cb1383cSDoug Ambrisko 
55008cb1383cSDoug Ambrisko 	/*
55018cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
55028cb1383cSDoug Ambrisko 	 */
55038cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
55048cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
55058cb1383cSDoug Ambrisko 	else
550695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
550795d67482SBill Paul 
550895d67482SBill Paul 	/* Free the RX lists. */
550995d67482SBill Paul 	bge_free_rx_ring_std(sc);
551095d67482SBill Paul 
551195d67482SBill Paul 	/* Free jumbo RX list. */
55124c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
551395d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
551495d67482SBill Paul 
551595d67482SBill Paul 	/* Free TX buffers. */
551695d67482SBill Paul 	bge_free_tx_ring(sc);
551795d67482SBill Paul 
551895d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
551995d67482SBill Paul 
55205dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
55211493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
55221493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
55231493e883SOleg Bulyzhin 	sc->bge_link = 0;
552495d67482SBill Paul 
55251493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
552695d67482SBill Paul }
552795d67482SBill Paul 
552895d67482SBill Paul /*
552995d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
553095d67482SBill Paul  * get confused by errant DMAs when rebooting.
553195d67482SBill Paul  */
5532b6c974e8SWarner Losh static int
55333f74909aSGleb Smirnoff bge_shutdown(device_t dev)
553495d67482SBill Paul {
553595d67482SBill Paul 	struct bge_softc *sc;
553695d67482SBill Paul 
553795d67482SBill Paul 	sc = device_get_softc(dev);
55380f9bd73bSSam Leffler 	BGE_LOCK(sc);
553995d67482SBill Paul 	bge_stop(sc);
554095d67482SBill Paul 	bge_reset(sc);
55410f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
5542b6c974e8SWarner Losh 
5543b6c974e8SWarner Losh 	return (0);
554495d67482SBill Paul }
554514afefa3SPawel Jakub Dawidek 
554614afefa3SPawel Jakub Dawidek static int
554714afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
554814afefa3SPawel Jakub Dawidek {
554914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
555014afefa3SPawel Jakub Dawidek 
555114afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
555214afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
555314afefa3SPawel Jakub Dawidek 	bge_stop(sc);
555414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
555514afefa3SPawel Jakub Dawidek 
555614afefa3SPawel Jakub Dawidek 	return (0);
555714afefa3SPawel Jakub Dawidek }
555814afefa3SPawel Jakub Dawidek 
555914afefa3SPawel Jakub Dawidek static int
556014afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
556114afefa3SPawel Jakub Dawidek {
556214afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
556314afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
556414afefa3SPawel Jakub Dawidek 
556514afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
556614afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
556714afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
556814afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
556914afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
557014afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
557114afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
557214afefa3SPawel Jakub Dawidek 	}
557314afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
557414afefa3SPawel Jakub Dawidek 
557514afefa3SPawel Jakub Dawidek 	return (0);
557614afefa3SPawel Jakub Dawidek }
5577dab5cd05SOleg Bulyzhin 
5578dab5cd05SOleg Bulyzhin static void
55793f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
5580dab5cd05SOleg Bulyzhin {
55811f313773SOleg Bulyzhin 	struct mii_data *mii;
55821f313773SOleg Bulyzhin 	uint32_t link, status;
5583dab5cd05SOleg Bulyzhin 
5584dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
55851f313773SOleg Bulyzhin 
55863f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
55877b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
55887b97099dSOleg Bulyzhin 
5589dab5cd05SOleg Bulyzhin 	/*
5590dab5cd05SOleg Bulyzhin 	 * Process link state changes.
5591dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
5592dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
5593dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
5594dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
5595dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
5596dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
5597dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
5598dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
55991f313773SOleg Bulyzhin 	 *
56001f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
56014c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
5602dab5cd05SOleg Bulyzhin 	 */
5603dab5cd05SOleg Bulyzhin 
56041f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
56054c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
5606dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
5607dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
56081f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
56095dda8085SOleg Bulyzhin 			mii_pollstat(mii);
56101f313773SOleg Bulyzhin 			if (!sc->bge_link &&
56111f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
56121f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
56131f313773SOleg Bulyzhin 				sc->bge_link++;
56141f313773SOleg Bulyzhin 				if (bootverbose)
56151f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
56161f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
56171f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
56181f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
56191f313773SOleg Bulyzhin 				sc->bge_link = 0;
56201f313773SOleg Bulyzhin 				if (bootverbose)
56211f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
56221f313773SOleg Bulyzhin 			}
56231f313773SOleg Bulyzhin 
56243f74909aSGleb Smirnoff 			/* Clear the interrupt. */
5625dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
5626dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
5627dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
5628dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
5629dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
5630dab5cd05SOleg Bulyzhin 		}
5631dab5cd05SOleg Bulyzhin 		return;
5632dab5cd05SOleg Bulyzhin 	}
5633dab5cd05SOleg Bulyzhin 
5634652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
56351f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
56367b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
56377b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
56381f313773SOleg Bulyzhin 				sc->bge_link++;
56399b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
56401f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
56411f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
56429b80ffe7SPyun YongHyeon 					DELAY(40);
56439b80ffe7SPyun YongHyeon 				}
56440c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
56451f313773SOleg Bulyzhin 				if (bootverbose)
56461f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
56473f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
56483f74909aSGleb Smirnoff 				    LINK_STATE_UP);
56497b97099dSOleg Bulyzhin 			}
56501f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
5651dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
56521f313773SOleg Bulyzhin 			if (bootverbose)
56531f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
56547b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
56551f313773SOleg Bulyzhin 		}
56566ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
56571f313773SOleg Bulyzhin 		/*
56580c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
56590c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
56600c8aa4eaSJung-uk Kim 		 * PHY link status directly.
56611f313773SOleg Bulyzhin 		 */
56621f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
56631f313773SOleg Bulyzhin 
56641f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
56651f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
56661f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
56675dda8085SOleg Bulyzhin 			mii_pollstat(mii);
56681f313773SOleg Bulyzhin 			if (!sc->bge_link &&
56691f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
56701f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
56711f313773SOleg Bulyzhin 				sc->bge_link++;
56721f313773SOleg Bulyzhin 				if (bootverbose)
56731f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
56741f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
56751f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
56761f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
56771f313773SOleg Bulyzhin 				sc->bge_link = 0;
56781f313773SOleg Bulyzhin 				if (bootverbose)
56791f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
56801f313773SOleg Bulyzhin 			}
56811f313773SOleg Bulyzhin 		}
56820c8aa4eaSJung-uk Kim 	} else {
56830c8aa4eaSJung-uk Kim 		/*
56846ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
56856ede2cfaSPyun YongHyeon 		 * link status.
56860c8aa4eaSJung-uk Kim 		 */
56876ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
56886ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
56896ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
5690dab5cd05SOleg Bulyzhin 	}
5691dab5cd05SOleg Bulyzhin 
5692*2246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
5693dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
5694dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
5695dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
5696dab5cd05SOleg Bulyzhin }
56976f8718a3SScott Long 
56986f8718a3SScott Long static void
56996f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
57006f8718a3SScott Long {
57016f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
57022280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
57037e32f79aSPyun YongHyeon 	char tn[32];
57047e32f79aSPyun YongHyeon 	int unit;
57056f8718a3SScott Long 
57066f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
57076f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
57086f8718a3SScott Long 
57096f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
57106f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
57116f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
57126f8718a3SScott Long 	    "Debug Information");
57136f8718a3SScott Long 
57146f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
57156f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
57166f8718a3SScott Long 	    "Register Read");
57176f8718a3SScott Long 
57186f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
57196f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
57206f8718a3SScott Long 	    "Memory Read");
57216f8718a3SScott Long 
57226f8718a3SScott Long #endif
5723763757b2SScott Long 
57247e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
5725beaa2ae1SPyun YongHyeon 	/*
5726beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
5727beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
5728beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
5729beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
5730beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
5731beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
5732beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
5733beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
5734beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
5735beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
5736beaa2ae1SPyun YongHyeon 	 */
57377e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
57387e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
57397e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
5740beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
5741beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
5742beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
5743beaa2ae1SPyun YongHyeon 	    "forced collapsing");
5744beaa2ae1SPyun YongHyeon 
57452ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
57462ae7f64bSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.msi", unit);
57472ae7f64bSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_msi);
57482ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
57492ae7f64bSPyun YongHyeon 	    CTLFLAG_RD, &sc->bge_msi, 0, "Enable MSI");
57505c952e8dSPyun YongHyeon 
575135f945cdSPyun YongHyeon 	/*
575235f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
575335f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
575435f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
575535f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
575635f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
575735f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
575835f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
575935f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
576035f945cdSPyun YongHyeon 	 */
576135f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
576235f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
576335f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
576435f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
576535f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
576635f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
576735f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
576835f945cdSPyun YongHyeon 
5769d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
57702280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
57712280c16bSPyun YongHyeon 	else
57722280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
57732280c16bSPyun YongHyeon }
5774d949071dSJung-uk Kim 
57752280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
57762280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
57772280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
57782280c16bSPyun YongHyeon 	    desc)
57792280c16bSPyun YongHyeon 
57802280c16bSPyun YongHyeon static void
57812280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
57822280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
57832280c16bSPyun YongHyeon {
57842280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
57852280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
57862280c16bSPyun YongHyeon 
57872280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
5788763757b2SScott Long 	    NULL, "BGE Statistics");
5789763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
5790763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
5791763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
5792763757b2SScott Long 	    "FramesDroppedDueToFilters");
5793763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
5794763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
5795763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
5796763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
5797763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
5798763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
579906e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
580006e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
580106e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
580206e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
5803763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
5804763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
5805763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
5806763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
5807763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
5808763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
5809763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
5810763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
5811763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
5812763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
5813763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
5814763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
5815763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
5816763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
5817763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
5818763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
5819763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
5820763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
5821763757b2SScott Long 
5822763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
5823763757b2SScott Long 	    NULL, "BGE RX Statistics");
5824763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5825763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
58261cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
5827763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
5828763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
5829763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
58301cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
5831763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
5832763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
5833763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
5834763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
5835763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
5836763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
5837763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
5838763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
5839763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
5840763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
5841763757b2SScott Long 	    "xoffPauseFramesReceived");
5842763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
5843763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
5844763757b2SScott Long 	    "ControlFramesReceived");
5845763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
5846763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
5847763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
5848763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
5849763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
5850763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
5851763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
5852763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
5853763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
585406e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
5855763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
585606e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
5857763757b2SScott Long 
5858763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
5859763757b2SScott Long 	    NULL, "BGE TX Statistics");
5860763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5861763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
58621cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
5863763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
5864763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
5865763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
5866763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
5867763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
5868763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
5869763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
5870763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
5871763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
5872763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
5873763757b2SScott Long 	    "InternalMacTransmitErrors");
5874763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
5875763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
5876763757b2SScott Long 	    "SingleCollisionFrames");
5877763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
5878763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
5879763757b2SScott Long 	    "MultipleCollisionFrames");
5880763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
5881763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
5882763757b2SScott Long 	    "DeferredTransmissions");
5883763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
5884763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
5885763757b2SScott Long 	    "ExcessiveCollisions");
5886763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
588706e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
588806e83c7eSScott Long 	    "LateCollisions");
5889763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
58901cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
5891763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
5892763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
5893763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
5894763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
5895763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
5896763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
5897763757b2SScott Long 	    "CarrierSenseErrors");
5898763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
5899763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
5900763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
5901763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
5902763757b2SScott Long }
5903763757b2SScott Long 
59042280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
59052280c16bSPyun YongHyeon 
59062280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
59076dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
59082280c16bSPyun YongHyeon 
59092280c16bSPyun YongHyeon static void
59102280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
59112280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
59122280c16bSPyun YongHyeon {
59132280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
59142280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
59152280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
59162280c16bSPyun YongHyeon 
59172280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
59182280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
59192280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
59202280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
59212280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
59222280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
59232280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
59242280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
59252280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
59262280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
59272280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
59282280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
59292280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
59302280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
59312280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
59322280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
59332280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
59342280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
59352280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
59362280c16bSPyun YongHyeon 
59372280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
59382280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
59392280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
59402280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
59412280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
59422280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
59432280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
59441cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
59452280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
59462280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
59472280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
59482280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
59492280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
59502280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
59512280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
59522280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
59532280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
59542280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
59552280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
59562280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
59572280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
59582280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
59592280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
59602280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
59612280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
59622280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
59632280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
59642280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
59652280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
59662280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
59672280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
59682280c16bSPyun YongHyeon 
59692280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
59702280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
59712280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
59721cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
59732280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
59742280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
59752280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
59762280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
59772280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
59782280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
59792280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
59802280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
59812280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
59822280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
59832280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
59842280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
59852280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
59862280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
59872280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
59882280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
59892280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
59902280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
59912280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
59922280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
59932280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
59941cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
59952280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
59961cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
59972280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
59981cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
59992280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
60002280c16bSPyun YongHyeon }
60012280c16bSPyun YongHyeon 
60022280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
60032280c16bSPyun YongHyeon 
6004763757b2SScott Long static int
6005763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6006763757b2SScott Long {
6007763757b2SScott Long 	struct bge_softc *sc;
600806e83c7eSScott Long 	uint32_t result;
6009d949071dSJung-uk Kim 	int offset;
6010763757b2SScott Long 
6011763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6012763757b2SScott Long 	offset = arg2;
6013d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6014d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6015041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
60166f8718a3SScott Long }
60176f8718a3SScott Long 
60186f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
60196f8718a3SScott Long static int
60206f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
60216f8718a3SScott Long {
60226f8718a3SScott Long 	struct bge_softc *sc;
60236f8718a3SScott Long 	uint16_t *sbdata;
602428276ad6SPyun YongHyeon 	int error, result, sbsz;
60256f8718a3SScott Long 	int i, j;
60266f8718a3SScott Long 
60276f8718a3SScott Long 	result = -1;
60286f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
60296f8718a3SScott Long 	if (error || (req->newptr == NULL))
60306f8718a3SScott Long 		return (error);
60316f8718a3SScott Long 
60326f8718a3SScott Long 	if (result == 1) {
60336f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
60346f8718a3SScott Long 
603528276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
603628276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
603728276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
603828276ad6SPyun YongHyeon 		else
603928276ad6SPyun YongHyeon 			sbsz = 32;
60406f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
60416f8718a3SScott Long 		printf("Status Block:\n");
604228276ad6SPyun YongHyeon 		BGE_LOCK(sc);
604328276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
604428276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
604528276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
604628276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
60476f8718a3SScott Long 			printf("%06x:", i);
604828276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
604928276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
60506f8718a3SScott Long 			printf("\n");
60516f8718a3SScott Long 		}
60526f8718a3SScott Long 
60536f8718a3SScott Long 		printf("Registers:\n");
60540c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
60556f8718a3SScott Long 			printf("%06x:", i);
60566f8718a3SScott Long 			for (j = 0; j < 8; j++) {
60576f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
60586f8718a3SScott Long 				i += 4;
60596f8718a3SScott Long 			}
60606f8718a3SScott Long 			printf("\n");
60616f8718a3SScott Long 		}
606228276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
60636f8718a3SScott Long 
60646f8718a3SScott Long 		printf("Hardware Flags:\n");
606528276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
606628276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6067a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6068a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
60695345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
60706f8718a3SScott Long 			printf(" - 575X Plus\n");
60715345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
60726f8718a3SScott Long 			printf(" - 5705 Plus\n");
60735345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
60745345bad0SScott Long 			printf(" - 5714 Family\n");
60755345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
60765345bad0SScott Long 			printf(" - 5700 Family\n");
60776f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
60786f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
60796f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
60806f8718a3SScott Long 			printf(" - PCI-X Bus\n");
60816f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
60826f8718a3SScott Long 			printf(" - PCI Express Bus\n");
60837d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
60846f8718a3SScott Long 			printf(" - No 3 LEDs\n");
60856f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
60866f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
60876f8718a3SScott Long 	}
60886f8718a3SScott Long 
60896f8718a3SScott Long 	return (error);
60906f8718a3SScott Long }
60916f8718a3SScott Long 
60926f8718a3SScott Long static int
60936f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
60946f8718a3SScott Long {
60956f8718a3SScott Long 	struct bge_softc *sc;
60966f8718a3SScott Long 	int error;
60976f8718a3SScott Long 	uint16_t result;
60986f8718a3SScott Long 	uint32_t val;
60996f8718a3SScott Long 
61006f8718a3SScott Long 	result = -1;
61016f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
61026f8718a3SScott Long 	if (error || (req->newptr == NULL))
61036f8718a3SScott Long 		return (error);
61046f8718a3SScott Long 
61056f8718a3SScott Long 	if (result < 0x8000) {
61066f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
61076f8718a3SScott Long 		val = CSR_READ_4(sc, result);
61086f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
61096f8718a3SScott Long 	}
61106f8718a3SScott Long 
61116f8718a3SScott Long 	return (error);
61126f8718a3SScott Long }
61136f8718a3SScott Long 
61146f8718a3SScott Long static int
61156f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
61166f8718a3SScott Long {
61176f8718a3SScott Long 	struct bge_softc *sc;
61186f8718a3SScott Long 	int error;
61196f8718a3SScott Long 	uint16_t result;
61206f8718a3SScott Long 	uint32_t val;
61216f8718a3SScott Long 
61226f8718a3SScott Long 	result = -1;
61236f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
61246f8718a3SScott Long 	if (error || (req->newptr == NULL))
61256f8718a3SScott Long 		return (error);
61266f8718a3SScott Long 
61276f8718a3SScott Long 	if (result < 0x8000) {
61286f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
61296f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
61306f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
61316f8718a3SScott Long 	}
61326f8718a3SScott Long 
61336f8718a3SScott Long 	return (error);
61346f8718a3SScott Long }
61356f8718a3SScott Long #endif
613638cc658fSJohn Baldwin 
613738cc658fSJohn Baldwin static int
61385fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
61395fea260fSMarius Strobl {
61405fea260fSMarius Strobl 
61415fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
61425fea260fSMarius Strobl 		return (1);
61435fea260fSMarius Strobl 
61445fea260fSMarius Strobl #ifdef __sparc64__
61455fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
61465fea260fSMarius Strobl 	return (0);
61475fea260fSMarius Strobl #endif
61485fea260fSMarius Strobl 	return (1);
61495fea260fSMarius Strobl }
61505fea260fSMarius Strobl 
61515fea260fSMarius Strobl static int
615238cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
615338cc658fSJohn Baldwin {
615438cc658fSJohn Baldwin 	uint32_t mac_addr;
615538cc658fSJohn Baldwin 
615673635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
615738cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
615838cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
615938cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
616073635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
616138cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
616238cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
616338cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
616438cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
61655fea260fSMarius Strobl 		return (0);
616638cc658fSJohn Baldwin 	}
61675fea260fSMarius Strobl 	return (1);
616838cc658fSJohn Baldwin }
616938cc658fSJohn Baldwin 
617038cc658fSJohn Baldwin static int
617138cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
617238cc658fSJohn Baldwin {
617338cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
617438cc658fSJohn Baldwin 
617538cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
617638cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
617738cc658fSJohn Baldwin 
61785fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
61795fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
618038cc658fSJohn Baldwin }
618138cc658fSJohn Baldwin 
618238cc658fSJohn Baldwin static int
618338cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
618438cc658fSJohn Baldwin {
618538cc658fSJohn Baldwin 
61865fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
61875fea260fSMarius Strobl 		return (1);
61885fea260fSMarius Strobl 
61895fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
61905fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
619138cc658fSJohn Baldwin }
619238cc658fSJohn Baldwin 
619338cc658fSJohn Baldwin static int
619438cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
619538cc658fSJohn Baldwin {
619638cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
619738cc658fSJohn Baldwin 		/* NOTE: Order is critical */
61985fea260fSMarius Strobl 		bge_get_eaddr_fw,
619938cc658fSJohn Baldwin 		bge_get_eaddr_mem,
620038cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
620138cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
620238cc658fSJohn Baldwin 		NULL
620338cc658fSJohn Baldwin 	};
620438cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
620538cc658fSJohn Baldwin 
620638cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
620738cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
620838cc658fSJohn Baldwin 			break;
620938cc658fSJohn Baldwin 	}
621038cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
621138cc658fSJohn Baldwin }
6212