xref: /freebsd/sys/dev/bge/if_bge.c (revision e4146b9510d06cf15a49124ae74a11dd4b5ace07)
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,
88838cc658fSJohn Baldwin 		    "PHY write timed out (phy %d, reg %d, val %d)\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;
899*e4146b95SPyun YongHyeon 
90095d67482SBill Paul 	sc = device_get_softc(dev);
901*e4146b95SPyun YongHyeon 	if ((sc->bge_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
902*e4146b95SPyun YongHyeon 		return;
90395d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
90495d67482SBill Paul 
905d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
906d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
907d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
908d4f5240aSPyun YongHyeon 		case IFM_10_T:
909d4f5240aSPyun YongHyeon 		case IFM_100_TX:
910d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
911d4f5240aSPyun YongHyeon 			break;
912d4f5240aSPyun YongHyeon 		case IFM_1000_T:
913d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
914d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
915d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
916d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
917d4f5240aSPyun YongHyeon 			else
918d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
919d4f5240aSPyun YongHyeon 			break;
920d4f5240aSPyun YongHyeon 		default:
921d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
922d4f5240aSPyun YongHyeon 			break;
923d4f5240aSPyun YongHyeon 		}
924d4f5240aSPyun YongHyeon 	} else
925d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
926d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
927d4f5240aSPyun YongHyeon 		return;
92895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
929ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
930ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
93195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
9323f74909aSGleb Smirnoff 	else
93395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
93495d67482SBill Paul 
9356854be25SPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0) {
93695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
937efd4fc3fSMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
938efd4fc3fSMarius Strobl 		    IFM_ETH_TXPAUSE) != 0)
9396854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
9403f74909aSGleb Smirnoff 		else
9416854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
942efd4fc3fSMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
943efd4fc3fSMarius Strobl 		    IFM_ETH_RXPAUSE) != 0)
9446854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9456854be25SPyun YongHyeon 		else
9466854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9476854be25SPyun YongHyeon 	} else {
94895d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
9496854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
9506854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9516854be25SPyun YongHyeon 	}
95295d67482SBill Paul }
95395d67482SBill Paul 
95495d67482SBill Paul /*
95595d67482SBill Paul  * Intialize a standard receive ring descriptor.
95695d67482SBill Paul  */
95795d67482SBill Paul static int
958943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
95995d67482SBill Paul {
960943787f3SPyun YongHyeon 	struct mbuf *m;
96195d67482SBill Paul 	struct bge_rx_bd *r;
962a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
963943787f3SPyun YongHyeon 	bus_dmamap_t map;
964a23634a1SPyun YongHyeon 	int error, nsegs;
96595d67482SBill Paul 
966f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
967f5459d4cSPyun YongHyeon 	    (sc->bge_ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
968f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
969f5459d4cSPyun YongHyeon 		m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
970f5459d4cSPyun YongHyeon 		if (m == NULL)
971f5459d4cSPyun YongHyeon 			return (ENOBUFS);
972f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
973f5459d4cSPyun YongHyeon 	} else {
974943787f3SPyun YongHyeon 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
975943787f3SPyun YongHyeon 		if (m == NULL)
97695d67482SBill Paul 			return (ENOBUFS);
977943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
978f5459d4cSPyun YongHyeon 	}
979652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
980943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
981943787f3SPyun YongHyeon 
9820ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
983943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
984a23634a1SPyun YongHyeon 	if (error != 0) {
985943787f3SPyun YongHyeon 		m_freem(m);
986a23634a1SPyun YongHyeon 		return (error);
987f41ac2beSBill Paul 	}
988943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
989943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
990943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
991943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
992943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
993943787f3SPyun YongHyeon 	}
994943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
995943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
996943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
997943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
998e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
999943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1000a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1001a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1002e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1003a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1004e907febfSPyun YongHyeon 	r->bge_idx = i;
1005f41ac2beSBill Paul 
10060ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1007943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
100895d67482SBill Paul 
100995d67482SBill Paul 	return (0);
101095d67482SBill Paul }
101195d67482SBill Paul 
101295d67482SBill Paul /*
101395d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
101495d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
101595d67482SBill Paul  */
101695d67482SBill Paul static int
1017943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
101895d67482SBill Paul {
10191be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1020943787f3SPyun YongHyeon 	bus_dmamap_t map;
10211be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1022943787f3SPyun YongHyeon 	struct mbuf *m;
1023943787f3SPyun YongHyeon 	int error, nsegs;
102495d67482SBill Paul 
1025943787f3SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1026943787f3SPyun YongHyeon 	if (m == NULL)
102795d67482SBill Paul 		return (ENOBUFS);
102895d67482SBill Paul 
1029943787f3SPyun YongHyeon 	m_cljget(m, M_DONTWAIT, MJUM9BYTES);
1030943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
1031943787f3SPyun YongHyeon 		m_freem(m);
103295d67482SBill Paul 		return (ENOBUFS);
103395d67482SBill Paul 	}
1034943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1035652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1036943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
10371be6acb7SGleb Smirnoff 
10381be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1039943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1040943787f3SPyun YongHyeon 	if (error != 0) {
1041943787f3SPyun YongHyeon 		m_freem(m);
10421be6acb7SGleb Smirnoff 		return (error);
1043f7cea149SGleb Smirnoff 	}
10441be6acb7SGleb Smirnoff 
1045aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1046943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1047943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1048943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1049943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1050943787f3SPyun YongHyeon 	}
1051943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1052943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1053943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1054943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1055943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1056e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1057e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1058e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1059e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1060e0b7b101SPyun YongHyeon 
10611be6acb7SGleb Smirnoff 	/*
10621be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
10631be6acb7SGleb Smirnoff 	 */
1064943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
10654e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
10664e7ba1abSGleb Smirnoff 	r->bge_idx = i;
10674e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
10684e7ba1abSGleb Smirnoff 	switch (nsegs) {
10694e7ba1abSGleb Smirnoff 	case 4:
10704e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
10714e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
10724e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1073e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
10744e7ba1abSGleb Smirnoff 	case 3:
1075e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1076e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1077e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1078e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
10794e7ba1abSGleb Smirnoff 	case 2:
10804e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
10814e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
10824e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1083e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
10844e7ba1abSGleb Smirnoff 	case 1:
10854e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
10864e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
10874e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1088e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
10894e7ba1abSGleb Smirnoff 		break;
10904e7ba1abSGleb Smirnoff 	default:
10914e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
10924e7ba1abSGleb Smirnoff 	}
1093f41ac2beSBill Paul 
1094a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1095943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
109695d67482SBill Paul 
109795d67482SBill Paul 	return (0);
109895d67482SBill Paul }
109995d67482SBill Paul 
110095d67482SBill Paul static int
11013f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
110295d67482SBill Paul {
11033ee5d7daSPyun YongHyeon 	int error, i;
110495d67482SBill Paul 
1105e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
110603e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1107e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1108943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
11093ee5d7daSPyun YongHyeon 			return (error);
111003e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
11111888f324SPyun YongHyeon 	}
111295d67482SBill Paul 
1113f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1114d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1115f41ac2beSBill Paul 
1116e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1117e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
111895d67482SBill Paul 
111995d67482SBill Paul 	return (0);
112095d67482SBill Paul }
112195d67482SBill Paul 
112295d67482SBill Paul static void
11233f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
112495d67482SBill Paul {
112595d67482SBill Paul 	int i;
112695d67482SBill Paul 
112795d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
112895d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
11290ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1130e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1131e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
11320ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1133f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1134e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1135e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
113695d67482SBill Paul 		}
1137f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
113895d67482SBill Paul 		    sizeof(struct bge_rx_bd));
113995d67482SBill Paul 	}
114095d67482SBill Paul }
114195d67482SBill Paul 
114295d67482SBill Paul static int
11433f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
114495d67482SBill Paul {
114595d67482SBill Paul 	struct bge_rcb *rcb;
11463ee5d7daSPyun YongHyeon 	int error, i;
114795d67482SBill Paul 
1148e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
114903e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
115095d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1151943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
11523ee5d7daSPyun YongHyeon 			return (error);
115303e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
11541888f324SPyun YongHyeon 	}
115595d67482SBill Paul 
1156f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1157d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1158f41ac2beSBill Paul 
1159e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
116095d67482SBill Paul 
11618a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1162f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
11638a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
11648a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
116567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
116695d67482SBill Paul 
1167e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
116895d67482SBill Paul 
116995d67482SBill Paul 	return (0);
117095d67482SBill Paul }
117195d67482SBill Paul 
117295d67482SBill Paul static void
11733f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
117495d67482SBill Paul {
117595d67482SBill Paul 	int i;
117695d67482SBill Paul 
117795d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
117895d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1179e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1180e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1181e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1182f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1183f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1184e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1185e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
118695d67482SBill Paul 		}
1187f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
11881be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
118995d67482SBill Paul 	}
119095d67482SBill Paul }
119195d67482SBill Paul 
119295d67482SBill Paul static void
11933f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
119495d67482SBill Paul {
119595d67482SBill Paul 	int i;
119695d67482SBill Paul 
1197f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
119895d67482SBill Paul 		return;
119995d67482SBill Paul 
120095d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
120195d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
12020ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1203e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1204e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
12050ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1206f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1207e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1208e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
120995d67482SBill Paul 		}
1210f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
121195d67482SBill Paul 		    sizeof(struct bge_tx_bd));
121295d67482SBill Paul 	}
121395d67482SBill Paul }
121495d67482SBill Paul 
121595d67482SBill Paul static int
12163f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
121795d67482SBill Paul {
121895d67482SBill Paul 	sc->bge_txcnt = 0;
121995d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
12203927098fSPaul Saab 
1221e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1222e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
12235c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1224e6bf277eSPyun YongHyeon 
122514bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
122614bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
122738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
122814bbd30fSGleb Smirnoff 
12293927098fSPaul Saab 	/* 5700 b2 errata */
1230e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
123138cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
12323927098fSPaul Saab 
123314bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
123438cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
12353927098fSPaul Saab 	/* 5700 b2 errata */
1236e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
123738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
123895d67482SBill Paul 
123995d67482SBill Paul 	return (0);
124095d67482SBill Paul }
124195d67482SBill Paul 
124295d67482SBill Paul static void
12433e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
12443e9b1bcaSJung-uk Kim {
12453e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
12463e9b1bcaSJung-uk Kim 
12473e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
12483e9b1bcaSJung-uk Kim 
12493e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
12503e9b1bcaSJung-uk Kim 
125145ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
12523e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
125345ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12543e9b1bcaSJung-uk Kim 	else
125545ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12563e9b1bcaSJung-uk Kim }
12573e9b1bcaSJung-uk Kim 
12583e9b1bcaSJung-uk Kim static void
12593f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
126095d67482SBill Paul {
126195d67482SBill Paul 	struct ifnet *ifp;
126295d67482SBill Paul 	struct ifmultiaddr *ifma;
12633f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
126495d67482SBill Paul 	int h, i;
126595d67482SBill Paul 
12660f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
12670f9bd73bSSam Leffler 
1268fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
126995d67482SBill Paul 
127095d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
127195d67482SBill Paul 		for (i = 0; i < 4; i++)
12720c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
127395d67482SBill Paul 		return;
127495d67482SBill Paul 	}
127595d67482SBill Paul 
127695d67482SBill Paul 	/* First, zot all the existing filters. */
127795d67482SBill Paul 	for (i = 0; i < 4; i++)
127895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
127995d67482SBill Paul 
128095d67482SBill Paul 	/* Now program new ones. */
1281eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
128295d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
128395d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
128495d67482SBill Paul 			continue;
12850e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
12860c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
12870c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
128895d67482SBill Paul 	}
1289eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
129095d67482SBill Paul 
129195d67482SBill Paul 	for (i = 0; i < 4; i++)
129295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
129395d67482SBill Paul }
129495d67482SBill Paul 
12958cb1383cSDoug Ambrisko static void
1296cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1297cb2eacc7SYaroslav Tykhiy {
1298cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1299cb2eacc7SYaroslav Tykhiy 
1300cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1301cb2eacc7SYaroslav Tykhiy 
1302cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1303cb2eacc7SYaroslav Tykhiy 
1304cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1305cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1306cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1307cb2eacc7SYaroslav Tykhiy 	else
1308cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1309cb2eacc7SYaroslav Tykhiy }
1310cb2eacc7SYaroslav Tykhiy 
1311cb2eacc7SYaroslav Tykhiy static void
1312797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
13138cb1383cSDoug Ambrisko {
1314797ab05eSPyun YongHyeon 
13158cb1383cSDoug Ambrisko 	/*
13168cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
13178cb1383cSDoug Ambrisko 	 */
13188cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1319888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
13208cb1383cSDoug Ambrisko 
13218cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
13228cb1383cSDoug Ambrisko 		switch (type) {
13238cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1324224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1325224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
13268cb1383cSDoug Ambrisko 			break;
13278cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
1328224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1329224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
13308cb1383cSDoug Ambrisko 			break;
13318cb1383cSDoug Ambrisko 		}
13328cb1383cSDoug Ambrisko 	}
13338cb1383cSDoug Ambrisko }
13348cb1383cSDoug Ambrisko 
13358cb1383cSDoug Ambrisko static void
1336797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
13378cb1383cSDoug Ambrisko {
1338797ab05eSPyun YongHyeon 
13398cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
13408cb1383cSDoug Ambrisko 		switch (type) {
13418cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1342224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1343224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
13448cb1383cSDoug Ambrisko 			/* START DONE */
13458cb1383cSDoug Ambrisko 			break;
13468cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
1347224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1348224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
13498cb1383cSDoug Ambrisko 			break;
13508cb1383cSDoug Ambrisko 		}
13518cb1383cSDoug Ambrisko 	}
13528cb1383cSDoug Ambrisko }
13538cb1383cSDoug Ambrisko 
13548cb1383cSDoug Ambrisko static void
1355797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
13568cb1383cSDoug Ambrisko {
1357797ab05eSPyun YongHyeon 
13588cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13598cb1383cSDoug Ambrisko 		switch (type) {
13608cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1361224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1362224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
13638cb1383cSDoug Ambrisko 			break;
13648cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
1365224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1366224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
13678cb1383cSDoug Ambrisko 			break;
13688cb1383cSDoug Ambrisko 		}
13698cb1383cSDoug Ambrisko 	}
13708cb1383cSDoug Ambrisko }
13718cb1383cSDoug Ambrisko 
1372797ab05eSPyun YongHyeon static void
1373797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
13748cb1383cSDoug Ambrisko {
13758cb1383cSDoug Ambrisko 	int i;
13768cb1383cSDoug Ambrisko 
13778cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13783c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
13793fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
13809931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
13818cb1383cSDoug Ambrisko 
13828cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
13839931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
13849931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
13858cb1383cSDoug Ambrisko 				break;
13868cb1383cSDoug Ambrisko 			DELAY(10);
13878cb1383cSDoug Ambrisko 		}
13888cb1383cSDoug Ambrisko 	}
13898cb1383cSDoug Ambrisko }
13908cb1383cSDoug Ambrisko 
139150515680SPyun YongHyeon static uint32_t
139250515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
139350515680SPyun YongHyeon {
139450515680SPyun YongHyeon 	uint32_t dma_options;
139550515680SPyun YongHyeon 
139650515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
139750515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
139850515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
139950515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
140050515680SPyun YongHyeon #endif
140150515680SPyun YongHyeon 	if ((sc)->bge_asicrev == BGE_ASICREV_BCM5720)
140250515680SPyun YongHyeon 		dma_options |= BGE_MODECTL_BYTESWAP_B2HRX_DATA |
140350515680SPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA | BGE_MODECTL_B2HRX_ENABLE |
140450515680SPyun YongHyeon 		    BGE_MODECTL_HTX2B_ENABLE;
140550515680SPyun YongHyeon 
140650515680SPyun YongHyeon 	return (dma_options);
140750515680SPyun YongHyeon }
140850515680SPyun YongHyeon 
140995d67482SBill Paul /*
1410c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
141195d67482SBill Paul  */
141295d67482SBill Paul static int
14133f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
141495d67482SBill Paul {
141550515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1416fbc374afSPyun YongHyeon 	uint16_t val;
141795d67482SBill Paul 	int i;
141895d67482SBill Paul 
14198cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
14201108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
14211108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
14221108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
14231108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
142495d67482SBill Paul 
142595d67482SBill Paul 	/* Clear the MAC control register */
142695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
142795d67482SBill Paul 
142895d67482SBill Paul 	/*
142995d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
143095d67482SBill Paul 	 * internal memory.
143195d67482SBill Paul 	 */
143295d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
14333f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
143495d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
143595d67482SBill Paul 
143695d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
14373f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
143895d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
143995d67482SBill Paul 
1440fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1441fbc374afSPyun YongHyeon 		/*
1442d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1443fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1444fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1445fbc374afSPyun YongHyeon 		 */
1446fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1447fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1448fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1449fbc374afSPyun YongHyeon 	}
1450fbc374afSPyun YongHyeon 
1451186f842bSJung-uk Kim 	/*
1452186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1453186f842bSJung-uk Kim 	 */
1454186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1455186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1456652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
1457186f842bSJung-uk Kim 		/* Read watermark not used, 128 bytes for write. */
1458186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1459652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
14604c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1461186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1462186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1463186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1464186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1465186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1466186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1467cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1468cbb2b2feSPyun YongHyeon 			/*
1469cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1470cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1471cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1472cbb2b2feSPyun YongHyeon 			 * register.
1473cbb2b2feSPyun YongHyeon 			 */
1474cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1475cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1476186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1477186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1478186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1479186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1480186f842bSJung-uk Kim 		} else {
1481186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1482186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1483186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
14840c8aa4eaSJung-uk Kim 			    0x0F;
1485186f842bSJung-uk Kim 		}
1486e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1487e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
14883f74909aSGleb Smirnoff 			uint32_t tmp;
14895cba12d3SPaul Saab 
1490186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
14910c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1492186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1493186f842bSJung-uk Kim 				dma_rw_ctl |=
1494186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
14955cba12d3SPaul Saab 
1496186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1497186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1498186f842bSJung-uk Kim 		}
1499186f842bSJung-uk Kim 	} else {
1500186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1501186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1502186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1503186f842bSJung-uk Kim 
1504186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1505186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1506186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1507186f842bSJung-uk Kim 	}
1508186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1509186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1510186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1511186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1512e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1513186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
15145cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1515b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
15161108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1517b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1518b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1519bbe2ca75SPyun YongHyeon 		/*
1520bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1521bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1522bbe2ca75SPyun YongHyeon 		 * disabled.
1523bbe2ca75SPyun YongHyeon 		 */
1524bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
1525bbe2ca75SPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM57765)
1526bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1527b4a256acSPyun YongHyeon 	}
15285cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
152995d67482SBill Paul 
153095d67482SBill Paul 	/*
153195d67482SBill Paul 	 * Set up general mode register.
153295d67482SBill Paul 	 */
153350515680SPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc) | BGE_MODECTL_MAC_ATTN_INTR |
153450515680SPyun YongHyeon 	    BGE_MODECTL_HOST_SEND_BDS | BGE_MODECTL_TX_NO_PHDR_CSUM;
153595d67482SBill Paul 
153695d67482SBill Paul 	/*
153790447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
153890447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
153990447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
154090447aadSMarius Strobl 	 * certain bridges.
154190447aadSMarius Strobl 	 */
154290447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
154390447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
154450515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
154590447aadSMarius Strobl 
154690447aadSMarius Strobl 	/*
15478cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
15488cb1383cSDoug Ambrisko 	 */
15498cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
155050515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
155150515680SPyun YongHyeon 
155250515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
15538cb1383cSDoug Ambrisko 
15548cb1383cSDoug Ambrisko 	/*
1555ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1556c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1557c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
155895d67482SBill Paul 	 */
1559c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1560c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
156195d67482SBill Paul 
156295d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
15630c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
156495d67482SBill Paul 
156538cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
156638cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
156738cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
156838cc658fSJohn Baldwin 
156938cc658fSJohn Baldwin 		/* Put PHY into ready state */
157038cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
157138cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
157238cc658fSJohn Baldwin 		DELAY(40);
157338cc658fSJohn Baldwin 	}
157438cc658fSJohn Baldwin 
157595d67482SBill Paul 	return (0);
157695d67482SBill Paul }
157795d67482SBill Paul 
157895d67482SBill Paul static int
15793f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
158095d67482SBill Paul {
158195d67482SBill Paul 	struct bge_rcb *rcb;
1582e907febfSPyun YongHyeon 	bus_size_t vrcb;
1583e907febfSPyun YongHyeon 	bge_hostaddr taddr;
1584bbe2ca75SPyun YongHyeon 	uint32_t dmactl, val;
15858a315a6dSPyun YongHyeon 	int i, limit;
158695d67482SBill Paul 
158795d67482SBill Paul 	/*
158895d67482SBill Paul 	 * Initialize the memory window pointer register so that
158995d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
159095d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
159195d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
159295d67482SBill Paul 	 */
159395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
159495d67482SBill Paul 
1595822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1596822f63fcSBill Paul 
15977ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
159895d67482SBill Paul 		/* Configure mbuf memory pool */
15990dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1600822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1601822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1602822f63fcSBill Paul 		else
160395d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
160495d67482SBill Paul 
160595d67482SBill Paul 		/* Configure DMA resource pool */
16060434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
16070434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
160895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
16090434d1b8SBill Paul 	}
161095d67482SBill Paul 
161195d67482SBill Paul 	/* Configure mbuf pool watermarks */
161250515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
16131108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
16141108273aSPyun YongHyeon 		if (sc->bge_ifp->if_mtu > ETHERMTU) {
16151108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
16161108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
16171108273aSPyun YongHyeon 		} else {
16181108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
16191108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
16201108273aSPyun YongHyeon 		}
16211108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
1622fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1623fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1624fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
162538cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
162638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
162738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
162838cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
162938cc658fSJohn Baldwin 	} else {
163038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
163138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
163238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
163338cc658fSJohn Baldwin 	}
163495d67482SBill Paul 
163595d67482SBill Paul 	/* Configure DMA resource watermarks */
163695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
163795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
163895d67482SBill Paul 
163995d67482SBill Paul 	/* Enable buffer manager */
1640bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1641bbe2ca75SPyun YongHyeon 	/*
1642bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
1643bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
1644bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
1645bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
1646bbe2ca75SPyun YongHyeon 	 */
1647bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
1648bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1649bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
165095d67482SBill Paul 
165195d67482SBill Paul 	/* Poll for buffer manager start indication */
165295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1653d5d23857SJung-uk Kim 		DELAY(10);
16540c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
165595d67482SBill Paul 			break;
165695d67482SBill Paul 	}
165795d67482SBill Paul 
165895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
16595a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
166095d67482SBill Paul 		return (ENXIO);
166195d67482SBill Paul 	}
166295d67482SBill Paul 
166395d67482SBill Paul 	/* Enable flow-through queues */
16640c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
166595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
166695d67482SBill Paul 
166795d67482SBill Paul 	/* Wait until queue initialization is complete */
166895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1669d5d23857SJung-uk Kim 		DELAY(10);
167095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
167195d67482SBill Paul 			break;
167295d67482SBill Paul 	}
167395d67482SBill Paul 
167495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1675fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
167695d67482SBill Paul 		return (ENXIO);
167795d67482SBill Paul 	}
167895d67482SBill Paul 
16798a315a6dSPyun YongHyeon 	/*
16808a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
16818a315a6dSPyun YongHyeon 	 *
16828a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
16838a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
16848a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
16858a315a6dSPyun YongHyeon 	 *
16868a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
16878a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
16888a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
16898a315a6dSPyun YongHyeon 	 *   to the controller.
16908a315a6dSPyun YongHyeon 	 *
16918a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
16928a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
16938a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
16948a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
16958a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
16968a315a6dSPyun YongHyeon 	 *   be disabled.
16978a315a6dSPyun YongHyeon 	 *
16988a315a6dSPyun YongHyeon 	 * Receive Return Ring
16998a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
17008a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
17018a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
17028a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
17038a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
17048a315a6dSPyun YongHyeon 	 *
17058a315a6dSPyun YongHyeon 	 * Send Ring
17068a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
17078a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
17088a315a6dSPyun YongHyeon 	 */
17098a315a6dSPyun YongHyeon 
17108a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
1711f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1712f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1713f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1714f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1715f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1716f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1717f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
17181108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
17191108273aSPyun YongHyeon 		/*
17201108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
17211108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
17221108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
17231108273aSPyun YongHyeon 		 * Bit 0     : Reserved
17241108273aSPyun YongHyeon 		 */
17251108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
17261108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
17271108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
17288a315a6dSPyun YongHyeon 		/*
17298a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
17308a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
17318a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
17328a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
17338a315a6dSPyun YongHyeon 		 */
17340434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
17358a315a6dSPyun YongHyeon 	} else {
17368a315a6dSPyun YongHyeon 		/*
17378a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
17388a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
17398a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
17408a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
17418a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
17428a315a6dSPyun YongHyeon 		 */
17430434d1b8SBill Paul 		rcb->bge_maxlen_flags =
17440434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
17458a315a6dSPyun YongHyeon 	}
1746bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
174750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
174850515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
17491108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
17501108273aSPyun YongHyeon 	else
175195d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
17528a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
17530c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
17540c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
175567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
175667111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
175795d67482SBill Paul 
17588a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
17598a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
17608a315a6dSPyun YongHyeon 
176195d67482SBill Paul 	/*
17628a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
17638a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
17648a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
176595d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
176695d67482SBill Paul 	 * high enough to require it).
176795d67482SBill Paul 	 */
17684c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1769f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
17708a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
1771f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1772f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1773f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1774f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1775f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1776f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1777f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
17781be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
17791be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
1780bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
178150515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
178250515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
17831108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
17841108273aSPyun YongHyeon 		else
178595d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
178667111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
178767111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
178867111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
178967111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
17908a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
17910434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
17920434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
179367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
17948a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
17958a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
17968a315a6dSPyun YongHyeon 	}
179795d67482SBill Paul 
17988a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
17995e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
1800f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
180167111612SJohn Polstra 		rcb->bge_maxlen_flags =
180267111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
18030434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
18040434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
18058a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
18068a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
18070434d1b8SBill Paul 	}
180895d67482SBill Paul 
1809ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
1810ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
1811427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
1812427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
1813427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
18148d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
18158d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
1816ca4f8986SPyun YongHyeon 	}
181795d67482SBill Paul 	/*
18188a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
18198a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
18208a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
18218a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
18228a315a6dSPyun YongHyeon 	 *
182395d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
182495d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
182595d67482SBill Paul 	 * each ring.
18269ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
18279ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
18289ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
18299ba784dbSScott Long 	 * are reports that it might not need to be so strict.
183038cc658fSJohn Baldwin 	 *
183138cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
183238cc658fSJohn Baldwin 	 * well.
183395d67482SBill Paul 	 */
18345345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
18356f8718a3SScott Long 		val = 8;
18366f8718a3SScott Long 	else
18376f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
18386f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
18392a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
18402a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
18412a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
18421108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18431108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
18441108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
18451108273aSPyun YongHyeon 	}
184695d67482SBill Paul 
184795d67482SBill Paul 	/*
18488a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
18498a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
18508a315a6dSPyun YongHyeon 	 * located in NIC memory.
185195d67482SBill Paul 	 */
18528a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
18538a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
18548a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
18558a315a6dSPyun YongHyeon 	else
18568a315a6dSPyun YongHyeon 		limit = 1;
1857e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
18588a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1859e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1860e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1861e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1862e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
186395d67482SBill Paul 	}
186495d67482SBill Paul 
18658a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
1866e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1867e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1868e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1869e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1870bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
187150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
187250515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
18731108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
18741108273aSPyun YongHyeon 	else
1875e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1876e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1877e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1878e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
187995d67482SBill Paul 
18808a315a6dSPyun YongHyeon 	/*
18818a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
18828a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
18838a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
18848a315a6dSPyun YongHyeon 	 */
1885bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
188650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
188750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
18881108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
18891108273aSPyun YongHyeon 		limit = 16;
18901108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
18918a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
1892b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
1893b4a256acSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57765)
18948a315a6dSPyun YongHyeon 		limit = 4;
18958a315a6dSPyun YongHyeon 	else
18968a315a6dSPyun YongHyeon 		limit = 1;
18978a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
1898e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
18998a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1900e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1901e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1902e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
19038a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
1904e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
190538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
19063f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1907e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
190895d67482SBill Paul 	}
190995d67482SBill Paul 
191095d67482SBill Paul 	/*
19118a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
19128a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
19138a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
191495d67482SBill Paul 	 */
1915e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1916e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1917e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1918e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
19198a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1920e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1921e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
192295d67482SBill Paul 
192395d67482SBill Paul 	/* Set random backoff seed for TX */
192495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
19254a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
19264a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
19274a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
192895d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
192995d67482SBill Paul 
193095d67482SBill Paul 	/* Set inter-packet gap */
193150515680SPyun YongHyeon 	val = 0x2620;
193250515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
193350515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
193450515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
193550515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
193695d67482SBill Paul 
193795d67482SBill Paul 	/*
193895d67482SBill Paul 	 * Specify which ring to use for packets that don't match
193995d67482SBill Paul 	 * any RX rules.
194095d67482SBill Paul 	 */
194195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
194295d67482SBill Paul 
194395d67482SBill Paul 	/*
194495d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
194595d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
194695d67482SBill Paul 	 */
194795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
194895d67482SBill Paul 
194995d67482SBill Paul 	/* Inialize RX list placement stats mask. */
19500c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
195195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
195295d67482SBill Paul 
195395d67482SBill Paul 	/* Disable host coalescing until we get it set up */
195495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
195595d67482SBill Paul 
195695d67482SBill Paul 	/* Poll to make sure it's shut down. */
195795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1958d5d23857SJung-uk Kim 		DELAY(10);
195995d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
196095d67482SBill Paul 			break;
196195d67482SBill Paul 	}
196295d67482SBill Paul 
196395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1964fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1965fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
196695d67482SBill Paul 		return (ENXIO);
196795d67482SBill Paul 	}
196895d67482SBill Paul 
196995d67482SBill Paul 	/* Set up host coalescing defaults */
197095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
197195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
197295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
197395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
19747ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
197595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
197695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
19770434d1b8SBill Paul 	}
1978b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1979b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
198095d67482SBill Paul 
198195d67482SBill Paul 	/* Set up address of statistics block */
19827ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1983f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1984f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
198595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1986f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
19870434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
198895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
19890434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
19900434d1b8SBill Paul 	}
19910434d1b8SBill Paul 
19920434d1b8SBill Paul 	/* Set up address of status block */
1993f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1994f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
199595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1996f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
199795d67482SBill Paul 
199830f57f61SPyun YongHyeon 	/* Set up status block size. */
199930f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2000864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
200130f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2002864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2003864104feSPyun YongHyeon 	} else {
200430f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2005864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2006864104feSPyun YongHyeon 	}
2007864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2008864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2009864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
201030f57f61SPyun YongHyeon 
201195d67482SBill Paul 	/* Turn on host coalescing state machine */
201230f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
201395d67482SBill Paul 
201495d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
201595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
201695d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
201795d67482SBill Paul 
201895d67482SBill Paul 	/* Turn on RX list placement state machine */
201995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
202095d67482SBill Paul 
202195d67482SBill Paul 	/* Turn on RX list selector state machine. */
20227ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
202395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
202495d67482SBill Paul 
2025ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2026ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2027ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2028ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2029ea3b4127SPyun YongHyeon 
2030ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2031ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2032ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2033ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2034ea3b4127SPyun YongHyeon 	else
2035ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2036ea3b4127SPyun YongHyeon 
203795d67482SBill Paul 	/* Turn on DMA, clear stats */
2038ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
203995d67482SBill Paul 
204095d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
204195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
204295d67482SBill Paul 
204395d67482SBill Paul #ifdef notdef
204495d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
204595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
204695d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
204795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
204895d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
204995d67482SBill Paul #endif
205095d67482SBill Paul 
205195d67482SBill Paul 	/* Turn on DMA completion state machine */
20527ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
205395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
205495d67482SBill Paul 
20556f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
20566f8718a3SScott Long 
20576f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2058a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
20593889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
20606f8718a3SScott Long 
20617aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
20627aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
20637aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
20647aa4b937SPyun YongHyeon 
206595d67482SBill Paul 	/* Turn on write DMA state machine */
20666f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
20674f09c4c7SMarius Strobl 	DELAY(40);
206895d67482SBill Paul 
206995d67482SBill Paul 	/* Turn on read DMA state machine */
20704f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
20711108273aSPyun YongHyeon 
20721108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
20731108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
20741108273aSPyun YongHyeon 
2075a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2076a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2077a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2078a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2079a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2080a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
20814f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
20824f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
20831108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2084ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
20851108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
20861108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
208755a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
208855a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
208955a24a05SPyun YongHyeon 	}
209050515680SPyun YongHyeon 
2091e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
209250515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
209350515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2094e3215f76SPyun YongHyeon 		/*
2095e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2096e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2097e3215f76SPyun YongHyeon 		 */
2098e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2099e3215f76SPyun YongHyeon 	}
210050515680SPyun YongHyeon 
2101d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2102d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2103d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
21041108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
21051108273aSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc)) {
2106bbe2ca75SPyun YongHyeon 		dmactl = CSR_READ_4(sc, BGE_RDMA_RSRVCTRL);
2107bbe2ca75SPyun YongHyeon 		/*
2108bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2109bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2110bbe2ca75SPyun YongHyeon 		 */
211150515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
211250515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2113bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2114bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2115bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2116bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2117bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2118bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2119bbe2ca75SPyun YongHyeon 		}
2120d255f2a9SPyun YongHyeon 		/*
2121d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2122d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2123d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2124d255f2a9SPyun YongHyeon 		 */
2125bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL, dmactl |
2126d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2127d255f2a9SPyun YongHyeon 	}
2128bbe2ca75SPyun YongHyeon 
2129e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2130bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2131bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2132bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2133bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2134e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2135e3215f76SPyun YongHyeon 		/*
2136e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2137e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2138e3215f76SPyun YongHyeon 		 */
2139e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2140e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2141e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2142e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2143bbe2ca75SPyun YongHyeon 	}
2144bbe2ca75SPyun YongHyeon 
21454f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
21464f09c4c7SMarius Strobl 	DELAY(40);
214795d67482SBill Paul 
214895d67482SBill Paul 	/* Turn on RX data completion state machine */
214995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
215095d67482SBill Paul 
215195d67482SBill Paul 	/* Turn on RX BD initiator state machine */
215295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
215395d67482SBill Paul 
215495d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
215595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
215695d67482SBill Paul 
215795d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
21587ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
215995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
216095d67482SBill Paul 
216195d67482SBill Paul 	/* Turn on send BD completion state machine */
216295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
216395d67482SBill Paul 
216495d67482SBill Paul 	/* Turn on send data completion state machine */
2165a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2166a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2167a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2168a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
216995d67482SBill Paul 
217095d67482SBill Paul 	/* Turn on send data initiator state machine */
21711108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
21721108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
21731108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2174ca3f1187SPyun YongHyeon 	else
217595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
217695d67482SBill Paul 
217795d67482SBill Paul 	/* Turn on send BD initiator state machine */
217895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
217995d67482SBill Paul 
218095d67482SBill Paul 	/* Turn on send BD selector state machine */
218195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
218295d67482SBill Paul 
21830c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
218495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
218595d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
218695d67482SBill Paul 
218795d67482SBill Paul 	/* ack/clear link change events */
218895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
21890434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
21900434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2191f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
219295d67482SBill Paul 
21936ede2cfaSPyun YongHyeon 	/*
21946ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
21956ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
21966ede2cfaSPyun YongHyeon 	 */
2197652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
219895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2199a1d52896SBill Paul 	} else {
22007ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
22017ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
22027ed3f0f0SPyun YongHyeon 			DELAY(80);
22037ed3f0f0SPyun YongHyeon 		}
22041f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
22054c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2206a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2207a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2208a1d52896SBill Paul 	}
220995d67482SBill Paul 
22101f313773SOleg Bulyzhin 	/*
22111f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
22121f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
22131f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
22141f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
22151f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
22161f313773SOleg Bulyzhin 	 */
22171f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
22181f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
22191f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
22201f313773SOleg Bulyzhin 
222195d67482SBill Paul 	/* Enable link state change attentions. */
222295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
222395d67482SBill Paul 
222495d67482SBill Paul 	return (0);
222595d67482SBill Paul }
222695d67482SBill Paul 
22274c0da0ffSGleb Smirnoff const struct bge_revision *
22284c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
22294c0da0ffSGleb Smirnoff {
22304c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
22314c0da0ffSGleb Smirnoff 
22324c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
22334c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
22344c0da0ffSGleb Smirnoff 			return (br);
22354c0da0ffSGleb Smirnoff 	}
22364c0da0ffSGleb Smirnoff 
22374c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
22384c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
22394c0da0ffSGleb Smirnoff 			return (br);
22404c0da0ffSGleb Smirnoff 	}
22414c0da0ffSGleb Smirnoff 
22424c0da0ffSGleb Smirnoff 	return (NULL);
22434c0da0ffSGleb Smirnoff }
22444c0da0ffSGleb Smirnoff 
22454c0da0ffSGleb Smirnoff const struct bge_vendor *
22464c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
22474c0da0ffSGleb Smirnoff {
22484c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
22494c0da0ffSGleb Smirnoff 
22504c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
22514c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
22524c0da0ffSGleb Smirnoff 			return (v);
22534c0da0ffSGleb Smirnoff 
22544c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
22554c0da0ffSGleb Smirnoff 	return (NULL);
22564c0da0ffSGleb Smirnoff }
22574c0da0ffSGleb Smirnoff 
225895d67482SBill Paul /*
225995d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
22604c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
22614c0da0ffSGleb Smirnoff  *
22624c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
22637c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
22647c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
22657c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
22667c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
226795d67482SBill Paul  */
226895d67482SBill Paul static int
22693f74909aSGleb Smirnoff bge_probe(device_t dev)
227095d67482SBill Paul {
2271978f2704SMarius Strobl 	char buf[96];
2272978f2704SMarius Strobl 	char model[64];
2273978f2704SMarius Strobl 	const struct bge_revision *br;
2274978f2704SMarius Strobl 	const char *pname;
22754c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
2276978f2704SMarius Strobl 	const struct bge_type *t = bge_devs;
2277978f2704SMarius Strobl 	const struct bge_vendor *v;
2278978f2704SMarius Strobl 	uint32_t id;
2279978f2704SMarius Strobl 	uint16_t did, vid;
228095d67482SBill Paul 
228195d67482SBill Paul 	sc->bge_dev = dev;
22827c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
22837c929cf9SJung-uk Kim 	did = pci_get_device(dev);
22844c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
22857c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2286a5779553SStanislav Sedov 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2287a5779553SStanislav Sedov 			    BGE_PCIMISCCTL_ASICREV_SHIFT;
22881108273aSPyun YongHyeon 			if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
22891108273aSPyun YongHyeon 				/*
22901108273aSPyun YongHyeon 				 * Find the ASCI revision.  Different chips
22911108273aSPyun YongHyeon 				 * use different registers.
22921108273aSPyun YongHyeon 				 */
22931108273aSPyun YongHyeon 				switch (pci_get_device(dev)) {
22941108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5717:
22951108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5718:
2296bbe2ca75SPyun YongHyeon 				case BCOM_DEVICEID_BCM5719:
229750515680SPyun YongHyeon 				case BCOM_DEVICEID_BCM5720:
22981108273aSPyun YongHyeon 					id = pci_read_config(dev,
22991108273aSPyun YongHyeon 					    BGE_PCI_GEN2_PRODID_ASICREV, 4);
23001108273aSPyun YongHyeon 					break;
2301b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57761:
2302b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57765:
2303b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57781:
2304b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57785:
2305b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57791:
2306b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57795:
2307b4a256acSPyun YongHyeon 					id = pci_read_config(dev,
2308b4a256acSPyun YongHyeon 					    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2309b4a256acSPyun YongHyeon 					break;
23101108273aSPyun YongHyeon 				default:
2311a5779553SStanislav Sedov 					id = pci_read_config(dev,
2312a5779553SStanislav Sedov 					    BGE_PCI_PRODID_ASICREV, 4);
23131108273aSPyun YongHyeon 				}
23141108273aSPyun YongHyeon 			}
23154c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
23167c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
2317852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2318852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
23194e35d186SJung-uk Kim 				snprintf(model, 64, "%s", pname);
23204e35d186SJung-uk Kim 			else
2321978f2704SMarius Strobl 				snprintf(model, 64, "%s %s", v->v_name,
23227c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
23237c929cf9SJung-uk Kim 				    "NetXtreme Ethernet Controller");
2324a5779553SStanislav Sedov 			snprintf(buf, 96, "%s, %sASIC rev. %#08x", model,
2325a5779553SStanislav Sedov 			    br != NULL ? "" : "unknown ", id);
23264c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
232795d67482SBill Paul 			return (0);
232895d67482SBill Paul 		}
232995d67482SBill Paul 		t++;
233095d67482SBill Paul 	}
233195d67482SBill Paul 
233295d67482SBill Paul 	return (ENXIO);
233395d67482SBill Paul }
233495d67482SBill Paul 
2335f41ac2beSBill Paul static void
23363f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2337f41ac2beSBill Paul {
2338f41ac2beSBill Paul 	int i;
2339f41ac2beSBill Paul 
23403f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2341f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2342f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
23430ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2344f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2345f41ac2beSBill Paul 	}
2346943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2347943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2348943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2349f41ac2beSBill Paul 
23503f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2351f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2352f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2353f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2354f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2355f41ac2beSBill Paul 	}
2356943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2357943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2358943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2359f41ac2beSBill Paul 
23603f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2361f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2362f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
23630ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2364f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2365f41ac2beSBill Paul 	}
2366f41ac2beSBill Paul 
23670ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
23680ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2369c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2370c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
23710ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
23720ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2373f41ac2beSBill Paul 
23743f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2375e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2376e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2377e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2378e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2379f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2380f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2381f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2382f41ac2beSBill Paul 
2383f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2384f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2385f41ac2beSBill Paul 
23863f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2387e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2388e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2389e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2390e65bed95SPyun YongHyeon 
2391e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2392e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2393f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2394f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2395f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2396f41ac2beSBill Paul 
2397f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2398f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2399f41ac2beSBill Paul 
24003f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2401e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2402e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2403e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2404e65bed95SPyun YongHyeon 
2405e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2406e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2407f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2408f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2409f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2410f41ac2beSBill Paul 
2411f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2412f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2413f41ac2beSBill Paul 
24143f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2415e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2416e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2417e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2418e65bed95SPyun YongHyeon 
2419e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2420f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2421f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2422f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2423f41ac2beSBill Paul 
2424f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2425f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2426f41ac2beSBill Paul 
24273f74909aSGleb Smirnoff 	/* Destroy status block. */
2428e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2429e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2430e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2431e65bed95SPyun YongHyeon 
2432e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2433f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2434f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2435f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2436f41ac2beSBill Paul 
2437f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2438f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2439f41ac2beSBill Paul 
24403f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2441e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2442e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2443e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2444e65bed95SPyun YongHyeon 
2445e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2446f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2447f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2448f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2449f41ac2beSBill Paul 
2450f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2451f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2452f41ac2beSBill Paul 
24535b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
24545b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
24555b610048SPyun YongHyeon 
24563f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2457f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2458f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2459f41ac2beSBill Paul }
2460f41ac2beSBill Paul 
2461f41ac2beSBill Paul static int
24625b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
24635b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
24645b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2465f41ac2beSBill Paul {
24663f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
24675b610048SPyun YongHyeon 	int error;
2468f41ac2beSBill Paul 
24695b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2470fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
24715b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
24725b610048SPyun YongHyeon 	if (error != 0) {
24735b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
24745b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
24755b610048SPyun YongHyeon 		return (ENOMEM);
24765b610048SPyun YongHyeon 	}
24775b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
24785b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
24795b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
24805b610048SPyun YongHyeon 	if (error != 0) {
24815b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
24825b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
24835b610048SPyun YongHyeon 		return (ENOMEM);
24845b610048SPyun YongHyeon 	}
24855b610048SPyun YongHyeon 	/* Load the address of the ring. */
24865b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
24875b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
24885b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
24895b610048SPyun YongHyeon 	if (error != 0) {
24905b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
24915b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
24925b610048SPyun YongHyeon 		return (ENOMEM);
24935b610048SPyun YongHyeon 	}
24945b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
24955b610048SPyun YongHyeon 	return (0);
24965b610048SPyun YongHyeon }
24975b610048SPyun YongHyeon 
24985b610048SPyun YongHyeon static int
24995b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
25005b610048SPyun YongHyeon {
25015b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2502fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
25035b610048SPyun YongHyeon 	int i, error;
2504f41ac2beSBill Paul 
2505f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2506f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2507f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2508f41ac2beSBill Paul 	/*
2509f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2510f41ac2beSBill Paul 	 */
25114eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2512f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
25134eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
25144eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2515e65bed95SPyun YongHyeon 	if (error != 0) {
2516fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2517fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2518e65bed95SPyun YongHyeon 		return (ENOMEM);
2519e65bed95SPyun YongHyeon 	}
2520e65bed95SPyun YongHyeon 
25215b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
25225b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
25235b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
25245b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
25255b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
25265b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
25275b610048SPyun YongHyeon 	if (error)
25285b610048SPyun YongHyeon 		return (error);
25295b610048SPyun YongHyeon 
25305b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
25315b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
25325b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
25335b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
25345b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
25355b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
25365b610048SPyun YongHyeon 	if (error)
25375b610048SPyun YongHyeon 		return (error);
25385b610048SPyun YongHyeon 
25395b610048SPyun YongHyeon 	/* Create tag for TX ring. */
25405b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
25415b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
25425b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
25435b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
25445b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
25455b610048SPyun YongHyeon 	if (error)
25465b610048SPyun YongHyeon 		return (error);
25475b610048SPyun YongHyeon 
2548f41ac2beSBill Paul 	/*
25495b610048SPyun YongHyeon 	 * Create tag for status block.
25505b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
25515b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
25525b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
25535b610048SPyun YongHyeon 	 * of configured number of ring.
2554f41ac2beSBill Paul 	 */
25555b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
25565b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
25575b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
25585b610048SPyun YongHyeon 	else
25595b610048SPyun YongHyeon 		sbsz = 32;
25605b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
25615b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
25625b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
25635b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
25645b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
25655b610048SPyun YongHyeon 	if (error)
25665b610048SPyun YongHyeon 		return (error);
25675b610048SPyun YongHyeon 
256812c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
256912c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
257012c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
257112c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
257212c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
257312c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
257412c65daeSPyun YongHyeon 	if (error)
257512c65daeSPyun YongHyeon 		return (error);
257612c65daeSPyun YongHyeon 
25775b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
25785b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
25795b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
25805b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
25815b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
25825b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
25835b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
25845b610048SPyun YongHyeon 		if (error)
25855b610048SPyun YongHyeon 			return (error);
25865b610048SPyun YongHyeon 	}
25875b610048SPyun YongHyeon 
25885b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
2589d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
2590d2ffe15aSPyun YongHyeon 		/*
2591d2ffe15aSPyun YongHyeon 		 * XXX
2592d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
2593d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
2594062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
2595062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
2596d2ffe15aSPyun YongHyeon 		 */
2597062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
2598d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
2599d2ffe15aSPyun YongHyeon 	}
2600fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
2601fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
2602fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
2603fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
26045b610048SPyun YongHyeon 	if (error != 0) {
26055b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
26065b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
26075b610048SPyun YongHyeon 		return (ENOMEM);
26085b610048SPyun YongHyeon 	}
26095b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
26101108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2611ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
2612ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
2613ca3f1187SPyun YongHyeon 	} else {
2614ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
2615ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
2616ca3f1187SPyun YongHyeon 	}
26175b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
2618ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2619ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
2620ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
2621f41ac2beSBill Paul 
2622f41ac2beSBill Paul 	if (error) {
26230ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
26240ac56796SPyun YongHyeon 		return (ENOMEM);
26250ac56796SPyun YongHyeon 	}
26260ac56796SPyun YongHyeon 
26275b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
2628f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
2629f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
2630f5459d4cSPyun YongHyeon 	else
2631f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
26325b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
2633f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
2634f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
26350ac56796SPyun YongHyeon 
26360ac56796SPyun YongHyeon 	if (error) {
26370ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
2638f41ac2beSBill Paul 		return (ENOMEM);
2639f41ac2beSBill Paul 	}
2640f41ac2beSBill Paul 
26413f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
2642943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2643943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
2644943787f3SPyun YongHyeon 	if (error) {
2645943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
2646943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
2647943787f3SPyun YongHyeon 		return (ENOMEM);
2648943787f3SPyun YongHyeon 	}
2649f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
26500ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2651f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
2652f41ac2beSBill Paul 		if (error) {
2653fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
2654fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
2655f41ac2beSBill Paul 			return (ENOMEM);
2656f41ac2beSBill Paul 		}
2657f41ac2beSBill Paul 	}
2658f41ac2beSBill Paul 
26593f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
2660f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
26610ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
2662f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
2663f41ac2beSBill Paul 		if (error) {
2664fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
26650ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
2666f41ac2beSBill Paul 			return (ENOMEM);
2667f41ac2beSBill Paul 		}
2668f41ac2beSBill Paul 	}
2669f41ac2beSBill Paul 
26705b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
26714c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
26725b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
26738a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
26741be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
26751be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
2676f41ac2beSBill Paul 		if (error) {
2677fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
26783f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
2679f41ac2beSBill Paul 			return (ENOMEM);
2680f41ac2beSBill Paul 		}
26813f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
2682943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2683943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
2684943787f3SPyun YongHyeon 		if (error) {
2685943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
26861b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
2687943787f3SPyun YongHyeon 			return (ENOMEM);
2688943787f3SPyun YongHyeon 		}
2689f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2690f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2691f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2692f41ac2beSBill Paul 			if (error) {
2693fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
26943f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
2695f41ac2beSBill Paul 				return (ENOMEM);
2696f41ac2beSBill Paul 			}
2697f41ac2beSBill Paul 		}
2698f41ac2beSBill Paul 	}
2699f41ac2beSBill Paul 
2700f41ac2beSBill Paul 	return (0);
2701f41ac2beSBill Paul }
2702f41ac2beSBill Paul 
2703bf6ef57aSJohn Polstra /*
2704bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2705bf6ef57aSJohn Polstra  */
2706bf6ef57aSJohn Polstra static int
2707bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2708bf6ef57aSJohn Polstra {
2709bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
271055aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
2711bf6ef57aSJohn Polstra 
271255aaf894SMarius Strobl 	d = pci_get_domain(dev);
2713bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2714bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2715bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2716bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
271755aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2718bf6ef57aSJohn Polstra 			return (1);
2719bf6ef57aSJohn Polstra 	return (0);
2720bf6ef57aSJohn Polstra }
2721bf6ef57aSJohn Polstra 
2722bf6ef57aSJohn Polstra /*
2723bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2724bf6ef57aSJohn Polstra  */
2725bf6ef57aSJohn Polstra static int
2726bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2727bf6ef57aSJohn Polstra {
2728bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2729bf6ef57aSJohn Polstra 
2730d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
27315c952e8dSPyun YongHyeon 		return (0);
27325c952e8dSPyun YongHyeon 
27331108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
27341108273aSPyun YongHyeon #ifdef DEVICE_POLLING
27351108273aSPyun YongHyeon 	return (0);
27361108273aSPyun YongHyeon #endif
2737bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2738a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
2739bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2740bf6ef57aSJohn Polstra 		/*
2741a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
2742a8376f70SMarius Strobl 		 * configured in single-port mode.
2743bf6ef57aSJohn Polstra 		 */
2744bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2745bf6ef57aSJohn Polstra 			can_use_msi = 1;
2746bf6ef57aSJohn Polstra 		break;
2747bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2748bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2749bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2750bf6ef57aSJohn Polstra 			can_use_msi = 1;
2751bf6ef57aSJohn Polstra 		break;
2752a8376f70SMarius Strobl 	default:
2753a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
2754bf6ef57aSJohn Polstra 			can_use_msi = 1;
2755bf6ef57aSJohn Polstra 	}
2756bf6ef57aSJohn Polstra 	return (can_use_msi);
2757bf6ef57aSJohn Polstra }
2758bf6ef57aSJohn Polstra 
275995d67482SBill Paul static int
2760062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
2761062af0b0SPyun YongHyeon {
2762062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
2763062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
2764062af0b0SPyun YongHyeon 		const uint16_t vendor;
2765062af0b0SPyun YongHyeon 		const uint16_t device;
2766062af0b0SPyun YongHyeon 		const char *desc;
2767062af0b0SPyun YongHyeon 	} const mbox_reorder_lists[] = {
2768062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
2769062af0b0SPyun YongHyeon 	};
2770062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
2771062af0b0SPyun YongHyeon 	device_t bus, dev;
277247f4a4dcSMarius Strobl 	int i;
2773062af0b0SPyun YongHyeon 
2774062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
2775062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
2776062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
2777062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
2778062af0b0SPyun YongHyeon 	for (;;) {
2779062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
2780062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
2781062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
2782062af0b0SPyun YongHyeon 			break;
278347f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
2784062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
2785062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
2786062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
2787062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
2788062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
2789062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
2790062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
2791062af0b0SPyun YongHyeon 				return (1);
2792062af0b0SPyun YongHyeon 			}
2793062af0b0SPyun YongHyeon 		}
2794062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
2795062af0b0SPyun YongHyeon 			break;
2796062af0b0SPyun YongHyeon 	}
2797062af0b0SPyun YongHyeon 	return (0);
2798062af0b0SPyun YongHyeon }
2799062af0b0SPyun YongHyeon 
2800ea9c3a30SPyun YongHyeon static void
2801ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
2802ea9c3a30SPyun YongHyeon {
2803ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
2804ea9c3a30SPyun YongHyeon 
2805ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
2806ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
2807ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
2808ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
2809ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
2810ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
2811ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
2812ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
2813ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
2814ea9c3a30SPyun YongHyeon 			clk = 133;
2815ea9c3a30SPyun YongHyeon 		else {
2816ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
2817ea9c3a30SPyun YongHyeon 			switch (clk) {
2818ea9c3a30SPyun YongHyeon 			case 0:
2819ea9c3a30SPyun YongHyeon 				clk = 33;
2820ea9c3a30SPyun YongHyeon 				break;
2821ea9c3a30SPyun YongHyeon 			case 2:
2822ea9c3a30SPyun YongHyeon 				clk = 50;
2823ea9c3a30SPyun YongHyeon 				break;
2824ea9c3a30SPyun YongHyeon 			case 4:
2825ea9c3a30SPyun YongHyeon 				clk = 66;
2826ea9c3a30SPyun YongHyeon 				break;
2827ea9c3a30SPyun YongHyeon 			case 6:
2828ea9c3a30SPyun YongHyeon 				clk = 100;
2829ea9c3a30SPyun YongHyeon 				break;
2830ea9c3a30SPyun YongHyeon 			case 7:
2831ea9c3a30SPyun YongHyeon 				clk = 133;
2832ea9c3a30SPyun YongHyeon 				break;
2833ea9c3a30SPyun YongHyeon 			}
2834ea9c3a30SPyun YongHyeon 		}
2835ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
2836ea9c3a30SPyun YongHyeon 	} else {
2837ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
2838ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
2839ea9c3a30SPyun YongHyeon 		else
2840ea9c3a30SPyun YongHyeon 			printf("PCI ");
2841ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
2842ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
2843ea9c3a30SPyun YongHyeon 			clk = 66;
2844ea9c3a30SPyun YongHyeon 		else
2845ea9c3a30SPyun YongHyeon 			clk = 33;
2846ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
2847ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
2848ea9c3a30SPyun YongHyeon 		else
2849ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
2850ea9c3a30SPyun YongHyeon 	}
2851ea9c3a30SPyun YongHyeon }
2852ea9c3a30SPyun YongHyeon 
2853062af0b0SPyun YongHyeon static int
28543f74909aSGleb Smirnoff bge_attach(device_t dev)
285595d67482SBill Paul {
285695d67482SBill Paul 	struct ifnet *ifp;
285795d67482SBill Paul 	struct bge_softc *sc;
28584f0794ffSBjoern A. Zeeb 	uint32_t hwcfg = 0, misccfg;
285908013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
2860fb772a6cSMarius Strobl 	int capmask, error, f, msicount, phy_addr, reg, rid, trys;
286195d67482SBill Paul 
286295d67482SBill Paul 	sc = device_get_softc(dev);
286395d67482SBill Paul 	sc->bge_dev = dev;
286495d67482SBill Paul 
2865dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
2866dfe0df9aSPyun YongHyeon 
286795d67482SBill Paul 	/*
286895d67482SBill Paul 	 * Map control/status registers.
286995d67482SBill Paul 	 */
287095d67482SBill Paul 	pci_enable_busmaster(dev);
287195d67482SBill Paul 
2872736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
28735f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
287444f8f2fcSMarius Strobl 	    RF_ACTIVE);
287595d67482SBill Paul 
287695d67482SBill Paul 	if (sc->bge_res == NULL) {
2877fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
287895d67482SBill Paul 		error = ENXIO;
287995d67482SBill Paul 		goto fail;
288095d67482SBill Paul 	}
288195d67482SBill Paul 
28824f09c4c7SMarius Strobl 	/* Save various chip information. */
2883e53d81eeSPaul Saab 	sc->bge_chipid =
2884a5779553SStanislav Sedov 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2885a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
28861108273aSPyun YongHyeon 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG) {
28871108273aSPyun YongHyeon 		/*
28881108273aSPyun YongHyeon 		 * Find the ASCI revision.  Different chips use different
28891108273aSPyun YongHyeon 		 * registers.
28901108273aSPyun YongHyeon 		 */
28911108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
28921108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
28931108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2894bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
289550515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
28961108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
28971108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
28981108273aSPyun YongHyeon 			break;
2899b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2900b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2901b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
2902b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
2903b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2904b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2905b4a256acSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
2906b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2907b4a256acSPyun YongHyeon 			break;
29081108273aSPyun YongHyeon 		default:
29091108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
29101108273aSPyun YongHyeon 			    BGE_PCI_PRODID_ASICREV, 4);
29111108273aSPyun YongHyeon 		}
29121108273aSPyun YongHyeon 	}
2913e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2914e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2915e53d81eeSPaul Saab 
2916a813ed78SPyun YongHyeon 	/* Set default PHY address. */
29178e5d93dbSMarius Strobl 	phy_addr = 1;
29181108273aSPyun YongHyeon 	 /*
29191108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
29201108273aSPyun YongHyeon 	  *
29211108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
29221108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
29231108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
29241108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
29251108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
2926bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
292750515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
29281108273aSPyun YongHyeon 	  *
29291108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
29301108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
29311108273aSPyun YongHyeon 	  */
2932bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
293350515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
293450515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
29351108273aSPyun YongHyeon 		f = pci_get_function(dev);
29361108273aSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5717_A0) {
29371108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
29381108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
29391108273aSPyun YongHyeon 				phy_addr = f + 8;
29401108273aSPyun YongHyeon 			else
29411108273aSPyun YongHyeon 				phy_addr = f + 1;
2942bbe2ca75SPyun YongHyeon 		} else {
29431108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
29441108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
29451108273aSPyun YongHyeon 				phy_addr = f + 8;
29461108273aSPyun YongHyeon 			else
29471108273aSPyun YongHyeon 				phy_addr = f + 1;
29481108273aSPyun YongHyeon 		}
29491108273aSPyun YongHyeon 	}
2950a813ed78SPyun YongHyeon 
295186543395SJung-uk Kim 	/*
295238cc658fSJohn Baldwin 	 * Don't enable Ethernet@WireSpeed for the 5700, 5906, or the
295386543395SJung-uk Kim 	 * 5705 A0 and A1 chips.
295486543395SJung-uk Kim 	 */
2955cb777a07SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
2956cb777a07SPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
2957cb777a07SPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
2958cb777a07SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)) ||
2959cb777a07SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5906)
2960cb777a07SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
296186543395SJung-uk Kim 
29625fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
29635fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
296408013fd3SMarius Strobl 
29650dae9719SJung-uk Kim 	/* Save chipset family. */
29660dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
29671108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
2968bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
296950515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
2970b4a256acSPyun YongHyeon 	case BGE_ASICREV_BCM57765:
29711108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
29721108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
2973b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
2974bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
2975bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
2976bbe2ca75SPyun YongHyeon 			/* Jumbo frame on BCM5719 A0 does not work. */
2977463a7e27SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_JUMBO;
2978bbe2ca75SPyun YongHyeon 		}
29791108273aSPyun YongHyeon 		break;
2980a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
2981a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
2982a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
2983a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
2984a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
2985a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
2986a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
2987a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
2988a5779553SStanislav Sedov 		break;
29890dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
29900dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
29910dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
29920dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
29937ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
29940dae9719SJung-uk Kim 		break;
29950dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
29960dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
29970dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
2998f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
29999fe569d8SXin LI 		/* FALLTHROUGH */
30000dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
30010dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
300238cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
30030dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
30049fe569d8SXin LI 		/* FALLTHROUGH */
30050dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
30060dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
30070dae9719SJung-uk Kim 		break;
30080dae9719SJung-uk Kim 	}
30090dae9719SJung-uk Kim 
3010749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3011749a5269SMarius Strobl 	bge_add_sysctls(sc);
3012749a5269SMarius Strobl 
3013757402fbSPyun YongHyeon 	/* Set various PHY bug flags. */
30141ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
30151ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
3016757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
30175ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
30185ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
3019757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
30205ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
3021757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
30224150ce6fSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
3023757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
3024eea8956aSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
3025eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
30261108273aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
3027bbe2ca75SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5719 &&
302850515680SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5720 &&
3029eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3030b4a256acSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57765 &&
3031eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780) {
30325ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
3033a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3034a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
30354fcf220bSJohn Baldwin 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
3036f7d1b2ebSXin LI 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
3037f7d1b2ebSXin LI 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
3038757402fbSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
3039eea8956aSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
3040eea8956aSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
3041eea8956aSPyun YongHyeon 		} else
3042757402fbSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
30435ee49a3aSJung-uk Kim 	}
30445ee49a3aSJung-uk Kim 
3045a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
30461108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
30471108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3048a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3049a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3050a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3051a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3052a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3053a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3054a813ed78SPyun YongHyeon 	else
3055a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
30567ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
30577ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
30587ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3059a813ed78SPyun YongHyeon 
3060f681b29aSPyun YongHyeon 	/*
3061d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3062f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3063f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3064f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3065f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3066f681b29aSPyun YongHyeon 	 */
3067f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
30684f0794ffSBjoern A. Zeeb 
3069d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3070d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3071d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3072d9820cd8SPyun YongHyeon 
3073a7fcfcf3SPyun YongHyeon 	/*
3074a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3075a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3076a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3077a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3078a7fcfcf3SPyun YongHyeon 	 */
3079a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3080a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3081a7fcfcf3SPyun YongHyeon 
3082ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3083fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
30844f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
30854f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
30864f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
308784ac96f8SPyun YongHyeon 	}
30884f0794ffSBjoern A. Zeeb 
3089fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3090fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3091fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3092fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3093fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3094fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3095fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3096fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3097fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3098fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3099fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3100fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3101fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3102fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3103fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3104fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3105fb772a6cSMarius Strobl 	}
3106fb772a6cSMarius Strobl 
3107e53d81eeSPaul Saab 	/*
3108ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3109ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3110ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3111ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3112ca3f1187SPyun YongHyeon 	 * known bug which can't handle TSO if ethernet header + IP/TCP
3113ca3f1187SPyun YongHyeon 	 * header is greater than 80 bytes. The workaround for the TSO
3114ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3115ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3116ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3117ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3118ca3f1187SPyun YongHyeon 	 */
31191108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
31201108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
31211108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3122bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3123bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3124bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3125bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3126bbe2ca75SPyun YongHyeon 		}
31271108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
31284f4a16e1SPyun YongHyeon 		/*
31294f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
31304f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3131be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
31324f4a16e1SPyun YongHyeon 		 */
31334f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3134be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3135be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3136ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
31374f4a16e1SPyun YongHyeon 	}
3138ca3f1187SPyun YongHyeon 
3139ca3f1187SPyun YongHyeon 	/*
31406f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3141e53d81eeSPaul Saab 	 */
31423b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
31434c0da0ffSGleb Smirnoff 		/*
31446f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
31456f8718a3SScott Long 		 * must be a PCI Express device.
31466f8718a3SScott Long 		 */
31476f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
31480aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
314950515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
315050515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
3151bbe2ca75SPyun YongHyeon 			pci_set_max_read_req(dev, 2048);
3152bbe2ca75SPyun YongHyeon 		else if (pci_get_max_read_req(dev) != 4096)
3153d2b6e9a0SPyun YongHyeon 			pci_set_max_read_req(dev, 4096);
31546f8718a3SScott Long 	} else {
31556f8718a3SScott Long 		/*
31566f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
31576f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
31584c0da0ffSGleb Smirnoff 		 */
31593b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
31600aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
316190447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
31624c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3163652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
31646f8718a3SScott Long 	}
31654c0da0ffSGleb Smirnoff 
3166bf6ef57aSJohn Polstra 	/*
3167fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3168fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3169fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3170fd4d32feSPyun YongHyeon 	 */
3171fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3172fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3173fd4d32feSPyun YongHyeon 	/*
3174062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3175062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3176062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3177062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3178062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3179062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3180062af0b0SPyun YongHyeon 	 */
3181062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3182062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3183062af0b0SPyun YongHyeon 	/*
3184bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3185bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3186bf6ef57aSJohn Polstra 	 * normal operation.
3187bf6ef57aSJohn Polstra 	 */
31880aaf1057SPyun YongHyeon 	rid = 0;
31893b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
31900aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3191bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
3192bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
3193bf6ef57aSJohn Polstra 			if (msicount > 1)
3194bf6ef57aSJohn Polstra 				msicount = 1;
3195bf6ef57aSJohn Polstra 		} else
3196bf6ef57aSJohn Polstra 			msicount = 0;
3197bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
3198bf6ef57aSJohn Polstra 			rid = 1;
3199bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
32000aaf1057SPyun YongHyeon 		}
32010aaf1057SPyun YongHyeon 	}
3202bf6ef57aSJohn Polstra 
32031108273aSPyun YongHyeon 	/*
32041108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
32051108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
32061108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
32071108273aSPyun YongHyeon 	 */
32081108273aSPyun YongHyeon #ifndef DEVICE_POLLING
32091108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
32101108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
32111108273aSPyun YongHyeon #endif
32121108273aSPyun YongHyeon 
3213bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3214bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
3215bf6ef57aSJohn Polstra 
3216bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3217bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3218bf6ef57aSJohn Polstra 		error = ENXIO;
3219bf6ef57aSJohn Polstra 		goto fail;
3220bf6ef57aSJohn Polstra 	}
3221bf6ef57aSJohn Polstra 
3222ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
32234f09c4c7SMarius Strobl 
3224bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3225bf6ef57aSJohn Polstra 
322695d67482SBill Paul 	/* Try to reset the chip. */
32278cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
32288cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
32298cb1383cSDoug Ambrisko 		error = ENXIO;
32308cb1383cSDoug Ambrisko 		goto fail;
32318cb1383cSDoug Ambrisko 	}
32328cb1383cSDoug Ambrisko 
32338cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3234888b47f0SPyun YongHyeon 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3235888b47f0SPyun YongHyeon 	    BGE_SRAM_DATA_SIG_MAGIC)) {
3236888b47f0SPyun YongHyeon 		if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG)
32378cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
32388cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
32398cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
3240d67eba2fSPyun YongHyeon 			if (BGE_IS_575X_PLUS(sc))
32418cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
32428cb1383cSDoug Ambrisko 		}
32438cb1383cSDoug Ambrisko 	}
32448cb1383cSDoug Ambrisko 
32458cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
32468cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
32478cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
32488cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
32498cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
32508cb1383cSDoug Ambrisko 		error = ENXIO;
32518cb1383cSDoug Ambrisko 		goto fail;
32528cb1383cSDoug Ambrisko 	}
32538cb1383cSDoug Ambrisko 
32548cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
32558cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
325695d67482SBill Paul 
325795d67482SBill Paul 	if (bge_chipinit(sc)) {
3258fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
325995d67482SBill Paul 		error = ENXIO;
326095d67482SBill Paul 		goto fail;
326195d67482SBill Paul 	}
326295d67482SBill Paul 
326338cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
326438cc658fSJohn Baldwin 	if (error) {
326508013fd3SMarius Strobl 		device_printf(sc->bge_dev,
326608013fd3SMarius Strobl 		    "failed to read station address\n");
326795d67482SBill Paul 		error = ENXIO;
326895d67482SBill Paul 		goto fail;
326995d67482SBill Paul 	}
327095d67482SBill Paul 
3271f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
32721108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
32731108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
32741108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3275f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3276f41ac2beSBill Paul 	else
3277f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3278f41ac2beSBill Paul 
32795b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3280fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3281fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3282f41ac2beSBill Paul 		error = ENXIO;
3283f41ac2beSBill Paul 		goto fail;
3284f41ac2beSBill Paul 	}
3285f41ac2beSBill Paul 
328695d67482SBill Paul 	/* Set default tuneable values. */
328795d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
328895d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
328995d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
32906f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
32916f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
329295d67482SBill Paul 
329335f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
329435f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
329535f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
329635f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
329735f945cdSPyun YongHyeon 
329895d67482SBill Paul 	/* Set up ifnet structure */
3299fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3300fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3301fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3302fc74a9f9SBrooks Davis 		error = ENXIO;
3303fc74a9f9SBrooks Davis 		goto fail;
3304fc74a9f9SBrooks Davis 	}
330595d67482SBill Paul 	ifp->if_softc = sc;
33069bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
330795d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
330895d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
330995d67482SBill Paul 	ifp->if_start = bge_start;
331095d67482SBill Paul 	ifp->if_init = bge_init;
33114d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
33124d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
33134d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
331435f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
3315d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
33164e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
33171108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3318ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
331904bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
3320ca3f1187SPyun YongHyeon 	}
33214e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
33224e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
33234e35d186SJung-uk Kim #endif
332495d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
332575719184SGleb Smirnoff #ifdef DEVICE_POLLING
332675719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
332775719184SGleb Smirnoff #endif
332895d67482SBill Paul 
3329a1d52896SBill Paul 	/*
3330d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3331d375e524SGleb Smirnoff 	 * to hardware bugs.
3332d375e524SGleb Smirnoff 	 */
3333d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3334d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
33354d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
3336d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
3337d375e524SGleb Smirnoff 	}
3338d375e524SGleb Smirnoff 
3339d375e524SGleb Smirnoff 	/*
3340a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
334141abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
334241abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
334341abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
334441abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
334541abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
334641abcc1bSPaul Saab 	 * SK-9D41.
3347a1d52896SBill Paul 	 */
3348888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3349888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
33505fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
33515fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3352f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3353f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3354fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3355f6789fbaSPyun YongHyeon 			error = ENXIO;
3356f6789fbaSPyun YongHyeon 			goto fail;
3357f6789fbaSPyun YongHyeon 		}
335841abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
335941abcc1bSPaul Saab 	}
336041abcc1bSPaul Saab 
336195d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3362ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3363ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
3364ea3b4127SPyun YongHyeon 		if (BGE_IS_5714_FAMILY(sc))
3365ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
3366ea3b4127SPyun YongHyeon 		else
3367652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3368ea3b4127SPyun YongHyeon 	}
336995d67482SBill Paul 
3370652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
33710c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
33720c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
33730c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
33746098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
33756098821cSJung-uk Kim 		    0, NULL);
337695d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
337795d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3378da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
337995d67482SBill Paul 	} else {
338095d67482SBill Paul 		/*
33818cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
33828cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
33838cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
33848cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
33858cb1383cSDoug Ambrisko 		 * the PHY.
338695d67482SBill Paul 		 */
33874012d104SMarius Strobl 		trys = 0;
33888cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
33898cb1383cSDoug Ambrisko again:
33908cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
33918cb1383cSDoug Ambrisko 
3392fb772a6cSMarius Strobl 		error = mii_attach(dev, &sc->bge_miibus, ifp, bge_ifmedia_upd,
3393fb772a6cSMarius Strobl 		    bge_ifmedia_sts, capmask, phy_addr, MII_OFFSET_ANY,
3394fb772a6cSMarius Strobl 		    MIIF_DOPAUSE);
33958e5d93dbSMarius Strobl 		if (error != 0) {
33968cb1383cSDoug Ambrisko 			if (trys++ < 4) {
33978cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
33984e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
33994e35d186SJung-uk Kim 				    BMCR_RESET);
34008cb1383cSDoug Ambrisko 				goto again;
34018cb1383cSDoug Ambrisko 			}
34028e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
340395d67482SBill Paul 			goto fail;
340495d67482SBill Paul 		}
34058cb1383cSDoug Ambrisko 
34068cb1383cSDoug Ambrisko 		/*
34078cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
34088cb1383cSDoug Ambrisko 		 */
34098cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
34108cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
341195d67482SBill Paul 	}
341295d67482SBill Paul 
341395d67482SBill Paul 	/*
3414e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3415e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3416e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3417e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3418e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3419e255b776SJohn Polstra 	 * payloads by copying the received packets.
3420e255b776SJohn Polstra 	 */
3421652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3422652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3423652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3424e255b776SJohn Polstra 
3425e255b776SJohn Polstra 	/*
342695d67482SBill Paul 	 * Call MI attach routine.
342795d67482SBill Paul 	 */
3428fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
3429b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
34300f9bd73bSSam Leffler 
343161ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
343261ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
343361ccb9daSPyun YongHyeon 
34340f9bd73bSSam Leffler 	/*
34350f9bd73bSSam Leffler 	 * Hookup IRQ last.
34360f9bd73bSSam Leffler 	 */
3437dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3438dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
34397e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
34407e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3441dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3442dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3443dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3444dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3445dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3446dfe0df9aSPyun YongHyeon 			error = ENXIO;
3447dfe0df9aSPyun YongHyeon 			goto fail;
3448dfe0df9aSPyun YongHyeon 		}
3449dfe0df9aSPyun YongHyeon 		taskqueue_start_threads(&sc->bge_tq, 1, PI_NET, "%s taskq",
3450dfe0df9aSPyun YongHyeon 		    device_get_nameunit(sc->bge_dev));
3451dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3452dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3453dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3454dfe0df9aSPyun YongHyeon 		if (error)
3455dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3456dfe0df9aSPyun YongHyeon 	} else
3457dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3458dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3459dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
34600f9bd73bSSam Leffler 
34610f9bd73bSSam Leffler 	if (error) {
3462fc74a9f9SBrooks Davis 		bge_detach(dev);
3463fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
34640f9bd73bSSam Leffler 	}
346595d67482SBill Paul 
346608013fd3SMarius Strobl 	return (0);
346708013fd3SMarius Strobl 
346895d67482SBill Paul fail:
346908013fd3SMarius Strobl 	bge_release_resources(sc);
347008013fd3SMarius Strobl 
347195d67482SBill Paul 	return (error);
347295d67482SBill Paul }
347395d67482SBill Paul 
347495d67482SBill Paul static int
34753f74909aSGleb Smirnoff bge_detach(device_t dev)
347695d67482SBill Paul {
347795d67482SBill Paul 	struct bge_softc *sc;
347895d67482SBill Paul 	struct ifnet *ifp;
347995d67482SBill Paul 
348095d67482SBill Paul 	sc = device_get_softc(dev);
3481fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
348295d67482SBill Paul 
348375719184SGleb Smirnoff #ifdef DEVICE_POLLING
348475719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
348575719184SGleb Smirnoff 		ether_poll_deregister(ifp);
348675719184SGleb Smirnoff #endif
348775719184SGleb Smirnoff 
34880f9bd73bSSam Leffler 	BGE_LOCK(sc);
348995d67482SBill Paul 	bge_stop(sc);
349095d67482SBill Paul 	bge_reset(sc);
34910f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
34920f9bd73bSSam Leffler 
34935dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
34945dda8085SOleg Bulyzhin 
3495dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3496dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
34970f9bd73bSSam Leffler 	ether_ifdetach(ifp);
349895d67482SBill Paul 
3499652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
350095d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
350195d67482SBill Paul 	} else {
350295d67482SBill Paul 		bus_generic_detach(dev);
350395d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
350495d67482SBill Paul 	}
350595d67482SBill Paul 
350695d67482SBill Paul 	bge_release_resources(sc);
350795d67482SBill Paul 
350895d67482SBill Paul 	return (0);
350995d67482SBill Paul }
351095d67482SBill Paul 
351195d67482SBill Paul static void
35123f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
351395d67482SBill Paul {
351495d67482SBill Paul 	device_t dev;
351595d67482SBill Paul 
351695d67482SBill Paul 	dev = sc->bge_dev;
351795d67482SBill Paul 
3518dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3519dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3520dfe0df9aSPyun YongHyeon 
352195d67482SBill Paul 	if (sc->bge_intrhand != NULL)
352295d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
352395d67482SBill Paul 
352495d67482SBill Paul 	if (sc->bge_irq != NULL)
3525724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3526724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3527724bd939SJohn Polstra 
3528724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3529724bd939SJohn Polstra 		pci_release_msi(dev);
353095d67482SBill Paul 
353195d67482SBill Paul 	if (sc->bge_res != NULL)
353295d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3533736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
353495d67482SBill Paul 
3535ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3536ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3537ad61f896SRuslan Ermilov 
3538f41ac2beSBill Paul 	bge_dma_free(sc);
353995d67482SBill Paul 
35400f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
35410f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
354295d67482SBill Paul }
354395d67482SBill Paul 
35448cb1383cSDoug Ambrisko static int
35453f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
354695d67482SBill Paul {
354795d67482SBill Paul 	device_t dev;
35485fea260fSMarius Strobl 	uint32_t cachesize, command, pcistate, reset, val;
35496f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
35500aaf1057SPyun YongHyeon 	uint16_t devctl;
35515fea260fSMarius Strobl 	int i;
355295d67482SBill Paul 
355395d67482SBill Paul 	dev = sc->bge_dev;
355495d67482SBill Paul 
355538cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
355638cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
35576f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
35586f8718a3SScott Long 			write_op = bge_writemem_direct;
35596f8718a3SScott Long 		else
35606f8718a3SScott Long 			write_op = bge_writemem_ind;
35619ba784dbSScott Long 	} else
35626f8718a3SScott Long 		write_op = bge_writereg_ind;
35636f8718a3SScott Long 
356495d67482SBill Paul 	/* Save some important PCI state. */
356595d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
356695d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
356795d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
356895d67482SBill Paul 
356995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
357095d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3571e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
357295d67482SBill Paul 
35736f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
35746f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
3575a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
35766f8718a3SScott Long 		if (bootverbose)
3577333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
35786f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
35796f8718a3SScott Long 	}
35806f8718a3SScott Long 
35816f8718a3SScott Long 	/*
35826f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
35836f8718a3SScott Long 	 * When firmware finishes its initialization it will
3584888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
35856f8718a3SScott Long 	 */
3586888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
35876f8718a3SScott Long 
35880c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
3589e53d81eeSPaul Saab 
3590e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3591652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
35920c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
35930c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
3594e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
3595e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
35960c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
35970c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
3598e53d81eeSPaul Saab 		}
3599e53d81eeSPaul Saab 	}
3600e53d81eeSPaul Saab 
360121c9e407SDavid Christensen 	/*
36026f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
36036f8718a3SScott Long 	 * powered up in D0 uninitialized.
36046f8718a3SScott Long 	 */
36055512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
36065512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
3607caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
36086f8718a3SScott Long 
360995d67482SBill Paul 	/* Issue global reset */
36106f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
361195d67482SBill Paul 
361238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
36135fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
361438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
36155fea260fSMarius Strobl 		    val | BGE_VCPU_STATUS_DRV_RESET);
36165fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
361738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
36185fea260fSMarius Strobl 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
361938cc658fSJohn Baldwin 	}
362038cc658fSJohn Baldwin 
362195d67482SBill Paul 	DELAY(1000);
362295d67482SBill Paul 
3623e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3624652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
3625e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
3626e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
36275fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
36285fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
3629e53d81eeSPaul Saab 		}
36300aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
3631389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
36320aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
3633389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
3634389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
3635389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
36360aaf1057SPyun YongHyeon 		    devctl, 2);
36370aaf1057SPyun YongHyeon 		/* Clear error status. */
3638389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
3639389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
3640389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
3641389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
3642e53d81eeSPaul Saab 	}
3643e53d81eeSPaul Saab 
36443f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
364595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
364695d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3647e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
364895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
364995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
36500c8aa4eaSJung-uk Kim 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
3651cbb2b2feSPyun YongHyeon 	/*
3652cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
3653fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
3654cbb2b2feSPyun YongHyeon 	 * read stale status block.
3655cbb2b2feSPyun YongHyeon 	 */
3656cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
3657cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
3658cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
3659cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
3660cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
3661cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
3662cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3663cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3664cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
3665cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
3666cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3667cbb2b2feSPyun YongHyeon 		}
3668cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
3669cbb2b2feSPyun YongHyeon 		    devctl, 2);
3670cbb2b2feSPyun YongHyeon 	}
367122a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
36724c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
3673bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
3674bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
36750aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
36760aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
36770aaf1057SPyun YongHyeon 			pci_write_config(dev,
36780aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
3679bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
3680bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
3681bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
3682bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
3683bf6ef57aSJohn Polstra 		}
36844c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
36854c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
36864c0da0ffSGleb Smirnoff 	} else
3687a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3688a7b0c314SPaul Saab 
368938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
369038cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
369138cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
369238cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
369338cc658fSJohn Baldwin 				break;
369438cc658fSJohn Baldwin 			DELAY(100);
369538cc658fSJohn Baldwin 		}
369638cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
3697333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
369838cc658fSJohn Baldwin 			return (1);
369938cc658fSJohn Baldwin 		}
370038cc658fSJohn Baldwin 	} else {
370195d67482SBill Paul 		/*
37026f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
370308013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
37045fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
37055fea260fSMarius Strobl 		 * address is fitted though.
370695d67482SBill Paul 		 */
370795d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
3708d5d23857SJung-uk Kim 			DELAY(10);
3709888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
3710888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
371195d67482SBill Paul 				break;
371295d67482SBill Paul 		}
371395d67482SBill Paul 
37145fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
3715333704a3SPyun YongHyeon 			device_printf(dev,
3716333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
3717333704a3SPyun YongHyeon 			    val);
3718b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
3719b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
3720b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
372138cc658fSJohn Baldwin 	}
372295d67482SBill Paul 
372395d67482SBill Paul 	/*
372495d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
372595d67482SBill Paul 	 * return to its original pre-reset state. This is a
372695d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
372795d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
372895d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
372995d67482SBill Paul 	 * results.
373095d67482SBill Paul 	 */
373195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
373295d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
373395d67482SBill Paul 			break;
373495d67482SBill Paul 		DELAY(10);
373595d67482SBill Paul 	}
373695d67482SBill Paul 
37373f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
373850515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
373995d67482SBill Paul 
37408cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
37418cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
37428cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
37438cb1383cSDoug Ambrisko 
374495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
374595d67482SBill Paul 
3746da3003f0SBill Paul 	/*
3747da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
3748da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
3749da3003f0SBill Paul 	 * to 1.2V.
3750da3003f0SBill Paul 	 */
3751652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
3752652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
37535fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
37545fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
37555fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
3756da3003f0SBill Paul 	}
3757da3003f0SBill Paul 
3758e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3759652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
3760b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
3761a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
3762a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
3763a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
37645fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
37655fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
3766e53d81eeSPaul Saab 	}
376795d67482SBill Paul 	DELAY(10000);
37688cb1383cSDoug Ambrisko 
376950515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
377050515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
377150515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
377250515680SPyun YongHyeon 
37738cb1383cSDoug Ambrisko 	return (0);
377495d67482SBill Paul }
377595d67482SBill Paul 
3776e0b7b101SPyun YongHyeon static __inline void
3777e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
3778e0b7b101SPyun YongHyeon {
3779e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
3780e0b7b101SPyun YongHyeon 
3781e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
3782e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
3783e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
3784e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3785e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3786e0b7b101SPyun YongHyeon }
3787e0b7b101SPyun YongHyeon 
3788e0b7b101SPyun YongHyeon static __inline void
3789e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
3790e0b7b101SPyun YongHyeon {
3791e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
3792e0b7b101SPyun YongHyeon 
3793e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
3794e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
3795e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
3796e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
3797e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
3798e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
3799e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3800e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3801e0b7b101SPyun YongHyeon }
3802e0b7b101SPyun YongHyeon 
380395d67482SBill Paul /*
380495d67482SBill Paul  * Frame reception handling. This is called if there's a frame
380595d67482SBill Paul  * on the receive return list.
380695d67482SBill Paul  *
380795d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
38081be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
380995d67482SBill Paul  * 2) the frame is from the standard receive ring
381095d67482SBill Paul  */
381195d67482SBill Paul 
38121abcdbd1SAttilio Rao static int
3813dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
381495d67482SBill Paul {
381595d67482SBill Paul 	struct ifnet *ifp;
38161abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
3817b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
381895d67482SBill Paul 
38197f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
38200f9bd73bSSam Leffler 
38213f74909aSGleb Smirnoff 	/* Nothing to do. */
38227f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
38231abcdbd1SAttilio Rao 		return (rx_npkts);
3824cfcb5025SOleg Bulyzhin 
3825fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
382695d67482SBill Paul 
3827f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
3828e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
3829f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
383015eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
3831f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
3832f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
3833c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
3834f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
383515eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
3836f41ac2beSBill Paul 
38377f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
383895d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
38393f74909aSGleb Smirnoff 		uint32_t		rxidx;
384095d67482SBill Paul 		struct mbuf		*m = NULL;
38413f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
384295d67482SBill Paul 		int			have_tag = 0;
384395d67482SBill Paul 
384475719184SGleb Smirnoff #ifdef DEVICE_POLLING
384575719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
384675719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
384775719184SGleb Smirnoff 				break;
384875719184SGleb Smirnoff 			sc->rxcycles--;
384975719184SGleb Smirnoff 		}
385075719184SGleb Smirnoff #endif
385175719184SGleb Smirnoff 
38527f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
385395d67482SBill Paul 
385495d67482SBill Paul 		rxidx = cur_rx->bge_idx;
38557f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
385695d67482SBill Paul 
3857cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
3858cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
385995d67482SBill Paul 			have_tag = 1;
386095d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
386195d67482SBill Paul 		}
386295d67482SBill Paul 
386395d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
386495d67482SBill Paul 			jumbocnt++;
3865943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
386695d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3867e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
386895d67482SBill Paul 				continue;
386995d67482SBill Paul 			}
3870943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
3871e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
3872943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
387395d67482SBill Paul 				continue;
387495d67482SBill Paul 			}
387503e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
387695d67482SBill Paul 		} else {
387795d67482SBill Paul 			stdcnt++;
3878e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
387995d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3880e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
388195d67482SBill Paul 				continue;
388295d67482SBill Paul 			}
3883943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
3884e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
3885943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
388695d67482SBill Paul 				continue;
388795d67482SBill Paul 			}
388803e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
388995d67482SBill Paul 		}
389095d67482SBill Paul 
389195d67482SBill Paul 		ifp->if_ipackets++;
3892e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3893e255b776SJohn Polstra 		/*
3894e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
3895e65bed95SPyun YongHyeon 		 * the payload is aligned.
3896e255b776SJohn Polstra 		 */
3897652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
3898e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
3899e255b776SJohn Polstra 			    cur_rx->bge_len);
3900e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
3901e255b776SJohn Polstra 		}
3902e255b776SJohn Polstra #endif
3903473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
390495d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
390595d67482SBill Paul 
39061108273aSPyun YongHyeon 		if (ifp->if_capenable & IFCAP_RXCSUM)
39071108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
390895d67482SBill Paul 
390995d67482SBill Paul 		/*
3910673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
3911673d9191SSam Leffler 		 * attach that information to the packet.
391295d67482SBill Paul 		 */
3913d147662cSGleb Smirnoff 		if (have_tag) {
391478ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
391578ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
3916d147662cSGleb Smirnoff 		}
391795d67482SBill Paul 
3918dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
39190f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
3920673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
39210f9bd73bSSam Leffler 			BGE_LOCK(sc);
3922dfe0df9aSPyun YongHyeon 		} else
3923dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
3924d4da719cSAttilio Rao 		rx_npkts++;
392525e13e68SXin LI 
392625e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
39278cf7d13dSAttilio Rao 			return (rx_npkts);
392895d67482SBill Paul 	}
392995d67482SBill Paul 
393015eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
393115eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
3932e65bed95SPyun YongHyeon 	if (stdcnt > 0)
3933f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3934e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
39354c0da0ffSGleb Smirnoff 
3936c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
3937f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
39384c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3939f41ac2beSBill Paul 
39407f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
394138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
394295d67482SBill Paul 	if (stdcnt)
3943767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
3944767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
394595d67482SBill Paul 	if (jumbocnt)
3946767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
3947767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
3948f5a034f9SPyun YongHyeon #ifdef notyet
3949f5a034f9SPyun YongHyeon 	/*
3950f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
3951f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
3952f5a034f9SPyun YongHyeon 	 */
3953f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
3954f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3955f5a034f9SPyun YongHyeon #endif
39561abcdbd1SAttilio Rao 	return (rx_npkts);
395795d67482SBill Paul }
395895d67482SBill Paul 
395995d67482SBill Paul static void
39601108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
39611108273aSPyun YongHyeon {
39621108273aSPyun YongHyeon 
39631108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
39641108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
39651108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
39661108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
39671108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
39681108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
39691108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
39701108273aSPyun YongHyeon 			}
39711108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
39721108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
39731108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
39741108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
39751108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
39761108273aSPyun YongHyeon 			}
39771108273aSPyun YongHyeon 		}
39781108273aSPyun YongHyeon 	} else {
39791108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
39801108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
39811108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
39821108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
39831108273aSPyun YongHyeon 		}
39841108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
39851108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
39861108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
39871108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
39881108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
39891108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
39901108273aSPyun YongHyeon 		}
39911108273aSPyun YongHyeon 	}
39921108273aSPyun YongHyeon }
39931108273aSPyun YongHyeon 
39941108273aSPyun YongHyeon static void
3995b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
399695d67482SBill Paul {
399795a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
399895d67482SBill Paul 	struct ifnet *ifp;
399995d67482SBill Paul 
40000f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
40010f9bd73bSSam Leffler 
40023f74909aSGleb Smirnoff 	/* Nothing to do. */
4003b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4004cfcb5025SOleg Bulyzhin 		return;
4005cfcb5025SOleg Bulyzhin 
4006fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
400795d67482SBill Paul 
4008e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
40095c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
401095d67482SBill Paul 	/*
401195d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
401295d67482SBill Paul 	 * frames that have been sent.
401395d67482SBill Paul 	 */
4014b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
401595a0a340SPyun YongHyeon 		uint32_t		idx;
401695d67482SBill Paul 
401795d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4018f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
401995d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
402095d67482SBill Paul 			ifp->if_opackets++;
402195d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
40220ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4023e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4024e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
40250ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4026f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4027e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4028e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
402995d67482SBill Paul 		}
403095d67482SBill Paul 		sc->bge_txcnt--;
403195d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
403295d67482SBill Paul 	}
403395d67482SBill Paul 
403413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
40355b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
40365b01e77cSBruce Evans 		sc->bge_timer = 0;
403795d67482SBill Paul }
403895d67482SBill Paul 
403975719184SGleb Smirnoff #ifdef DEVICE_POLLING
40401abcdbd1SAttilio Rao static int
404175719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
404275719184SGleb Smirnoff {
404375719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
4044b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4045366454f2SOleg Bulyzhin 	uint32_t statusword;
40461abcdbd1SAttilio Rao 	int rx_npkts = 0;
404775719184SGleb Smirnoff 
40483f74909aSGleb Smirnoff 	BGE_LOCK(sc);
40493f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
40503f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
40511abcdbd1SAttilio Rao 		return (rx_npkts);
40523f74909aSGleb Smirnoff 	}
405375719184SGleb Smirnoff 
4054dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4055b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4056b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4057b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4058b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4059dab5cd05SOleg Bulyzhin 
4060175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
4061175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4062dab5cd05SOleg Bulyzhin 
4063dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4064b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4065b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4066366454f2SOleg Bulyzhin 
40670c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4068366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4069366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4070366454f2SOleg Bulyzhin 
4071366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4072366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
40734c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4074652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4075366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4076366454f2SOleg Bulyzhin 
4077366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4078dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
407925e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
408025e13e68SXin LI 		BGE_UNLOCK(sc);
40818cf7d13dSAttilio Rao 		return (rx_npkts);
408225e13e68SXin LI 	}
4083b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4084366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4085366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
40863f74909aSGleb Smirnoff 
40873f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
40881abcdbd1SAttilio Rao 	return (rx_npkts);
408975719184SGleb Smirnoff }
409075719184SGleb Smirnoff #endif /* DEVICE_POLLING */
409175719184SGleb Smirnoff 
4092dfe0df9aSPyun YongHyeon static int
4093dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4094dfe0df9aSPyun YongHyeon {
4095dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4096dfe0df9aSPyun YongHyeon 
4097dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4098dfe0df9aSPyun YongHyeon 	/*
4099dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4100dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4101dfe0df9aSPyun YongHyeon 	 */
4102dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4103dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4104dfe0df9aSPyun YongHyeon }
4105dfe0df9aSPyun YongHyeon 
4106dfe0df9aSPyun YongHyeon static void
4107dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4108dfe0df9aSPyun YongHyeon {
4109dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4110dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
41111108273aSPyun YongHyeon 	uint32_t status, status_tag;
4112dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4113dfe0df9aSPyun YongHyeon 
4114dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4115dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4116dfe0df9aSPyun YongHyeon 
411766151edfSPyun YongHyeon 	BGE_LOCK(sc);
411866151edfSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
411966151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4120dfe0df9aSPyun YongHyeon 		return;
412166151edfSPyun YongHyeon 	}
4122dfe0df9aSPyun YongHyeon 
4123dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4124dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4125dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4126dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4127dfe0df9aSPyun YongHyeon 
4128dfe0df9aSPyun YongHyeon 	/* Save producer/consumer indexess. */
4129dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4130dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4131dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
41321108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
4133dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4134dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4135dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4136dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
41371108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
41381108273aSPyun YongHyeon 		status_tag = 0;
413966151edfSPyun YongHyeon 
414066151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
414166151edfSPyun YongHyeon 		bge_link_upd(sc);
414266151edfSPyun YongHyeon 
4143dfe0df9aSPyun YongHyeon 	/* Let controller work. */
41441108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4145dfe0df9aSPyun YongHyeon 
414666151edfSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
414766151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4148dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
414966151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4150dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
415166151edfSPyun YongHyeon 		BGE_LOCK(sc);
4152dfe0df9aSPyun YongHyeon 	}
4153dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4154dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4155dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4156dfe0df9aSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4157dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4158dfe0df9aSPyun YongHyeon 	}
415966151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4160dfe0df9aSPyun YongHyeon }
4161dfe0df9aSPyun YongHyeon 
416295d67482SBill Paul static void
41633f74909aSGleb Smirnoff bge_intr(void *xsc)
416495d67482SBill Paul {
416595d67482SBill Paul 	struct bge_softc *sc;
416695d67482SBill Paul 	struct ifnet *ifp;
4167dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4168b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
416995d67482SBill Paul 
417095d67482SBill Paul 	sc = xsc;
4171f41ac2beSBill Paul 
41720f9bd73bSSam Leffler 	BGE_LOCK(sc);
41730f9bd73bSSam Leffler 
4174dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4175dab5cd05SOleg Bulyzhin 
417675719184SGleb Smirnoff #ifdef DEVICE_POLLING
417775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
417875719184SGleb Smirnoff 		BGE_UNLOCK(sc);
417975719184SGleb Smirnoff 		return;
418075719184SGleb Smirnoff 	}
418175719184SGleb Smirnoff #endif
418275719184SGleb Smirnoff 
4183f30cbfc6SScott Long 	/*
4184b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4185b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4186b848e032SBruce Evans 	 * our current organization this just gives complications and
4187b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4188b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4189b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4190b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4191b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4192b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4193b848e032SBruce Evans 	 *
4194b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4195b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4196b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4197b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4198b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4199b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4200b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4201b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4202b848e032SBruce Evans 	 */
420338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4204b848e032SBruce Evans 
4205f584dfd1SPyun YongHyeon 	/*
4206f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4207f584dfd1SPyun YongHyeon 	 */
4208f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4209f584dfd1SPyun YongHyeon 
4210f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4211f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4212f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4213f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4214f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4215f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4216f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4217f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4218f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4219f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4220f584dfd1SPyun YongHyeon 
42211f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
42224c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4223f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4224dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
422595d67482SBill Paul 
422613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
42273f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4228dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
422925e13e68SXin LI 	}
423095d67482SBill Paul 
423125e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
42323f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4233b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
423495d67482SBill Paul 	}
423595d67482SBill Paul 
423613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
423713f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
42380f9bd73bSSam Leffler 		bge_start_locked(ifp);
42390f9bd73bSSam Leffler 
42400f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
424195d67482SBill Paul }
424295d67482SBill Paul 
424395d67482SBill Paul static void
42448cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
42458cb1383cSDoug Ambrisko {
42468cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
42478cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
42488cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
42498cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
42508cb1383cSDoug Ambrisko 		else {
4251899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4252888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
42533c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4254888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4255941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4256941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
42573fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
42589931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
42599931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
42608cb1383cSDoug Ambrisko 		}
42618cb1383cSDoug Ambrisko 	}
42628cb1383cSDoug Ambrisko }
42638cb1383cSDoug Ambrisko 
42648cb1383cSDoug Ambrisko static void
4265b74e67fbSGleb Smirnoff bge_tick(void *xsc)
42660f9bd73bSSam Leffler {
4267b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
426895d67482SBill Paul 	struct mii_data *mii = NULL;
426995d67482SBill Paul 
42700f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
427195d67482SBill Paul 
42725dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
42735dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
42745dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
42755dda8085SOleg Bulyzhin 		return;
42765dda8085SOleg Bulyzhin 
42777ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
42780434d1b8SBill Paul 		bge_stats_update_regs(sc);
42790434d1b8SBill Paul 	else
428095d67482SBill Paul 		bge_stats_update(sc);
428195d67482SBill Paul 
4282652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
428395d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
428482b67c01SOleg Bulyzhin 		/*
428582b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
428682b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
428782b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
428882b67c01SOleg Bulyzhin 		 */
428982b67c01SOleg Bulyzhin 		if (!sc->bge_link)
429095d67482SBill Paul 			mii_tick(mii);
42917b97099dSOleg Bulyzhin 	} else {
42927b97099dSOleg Bulyzhin 		/*
42937b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
42947b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
42957b97099dSOleg Bulyzhin 		 * and trigger interrupt.
42967b97099dSOleg Bulyzhin 		 */
42977b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
42983f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
42997b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
43007b97099dSOleg Bulyzhin #endif
43017b97099dSOleg Bulyzhin 		{
43027b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
43034f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
43044f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
43057b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
43064f0794ffSBjoern A. Zeeb 		else
43074f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
43087b97099dSOleg Bulyzhin 		}
4309dab5cd05SOleg Bulyzhin 	}
431095d67482SBill Paul 
43118cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4312b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
43138cb1383cSDoug Ambrisko 
4314dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
431595d67482SBill Paul }
431695d67482SBill Paul 
431795d67482SBill Paul static void
43183f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
43190434d1b8SBill Paul {
43203f74909aSGleb Smirnoff 	struct ifnet *ifp;
43212280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
43220434d1b8SBill Paul 
4323fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
43242280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
43250434d1b8SBill Paul 
43262280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
43272280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
43282280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
43292280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
43302280c16bSPyun YongHyeon 	stats->outXonSent +=
43312280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
43322280c16bSPyun YongHyeon 	stats->outXoffSent +=
43332280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
43342280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
43352280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
43362280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
43372280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
43382280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
43392280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
43402280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
43412280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
43422280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
43432280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
43442280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
43452280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
43462280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
43472280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
43482280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
43492280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
43502280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
43512280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
43527e6e2507SJung-uk Kim 
43532280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
43542280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
43552280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
43562280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
43572280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
43582280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
43592280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
43602280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
43612280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
43622280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
43632280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
43642280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
43652280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
43662280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
43672280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
43682280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
43692280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
43702280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
43712280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
43722280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
43732280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
43742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
43752280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
43762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
43772280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
43782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
43792280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
43802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
43812280c16bSPyun YongHyeon 
43822280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
43832280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
43842280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
43852280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
43862280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
43872280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
43882280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
43892280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4390f78094a5SPyun YongHyeon 	/*
4391f78094a5SPyun YongHyeon 	 * XXX
4392f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4393f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4394f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4395f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4396f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4397f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4398f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4399f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4400f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4401f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4402f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4403f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4404f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4405f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4406f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4407f78094a5SPyun YongHyeon 	 * silicon bug.
4408f78094a5SPyun YongHyeon 	 */
4409f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4410f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4411f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
44122280c16bSPyun YongHyeon 		stats->InputDiscards +=
44132280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
44142280c16bSPyun YongHyeon 	stats->InputErrors +=
44152280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
44162280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
44172280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
44182280c16bSPyun YongHyeon 
44192280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
44202280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
44212280c16bSPyun YongHyeon 	    stats->InputErrors);
44222280c16bSPyun YongHyeon }
44232280c16bSPyun YongHyeon 
44242280c16bSPyun YongHyeon static void
44252280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
44262280c16bSPyun YongHyeon {
44272280c16bSPyun YongHyeon 
44282280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
44292280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
44302280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
44312280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
44322280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
44332280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
44342280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
44352280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
44362280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
44372280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
44382280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
44392280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
44402280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
44412280c16bSPyun YongHyeon 
44422280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
44432280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
44442280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
44452280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
44462280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
44472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
44482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
44492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
44502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
44512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
44522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
44532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
44542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
44552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
44562280c16bSPyun YongHyeon 
44572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
44582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
44592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
44602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
44612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
44622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
44632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
44640434d1b8SBill Paul }
44650434d1b8SBill Paul 
44660434d1b8SBill Paul static void
44673f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
446895d67482SBill Paul {
446995d67482SBill Paul 	struct ifnet *ifp;
4470e907febfSPyun YongHyeon 	bus_size_t stats;
44717e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
447295d67482SBill Paul 
4473fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
447495d67482SBill Paul 
4475e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4476e907febfSPyun YongHyeon 
4477e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4478e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
447995d67482SBill Paul 
44808634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
44816b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
44826fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
44836fb34dd2SOleg Bulyzhin 
448437ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
448537ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_nobds);
448637ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
448737ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
448837ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_inerrs);
448937ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
44906fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
44916b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
44926fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
44936fb34dd2SOleg Bulyzhin 
44946fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
44956b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
44966fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
449795d67482SBill Paul 
4498e907febfSPyun YongHyeon #undef	READ_STAT
449995d67482SBill Paul }
450095d67482SBill Paul 
450195d67482SBill Paul /*
4502d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
4503d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
4504d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
4505d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
4506d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
4507d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
4508d375e524SGleb Smirnoff  */
4509d375e524SGleb Smirnoff static __inline int
4510d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
4511d375e524SGleb Smirnoff {
4512d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
4513d375e524SGleb Smirnoff 	struct mbuf *last;
4514d375e524SGleb Smirnoff 
4515d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
4516d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
4517d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
4518d375e524SGleb Smirnoff 		last = m;
4519d375e524SGleb Smirnoff 	} else {
4520d375e524SGleb Smirnoff 		/*
4521d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
4522d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
4523d375e524SGleb Smirnoff 		 */
4524d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
4525d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
4526d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
4527d375e524SGleb Smirnoff 			struct mbuf *n;
4528d375e524SGleb Smirnoff 
4529d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
4530d375e524SGleb Smirnoff 			if (n == NULL)
4531d375e524SGleb Smirnoff 				return (ENOBUFS);
4532d375e524SGleb Smirnoff 			n->m_len = 0;
4533d375e524SGleb Smirnoff 			last->m_next = n;
4534d375e524SGleb Smirnoff 			last = n;
4535d375e524SGleb Smirnoff 		}
4536d375e524SGleb Smirnoff 	}
4537d375e524SGleb Smirnoff 
4538d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
4539d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
4540d375e524SGleb Smirnoff 	last->m_len += padlen;
4541d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
4542d375e524SGleb Smirnoff 
4543d375e524SGleb Smirnoff 	return (0);
4544d375e524SGleb Smirnoff }
4545d375e524SGleb Smirnoff 
4546ca3f1187SPyun YongHyeon static struct mbuf *
4547d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
4548d598b626SPyun YongHyeon {
4549d598b626SPyun YongHyeon 	struct mbuf *n;
4550d598b626SPyun YongHyeon 	int found;
4551d598b626SPyun YongHyeon 
4552d598b626SPyun YongHyeon 	/*
4553d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
4554d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
4555d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
4556d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
4557d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
4558d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
4559d598b626SPyun YongHyeon 	 */
4560d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
4561d598b626SPyun YongHyeon 		if (n->m_len < 8) {
4562d598b626SPyun YongHyeon 			found++;
4563d598b626SPyun YongHyeon 			if (found > 1)
4564d598b626SPyun YongHyeon 				break;
4565d598b626SPyun YongHyeon 			continue;
4566d598b626SPyun YongHyeon 		}
4567d598b626SPyun YongHyeon 		found = 0;
4568d598b626SPyun YongHyeon 	}
4569d598b626SPyun YongHyeon 
4570d598b626SPyun YongHyeon 	if (found > 1) {
4571d598b626SPyun YongHyeon 		n = m_defrag(m, M_DONTWAIT);
4572d598b626SPyun YongHyeon 		if (n == NULL)
4573d598b626SPyun YongHyeon 			m_freem(m);
4574d598b626SPyun YongHyeon 	} else
4575d598b626SPyun YongHyeon 		n = m;
4576d598b626SPyun YongHyeon 	return (n);
4577d598b626SPyun YongHyeon }
4578d598b626SPyun YongHyeon 
4579d598b626SPyun YongHyeon static struct mbuf *
45801108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
45811108273aSPyun YongHyeon     uint16_t *flags)
4582ca3f1187SPyun YongHyeon {
4583ca3f1187SPyun YongHyeon 	struct ip *ip;
4584ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
4585ca3f1187SPyun YongHyeon 	struct mbuf *n;
4586ca3f1187SPyun YongHyeon 	uint16_t hlen;
45875b355c4fSPyun YongHyeon 	uint32_t poff;
4588ca3f1187SPyun YongHyeon 
4589ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
4590ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
4591ca3f1187SPyun YongHyeon 		n = m_dup(m, M_DONTWAIT);
4592ca3f1187SPyun YongHyeon 		m_freem(m);
4593ca3f1187SPyun YongHyeon 		if (n == NULL)
4594ca3f1187SPyun YongHyeon 			return (NULL);
4595ca3f1187SPyun YongHyeon 		m = n;
4596ca3f1187SPyun YongHyeon 	}
45975b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
4598ca3f1187SPyun YongHyeon 	if (m == NULL)
4599ca3f1187SPyun YongHyeon 		return (NULL);
46005b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
46015b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
4602ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
4603ca3f1187SPyun YongHyeon 	if (m == NULL)
4604ca3f1187SPyun YongHyeon 		return (NULL);
4605ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
46065b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
4607ca3f1187SPyun YongHyeon 	if (m == NULL)
4608ca3f1187SPyun YongHyeon 		return (NULL);
4609ca3f1187SPyun YongHyeon 	/*
4610ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
4611ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
4612ca3f1187SPyun YongHyeon 	 */
4613ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
461496486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
4615ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
4616ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
4617ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
461896486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
4619ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
4620ca3f1187SPyun YongHyeon 	/*
4621ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
4622ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
4623ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
46241108273aSPyun YongHyeon 	 * we only support hardware based TSO.
4625ca3f1187SPyun YongHyeon 	 */
46261108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
4627ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
46281108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
46291108273aSPyun YongHyeon 		/*
46301108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
46311108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
46321108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
46331108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
46341108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
46351108273aSPyun YongHyeon 		 * frames are supported.
46361108273aSPyun YongHyeon 		 */
46371108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
46381108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
46391108273aSPyun YongHyeon 	} else {
46401108273aSPyun YongHyeon 		/*
46411108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
46421108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
46431108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
46441108273aSPyun YongHyeon 		 * supported.
46451108273aSPyun YongHyeon 		 */
4646ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
46471108273aSPyun YongHyeon 	}
4648ca3f1187SPyun YongHyeon 	return (m);
4649ca3f1187SPyun YongHyeon }
4650ca3f1187SPyun YongHyeon 
4651d375e524SGleb Smirnoff /*
465295d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
465395d67482SBill Paul  * pointers to descriptors.
465495d67482SBill Paul  */
465595d67482SBill Paul static int
4656676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
465795d67482SBill Paul {
46587e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
4659f41ac2beSBill Paul 	bus_dmamap_t		map;
4660676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
4661676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
46627e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
4663ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
46647e27542aSGleb Smirnoff 	int			nsegs, i, error;
466595d67482SBill Paul 
46666909dc43SGleb Smirnoff 	csum_flags = 0;
4667ca3f1187SPyun YongHyeon 	mss = 0;
4668ca3f1187SPyun YongHyeon 	vlan_tag = 0;
4669d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
4670d598b626SPyun YongHyeon 	    m->m_next != NULL) {
4671d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
4672d598b626SPyun YongHyeon 		if (*m_head == NULL)
4673d598b626SPyun YongHyeon 			return (ENOBUFS);
4674d598b626SPyun YongHyeon 		m = *m_head;
4675d598b626SPyun YongHyeon 	}
4676ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
46771108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
4678ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
4679ca3f1187SPyun YongHyeon 			return (ENOBUFS);
4680ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
4681ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
468235f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
46836909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
46846909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
46856909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
46866909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
46876909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
46886909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
46896909dc43SGleb Smirnoff 				m_freem(m);
46906909dc43SGleb Smirnoff 				*m_head = NULL;
46916909dc43SGleb Smirnoff 				return (error);
46926909dc43SGleb Smirnoff 			}
46936909dc43SGleb Smirnoff 		}
46946909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
46956909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
46966909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
46976909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
46986909dc43SGleb Smirnoff 	}
46996909dc43SGleb Smirnoff 
47001108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
47011108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
47021108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
47031108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
47041108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
4705beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
4706d94f2b85SPyun YongHyeon 			/*
4707d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
4708d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
4709d94f2b85SPyun YongHyeon 			 * DMA read operation.
4710d94f2b85SPyun YongHyeon 			 */
4711beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
4712d94f2b85SPyun YongHyeon 				m = m_defrag(m, M_DONTWAIT);
4713d94f2b85SPyun YongHyeon 			else
47141108273aSPyun YongHyeon 				m = m_collapse(m, M_DONTWAIT,
47151108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
4716261f04d6SPyun YongHyeon 			if (m == NULL)
4717261f04d6SPyun YongHyeon 				m = *m_head;
4718d94f2b85SPyun YongHyeon 			*m_head = m;
4719d94f2b85SPyun YongHyeon 		}
47201108273aSPyun YongHyeon 	}
4721d94f2b85SPyun YongHyeon 
47227e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
47230ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
4724676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
47257e27542aSGleb Smirnoff 	if (error == EFBIG) {
47264eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
4727676ad2c9SGleb Smirnoff 		if (m == NULL) {
4728676ad2c9SGleb Smirnoff 			m_freem(*m_head);
4729676ad2c9SGleb Smirnoff 			*m_head = NULL;
47307e27542aSGleb Smirnoff 			return (ENOBUFS);
47317e27542aSGleb Smirnoff 		}
4732676ad2c9SGleb Smirnoff 		*m_head = m;
47330ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
47340ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
4735676ad2c9SGleb Smirnoff 		if (error) {
4736676ad2c9SGleb Smirnoff 			m_freem(m);
4737676ad2c9SGleb Smirnoff 			*m_head = NULL;
47387e27542aSGleb Smirnoff 			return (error);
47397e27542aSGleb Smirnoff 		}
4740676ad2c9SGleb Smirnoff 	} else if (error != 0)
4741676ad2c9SGleb Smirnoff 		return (error);
47427e27542aSGleb Smirnoff 
4743167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
4744167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
47450ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
474695d67482SBill Paul 		return (ENOBUFS);
47477e27542aSGleb Smirnoff 	}
47487e27542aSGleb Smirnoff 
47490ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
4750e65bed95SPyun YongHyeon 
4751ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
4752ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
4753ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
4754ca3f1187SPyun YongHyeon 	}
47557e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
47567e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
47577e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
47587e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
47597e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
47607e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
4761ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
4762ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
47637e27542aSGleb Smirnoff 		if (i == nsegs - 1)
47647e27542aSGleb Smirnoff 			break;
47657e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
47667e27542aSGleb Smirnoff 	}
47677e27542aSGleb Smirnoff 
47687e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
47697e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
4770676ad2c9SGleb Smirnoff 
4771f41ac2beSBill Paul 	/*
4772f41ac2beSBill Paul 	 * Insure that the map for this transmission
4773f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
4774f41ac2beSBill Paul 	 * in this chain.
4775f41ac2beSBill Paul 	 */
47767e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
47777e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
4778676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
47797e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
478095d67482SBill Paul 
47817e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
47827e27542aSGleb Smirnoff 	*txidx = idx;
478395d67482SBill Paul 
478495d67482SBill Paul 	return (0);
478595d67482SBill Paul }
478695d67482SBill Paul 
478795d67482SBill Paul /*
478895d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
478995d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
479095d67482SBill Paul  */
479195d67482SBill Paul static void
47923f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
479395d67482SBill Paul {
479495d67482SBill Paul 	struct bge_softc *sc;
4795167fdb62SPyun YongHyeon 	struct mbuf *m_head;
479614bbd30fSGleb Smirnoff 	uint32_t prodidx;
4797167fdb62SPyun YongHyeon 	int count;
479895d67482SBill Paul 
479995d67482SBill Paul 	sc = ifp->if_softc;
4800167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
480195d67482SBill Paul 
4802167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
4803167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4804167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
480595d67482SBill Paul 		return;
480695d67482SBill Paul 
480714bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
480895d67482SBill Paul 
4809167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
4810167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
4811167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4812167fdb62SPyun YongHyeon 			break;
4813167fdb62SPyun YongHyeon 		}
48144d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
481595d67482SBill Paul 		if (m_head == NULL)
481695d67482SBill Paul 			break;
481795d67482SBill Paul 
481895d67482SBill Paul 		/*
481995d67482SBill Paul 		 * XXX
4820b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
4821b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
4822b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
4823b874fdd4SYaroslav Tykhiy 		 *
4824b874fdd4SYaroslav Tykhiy 		 * XXX
482595d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
482695d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
482795d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
482895d67482SBill Paul 		 * chain at once.
482995d67482SBill Paul 		 * (paranoia -- may not actually be needed)
483095d67482SBill Paul 		 */
483195d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
483295d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
483395d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
483495d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
48354d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
483613f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
483795d67482SBill Paul 				break;
483895d67482SBill Paul 			}
483995d67482SBill Paul 		}
484095d67482SBill Paul 
484195d67482SBill Paul 		/*
484295d67482SBill Paul 		 * Pack the data into the transmit ring. If we
484395d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
484495d67482SBill Paul 		 * for the NIC to drain the ring.
484595d67482SBill Paul 		 */
4846676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
4847676ad2c9SGleb Smirnoff 			if (m_head == NULL)
4848676ad2c9SGleb Smirnoff 				break;
48494d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
485013f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
485195d67482SBill Paul 			break;
485295d67482SBill Paul 		}
4853303a718cSDag-Erling Smørgrav 		++count;
485495d67482SBill Paul 
485595d67482SBill Paul 		/*
485695d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
485795d67482SBill Paul 		 * to him.
485895d67482SBill Paul 		 */
48594e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
486045ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
48614e35d186SJung-uk Kim #else
48624e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
48634e35d186SJung-uk Kim #endif
486495d67482SBill Paul 	}
486595d67482SBill Paul 
4866167fdb62SPyun YongHyeon 	if (count > 0) {
4867aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
48685c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
48693f74909aSGleb Smirnoff 		/* Transmit. */
487038cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
48713927098fSPaul Saab 		/* 5700 b2 errata */
4872e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
487338cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
487495d67482SBill Paul 
487514bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
487614bbd30fSGleb Smirnoff 
487795d67482SBill Paul 		/*
487895d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
487995d67482SBill Paul 		 */
4880b74e67fbSGleb Smirnoff 		sc->bge_timer = 5;
488195d67482SBill Paul 	}
4882167fdb62SPyun YongHyeon }
488395d67482SBill Paul 
48840f9bd73bSSam Leffler /*
48850f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
48860f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
48870f9bd73bSSam Leffler  */
488895d67482SBill Paul static void
48893f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
489095d67482SBill Paul {
48910f9bd73bSSam Leffler 	struct bge_softc *sc;
48920f9bd73bSSam Leffler 
48930f9bd73bSSam Leffler 	sc = ifp->if_softc;
48940f9bd73bSSam Leffler 	BGE_LOCK(sc);
48950f9bd73bSSam Leffler 	bge_start_locked(ifp);
48960f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
48970f9bd73bSSam Leffler }
48980f9bd73bSSam Leffler 
48990f9bd73bSSam Leffler static void
49003f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
49010f9bd73bSSam Leffler {
490295d67482SBill Paul 	struct ifnet *ifp;
49033f74909aSGleb Smirnoff 	uint16_t *m;
4904f6a65488SPyun YongHyeon 	uint32_t mode;
490595d67482SBill Paul 
49060f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
490795d67482SBill Paul 
4908fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
490995d67482SBill Paul 
491013f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
491195d67482SBill Paul 		return;
491295d67482SBill Paul 
491395d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
491495d67482SBill Paul 	bge_stop(sc);
49158cb1383cSDoug Ambrisko 
49168cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
49178cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
491895d67482SBill Paul 	bge_reset(sc);
49198cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
49208cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
49218cb1383cSDoug Ambrisko 
492295d67482SBill Paul 	bge_chipinit(sc);
492395d67482SBill Paul 
492495d67482SBill Paul 	/*
492595d67482SBill Paul 	 * Init the various state machines, ring
492695d67482SBill Paul 	 * control blocks and firmware.
492795d67482SBill Paul 	 */
492895d67482SBill Paul 	if (bge_blockinit(sc)) {
4929fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
493095d67482SBill Paul 		return;
493195d67482SBill Paul 	}
493295d67482SBill Paul 
4933fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
493495d67482SBill Paul 
493595d67482SBill Paul 	/* Specify MTU. */
493695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
4937cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
4938cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
493995d67482SBill Paul 
494095d67482SBill Paul 	/* Load our MAC address. */
49413f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
494295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
494395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
494495d67482SBill Paul 
49453e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
49463e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
494795d67482SBill Paul 
494895d67482SBill Paul 	/* Program multicast filter. */
494995d67482SBill Paul 	bge_setmulti(sc);
495095d67482SBill Paul 
4951cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
4952cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
4953cb2eacc7SYaroslav Tykhiy 
495435f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
495535f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
495635f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
495735f945cdSPyun YongHyeon 	else
495835f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
495935f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
496035f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
496135f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
496235f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
496335f945cdSPyun YongHyeon 	}
496435f945cdSPyun YongHyeon 
496595d67482SBill Paul 	/* Init RX ring. */
49663ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
49673ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
49683ee5d7daSPyun YongHyeon 		bge_stop(sc);
49693ee5d7daSPyun YongHyeon 		return;
49703ee5d7daSPyun YongHyeon 	}
497195d67482SBill Paul 
49720434d1b8SBill Paul 	/*
49730434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
49740434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
49750434d1b8SBill Paul 	 * entry of the ring.
49760434d1b8SBill Paul 	 */
49770434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
49783f74909aSGleb Smirnoff 		uint32_t		v, i;
49790434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
49800434d1b8SBill Paul 			DELAY(20);
49810434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
49820434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
49830434d1b8SBill Paul 				break;
49840434d1b8SBill Paul 		}
49850434d1b8SBill Paul 		if (i == 10)
4986fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
4987fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
49880434d1b8SBill Paul 	}
49890434d1b8SBill Paul 
499095d67482SBill Paul 	/* Init jumbo RX ring. */
4991f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4992f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4993c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
49943ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
4995333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
4996b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
49973ee5d7daSPyun YongHyeon 			bge_stop(sc);
49983ee5d7daSPyun YongHyeon 			return;
49993ee5d7daSPyun YongHyeon 		}
50003ee5d7daSPyun YongHyeon 	}
500195d67482SBill Paul 
50023f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
500395d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
500495d67482SBill Paul 
50057e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
50067e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
50077e6e2507SJung-uk Kim 
500895d67482SBill Paul 	/* Init TX ring. */
500995d67482SBill Paul 	bge_init_tx_ring(sc);
501095d67482SBill Paul 
5011f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5012f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5013f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5014f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
501550515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
501650515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
501750515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
501850515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
501950515680SPyun YongHyeon 	}
50203f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5021f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
502295d67482SBill Paul 
50233f74909aSGleb Smirnoff 	/* Turn on receiver. */
502495d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
502595d67482SBill Paul 
5026dedcdf57SPyun YongHyeon 	/*
5027dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5028dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5029dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5030dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5031dedcdf57SPyun YongHyeon 	 */
5032b4a256acSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765)
5033b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5034b4a256acSPyun YongHyeon 	else
5035dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5036dedcdf57SPyun YongHyeon 
50372280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
50382280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
50392280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
50402280c16bSPyun YongHyeon 
504195d67482SBill Paul 	/* Tell firmware we're alive. */
504295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
504395d67482SBill Paul 
504475719184SGleb Smirnoff #ifdef DEVICE_POLLING
504575719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
504675719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
504775719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
504875719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
504938cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
505075719184SGleb Smirnoff 	} else
505175719184SGleb Smirnoff #endif
505275719184SGleb Smirnoff 
505395d67482SBill Paul 	/* Enable host interrupts. */
505475719184SGleb Smirnoff 	{
505595d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
505695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
505738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
505875719184SGleb Smirnoff 	}
505995d67482SBill Paul 
506013f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
506113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
506295d67482SBill Paul 
5063*e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5064*e4146b95SPyun YongHyeon 
50650f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
50660f9bd73bSSam Leffler }
50670f9bd73bSSam Leffler 
50680f9bd73bSSam Leffler static void
50693f74909aSGleb Smirnoff bge_init(void *xsc)
50700f9bd73bSSam Leffler {
50710f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
50720f9bd73bSSam Leffler 
50730f9bd73bSSam Leffler 	BGE_LOCK(sc);
50740f9bd73bSSam Leffler 	bge_init_locked(sc);
50750f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
507695d67482SBill Paul }
507795d67482SBill Paul 
507895d67482SBill Paul /*
507995d67482SBill Paul  * Set media options.
508095d67482SBill Paul  */
508195d67482SBill Paul static int
50823f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
508395d67482SBill Paul {
508467d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
508567d5e043SOleg Bulyzhin 	int res;
508667d5e043SOleg Bulyzhin 
508767d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
508867d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
508967d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
509067d5e043SOleg Bulyzhin 
509167d5e043SOleg Bulyzhin 	return (res);
509267d5e043SOleg Bulyzhin }
509367d5e043SOleg Bulyzhin 
509467d5e043SOleg Bulyzhin static int
509567d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
509667d5e043SOleg Bulyzhin {
509767d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
509895d67482SBill Paul 	struct mii_data *mii;
50994f09c4c7SMarius Strobl 	struct mii_softc *miisc;
510095d67482SBill Paul 	struct ifmedia *ifm;
510195d67482SBill Paul 
510267d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
510367d5e043SOleg Bulyzhin 
510495d67482SBill Paul 	ifm = &sc->bge_ifmedia;
510595d67482SBill Paul 
510695d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5107652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
510895d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
510995d67482SBill Paul 			return (EINVAL);
511095d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
511195d67482SBill Paul 		case IFM_AUTO:
5112ff50922bSDoug White 			/*
5113ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5114ff50922bSDoug White 			 * mechanism for programming the autoneg
5115ff50922bSDoug White 			 * advertisement registers in TBI mode.
5116ff50922bSDoug White 			 */
51170f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5118ff50922bSDoug White 				uint32_t sgdig;
51190f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
51200f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5121ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5122ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5123ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5124ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5125ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5126ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5127ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5128ff50922bSDoug White 					DELAY(5);
5129ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5130ff50922bSDoug White 				}
51310f89fde2SJung-uk Kim 			}
513295d67482SBill Paul 			break;
513395d67482SBill Paul 		case IFM_1000_SX:
513495d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
513595d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
513695d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
513795d67482SBill Paul 			} else {
513895d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
513995d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
514095d67482SBill Paul 			}
514195d67482SBill Paul 			break;
514295d67482SBill Paul 		default:
514395d67482SBill Paul 			return (EINVAL);
514495d67482SBill Paul 		}
514595d67482SBill Paul 		return (0);
514695d67482SBill Paul 	}
514795d67482SBill Paul 
51481493e883SOleg Bulyzhin 	sc->bge_link_evt++;
514995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
51504f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
51513fcb7a53SMarius Strobl 		PHY_RESET(miisc);
515295d67482SBill Paul 	mii_mediachg(mii);
515395d67482SBill Paul 
5154902827f6SBjoern A. Zeeb 	/*
5155902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5156902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5157902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5158902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5159902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5160902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5161902827f6SBjoern A. Zeeb 	 * get an RX intr.
5162902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5163902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5164902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5165902827f6SBjoern A. Zeeb 	 */
51664f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
51674f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5168902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
51694f0794ffSBjoern A. Zeeb 	else
517063ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5171902827f6SBjoern A. Zeeb 
517295d67482SBill Paul 	return (0);
517395d67482SBill Paul }
517495d67482SBill Paul 
517595d67482SBill Paul /*
517695d67482SBill Paul  * Report current media status.
517795d67482SBill Paul  */
517895d67482SBill Paul static void
51793f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
518095d67482SBill Paul {
518167d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
518295d67482SBill Paul 	struct mii_data *mii;
518395d67482SBill Paul 
518467d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
518595d67482SBill Paul 
5186652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
518795d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
518895d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
518995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
519095d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
519195d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
51924c0da0ffSGleb Smirnoff 		else {
51934c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
519467d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
51954c0da0ffSGleb Smirnoff 			return;
51964c0da0ffSGleb Smirnoff 		}
519795d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
519895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
519995d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
520095d67482SBill Paul 		else
520195d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
520267d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
520395d67482SBill Paul 		return;
520495d67482SBill Paul 	}
520595d67482SBill Paul 
520695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
520795d67482SBill Paul 	mii_pollstat(mii);
520895d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
520995d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
521067d5e043SOleg Bulyzhin 
521167d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
521295d67482SBill Paul }
521395d67482SBill Paul 
521495d67482SBill Paul static int
52153f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
521695d67482SBill Paul {
521795d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
521895d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
521995d67482SBill Paul 	struct mii_data *mii;
5220f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
522195d67482SBill Paul 
522295d67482SBill Paul 	switch (command) {
522395d67482SBill Paul 	case SIOCSIFMTU:
5224f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5225f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
52264c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5227f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
522895d67482SBill Paul 				error = EINVAL;
5229f5459d4cSPyun YongHyeon 				break;
5230f5459d4cSPyun YongHyeon 			}
5231f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5232f5459d4cSPyun YongHyeon 			error = EINVAL;
5233f5459d4cSPyun YongHyeon 			break;
5234f5459d4cSPyun YongHyeon 		}
5235f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5236f5459d4cSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
523795d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
52383a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
523913f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
52403a429c8fSPyun YongHyeon 				bge_init_locked(sc);
524195d67482SBill Paul 			}
52423a429c8fSPyun YongHyeon 		}
52433a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
524495d67482SBill Paul 		break;
524595d67482SBill Paul 	case SIOCSIFFLAGS:
52460f9bd73bSSam Leffler 		BGE_LOCK(sc);
524795d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
524895d67482SBill Paul 			/*
524995d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
525095d67482SBill Paul 			 * then just use the 'set promisc mode' command
525195d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
525295d67482SBill Paul 			 * a full re-init means reloading the firmware and
525395d67482SBill Paul 			 * waiting for it to start up, which may take a
5254d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
525595d67482SBill Paul 			 */
5256f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5257f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
52583e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
52593e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5260f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5261d183af7fSRuslan Ermilov 					bge_setmulti(sc);
526295d67482SBill Paul 			} else
52630f9bd73bSSam Leffler 				bge_init_locked(sc);
526495d67482SBill Paul 		} else {
526513f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
526695d67482SBill Paul 				bge_stop(sc);
526795d67482SBill Paul 			}
526895d67482SBill Paul 		}
526995d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
52700f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
527195d67482SBill Paul 		error = 0;
527295d67482SBill Paul 		break;
527395d67482SBill Paul 	case SIOCADDMULTI:
527495d67482SBill Paul 	case SIOCDELMULTI:
527513f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
52760f9bd73bSSam Leffler 			BGE_LOCK(sc);
527795d67482SBill Paul 			bge_setmulti(sc);
52780f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
527995d67482SBill Paul 			error = 0;
528095d67482SBill Paul 		}
528195d67482SBill Paul 		break;
528295d67482SBill Paul 	case SIOCSIFMEDIA:
528395d67482SBill Paul 	case SIOCGIFMEDIA:
5284652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
528595d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
528695d67482SBill Paul 			    &sc->bge_ifmedia, command);
528795d67482SBill Paul 		} else {
528895d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
528995d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
529095d67482SBill Paul 			    &mii->mii_media, command);
529195d67482SBill Paul 		}
529295d67482SBill Paul 		break;
529395d67482SBill Paul 	case SIOCSIFCAP:
529495d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
529575719184SGleb Smirnoff #ifdef DEVICE_POLLING
529675719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
529775719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
529875719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
529975719184SGleb Smirnoff 				if (error)
530075719184SGleb Smirnoff 					return (error);
530175719184SGleb Smirnoff 				BGE_LOCK(sc);
530275719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
530375719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
530438cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
530575719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
530675719184SGleb Smirnoff 				BGE_UNLOCK(sc);
530775719184SGleb Smirnoff 			} else {
530875719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
530975719184SGleb Smirnoff 				/* Enable interrupt even in error case */
531075719184SGleb Smirnoff 				BGE_LOCK(sc);
531175719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
531275719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
531338cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
531475719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
531575719184SGleb Smirnoff 				BGE_UNLOCK(sc);
531675719184SGleb Smirnoff 			}
531775719184SGleb Smirnoff 		}
531875719184SGleb Smirnoff #endif
5319d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5320d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
5321d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
5322d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
532335f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
532495d67482SBill Paul 			else
532535f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
532695d67482SBill Paul 		}
5327cb2eacc7SYaroslav Tykhiy 
5328d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5329d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
5330d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
5331d8b57f98SPyun YongHyeon 
5332ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5333ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
5334ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
5335ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
5336ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
5337ca3f1187SPyun YongHyeon 			else
5338ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
5339ca3f1187SPyun YongHyeon 		}
5340ca3f1187SPyun YongHyeon 
5341cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5342cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
5343cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5344cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5345cb2eacc7SYaroslav Tykhiy 		}
5346cb2eacc7SYaroslav Tykhiy 
534704bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
534804bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
534904bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
535004bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
535104bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
5352cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
535304bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
535404bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
5355cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5356cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5357cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
535804bde852SPyun YongHyeon 		}
5359cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5360cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
5361cb2eacc7SYaroslav Tykhiy #endif
536295d67482SBill Paul 		break;
536395d67482SBill Paul 	default:
5364673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
536595d67482SBill Paul 		break;
536695d67482SBill Paul 	}
536795d67482SBill Paul 
536895d67482SBill Paul 	return (error);
536995d67482SBill Paul }
537095d67482SBill Paul 
537195d67482SBill Paul static void
5372b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
537395d67482SBill Paul {
5374b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
537595d67482SBill Paul 
5376b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5377b74e67fbSGleb Smirnoff 
5378b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5379b74e67fbSGleb Smirnoff 		return;
5380b74e67fbSGleb Smirnoff 
5381b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
538295d67482SBill Paul 
5383fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
538495d67482SBill Paul 
538513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5386426742bfSGleb Smirnoff 	bge_init_locked(sc);
538795d67482SBill Paul 
538895d67482SBill Paul 	ifp->if_oerrors++;
538995d67482SBill Paul }
539095d67482SBill Paul 
53915a147ba6SPyun YongHyeon static void
53925a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
53935a147ba6SPyun YongHyeon {
53945a147ba6SPyun YongHyeon 	int i;
53955a147ba6SPyun YongHyeon 
53965a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
53975a147ba6SPyun YongHyeon 
53985a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
53995a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
54005a147ba6SPyun YongHyeon 			return;
54015a147ba6SPyun YongHyeon 		DELAY(100);
54025a147ba6SPyun YongHyeon         }
54035a147ba6SPyun YongHyeon }
54045a147ba6SPyun YongHyeon 
540595d67482SBill Paul /*
540695d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
540795d67482SBill Paul  * RX and TX lists.
540895d67482SBill Paul  */
540995d67482SBill Paul static void
54103f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
541195d67482SBill Paul {
541295d67482SBill Paul 	struct ifnet *ifp;
541395d67482SBill Paul 
54140f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
54150f9bd73bSSam Leffler 
5416fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
541795d67482SBill Paul 
54180f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
541995d67482SBill Paul 
542044b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
542144b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
542244b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
542344b63691SBjoern A. Zeeb 
542444b63691SBjoern A. Zeeb 	/*
542544b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
542644b63691SBjoern A. Zeeb 	 */
542744b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
542844b63691SBjoern A. Zeeb 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
542944b63691SBjoern A. Zeeb 
543095d67482SBill Paul 	/*
54313f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
543295d67482SBill Paul 	 */
54335a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
54345a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
54355a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
54365a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
54375a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
54385a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
54395a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
54405a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
544195d67482SBill Paul 
544295d67482SBill Paul 	/*
54433f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
544495d67482SBill Paul 	 */
54455a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
54465a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
54475a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
54485a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
54495a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
54505a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
54515a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
54525a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
545395d67482SBill Paul 
545495d67482SBill Paul 	/*
545595d67482SBill Paul 	 * Shut down all of the memory managers and related
545695d67482SBill Paul 	 * state machines.
545795d67482SBill Paul 	 */
54585a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
54595a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
54605a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
54615a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
54625a147ba6SPyun YongHyeon 
54630c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
546495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
54657ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
546695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
546795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
54680434d1b8SBill Paul 	}
54692280c16bSPyun YongHyeon 	/* Update MAC statistics. */
54702280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
54712280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
547295d67482SBill Paul 
54738cb1383cSDoug Ambrisko 	bge_reset(sc);
54748cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
54758cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
54768cb1383cSDoug Ambrisko 
54778cb1383cSDoug Ambrisko 	/*
54788cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
54798cb1383cSDoug Ambrisko 	 */
54808cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
54818cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
54828cb1383cSDoug Ambrisko 	else
548395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
548495d67482SBill Paul 
548595d67482SBill Paul 	/* Free the RX lists. */
548695d67482SBill Paul 	bge_free_rx_ring_std(sc);
548795d67482SBill Paul 
548895d67482SBill Paul 	/* Free jumbo RX list. */
54894c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
549095d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
549195d67482SBill Paul 
549295d67482SBill Paul 	/* Free TX buffers. */
549395d67482SBill Paul 	bge_free_tx_ring(sc);
549495d67482SBill Paul 
549595d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
549695d67482SBill Paul 
54975dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
54981493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
54991493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
55001493e883SOleg Bulyzhin 	sc->bge_link = 0;
550195d67482SBill Paul 
55021493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
550395d67482SBill Paul }
550495d67482SBill Paul 
550595d67482SBill Paul /*
550695d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
550795d67482SBill Paul  * get confused by errant DMAs when rebooting.
550895d67482SBill Paul  */
5509b6c974e8SWarner Losh static int
55103f74909aSGleb Smirnoff bge_shutdown(device_t dev)
551195d67482SBill Paul {
551295d67482SBill Paul 	struct bge_softc *sc;
551395d67482SBill Paul 
551495d67482SBill Paul 	sc = device_get_softc(dev);
55150f9bd73bSSam Leffler 	BGE_LOCK(sc);
551695d67482SBill Paul 	bge_stop(sc);
551795d67482SBill Paul 	bge_reset(sc);
55180f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
5519b6c974e8SWarner Losh 
5520b6c974e8SWarner Losh 	return (0);
552195d67482SBill Paul }
552214afefa3SPawel Jakub Dawidek 
552314afefa3SPawel Jakub Dawidek static int
552414afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
552514afefa3SPawel Jakub Dawidek {
552614afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
552714afefa3SPawel Jakub Dawidek 
552814afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
552914afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
553014afefa3SPawel Jakub Dawidek 	bge_stop(sc);
553114afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
553214afefa3SPawel Jakub Dawidek 
553314afefa3SPawel Jakub Dawidek 	return (0);
553414afefa3SPawel Jakub Dawidek }
553514afefa3SPawel Jakub Dawidek 
553614afefa3SPawel Jakub Dawidek static int
553714afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
553814afefa3SPawel Jakub Dawidek {
553914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
554014afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
554114afefa3SPawel Jakub Dawidek 
554214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
554314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
554414afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
554514afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
554614afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
554714afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
554814afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
554914afefa3SPawel Jakub Dawidek 	}
555014afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
555114afefa3SPawel Jakub Dawidek 
555214afefa3SPawel Jakub Dawidek 	return (0);
555314afefa3SPawel Jakub Dawidek }
5554dab5cd05SOleg Bulyzhin 
5555dab5cd05SOleg Bulyzhin static void
55563f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
5557dab5cd05SOleg Bulyzhin {
55581f313773SOleg Bulyzhin 	struct mii_data *mii;
55591f313773SOleg Bulyzhin 	uint32_t link, status;
5560dab5cd05SOleg Bulyzhin 
5561dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
55621f313773SOleg Bulyzhin 
55633f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
55647b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
55657b97099dSOleg Bulyzhin 
5566dab5cd05SOleg Bulyzhin 	/*
5567dab5cd05SOleg Bulyzhin 	 * Process link state changes.
5568dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
5569dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
5570dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
5571dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
5572dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
5573dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
5574dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
5575dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
55761f313773SOleg Bulyzhin 	 *
55771f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
55784c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
5579dab5cd05SOleg Bulyzhin 	 */
5580dab5cd05SOleg Bulyzhin 
55811f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
55824c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
5583dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
5584dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
55851f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
55865dda8085SOleg Bulyzhin 			mii_pollstat(mii);
55871f313773SOleg Bulyzhin 			if (!sc->bge_link &&
55881f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
55891f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
55901f313773SOleg Bulyzhin 				sc->bge_link++;
55911f313773SOleg Bulyzhin 				if (bootverbose)
55921f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
55931f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
55941f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
55951f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
55961f313773SOleg Bulyzhin 				sc->bge_link = 0;
55971f313773SOleg Bulyzhin 				if (bootverbose)
55981f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
55991f313773SOleg Bulyzhin 			}
56001f313773SOleg Bulyzhin 
56013f74909aSGleb Smirnoff 			/* Clear the interrupt. */
5602dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
5603dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
5604dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
5605dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
5606dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
5607dab5cd05SOleg Bulyzhin 		}
5608dab5cd05SOleg Bulyzhin 		return;
5609dab5cd05SOleg Bulyzhin 	}
5610dab5cd05SOleg Bulyzhin 
5611652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
56121f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
56137b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
56147b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
56151f313773SOleg Bulyzhin 				sc->bge_link++;
56161f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
56171f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
56181f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
56190c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
56201f313773SOleg Bulyzhin 				if (bootverbose)
56211f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
56223f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
56233f74909aSGleb Smirnoff 				    LINK_STATE_UP);
56247b97099dSOleg Bulyzhin 			}
56251f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
5626dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
56271f313773SOleg Bulyzhin 			if (bootverbose)
56281f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
56297b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
56301f313773SOleg Bulyzhin 		}
56316ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
56321f313773SOleg Bulyzhin 		/*
56330c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
56340c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
56350c8aa4eaSJung-uk Kim 		 * PHY link status directly.
56361f313773SOleg Bulyzhin 		 */
56371f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
56381f313773SOleg Bulyzhin 
56391f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
56401f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
56411f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
56425dda8085SOleg Bulyzhin 			mii_pollstat(mii);
56431f313773SOleg Bulyzhin 			if (!sc->bge_link &&
56441f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
56451f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
56461f313773SOleg Bulyzhin 				sc->bge_link++;
56471f313773SOleg Bulyzhin 				if (bootverbose)
56481f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
56491f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
56501f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
56511f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
56521f313773SOleg Bulyzhin 				sc->bge_link = 0;
56531f313773SOleg Bulyzhin 				if (bootverbose)
56541f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
56551f313773SOleg Bulyzhin 			}
56561f313773SOleg Bulyzhin 		}
56570c8aa4eaSJung-uk Kim 	} else {
56580c8aa4eaSJung-uk Kim 		/*
56596ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
56606ede2cfaSPyun YongHyeon 		 * link status.
56610c8aa4eaSJung-uk Kim 		 */
56626ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
56636ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
56646ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
5665dab5cd05SOleg Bulyzhin 	}
5666dab5cd05SOleg Bulyzhin 
56673f74909aSGleb Smirnoff 	/* Clear the attention. */
5668dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
5669dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
5670dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
5671dab5cd05SOleg Bulyzhin }
56726f8718a3SScott Long 
56736f8718a3SScott Long static void
56746f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
56756f8718a3SScott Long {
56766f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
56772280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
56787e32f79aSPyun YongHyeon 	char tn[32];
56797e32f79aSPyun YongHyeon 	int unit;
56806f8718a3SScott Long 
56816f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
56826f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
56836f8718a3SScott Long 
56846f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
56856f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
56866f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
56876f8718a3SScott Long 	    "Debug Information");
56886f8718a3SScott Long 
56896f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
56906f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
56916f8718a3SScott Long 	    "Register Read");
56926f8718a3SScott Long 
56936f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
56946f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
56956f8718a3SScott Long 	    "Memory Read");
56966f8718a3SScott Long 
56976f8718a3SScott Long #endif
5698763757b2SScott Long 
56997e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
5700beaa2ae1SPyun YongHyeon 	/*
5701beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
5702beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
5703beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
5704beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
5705beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
5706beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
5707beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
5708beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
5709beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
5710beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
5711beaa2ae1SPyun YongHyeon 	 */
57127e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
57137e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
57147e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
5715beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
5716beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
5717beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
5718beaa2ae1SPyun YongHyeon 	    "forced collapsing");
5719beaa2ae1SPyun YongHyeon 
57202ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
57212ae7f64bSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.msi", unit);
57222ae7f64bSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_msi);
57232ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
57242ae7f64bSPyun YongHyeon 	    CTLFLAG_RD, &sc->bge_msi, 0, "Enable MSI");
57255c952e8dSPyun YongHyeon 
572635f945cdSPyun YongHyeon 	/*
572735f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
572835f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
572935f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
573035f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
573135f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
573235f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
573335f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
573435f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
573535f945cdSPyun YongHyeon 	 */
573635f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
573735f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
573835f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
573935f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
574035f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
574135f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
574235f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
574335f945cdSPyun YongHyeon 
5744d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
57452280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
57462280c16bSPyun YongHyeon 	else
57472280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
57482280c16bSPyun YongHyeon }
5749d949071dSJung-uk Kim 
57502280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
57512280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
57522280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
57532280c16bSPyun YongHyeon 	    desc)
57542280c16bSPyun YongHyeon 
57552280c16bSPyun YongHyeon static void
57562280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
57572280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
57582280c16bSPyun YongHyeon {
57592280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
57602280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
57612280c16bSPyun YongHyeon 
57622280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
5763763757b2SScott Long 	    NULL, "BGE Statistics");
5764763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
5765763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
5766763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
5767763757b2SScott Long 	    "FramesDroppedDueToFilters");
5768763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
5769763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
5770763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
5771763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
5772763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
5773763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
577406e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
577506e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
577606e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
577706e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
5778763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
5779763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
5780763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
5781763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
5782763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
5783763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
5784763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
5785763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
5786763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
5787763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
5788763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
5789763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
5790763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
5791763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
5792763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
5793763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
5794763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
5795763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
5796763757b2SScott Long 
5797763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
5798763757b2SScott Long 	    NULL, "BGE RX Statistics");
5799763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5800763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
58011cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
5802763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
5803763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
5804763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
58051cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
5806763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
5807763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
5808763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
5809763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
5810763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
5811763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
5812763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
5813763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
5814763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
5815763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
5816763757b2SScott Long 	    "xoffPauseFramesReceived");
5817763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
5818763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
5819763757b2SScott Long 	    "ControlFramesReceived");
5820763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
5821763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
5822763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
5823763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
5824763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
5825763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
5826763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
5827763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
5828763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
582906e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
5830763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
583106e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
5832763757b2SScott Long 
5833763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
5834763757b2SScott Long 	    NULL, "BGE TX Statistics");
5835763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5836763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
58371cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
5838763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
5839763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
5840763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
5841763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
5842763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
5843763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
5844763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
5845763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
5846763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
5847763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
5848763757b2SScott Long 	    "InternalMacTransmitErrors");
5849763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
5850763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
5851763757b2SScott Long 	    "SingleCollisionFrames");
5852763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
5853763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
5854763757b2SScott Long 	    "MultipleCollisionFrames");
5855763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
5856763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
5857763757b2SScott Long 	    "DeferredTransmissions");
5858763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
5859763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
5860763757b2SScott Long 	    "ExcessiveCollisions");
5861763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
586206e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
586306e83c7eSScott Long 	    "LateCollisions");
5864763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
58651cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
5866763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
5867763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
5868763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
5869763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
5870763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
5871763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
5872763757b2SScott Long 	    "CarrierSenseErrors");
5873763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
5874763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
5875763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
5876763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
5877763757b2SScott Long }
5878763757b2SScott Long 
58792280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
58802280c16bSPyun YongHyeon 
58812280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
58826dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
58832280c16bSPyun YongHyeon 
58842280c16bSPyun YongHyeon static void
58852280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
58862280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
58872280c16bSPyun YongHyeon {
58882280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
58892280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
58902280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
58912280c16bSPyun YongHyeon 
58922280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
58932280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
58942280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
58952280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
58962280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
58972280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
58982280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
58992280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
59002280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
59012280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
59022280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
59032280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
59042280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
59052280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
59062280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
59072280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
59082280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
59092280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
59102280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
59112280c16bSPyun YongHyeon 
59122280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
59132280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
59142280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
59152280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
59162280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
59172280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
59182280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
59191cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
59202280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
59212280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
59222280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
59232280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
59242280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
59252280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
59262280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
59272280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
59282280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
59292280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
59302280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
59312280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
59322280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
59332280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
59342280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
59352280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
59362280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
59372280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
59382280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
59392280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
59402280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
59412280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
59422280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
59432280c16bSPyun YongHyeon 
59442280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
59452280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
59462280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
59471cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
59482280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
59492280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
59502280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
59512280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
59522280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
59532280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
59542280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
59552280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
59562280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
59572280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
59582280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
59592280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
59602280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
59612280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
59622280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
59632280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
59642280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
59652280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
59662280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
59672280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
59682280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
59691cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
59702280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
59711cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
59722280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
59731cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
59742280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
59752280c16bSPyun YongHyeon }
59762280c16bSPyun YongHyeon 
59772280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
59782280c16bSPyun YongHyeon 
5979763757b2SScott Long static int
5980763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
5981763757b2SScott Long {
5982763757b2SScott Long 	struct bge_softc *sc;
598306e83c7eSScott Long 	uint32_t result;
5984d949071dSJung-uk Kim 	int offset;
5985763757b2SScott Long 
5986763757b2SScott Long 	sc = (struct bge_softc *)arg1;
5987763757b2SScott Long 	offset = arg2;
5988d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
5989d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
5990041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
59916f8718a3SScott Long }
59926f8718a3SScott Long 
59936f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
59946f8718a3SScott Long static int
59956f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
59966f8718a3SScott Long {
59976f8718a3SScott Long 	struct bge_softc *sc;
59986f8718a3SScott Long 	uint16_t *sbdata;
599928276ad6SPyun YongHyeon 	int error, result, sbsz;
60006f8718a3SScott Long 	int i, j;
60016f8718a3SScott Long 
60026f8718a3SScott Long 	result = -1;
60036f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
60046f8718a3SScott Long 	if (error || (req->newptr == NULL))
60056f8718a3SScott Long 		return (error);
60066f8718a3SScott Long 
60076f8718a3SScott Long 	if (result == 1) {
60086f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
60096f8718a3SScott Long 
601028276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
601128276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
601228276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
601328276ad6SPyun YongHyeon 		else
601428276ad6SPyun YongHyeon 			sbsz = 32;
60156f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
60166f8718a3SScott Long 		printf("Status Block:\n");
601728276ad6SPyun YongHyeon 		BGE_LOCK(sc);
601828276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
601928276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
602028276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
602128276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
60226f8718a3SScott Long 			printf("%06x:", i);
602328276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
602428276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
60256f8718a3SScott Long 			printf("\n");
60266f8718a3SScott Long 		}
60276f8718a3SScott Long 
60286f8718a3SScott Long 		printf("Registers:\n");
60290c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
60306f8718a3SScott Long 			printf("%06x:", i);
60316f8718a3SScott Long 			for (j = 0; j < 8; j++) {
60326f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
60336f8718a3SScott Long 				i += 4;
60346f8718a3SScott Long 			}
60356f8718a3SScott Long 			printf("\n");
60366f8718a3SScott Long 		}
603728276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
60386f8718a3SScott Long 
60396f8718a3SScott Long 		printf("Hardware Flags:\n");
604028276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
604128276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6042a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6043a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
60445345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
60456f8718a3SScott Long 			printf(" - 575X Plus\n");
60465345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
60476f8718a3SScott Long 			printf(" - 5705 Plus\n");
60485345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
60495345bad0SScott Long 			printf(" - 5714 Family\n");
60505345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
60515345bad0SScott Long 			printf(" - 5700 Family\n");
60526f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
60536f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
60546f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
60556f8718a3SScott Long 			printf(" - PCI-X Bus\n");
60566f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
60576f8718a3SScott Long 			printf(" - PCI Express Bus\n");
60587d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
60596f8718a3SScott Long 			printf(" - No 3 LEDs\n");
60606f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
60616f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
60626f8718a3SScott Long 	}
60636f8718a3SScott Long 
60646f8718a3SScott Long 	return (error);
60656f8718a3SScott Long }
60666f8718a3SScott Long 
60676f8718a3SScott Long static int
60686f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
60696f8718a3SScott Long {
60706f8718a3SScott Long 	struct bge_softc *sc;
60716f8718a3SScott Long 	int error;
60726f8718a3SScott Long 	uint16_t result;
60736f8718a3SScott Long 	uint32_t val;
60746f8718a3SScott Long 
60756f8718a3SScott Long 	result = -1;
60766f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
60776f8718a3SScott Long 	if (error || (req->newptr == NULL))
60786f8718a3SScott Long 		return (error);
60796f8718a3SScott Long 
60806f8718a3SScott Long 	if (result < 0x8000) {
60816f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
60826f8718a3SScott Long 		val = CSR_READ_4(sc, result);
60836f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
60846f8718a3SScott Long 	}
60856f8718a3SScott Long 
60866f8718a3SScott Long 	return (error);
60876f8718a3SScott Long }
60886f8718a3SScott Long 
60896f8718a3SScott Long static int
60906f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
60916f8718a3SScott Long {
60926f8718a3SScott Long 	struct bge_softc *sc;
60936f8718a3SScott Long 	int error;
60946f8718a3SScott Long 	uint16_t result;
60956f8718a3SScott Long 	uint32_t val;
60966f8718a3SScott Long 
60976f8718a3SScott Long 	result = -1;
60986f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
60996f8718a3SScott Long 	if (error || (req->newptr == NULL))
61006f8718a3SScott Long 		return (error);
61016f8718a3SScott Long 
61026f8718a3SScott Long 	if (result < 0x8000) {
61036f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
61046f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
61056f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
61066f8718a3SScott Long 	}
61076f8718a3SScott Long 
61086f8718a3SScott Long 	return (error);
61096f8718a3SScott Long }
61106f8718a3SScott Long #endif
611138cc658fSJohn Baldwin 
611238cc658fSJohn Baldwin static int
61135fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
61145fea260fSMarius Strobl {
61155fea260fSMarius Strobl 
61165fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
61175fea260fSMarius Strobl 		return (1);
61185fea260fSMarius Strobl 
61195fea260fSMarius Strobl #ifdef __sparc64__
61205fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
61215fea260fSMarius Strobl 	return (0);
61225fea260fSMarius Strobl #endif
61235fea260fSMarius Strobl 	return (1);
61245fea260fSMarius Strobl }
61255fea260fSMarius Strobl 
61265fea260fSMarius Strobl static int
612738cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
612838cc658fSJohn Baldwin {
612938cc658fSJohn Baldwin 	uint32_t mac_addr;
613038cc658fSJohn Baldwin 
613173635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
613238cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
613338cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
613438cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
613573635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
613638cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
613738cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
613838cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
613938cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
61405fea260fSMarius Strobl 		return (0);
614138cc658fSJohn Baldwin 	}
61425fea260fSMarius Strobl 	return (1);
614338cc658fSJohn Baldwin }
614438cc658fSJohn Baldwin 
614538cc658fSJohn Baldwin static int
614638cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
614738cc658fSJohn Baldwin {
614838cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
614938cc658fSJohn Baldwin 
615038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
615138cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
615238cc658fSJohn Baldwin 
61535fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
61545fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
615538cc658fSJohn Baldwin }
615638cc658fSJohn Baldwin 
615738cc658fSJohn Baldwin static int
615838cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
615938cc658fSJohn Baldwin {
616038cc658fSJohn Baldwin 
61615fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
61625fea260fSMarius Strobl 		return (1);
61635fea260fSMarius Strobl 
61645fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
61655fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
616638cc658fSJohn Baldwin }
616738cc658fSJohn Baldwin 
616838cc658fSJohn Baldwin static int
616938cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
617038cc658fSJohn Baldwin {
617138cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
617238cc658fSJohn Baldwin 		/* NOTE: Order is critical */
61735fea260fSMarius Strobl 		bge_get_eaddr_fw,
617438cc658fSJohn Baldwin 		bge_get_eaddr_mem,
617538cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
617638cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
617738cc658fSJohn Baldwin 		NULL
617838cc658fSJohn Baldwin 	};
617938cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
618038cc658fSJohn Baldwin 
618138cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
618238cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
618338cc658fSJohn Baldwin 			break;
618438cc658fSJohn Baldwin 	}
618538cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
618638cc658fSJohn Baldwin }
6187