xref: /freebsd/sys/dev/bge/if_bge.c (revision 29b44b096f9088c53c0f1b311a708ec3bc2ef4de)
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 /*
38d7acafa1SMarius Strobl  * Broadcom BCM57xx(x)/BCM590x NetXtreme and NetLink family Ethernet driver
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
41d7acafa1SMarius Strobl  * 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;
14229658c96SDimitry Andric } 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 },
219fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57762 },
220b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57765 },
221fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57766 },
222a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
223b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57781 },
224b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57785 },
225a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
226a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
227b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57791 },
228b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57795 },
2294c0da0ffSGleb Smirnoff 
2304c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2314c0da0ffSGleb Smirnoff 
2324c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2334c0da0ffSGleb Smirnoff 
234a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
235a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
236a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PP250450 },
237a5779553SStanislav Sedov 
2384c0da0ffSGleb Smirnoff 	{ 0, 0 }
23995d67482SBill Paul };
24095d67482SBill Paul 
2414c0da0ffSGleb Smirnoff static const struct bge_vendor {
2424c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2434c0da0ffSGleb Smirnoff 	const char	*v_name;
24429658c96SDimitry Andric } bge_vendors[] = {
2454c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2464c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2474c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2484c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2494c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2504c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
251a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2524c0da0ffSGleb Smirnoff 
2534c0da0ffSGleb Smirnoff 	{ 0, NULL }
2544c0da0ffSGleb Smirnoff };
2554c0da0ffSGleb Smirnoff 
2564c0da0ffSGleb Smirnoff static const struct bge_revision {
2574c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2584c0da0ffSGleb Smirnoff 	const char	*br_name;
25929658c96SDimitry Andric } bge_revisions[] = {
2604c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2614c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2624c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2654c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2674c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2684c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2694c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2704c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2714c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2724c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2744c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2769e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2794c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2824c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2844c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2854c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2884c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2894c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2904c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2914c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2924c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
29342787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2944c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2954c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2964c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2974c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2984c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2994c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
3004c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
3014c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
3020c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
3031108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_A0,	"BCM5717 A0" },
3041108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_B0,	"BCM5717 B0" },
305bbe2ca75SPyun YongHyeon 	{ BGE_CHIPID_BCM5719_A0,	"BCM5719 A0" },
30650515680SPyun YongHyeon 	{ BGE_CHIPID_BCM5720_A0,	"BCM5720 A0" },
3070c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
3080c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
3090c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
310bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
311a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
312a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
313a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
314a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
31581179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3166f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
3176f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3186f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
31938cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
32038cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
321b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_A0,	"BCM57765 A0" },
322b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_B0,	"BCM57765 B0" },
323a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
324a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3254c0da0ffSGleb Smirnoff 
3264c0da0ffSGleb Smirnoff 	{ 0, NULL }
3274c0da0ffSGleb Smirnoff };
3284c0da0ffSGleb Smirnoff 
3294c0da0ffSGleb Smirnoff /*
3304c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3314c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3324c0da0ffSGleb Smirnoff  */
33329658c96SDimitry Andric static const struct bge_revision bge_majorrevs[] = {
3349e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3359e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3369e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3379e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3389e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3399e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3409e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3419e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3429e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3439e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3449e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
345a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
346a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
347a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
34881179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3496f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
35038cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
351b4a256acSPyun YongHyeon 	{ BGE_ASICREV_BCM57765,		"unknown BCM57765" },
352fe26ad88SPyun YongHyeon 	{ BGE_ASICREV_BCM57766,		"unknown BCM57766" },
353a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3541108273aSPyun YongHyeon 	{ BGE_ASICREV_BCM5717,		"unknown BCM5717" },
355bbe2ca75SPyun YongHyeon 	{ BGE_ASICREV_BCM5719,		"unknown BCM5719" },
35650515680SPyun YongHyeon 	{ BGE_ASICREV_BCM5720,		"unknown BCM5720" },
3574c0da0ffSGleb Smirnoff 
3584c0da0ffSGleb Smirnoff 	{ 0, NULL }
3594c0da0ffSGleb Smirnoff };
3604c0da0ffSGleb Smirnoff 
3610c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3620c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3630c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3640c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3650c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
366a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3671108273aSPyun YongHyeon #define	BGE_IS_5717_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5717_PLUS)
368fe26ad88SPyun YongHyeon #define	BGE_IS_57765_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_57765_PLUS)
3694c0da0ffSGleb Smirnoff 
370d7acafa1SMarius Strobl static uint32_t bge_chipid(device_t);
371d7acafa1SMarius Strobl static const struct bge_vendor * bge_lookup_vendor(uint16_t);
372d7acafa1SMarius Strobl static const struct bge_revision * bge_lookup_rev(uint32_t);
37338cc658fSJohn Baldwin 
37438cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
37538cc658fSJohn Baldwin 
376e51a25f8SAlfred Perlstein static int bge_probe(device_t);
377e51a25f8SAlfred Perlstein static int bge_attach(device_t);
378e51a25f8SAlfred Perlstein static int bge_detach(device_t);
37914afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
38014afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3813f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
382f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3835b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
384f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
3855b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
3865b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
387f41ac2beSBill Paul 
388ea9c3a30SPyun YongHyeon static void bge_devinfo(struct bge_softc *);
389062af0b0SPyun YongHyeon static int bge_mbox_reorder(struct bge_softc *);
390062af0b0SPyun YongHyeon 
3915fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
39238cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
39338cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
39438cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
39538cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
39638cc658fSJohn Baldwin 
397b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
3981108273aSPyun YongHyeon static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
399dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
40095d67482SBill Paul 
4018cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
402e51a25f8SAlfred Perlstein static void bge_tick(void *);
4032280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
404e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
4053f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
406d598b626SPyun YongHyeon static struct mbuf *bge_check_short_dma(struct mbuf *);
4072e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
4081108273aSPyun YongHyeon     uint16_t *, uint16_t *);
409676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
41095d67482SBill Paul 
411e51a25f8SAlfred Perlstein static void bge_intr(void *);
412dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
413dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
4140f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
415e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
416e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
4170f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
418e51a25f8SAlfred Perlstein static void bge_init(void *);
4195a147ba6SPyun YongHyeon static void bge_stop_block(struct bge_softc *, bus_size_t, uint32_t);
420e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
421b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
422b6c974e8SWarner Losh static int bge_shutdown(device_t);
42367d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
424e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
425e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
42695d67482SBill Paul 
42738cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
42838cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
42938cc658fSJohn Baldwin 
4303f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
431e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
43295d67482SBill Paul 
4333e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
434e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
435cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
43695d67482SBill Paul 
437e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
438e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
439943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
440943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
441e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
442e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
443e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
444e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
445e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
446e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
44795d67482SBill Paul 
448e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
449e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
45050515680SPyun YongHyeon static uint32_t bge_dma_swap_options(struct bge_softc *);
45195d67482SBill Paul 
4525fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4533f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
454e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
45538cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
45695d67482SBill Paul #ifdef notdef
4573f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
45895d67482SBill Paul #endif
4599ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
460e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
46195d67482SBill Paul 
462e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
463e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
464e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
46575719184SGleb Smirnoff #ifdef DEVICE_POLLING
4661abcdbd1SAttilio Rao static int bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
46775719184SGleb Smirnoff #endif
46895d67482SBill Paul 
469548c8f1aSPyun YongHyeon #define	BGE_RESET_SHUTDOWN	0
4708cb1383cSDoug Ambrisko #define	BGE_RESET_START		1
471548c8f1aSPyun YongHyeon #define	BGE_RESET_SUSPEND	2
4728cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4738cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4748cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
475797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4768cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
477dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
47895d67482SBill Paul 
479548c8f1aSPyun YongHyeon static void bge_ape_lock_init(struct bge_softc *);
480548c8f1aSPyun YongHyeon static void bge_ape_read_fw_ver(struct bge_softc *);
481548c8f1aSPyun YongHyeon static int bge_ape_lock(struct bge_softc *, int);
482548c8f1aSPyun YongHyeon static void bge_ape_unlock(struct bge_softc *, int);
483548c8f1aSPyun YongHyeon static void bge_ape_send_event(struct bge_softc *, uint32_t);
484548c8f1aSPyun YongHyeon static void bge_ape_driver_state_change(struct bge_softc *, int);
485548c8f1aSPyun YongHyeon 
4866f8718a3SScott Long /*
4876f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4886f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4896f8718a3SScott Long  * traps on certain architectures.
4906f8718a3SScott Long  */
4916f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4926f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4936f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
494548c8f1aSPyun YongHyeon static int bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS);
4956f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
4966f8718a3SScott Long #endif
4976f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
4982280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
4992280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
5002280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
5012280c16bSPyun YongHyeon     struct sysctl_oid_list *);
502763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
5036f8718a3SScott Long 
50495d67482SBill Paul static device_method_t bge_methods[] = {
50595d67482SBill Paul 	/* Device interface */
50695d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
50795d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
50895d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
50995d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
51014afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
51114afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
51295d67482SBill Paul 
51395d67482SBill Paul 	/* MII interface */
51495d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
51595d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
51695d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
51795d67482SBill Paul 
5184b7ec270SMarius Strobl 	DEVMETHOD_END
51995d67482SBill Paul };
52095d67482SBill Paul 
52195d67482SBill Paul static driver_t bge_driver = {
52295d67482SBill Paul 	"bge",
52395d67482SBill Paul 	bge_methods,
52495d67482SBill Paul 	sizeof(struct bge_softc)
52595d67482SBill Paul };
52695d67482SBill Paul 
52795d67482SBill Paul static devclass_t bge_devclass;
52895d67482SBill Paul 
529f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
53095d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
53195d67482SBill Paul 
532f1a7e6d5SScott Long static int bge_allow_asf = 1;
533f1a7e6d5SScott Long 
534f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
535f1a7e6d5SScott Long 
5366472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
537f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
538f1a7e6d5SScott Long 	"Allow ASF mode if available");
539c4529f41SMichael Reifenberger 
54008013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
54108013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
54208013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
54308013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
54408013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
54508013fd3SMarius Strobl 
54608013fd3SMarius Strobl static int
5475fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
54808013fd3SMarius Strobl {
54908013fd3SMarius Strobl #ifdef __sparc64__
55008013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
55108013fd3SMarius Strobl 	device_t dev;
55208013fd3SMarius Strobl 	uint32_t subvendor;
55308013fd3SMarius Strobl 
55408013fd3SMarius Strobl 	dev = sc->bge_dev;
55508013fd3SMarius Strobl 
55608013fd3SMarius Strobl 	/*
55708013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
55808013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
55908013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
56008013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
56108013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
56208013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
56308013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
56408013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
56508013fd3SMarius Strobl 	 * there.
56608013fd3SMarius Strobl 	 */
56708013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
56808013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5692d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
57008013fd3SMarius Strobl 		return (0);
57108013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
57208013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
57308013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
57408013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
57508013fd3SMarius Strobl 			return (0);
57608013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
57708013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
57808013fd3SMarius Strobl 			return (0);
57908013fd3SMarius Strobl 	}
58008013fd3SMarius Strobl #endif
58108013fd3SMarius Strobl 	return (1);
58208013fd3SMarius Strobl }
58308013fd3SMarius Strobl 
5843f74909aSGleb Smirnoff static uint32_t
5853f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
58695d67482SBill Paul {
58795d67482SBill Paul 	device_t dev;
5886f8718a3SScott Long 	uint32_t val;
58995d67482SBill Paul 
590a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
591a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
592a4431ebaSPyun YongHyeon 		return (0);
593a4431ebaSPyun YongHyeon 
59495d67482SBill Paul 	dev = sc->bge_dev;
59595d67482SBill Paul 
59695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5976f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5986f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5996f8718a3SScott Long 	return (val);
60095d67482SBill Paul }
60195d67482SBill Paul 
60295d67482SBill Paul static void
6033f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
60495d67482SBill Paul {
60595d67482SBill Paul 	device_t dev;
60695d67482SBill Paul 
607a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
608a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
609a4431ebaSPyun YongHyeon 		return;
610a4431ebaSPyun YongHyeon 
61195d67482SBill Paul 	dev = sc->bge_dev;
61295d67482SBill Paul 
61395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
61495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
6156f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
61695d67482SBill Paul }
61795d67482SBill Paul 
61895d67482SBill Paul #ifdef notdef
6193f74909aSGleb Smirnoff static uint32_t
6203f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
62195d67482SBill Paul {
62295d67482SBill Paul 	device_t dev;
62395d67482SBill Paul 
62495d67482SBill Paul 	dev = sc->bge_dev;
62595d67482SBill Paul 
62695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
62795d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
62895d67482SBill Paul }
62995d67482SBill Paul #endif
63095d67482SBill Paul 
63195d67482SBill Paul static void
6323f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
63395d67482SBill Paul {
63495d67482SBill Paul 	device_t dev;
63595d67482SBill Paul 
63695d67482SBill Paul 	dev = sc->bge_dev;
63795d67482SBill Paul 
63895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
63995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
64095d67482SBill Paul }
64195d67482SBill Paul 
6426f8718a3SScott Long static void
6436f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6446f8718a3SScott Long {
6456f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6466f8718a3SScott Long }
6476f8718a3SScott Long 
64838cc658fSJohn Baldwin static void
64938cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
65038cc658fSJohn Baldwin {
65138cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
65238cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
65338cc658fSJohn Baldwin 
65438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
655062af0b0SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
656062af0b0SPyun YongHyeon 		CSR_READ_4(sc, off);
65738cc658fSJohn Baldwin }
65838cc658fSJohn Baldwin 
659f41ac2beSBill Paul /*
660548c8f1aSPyun YongHyeon  * Clear all stale locks and select the lock for this driver instance.
661548c8f1aSPyun YongHyeon  */
662548c8f1aSPyun YongHyeon static void
663548c8f1aSPyun YongHyeon bge_ape_lock_init(struct bge_softc *sc)
664548c8f1aSPyun YongHyeon {
665548c8f1aSPyun YongHyeon 	uint32_t bit, regbase;
666548c8f1aSPyun YongHyeon 	int i;
667548c8f1aSPyun YongHyeon 
668548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
669548c8f1aSPyun YongHyeon 		regbase = BGE_APE_LOCK_GRANT;
670548c8f1aSPyun YongHyeon 	else
671548c8f1aSPyun YongHyeon 		regbase = BGE_APE_PER_LOCK_GRANT;
672548c8f1aSPyun YongHyeon 
673548c8f1aSPyun YongHyeon 	/* Clear any stale locks. */
674548c8f1aSPyun YongHyeon 	for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
675548c8f1aSPyun YongHyeon 		switch (i) {
676548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY0:
677548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY1:
678548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY2:
679548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY3:
680548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
681548c8f1aSPyun YongHyeon 			break;
682548c8f1aSPyun YongHyeon 		default:
683bd9c196aSPyun YongHyeon 			if (sc->bge_func_addr == 0)
684548c8f1aSPyun YongHyeon 				bit = BGE_APE_LOCK_GRANT_DRIVER0;
685548c8f1aSPyun YongHyeon 			else
686548c8f1aSPyun YongHyeon 				bit = (1 << sc->bge_func_addr);
687548c8f1aSPyun YongHyeon 		}
688548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, regbase + 4 * i, bit);
689548c8f1aSPyun YongHyeon 	}
690548c8f1aSPyun YongHyeon 
691548c8f1aSPyun YongHyeon 	/* Select the PHY lock based on the device's function number. */
692548c8f1aSPyun YongHyeon 	switch (sc->bge_func_addr) {
693548c8f1aSPyun YongHyeon 	case 0:
694548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY0;
695548c8f1aSPyun YongHyeon 		break;
696548c8f1aSPyun YongHyeon 	case 1:
697548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY1;
698548c8f1aSPyun YongHyeon 		break;
699548c8f1aSPyun YongHyeon 	case 2:
700548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY2;
701548c8f1aSPyun YongHyeon 		break;
702548c8f1aSPyun YongHyeon 	case 3:
703548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY3;
704548c8f1aSPyun YongHyeon 		break;
705548c8f1aSPyun YongHyeon 	default:
706548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev,
707548c8f1aSPyun YongHyeon 		    "PHY lock not supported on this function\n");
708548c8f1aSPyun YongHyeon 	}
709548c8f1aSPyun YongHyeon }
710548c8f1aSPyun YongHyeon 
711548c8f1aSPyun YongHyeon /*
712548c8f1aSPyun YongHyeon  * Check for APE firmware, set flags, and print version info.
713548c8f1aSPyun YongHyeon  */
714548c8f1aSPyun YongHyeon static void
715548c8f1aSPyun YongHyeon bge_ape_read_fw_ver(struct bge_softc *sc)
716548c8f1aSPyun YongHyeon {
717548c8f1aSPyun YongHyeon 	const char *fwtype;
718548c8f1aSPyun YongHyeon 	uint32_t apedata, features;
719548c8f1aSPyun YongHyeon 
720548c8f1aSPyun YongHyeon 	/* Check for a valid APE signature in shared memory. */
721548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
722548c8f1aSPyun YongHyeon 	if (apedata != BGE_APE_SEG_SIG_MAGIC) {
723548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags &= ~ BGE_MFW_ON_APE;
724548c8f1aSPyun YongHyeon 		return;
725548c8f1aSPyun YongHyeon 	}
726548c8f1aSPyun YongHyeon 
727548c8f1aSPyun YongHyeon 	/* Check if APE firmware is running. */
728548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
729548c8f1aSPyun YongHyeon 	if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
730548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE signature found "
731548c8f1aSPyun YongHyeon 		    "but FW status not ready! 0x%08x\n", apedata);
732548c8f1aSPyun YongHyeon 		return;
733548c8f1aSPyun YongHyeon 	}
734548c8f1aSPyun YongHyeon 
735548c8f1aSPyun YongHyeon 	sc->bge_mfw_flags |= BGE_MFW_ON_APE;
736548c8f1aSPyun YongHyeon 
737548c8f1aSPyun YongHyeon 	/* Fetch the APE firwmare type and version. */
738548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
739548c8f1aSPyun YongHyeon 	features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
740548c8f1aSPyun YongHyeon 	if ((features & BGE_APE_FW_FEATURE_NCSI) != 0) {
741548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_NCSI;
742548c8f1aSPyun YongHyeon 		fwtype = "NCSI";
743548c8f1aSPyun YongHyeon 	} else if ((features & BGE_APE_FW_FEATURE_DASH) != 0) {
744548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_DASH;
745548c8f1aSPyun YongHyeon 		fwtype = "DASH";
746548c8f1aSPyun YongHyeon 	} else
747548c8f1aSPyun YongHyeon 		fwtype = "UNKN";
748548c8f1aSPyun YongHyeon 
749548c8f1aSPyun YongHyeon 	/* Print the APE firmware version. */
750548c8f1aSPyun YongHyeon 	device_printf(sc->bge_dev, "APE FW version: %s v%d.%d.%d.%d\n",
751548c8f1aSPyun YongHyeon 	    fwtype,
752548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
753548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
754548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
755548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_BLDMSK));
756548c8f1aSPyun YongHyeon }
757548c8f1aSPyun YongHyeon 
758548c8f1aSPyun YongHyeon static int
759548c8f1aSPyun YongHyeon bge_ape_lock(struct bge_softc *sc, int locknum)
760548c8f1aSPyun YongHyeon {
761548c8f1aSPyun YongHyeon 	uint32_t bit, gnt, req, status;
762548c8f1aSPyun YongHyeon 	int i, off;
763548c8f1aSPyun YongHyeon 
764548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
765548c8f1aSPyun YongHyeon 		return (0);
766548c8f1aSPyun YongHyeon 
767548c8f1aSPyun YongHyeon 	/* Lock request/grant registers have different bases. */
768548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761) {
769548c8f1aSPyun YongHyeon 		req = BGE_APE_LOCK_REQ;
770548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
771548c8f1aSPyun YongHyeon 	} else {
772548c8f1aSPyun YongHyeon 		req = BGE_APE_PER_LOCK_REQ;
773548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
774548c8f1aSPyun YongHyeon 	}
775548c8f1aSPyun YongHyeon 
776548c8f1aSPyun YongHyeon 	off = 4 * locknum;
777548c8f1aSPyun YongHyeon 
778548c8f1aSPyun YongHyeon 	switch (locknum) {
779548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
780548c8f1aSPyun YongHyeon 		/* Lock required when using GPIO. */
781548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
782548c8f1aSPyun YongHyeon 			return (0);
783548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
784548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
785548c8f1aSPyun YongHyeon 		else
786548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
787548c8f1aSPyun YongHyeon 		break;
788548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
789548c8f1aSPyun YongHyeon 		/* Lock required to reset the device. */
790548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
791548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
792548c8f1aSPyun YongHyeon 		else
793548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
794548c8f1aSPyun YongHyeon 		break;
795548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
796548c8f1aSPyun YongHyeon 		/* Lock required when accessing certain APE memory. */
797548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
798548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
799548c8f1aSPyun YongHyeon 		else
800548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
801548c8f1aSPyun YongHyeon 		break;
802548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
803548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
804548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
805548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
806548c8f1aSPyun YongHyeon 		/* Lock required when accessing PHYs. */
807548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_REQ_DRIVER0;
808548c8f1aSPyun YongHyeon 		break;
809548c8f1aSPyun YongHyeon 	default:
810548c8f1aSPyun YongHyeon 		return (EINVAL);
811548c8f1aSPyun YongHyeon 	}
812548c8f1aSPyun YongHyeon 
813548c8f1aSPyun YongHyeon 	/* Request a lock. */
814548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, req + off, bit);
815548c8f1aSPyun YongHyeon 
816548c8f1aSPyun YongHyeon 	/* Wait up to 1 second to acquire lock. */
817548c8f1aSPyun YongHyeon 	for (i = 0; i < 20000; i++) {
818548c8f1aSPyun YongHyeon 		status = APE_READ_4(sc, gnt + off);
819548c8f1aSPyun YongHyeon 		if (status == bit)
820548c8f1aSPyun YongHyeon 			break;
821548c8f1aSPyun YongHyeon 		DELAY(50);
822548c8f1aSPyun YongHyeon 	}
823548c8f1aSPyun YongHyeon 
824548c8f1aSPyun YongHyeon 	/* Handle any errors. */
825548c8f1aSPyun YongHyeon 	if (status != bit) {
826548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE lock %d request failed! "
827548c8f1aSPyun YongHyeon 		    "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
828548c8f1aSPyun YongHyeon 		    locknum, req + off, bit & 0xFFFF, gnt + off,
829548c8f1aSPyun YongHyeon 		    status & 0xFFFF);
830548c8f1aSPyun YongHyeon 		/* Revoke the lock request. */
831548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, gnt + off, bit);
832548c8f1aSPyun YongHyeon 		return (EBUSY);
833548c8f1aSPyun YongHyeon 	}
834548c8f1aSPyun YongHyeon 
835548c8f1aSPyun YongHyeon 	return (0);
836548c8f1aSPyun YongHyeon }
837548c8f1aSPyun YongHyeon 
838548c8f1aSPyun YongHyeon static void
839548c8f1aSPyun YongHyeon bge_ape_unlock(struct bge_softc *sc, int locknum)
840548c8f1aSPyun YongHyeon {
841548c8f1aSPyun YongHyeon 	uint32_t bit, gnt;
842548c8f1aSPyun YongHyeon 	int off;
843548c8f1aSPyun YongHyeon 
844548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
845548c8f1aSPyun YongHyeon 		return;
846548c8f1aSPyun YongHyeon 
847548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
848548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
849548c8f1aSPyun YongHyeon 	else
850548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
851548c8f1aSPyun YongHyeon 
852548c8f1aSPyun YongHyeon 	off = 4 * locknum;
853548c8f1aSPyun YongHyeon 
854548c8f1aSPyun YongHyeon 	switch (locknum) {
855548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
856548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
857548c8f1aSPyun YongHyeon 			return;
858548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
859548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
860548c8f1aSPyun YongHyeon 		else
861548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
862548c8f1aSPyun YongHyeon 		break;
863548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
864548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
865548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
866548c8f1aSPyun YongHyeon 		else
867548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
868548c8f1aSPyun YongHyeon 		break;
869548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
870548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
871548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
872548c8f1aSPyun YongHyeon 		else
873548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
874548c8f1aSPyun YongHyeon 		break;
875548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
876548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
877548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
878548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
879548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_GRANT_DRIVER0;
880548c8f1aSPyun YongHyeon 		break;
881548c8f1aSPyun YongHyeon 	default:
882548c8f1aSPyun YongHyeon 		return;
883548c8f1aSPyun YongHyeon 	}
884548c8f1aSPyun YongHyeon 
885548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, gnt + off, bit);
886548c8f1aSPyun YongHyeon }
887548c8f1aSPyun YongHyeon 
888548c8f1aSPyun YongHyeon /*
889548c8f1aSPyun YongHyeon  * Send an event to the APE firmware.
890548c8f1aSPyun YongHyeon  */
891548c8f1aSPyun YongHyeon static void
892548c8f1aSPyun YongHyeon bge_ape_send_event(struct bge_softc *sc, uint32_t event)
893548c8f1aSPyun YongHyeon {
894548c8f1aSPyun YongHyeon 	uint32_t apedata;
895548c8f1aSPyun YongHyeon 	int i;
896548c8f1aSPyun YongHyeon 
897548c8f1aSPyun YongHyeon 	/* NCSI does not support APE events. */
898548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
899548c8f1aSPyun YongHyeon 		return;
900548c8f1aSPyun YongHyeon 
901548c8f1aSPyun YongHyeon 	/* Wait up to 1ms for APE to service previous event. */
902548c8f1aSPyun YongHyeon 	for (i = 10; i > 0; i--) {
903548c8f1aSPyun YongHyeon 		if (bge_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
904548c8f1aSPyun YongHyeon 			break;
905548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
906548c8f1aSPyun YongHyeon 		if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
907548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
908548c8f1aSPyun YongHyeon 			    BGE_APE_EVENT_STATUS_EVENT_PENDING);
909548c8f1aSPyun YongHyeon 			bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
910548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
911548c8f1aSPyun YongHyeon 			break;
912548c8f1aSPyun YongHyeon 		}
913548c8f1aSPyun YongHyeon 		bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
914548c8f1aSPyun YongHyeon 		DELAY(100);
915548c8f1aSPyun YongHyeon 	}
916548c8f1aSPyun YongHyeon 	if (i == 0)
917548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE event 0x%08x send timed out\n",
918548c8f1aSPyun YongHyeon 		    event);
919548c8f1aSPyun YongHyeon }
920548c8f1aSPyun YongHyeon 
921548c8f1aSPyun YongHyeon static void
922548c8f1aSPyun YongHyeon bge_ape_driver_state_change(struct bge_softc *sc, int kind)
923548c8f1aSPyun YongHyeon {
924548c8f1aSPyun YongHyeon 	uint32_t apedata, event;
925548c8f1aSPyun YongHyeon 
926548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
927548c8f1aSPyun YongHyeon 		return;
928548c8f1aSPyun YongHyeon 
929548c8f1aSPyun YongHyeon 	switch (kind) {
930548c8f1aSPyun YongHyeon 	case BGE_RESET_START:
931548c8f1aSPyun YongHyeon 		/* If this is the first load, clear the load counter. */
932548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
933548c8f1aSPyun YongHyeon 		if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC)
934548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
935548c8f1aSPyun YongHyeon 		else {
936548c8f1aSPyun YongHyeon 			apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
937548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
938548c8f1aSPyun YongHyeon 		}
939548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
940548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_SIG_MAGIC);
941548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
942548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_LEN_MAGIC);
943548c8f1aSPyun YongHyeon 
944548c8f1aSPyun YongHyeon 		/* Add some version info if bge(4) supports it. */
945548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
946548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
947548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
948548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_BEHAV_NO_PHYLOCK);
949548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
950548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
951548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
952548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_START);
953548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_START;
954548c8f1aSPyun YongHyeon 		break;
955548c8f1aSPyun YongHyeon 	case BGE_RESET_SHUTDOWN:
956548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
957548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_UNLOAD);
958548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
959548c8f1aSPyun YongHyeon 		break;
960548c8f1aSPyun YongHyeon 	case BGE_RESET_SUSPEND:
961548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
962548c8f1aSPyun YongHyeon 		break;
963548c8f1aSPyun YongHyeon 	default:
964548c8f1aSPyun YongHyeon 		return;
965548c8f1aSPyun YongHyeon 	}
966548c8f1aSPyun YongHyeon 
967548c8f1aSPyun YongHyeon 	bge_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
968548c8f1aSPyun YongHyeon 	    BGE_APE_EVENT_STATUS_STATE_CHNGE);
969548c8f1aSPyun YongHyeon }
970548c8f1aSPyun YongHyeon 
971548c8f1aSPyun YongHyeon /*
972f41ac2beSBill Paul  * Map a single buffer address.
973f41ac2beSBill Paul  */
974f41ac2beSBill Paul 
975f41ac2beSBill Paul static void
9763f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
977f41ac2beSBill Paul {
978f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
979f41ac2beSBill Paul 
980f41ac2beSBill Paul 	if (error)
981f41ac2beSBill Paul 		return;
982f41ac2beSBill Paul 
9835b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
9845b610048SPyun YongHyeon 
985f41ac2beSBill Paul 	ctx = arg;
986f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
987f41ac2beSBill Paul }
988f41ac2beSBill Paul 
98938cc658fSJohn Baldwin static uint8_t
99038cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
99138cc658fSJohn Baldwin {
99238cc658fSJohn Baldwin 	uint32_t access, byte = 0;
99338cc658fSJohn Baldwin 	int i;
99438cc658fSJohn Baldwin 
99538cc658fSJohn Baldwin 	/* Lock. */
99638cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
99738cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
99838cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
99938cc658fSJohn Baldwin 			break;
100038cc658fSJohn Baldwin 		DELAY(20);
100138cc658fSJohn Baldwin 	}
100238cc658fSJohn Baldwin 	if (i == 8000)
100338cc658fSJohn Baldwin 		return (1);
100438cc658fSJohn Baldwin 
100538cc658fSJohn Baldwin 	/* Enable access. */
100638cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
100738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
100838cc658fSJohn Baldwin 
100938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
101038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
101138cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
101238cc658fSJohn Baldwin 		DELAY(10);
101338cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
101438cc658fSJohn Baldwin 			DELAY(10);
101538cc658fSJohn Baldwin 			break;
101638cc658fSJohn Baldwin 		}
101738cc658fSJohn Baldwin 	}
101838cc658fSJohn Baldwin 
101938cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
102038cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
102138cc658fSJohn Baldwin 		return (1);
102238cc658fSJohn Baldwin 	}
102338cc658fSJohn Baldwin 
102438cc658fSJohn Baldwin 	/* Get result. */
102538cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
102638cc658fSJohn Baldwin 
102738cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
102838cc658fSJohn Baldwin 
102938cc658fSJohn Baldwin 	/* Disable access. */
103038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
103138cc658fSJohn Baldwin 
103238cc658fSJohn Baldwin 	/* Unlock. */
103338cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
103438cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
103538cc658fSJohn Baldwin 
103638cc658fSJohn Baldwin 	return (0);
103738cc658fSJohn Baldwin }
103838cc658fSJohn Baldwin 
103938cc658fSJohn Baldwin /*
104038cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
104138cc658fSJohn Baldwin  */
104238cc658fSJohn Baldwin static int
104338cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
104438cc658fSJohn Baldwin {
104538cc658fSJohn Baldwin 	int err = 0, i;
104638cc658fSJohn Baldwin 	uint8_t byte = 0;
104738cc658fSJohn Baldwin 
104838cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
104938cc658fSJohn Baldwin 		return (1);
105038cc658fSJohn Baldwin 
105138cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
105238cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
105338cc658fSJohn Baldwin 		if (err)
105438cc658fSJohn Baldwin 			break;
105538cc658fSJohn Baldwin 		*(dest + i) = byte;
105638cc658fSJohn Baldwin 	}
105738cc658fSJohn Baldwin 
105838cc658fSJohn Baldwin 	return (err ? 1 : 0);
105938cc658fSJohn Baldwin }
106038cc658fSJohn Baldwin 
106195d67482SBill Paul /*
106295d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
106395d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
106495d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
106595d67482SBill Paul  * access method.
106695d67482SBill Paul  */
10673f74909aSGleb Smirnoff static uint8_t
10683f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
106995d67482SBill Paul {
107095d67482SBill Paul 	int i;
10713f74909aSGleb Smirnoff 	uint32_t byte = 0;
107295d67482SBill Paul 
107395d67482SBill Paul 	/*
107495d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
107595d67482SBill Paul 	 * having to use the bitbang method.
107695d67482SBill Paul 	 */
107795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
107895d67482SBill Paul 
107995d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
108095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
108195d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
108295d67482SBill Paul 	DELAY(20);
108395d67482SBill Paul 
108495d67482SBill Paul 	/* Issue the read EEPROM command. */
108595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
108695d67482SBill Paul 
108795d67482SBill Paul 	/* Wait for completion */
108895d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
108995d67482SBill Paul 		DELAY(10);
109095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
109195d67482SBill Paul 			break;
109295d67482SBill Paul 	}
109395d67482SBill Paul 
1094d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
1095fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
1096f6789fbaSPyun YongHyeon 		return (1);
109795d67482SBill Paul 	}
109895d67482SBill Paul 
109995d67482SBill Paul 	/* Get result. */
110095d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
110195d67482SBill Paul 
11020c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
110395d67482SBill Paul 
110495d67482SBill Paul 	return (0);
110595d67482SBill Paul }
110695d67482SBill Paul 
110795d67482SBill Paul /*
110895d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
110995d67482SBill Paul  */
111095d67482SBill Paul static int
11113f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
111295d67482SBill Paul {
11133f74909aSGleb Smirnoff 	int i, error = 0;
11143f74909aSGleb Smirnoff 	uint8_t byte = 0;
111595d67482SBill Paul 
111695d67482SBill Paul 	for (i = 0; i < cnt; i++) {
11173f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
11183f74909aSGleb Smirnoff 		if (error)
111995d67482SBill Paul 			break;
112095d67482SBill Paul 		*(dest + i) = byte;
112195d67482SBill Paul 	}
112295d67482SBill Paul 
11233f74909aSGleb Smirnoff 	return (error ? 1 : 0);
112495d67482SBill Paul }
112595d67482SBill Paul 
112695d67482SBill Paul static int
11273f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
112895d67482SBill Paul {
112995d67482SBill Paul 	struct bge_softc *sc;
1130a813ed78SPyun YongHyeon 	uint32_t val;
113195d67482SBill Paul 	int i;
113295d67482SBill Paul 
113395d67482SBill Paul 	sc = device_get_softc(dev);
113495d67482SBill Paul 
1135548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1136548c8f1aSPyun YongHyeon 		return (0);
1137548c8f1aSPyun YongHyeon 
1138a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1139a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1140a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1141a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1142a813ed78SPyun YongHyeon 		DELAY(80);
114337ceeb4dSPaul Saab 	}
114437ceeb4dSPaul Saab 
114595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
114695d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
114795d67482SBill Paul 
1148a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
114995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1150d5d23857SJung-uk Kim 		DELAY(10);
115195d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
1152a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
1153a813ed78SPyun YongHyeon 			DELAY(5);
1154a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
115595d67482SBill Paul 			break;
115695d67482SBill Paul 		}
1157a813ed78SPyun YongHyeon 	}
115895d67482SBill Paul 
115995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
11605fea260fSMarius Strobl 		device_printf(sc->bge_dev,
11615fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
11625fea260fSMarius Strobl 		    phy, reg, val);
116337ceeb4dSPaul Saab 		val = 0;
116495d67482SBill Paul 	}
116595d67482SBill Paul 
1166a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1167a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1168a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1169a813ed78SPyun YongHyeon 		DELAY(80);
117037ceeb4dSPaul Saab 	}
117137ceeb4dSPaul Saab 
1172548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1173548c8f1aSPyun YongHyeon 
117495d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
117595d67482SBill Paul 		return (0);
117695d67482SBill Paul 
11770c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
117895d67482SBill Paul }
117995d67482SBill Paul 
118095d67482SBill Paul static int
11813f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
118295d67482SBill Paul {
118395d67482SBill Paul 	struct bge_softc *sc;
118495d67482SBill Paul 	int i;
118595d67482SBill Paul 
118695d67482SBill Paul 	sc = device_get_softc(dev);
118795d67482SBill Paul 
118838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
118938cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
119038cc658fSJohn Baldwin 		return (0);
119138cc658fSJohn Baldwin 
1192548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1193548c8f1aSPyun YongHyeon 		return (0);
1194548c8f1aSPyun YongHyeon 
1195a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1196a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1197a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1198a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1199a813ed78SPyun YongHyeon 		DELAY(80);
120037ceeb4dSPaul Saab 	}
120137ceeb4dSPaul Saab 
120295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
120395d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
120495d67482SBill Paul 
120595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1206d5d23857SJung-uk Kim 		DELAY(10);
120738cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
120838cc658fSJohn Baldwin 			DELAY(5);
120938cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
121095d67482SBill Paul 			break;
1211d5d23857SJung-uk Kim 		}
121238cc658fSJohn Baldwin 	}
1213d5d23857SJung-uk Kim 
1214a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1215a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1216a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1217a813ed78SPyun YongHyeon 		DELAY(80);
1218a813ed78SPyun YongHyeon 	}
1219a813ed78SPyun YongHyeon 
1220548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1221548c8f1aSPyun YongHyeon 
1222a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
122338cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
12242246e8c6SPyun YongHyeon 		    "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
122538cc658fSJohn Baldwin 		    phy, reg, val);
122637ceeb4dSPaul Saab 
122795d67482SBill Paul 	return (0);
122895d67482SBill Paul }
122995d67482SBill Paul 
123095d67482SBill Paul static void
12313f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
123295d67482SBill Paul {
123395d67482SBill Paul 	struct bge_softc *sc;
123495d67482SBill Paul 	struct mii_data *mii;
1235a0a03d1eSPyun YongHyeon 	uint32_t mac_mode, rx_mode, tx_mode;
1236e4146b95SPyun YongHyeon 
123795d67482SBill Paul 	sc = device_get_softc(dev);
1238e4146b95SPyun YongHyeon 	if ((sc->bge_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1239e4146b95SPyun YongHyeon 		return;
124095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
124195d67482SBill Paul 
1242d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1243d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
1244d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
1245d4f5240aSPyun YongHyeon 		case IFM_10_T:
1246d4f5240aSPyun YongHyeon 		case IFM_100_TX:
1247d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
1248d4f5240aSPyun YongHyeon 			break;
1249d4f5240aSPyun YongHyeon 		case IFM_1000_T:
1250d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
1251d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
1252d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1253d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
1254d4f5240aSPyun YongHyeon 			else
1255d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
1256d4f5240aSPyun YongHyeon 			break;
1257d4f5240aSPyun YongHyeon 		default:
1258d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
1259d4f5240aSPyun YongHyeon 			break;
1260d4f5240aSPyun YongHyeon 		}
1261d4f5240aSPyun YongHyeon 	} else
1262d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
1263d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
1264d4f5240aSPyun YongHyeon 		return;
1265a0a03d1eSPyun YongHyeon 
1266a0a03d1eSPyun YongHyeon 	/*
1267a0a03d1eSPyun YongHyeon 	 * APE firmware touches these registers to keep the MAC
1268a0a03d1eSPyun YongHyeon 	 * connected to the outside world.  Try to keep the
1269a0a03d1eSPyun YongHyeon 	 * accesses atomic.
1270a0a03d1eSPyun YongHyeon 	 */
1271a0a03d1eSPyun YongHyeon 
1272a0a03d1eSPyun YongHyeon 	/* Set the port mode (MII/GMII) to match the link speed. */
1273a0a03d1eSPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
1274a0a03d1eSPyun YongHyeon 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
1275a0a03d1eSPyun YongHyeon 	tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
1276a0a03d1eSPyun YongHyeon 	rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
1277a0a03d1eSPyun YongHyeon 
1278ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1279ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1280a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_GMII;
12813f74909aSGleb Smirnoff 	else
1282a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_MII;
128395d67482SBill Paul 
1284a0a03d1eSPyun YongHyeon 	/* Set MAC flow control behavior to match link flow control settings. */
1285a0a03d1eSPyun YongHyeon 	tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
1286a0a03d1eSPyun YongHyeon 	rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
12874951ca86SPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1288a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1289a0a03d1eSPyun YongHyeon 			tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
1290a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1291a0a03d1eSPyun YongHyeon 			rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
1292a0a03d1eSPyun YongHyeon 	} else
1293a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
1294a0a03d1eSPyun YongHyeon 
1295a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
12969b80ffe7SPyun YongHyeon 	DELAY(40);
1297a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
1298a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
129995d67482SBill Paul }
130095d67482SBill Paul 
130195d67482SBill Paul /*
130295d67482SBill Paul  * Intialize a standard receive ring descriptor.
130395d67482SBill Paul  */
130495d67482SBill Paul static int
1305943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
130695d67482SBill Paul {
1307943787f3SPyun YongHyeon 	struct mbuf *m;
130895d67482SBill Paul 	struct bge_rx_bd *r;
1309a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
1310943787f3SPyun YongHyeon 	bus_dmamap_t map;
1311a23634a1SPyun YongHyeon 	int error, nsegs;
131295d67482SBill Paul 
1313f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
1314f5459d4cSPyun YongHyeon 	    (sc->bge_ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
1315f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
1316c6499eccSGleb Smirnoff 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1317f5459d4cSPyun YongHyeon 		if (m == NULL)
1318f5459d4cSPyun YongHyeon 			return (ENOBUFS);
1319f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1320f5459d4cSPyun YongHyeon 	} else {
1321c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1322943787f3SPyun YongHyeon 		if (m == NULL)
132395d67482SBill Paul 			return (ENOBUFS);
1324943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
1325f5459d4cSPyun YongHyeon 	}
1326652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1327943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
1328943787f3SPyun YongHyeon 
13290ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
1330943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
1331a23634a1SPyun YongHyeon 	if (error != 0) {
1332943787f3SPyun YongHyeon 		m_freem(m);
1333a23634a1SPyun YongHyeon 		return (error);
1334f41ac2beSBill Paul 	}
1335943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1336943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1337943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1338943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1339943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
1340943787f3SPyun YongHyeon 	}
1341943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
1342943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1343943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
1344943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
1345e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1346943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1347a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1348a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1349e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1350a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1351e907febfSPyun YongHyeon 	r->bge_idx = i;
1352f41ac2beSBill Paul 
13530ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1354943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
135595d67482SBill Paul 
135695d67482SBill Paul 	return (0);
135795d67482SBill Paul }
135895d67482SBill Paul 
135995d67482SBill Paul /*
136095d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
136195d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
136295d67482SBill Paul  */
136395d67482SBill Paul static int
1364943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
136595d67482SBill Paul {
13661be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1367943787f3SPyun YongHyeon 	bus_dmamap_t map;
13681be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1369943787f3SPyun YongHyeon 	struct mbuf *m;
1370943787f3SPyun YongHyeon 	int error, nsegs;
137195d67482SBill Paul 
1372c6499eccSGleb Smirnoff 	MGETHDR(m, M_NOWAIT, MT_DATA);
1373943787f3SPyun YongHyeon 	if (m == NULL)
137495d67482SBill Paul 		return (ENOBUFS);
137595d67482SBill Paul 
1376c6499eccSGleb Smirnoff 	m_cljget(m, M_NOWAIT, MJUM9BYTES);
1377943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
1378943787f3SPyun YongHyeon 		m_freem(m);
137995d67482SBill Paul 		return (ENOBUFS);
138095d67482SBill Paul 	}
1381943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1382652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1383943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
13841be6acb7SGleb Smirnoff 
13851be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1386943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1387943787f3SPyun YongHyeon 	if (error != 0) {
1388943787f3SPyun YongHyeon 		m_freem(m);
13891be6acb7SGleb Smirnoff 		return (error);
1390f7cea149SGleb Smirnoff 	}
13911be6acb7SGleb Smirnoff 
1392aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1393943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1394943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1395943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1396943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1397943787f3SPyun YongHyeon 	}
1398943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1399943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1400943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1401943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1402943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1403e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1404e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1405e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1406e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1407e0b7b101SPyun YongHyeon 
14081be6acb7SGleb Smirnoff 	/*
14091be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
14101be6acb7SGleb Smirnoff 	 */
1411943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
14124e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
14134e7ba1abSGleb Smirnoff 	r->bge_idx = i;
14144e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
14154e7ba1abSGleb Smirnoff 	switch (nsegs) {
14164e7ba1abSGleb Smirnoff 	case 4:
14174e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
14184e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
14194e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1420e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
14214e7ba1abSGleb Smirnoff 	case 3:
1422e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1423e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1424e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1425e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
14264e7ba1abSGleb Smirnoff 	case 2:
14274e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
14284e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
14294e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1430e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
14314e7ba1abSGleb Smirnoff 	case 1:
14324e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
14334e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
14344e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1435e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
14364e7ba1abSGleb Smirnoff 		break;
14374e7ba1abSGleb Smirnoff 	default:
14384e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
14394e7ba1abSGleb Smirnoff 	}
1440f41ac2beSBill Paul 
1441a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1442943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
144395d67482SBill Paul 
144495d67482SBill Paul 	return (0);
144595d67482SBill Paul }
144695d67482SBill Paul 
144795d67482SBill Paul static int
14483f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
144995d67482SBill Paul {
14503ee5d7daSPyun YongHyeon 	int error, i;
145195d67482SBill Paul 
1452e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
145303e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1454e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1455943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
14563ee5d7daSPyun YongHyeon 			return (error);
145703e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
14581888f324SPyun YongHyeon 	}
145995d67482SBill Paul 
1460f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1461d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1462f41ac2beSBill Paul 
1463e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1464e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
146595d67482SBill Paul 
146695d67482SBill Paul 	return (0);
146795d67482SBill Paul }
146895d67482SBill Paul 
146995d67482SBill Paul static void
14703f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
147195d67482SBill Paul {
147295d67482SBill Paul 	int i;
147395d67482SBill Paul 
147495d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
147595d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
14760ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1477e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1478e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
14790ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1480f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1481e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1482e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
148395d67482SBill Paul 		}
1484f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
148595d67482SBill Paul 		    sizeof(struct bge_rx_bd));
148695d67482SBill Paul 	}
148795d67482SBill Paul }
148895d67482SBill Paul 
148995d67482SBill Paul static int
14903f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
149195d67482SBill Paul {
149295d67482SBill Paul 	struct bge_rcb *rcb;
14933ee5d7daSPyun YongHyeon 	int error, i;
149495d67482SBill Paul 
1495e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
149603e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
149795d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1498943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
14993ee5d7daSPyun YongHyeon 			return (error);
150003e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
15011888f324SPyun YongHyeon 	}
150295d67482SBill Paul 
1503f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1504d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1505f41ac2beSBill Paul 
1506e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
150795d67482SBill Paul 
15088a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1509f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
15108a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
15118a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
151267111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
151395d67482SBill Paul 
1514e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
151595d67482SBill Paul 
151695d67482SBill Paul 	return (0);
151795d67482SBill Paul }
151895d67482SBill Paul 
151995d67482SBill Paul static void
15203f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
152195d67482SBill Paul {
152295d67482SBill Paul 	int i;
152395d67482SBill Paul 
152495d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
152595d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1526e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1527e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1528e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1529f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1530f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1531e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1532e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
153395d67482SBill Paul 		}
1534f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
15351be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
153695d67482SBill Paul 	}
153795d67482SBill Paul }
153895d67482SBill Paul 
153995d67482SBill Paul static void
15403f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
154195d67482SBill Paul {
154295d67482SBill Paul 	int i;
154395d67482SBill Paul 
1544f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
154595d67482SBill Paul 		return;
154695d67482SBill Paul 
154795d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
154895d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
15490ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1550e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1551e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15520ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1553f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1554e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1555e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
155695d67482SBill Paul 		}
1557f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
155895d67482SBill Paul 		    sizeof(struct bge_tx_bd));
155995d67482SBill Paul 	}
156095d67482SBill Paul }
156195d67482SBill Paul 
156295d67482SBill Paul static int
15633f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
156495d67482SBill Paul {
156595d67482SBill Paul 	sc->bge_txcnt = 0;
156695d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
15673927098fSPaul Saab 
1568e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1569e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
15705c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1571e6bf277eSPyun YongHyeon 
157214bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
157314bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
157438cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
157514bbd30fSGleb Smirnoff 
15763927098fSPaul Saab 	/* 5700 b2 errata */
1577e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
157838cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
15793927098fSPaul Saab 
158014bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
158138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
15823927098fSPaul Saab 	/* 5700 b2 errata */
1583e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
158438cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
158595d67482SBill Paul 
158695d67482SBill Paul 	return (0);
158795d67482SBill Paul }
158895d67482SBill Paul 
158995d67482SBill Paul static void
15903e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
15913e9b1bcaSJung-uk Kim {
15923e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
15933e9b1bcaSJung-uk Kim 
15943e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
15953e9b1bcaSJung-uk Kim 
15963e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
15973e9b1bcaSJung-uk Kim 
159845ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
15993e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
160045ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16013e9b1bcaSJung-uk Kim 	else
160245ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16033e9b1bcaSJung-uk Kim }
16043e9b1bcaSJung-uk Kim 
16053e9b1bcaSJung-uk Kim static void
16063f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
160795d67482SBill Paul {
160895d67482SBill Paul 	struct ifnet *ifp;
160995d67482SBill Paul 	struct ifmultiaddr *ifma;
16103f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
161195d67482SBill Paul 	int h, i;
161295d67482SBill Paul 
16130f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
16140f9bd73bSSam Leffler 
1615fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
161695d67482SBill Paul 
161795d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
161895d67482SBill Paul 		for (i = 0; i < 4; i++)
16190c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
162095d67482SBill Paul 		return;
162195d67482SBill Paul 	}
162295d67482SBill Paul 
162395d67482SBill Paul 	/* First, zot all the existing filters. */
162495d67482SBill Paul 	for (i = 0; i < 4; i++)
162595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
162695d67482SBill Paul 
162795d67482SBill Paul 	/* Now program new ones. */
1628eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
162995d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
163095d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
163195d67482SBill Paul 			continue;
16320e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
16330c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
16340c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
163595d67482SBill Paul 	}
1636eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
163795d67482SBill Paul 
163895d67482SBill Paul 	for (i = 0; i < 4; i++)
163995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
164095d67482SBill Paul }
164195d67482SBill Paul 
16428cb1383cSDoug Ambrisko static void
1643cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1644cb2eacc7SYaroslav Tykhiy {
1645cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1646cb2eacc7SYaroslav Tykhiy 
1647cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1648cb2eacc7SYaroslav Tykhiy 
1649cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1650cb2eacc7SYaroslav Tykhiy 
1651cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1652cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1653cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1654cb2eacc7SYaroslav Tykhiy 	else
1655cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1656cb2eacc7SYaroslav Tykhiy }
1657cb2eacc7SYaroslav Tykhiy 
1658cb2eacc7SYaroslav Tykhiy static void
1659797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
16608cb1383cSDoug Ambrisko {
1661797ab05eSPyun YongHyeon 
16628cb1383cSDoug Ambrisko 	/*
16638cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
16648cb1383cSDoug Ambrisko 	 */
16658cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1666888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
16678cb1383cSDoug Ambrisko 
16688cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16698cb1383cSDoug Ambrisko 		switch (type) {
16708cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1671224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1672224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
16738cb1383cSDoug Ambrisko 			break;
1674548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1675224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1676224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
16778cb1383cSDoug Ambrisko 			break;
1678548c8f1aSPyun YongHyeon 		case BGE_RESET_SUSPEND:
1679548c8f1aSPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1680548c8f1aSPyun YongHyeon 			    BGE_FW_DRV_STATE_SUSPEND);
1681548c8f1aSPyun YongHyeon 			break;
16828cb1383cSDoug Ambrisko 		}
16838cb1383cSDoug Ambrisko 	}
1684548c8f1aSPyun YongHyeon 
1685548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1686548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
16878cb1383cSDoug Ambrisko }
16888cb1383cSDoug Ambrisko 
16898cb1383cSDoug Ambrisko static void
1690797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
16918cb1383cSDoug Ambrisko {
1692797ab05eSPyun YongHyeon 
16938cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16948cb1383cSDoug Ambrisko 		switch (type) {
16958cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1696224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1697224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
16988cb1383cSDoug Ambrisko 			/* START DONE */
16998cb1383cSDoug Ambrisko 			break;
1700548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1701224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1702224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
17038cb1383cSDoug Ambrisko 			break;
17048cb1383cSDoug Ambrisko 		}
17058cb1383cSDoug Ambrisko 	}
1706548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_SHUTDOWN)
1707548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17088cb1383cSDoug Ambrisko }
17098cb1383cSDoug Ambrisko 
17108cb1383cSDoug Ambrisko static void
1711797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
17128cb1383cSDoug Ambrisko {
1713797ab05eSPyun YongHyeon 
17148cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17158cb1383cSDoug Ambrisko 		switch (type) {
17168cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1717224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1718224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
17198cb1383cSDoug Ambrisko 			break;
1720548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1721224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1722224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
17238cb1383cSDoug Ambrisko 			break;
17248cb1383cSDoug Ambrisko 		}
17258cb1383cSDoug Ambrisko 	}
17268cb1383cSDoug Ambrisko }
17278cb1383cSDoug Ambrisko 
1728797ab05eSPyun YongHyeon static void
1729797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
17308cb1383cSDoug Ambrisko {
17318cb1383cSDoug Ambrisko 	int i;
17328cb1383cSDoug Ambrisko 
17338cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17343c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
17353fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
17369931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
17378cb1383cSDoug Ambrisko 
17388cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
17399931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
17409931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
17418cb1383cSDoug Ambrisko 				break;
17428cb1383cSDoug Ambrisko 			DELAY(10);
17438cb1383cSDoug Ambrisko 		}
17448cb1383cSDoug Ambrisko 	}
17458cb1383cSDoug Ambrisko }
17468cb1383cSDoug Ambrisko 
174750515680SPyun YongHyeon static uint32_t
174850515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
174950515680SPyun YongHyeon {
175050515680SPyun YongHyeon 	uint32_t dma_options;
175150515680SPyun YongHyeon 
175250515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
175350515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
175450515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
175550515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
175650515680SPyun YongHyeon #endif
175750515680SPyun YongHyeon 	return (dma_options);
175850515680SPyun YongHyeon }
175950515680SPyun YongHyeon 
176095d67482SBill Paul /*
1761c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
176295d67482SBill Paul  */
176395d67482SBill Paul static int
17643f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
176595d67482SBill Paul {
176650515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1767fbc374afSPyun YongHyeon 	uint16_t val;
176895d67482SBill Paul 	int i;
176995d67482SBill Paul 
17708cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
17711108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
17721108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
17731108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
17741108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
177595d67482SBill Paul 
177695d67482SBill Paul 	/*
177795d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
177895d67482SBill Paul 	 * internal memory.
177995d67482SBill Paul 	 */
178095d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
17813f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
178295d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
178395d67482SBill Paul 
178495d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
17853f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
178695d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
178795d67482SBill Paul 
1788fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1789fbc374afSPyun YongHyeon 		/*
1790d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1791fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1792fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1793fbc374afSPyun YongHyeon 		 */
1794fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1795fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1796fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1797fbc374afSPyun YongHyeon 	}
1798fbc374afSPyun YongHyeon 
1799186f842bSJung-uk Kim 	/*
1800186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1801186f842bSJung-uk Kim 	 */
1802186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1803186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1804652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
180548630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
180648630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
180748630d79SPyun YongHyeon 		else
1808186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1809652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
18104c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1811186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1812186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1813186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1814186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1815186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1816186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1817cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1818cbb2b2feSPyun YongHyeon 			/*
1819cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1820cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1821cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1822cbb2b2feSPyun YongHyeon 			 * register.
1823cbb2b2feSPyun YongHyeon 			 */
1824cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1825cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1826186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1827186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1828186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1829186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1830186f842bSJung-uk Kim 		} else {
1831186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1832186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1833186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
18340c8aa4eaSJung-uk Kim 			    0x0F;
1835186f842bSJung-uk Kim 		}
1836e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1837e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
18383f74909aSGleb Smirnoff 			uint32_t tmp;
18395cba12d3SPaul Saab 
1840186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
18410c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1842186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1843186f842bSJung-uk Kim 				dma_rw_ctl |=
1844186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
18455cba12d3SPaul Saab 
1846186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1847186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1848186f842bSJung-uk Kim 		}
1849186f842bSJung-uk Kim 	} else {
1850186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1851186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1852186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1853186f842bSJung-uk Kim 
1854186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1855186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1856186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1857186f842bSJung-uk Kim 	}
1858186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1859186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1860186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1861186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1862e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1863186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
18645cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1865b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18661108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1867b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1868b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1869bbe2ca75SPyun YongHyeon 		/*
1870bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1871bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1872bbe2ca75SPyun YongHyeon 		 * disabled.
1873bbe2ca75SPyun YongHyeon 		 */
1874bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
1875bbe2ca75SPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM57765)
1876bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1877b4a256acSPyun YongHyeon 	}
18785cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
187995d67482SBill Paul 
188095d67482SBill Paul 	/*
188195d67482SBill Paul 	 * Set up general mode register.
188295d67482SBill Paul 	 */
1883548c8f1aSPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc);
1884548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
1885548c8f1aSPyun YongHyeon 		/* Retain Host-2-BMC settings written by APE firmware. */
1886548c8f1aSPyun YongHyeon 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1887548c8f1aSPyun YongHyeon 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1888548c8f1aSPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1889548c8f1aSPyun YongHyeon 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1890548c8f1aSPyun YongHyeon 	}
1891548c8f1aSPyun YongHyeon 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1892548c8f1aSPyun YongHyeon 	    BGE_MODECTL_TX_NO_PHDR_CSUM;
189395d67482SBill Paul 
189495d67482SBill Paul 	/*
189590447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
189690447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
189790447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
189890447aadSMarius Strobl 	 * certain bridges.
189990447aadSMarius Strobl 	 */
190090447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
190190447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
190250515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
190390447aadSMarius Strobl 
190490447aadSMarius Strobl 	/*
19058cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
19068cb1383cSDoug Ambrisko 	 */
19078cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
190850515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
190950515680SPyun YongHyeon 
191050515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
19118cb1383cSDoug Ambrisko 
19128cb1383cSDoug Ambrisko 	/*
1913ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1914c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1915c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
191695d67482SBill Paul 	 */
1917c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1918c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
191995d67482SBill Paul 
1920d7acafa1SMarius Strobl 	/* Set the timer prescaler (always 66 MHz). */
19210c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
192295d67482SBill Paul 
192338cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
192438cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
192538cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
192638cc658fSJohn Baldwin 
192738cc658fSJohn Baldwin 		/* Put PHY into ready state */
192838cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
192938cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
193038cc658fSJohn Baldwin 		DELAY(40);
193138cc658fSJohn Baldwin 	}
193238cc658fSJohn Baldwin 
193395d67482SBill Paul 	return (0);
193495d67482SBill Paul }
193595d67482SBill Paul 
193695d67482SBill Paul static int
19373f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
193895d67482SBill Paul {
193995d67482SBill Paul 	struct bge_rcb *rcb;
1940e907febfSPyun YongHyeon 	bus_size_t vrcb;
1941e907febfSPyun YongHyeon 	bge_hostaddr taddr;
1942bbe2ca75SPyun YongHyeon 	uint32_t dmactl, val;
19438a315a6dSPyun YongHyeon 	int i, limit;
194495d67482SBill Paul 
194595d67482SBill Paul 	/*
194695d67482SBill Paul 	 * Initialize the memory window pointer register so that
194795d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
194895d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
194995d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
195095d67482SBill Paul 	 */
195195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
195295d67482SBill Paul 
1953822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1954822f63fcSBill Paul 
19557ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
195695d67482SBill Paul 		/* Configure mbuf memory pool */
19570dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1958822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1959822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1960822f63fcSBill Paul 		else
196195d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
196295d67482SBill Paul 
196395d67482SBill Paul 		/* Configure DMA resource pool */
19640434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
19650434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
196695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
19670434d1b8SBill Paul 	}
196895d67482SBill Paul 
196995d67482SBill Paul 	/* Configure mbuf pool watermarks */
197050515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
19711108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
19721108273aSPyun YongHyeon 		if (sc->bge_ifp->if_mtu > ETHERMTU) {
19731108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
19741108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
19751108273aSPyun YongHyeon 		} else {
19761108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
19771108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
19781108273aSPyun YongHyeon 		}
19791108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
1980fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1981fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1982fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
198338cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
198438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
198538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
198638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
198738cc658fSJohn Baldwin 	} else {
198838cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
198938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
199038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
199138cc658fSJohn Baldwin 	}
199295d67482SBill Paul 
199395d67482SBill Paul 	/* Configure DMA resource watermarks */
199495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
199595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
199695d67482SBill Paul 
199795d67482SBill Paul 	/* Enable buffer manager */
1998bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1999bbe2ca75SPyun YongHyeon 	/*
2000bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
2001bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
2002bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
2003bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
2004bbe2ca75SPyun YongHyeon 	 */
2005bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2006bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
2007bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
200895d67482SBill Paul 
200995d67482SBill Paul 	/* Poll for buffer manager start indication */
201095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2011d5d23857SJung-uk Kim 		DELAY(10);
20120c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
201395d67482SBill Paul 			break;
201495d67482SBill Paul 	}
201595d67482SBill Paul 
201695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
20175a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
201895d67482SBill Paul 		return (ENXIO);
201995d67482SBill Paul 	}
202095d67482SBill Paul 
202195d67482SBill Paul 	/* Enable flow-through queues */
20220c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
202395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
202495d67482SBill Paul 
202595d67482SBill Paul 	/* Wait until queue initialization is complete */
202695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2027d5d23857SJung-uk Kim 		DELAY(10);
202895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
202995d67482SBill Paul 			break;
203095d67482SBill Paul 	}
203195d67482SBill Paul 
203295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2033fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
203495d67482SBill Paul 		return (ENXIO);
203595d67482SBill Paul 	}
203695d67482SBill Paul 
20378a315a6dSPyun YongHyeon 	/*
20388a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
20398a315a6dSPyun YongHyeon 	 *
20408a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
20418a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
20428a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
20438a315a6dSPyun YongHyeon 	 *
20448a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
20458a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
20468a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
20478a315a6dSPyun YongHyeon 	 *   to the controller.
20488a315a6dSPyun YongHyeon 	 *
20498a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
20508a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
20518a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
20528a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
20538a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
20548a315a6dSPyun YongHyeon 	 *   be disabled.
20558a315a6dSPyun YongHyeon 	 *
20568a315a6dSPyun YongHyeon 	 * Receive Return Ring
20578a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
20588a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
20598a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
20608a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
20618a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
20628a315a6dSPyun YongHyeon 	 *
20638a315a6dSPyun YongHyeon 	 * Send Ring
20648a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
20658a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
20668a315a6dSPyun YongHyeon 	 */
20678a315a6dSPyun YongHyeon 
20688a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
2069f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2070f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
2071f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2072f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
2073f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2074f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2075f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
20761108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
20771108273aSPyun YongHyeon 		/*
20781108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
20791108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
20801108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
20811108273aSPyun YongHyeon 		 * Bit 0     : Reserved
20821108273aSPyun YongHyeon 		 */
20831108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
20841108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
20851108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
20868a315a6dSPyun YongHyeon 		/*
20878a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
20888a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
20898a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
20908a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
20918a315a6dSPyun YongHyeon 		 */
20920434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
20938a315a6dSPyun YongHyeon 	} else {
20948a315a6dSPyun YongHyeon 		/*
20958a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
20968a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
20978a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
20988a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
20998a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21008a315a6dSPyun YongHyeon 		 */
21010434d1b8SBill Paul 		rcb->bge_maxlen_flags =
21020434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
21038a315a6dSPyun YongHyeon 	}
2104bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
210550515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
210650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21071108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
21081108273aSPyun YongHyeon 	else
210995d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
21108a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
21110c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
21120c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
211367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
211467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
211595d67482SBill Paul 
21168a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
21178a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
21188a315a6dSPyun YongHyeon 
211995d67482SBill Paul 	/*
21208a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
21218a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
21228a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
212395d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
212495d67482SBill Paul 	 * high enough to require it).
212595d67482SBill Paul 	 */
21264c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2127f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
21288a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
2129f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
2130f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2131f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
2132f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2133f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2134f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2135f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
21361be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
21371be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2138bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
213950515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
214050515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21411108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
21421108273aSPyun YongHyeon 		else
214395d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
214467111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
214567111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
214667111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
214767111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
21488a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
21490434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
21500434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
215167111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
21528a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
21538a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
21548a315a6dSPyun YongHyeon 	}
215595d67482SBill Paul 
21568a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
21575e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
2158f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
215967111612SJohn Polstra 		rcb->bge_maxlen_flags =
216067111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
21610434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
21620434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
21638a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
21648a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
21650434d1b8SBill Paul 	}
216695d67482SBill Paul 
2167ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2168ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2169427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2170427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2171427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
21728d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
21738d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2174ca4f8986SPyun YongHyeon 	}
217595d67482SBill Paul 	/*
21768a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
21778a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
21788a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
21798a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
21808a315a6dSPyun YongHyeon 	 *
218195d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
218295d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
218395d67482SBill Paul 	 * each ring.
21849ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
21859ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
21869ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
21879ba784dbSScott Long 	 * are reports that it might not need to be so strict.
218838cc658fSJohn Baldwin 	 *
218938cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
219038cc658fSJohn Baldwin 	 * well.
219195d67482SBill Paul 	 */
21925345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
21936f8718a3SScott Long 		val = 8;
21946f8718a3SScott Long 	else
21956f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
21966f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
21972a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
21982a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
21992a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
22001108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
22011108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
22021108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
22031108273aSPyun YongHyeon 	}
220495d67482SBill Paul 
220595d67482SBill Paul 	/*
22068a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
22078a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
22088a315a6dSPyun YongHyeon 	 * located in NIC memory.
220995d67482SBill Paul 	 */
22108a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
22118a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
22128a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
22138a315a6dSPyun YongHyeon 	else
22148a315a6dSPyun YongHyeon 		limit = 1;
2215e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
22168a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2217e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2218e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2219e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2220e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
222195d67482SBill Paul 	}
222295d67482SBill Paul 
22238a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
2224e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2225e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2226e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2227e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2228bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
222950515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
223050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
22311108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
22321108273aSPyun YongHyeon 	else
2233e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2234e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2235e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2236e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
223795d67482SBill Paul 
22388a315a6dSPyun YongHyeon 	/*
22398a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
22408a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
22418a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
22428a315a6dSPyun YongHyeon 	 */
2243bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
224450515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
224550515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
22461108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
22471108273aSPyun YongHyeon 		limit = 16;
22481108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
22498a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
2250b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2251fe26ad88SPyun YongHyeon 	    BGE_IS_57765_PLUS(sc))
22528a315a6dSPyun YongHyeon 		limit = 4;
22538a315a6dSPyun YongHyeon 	else
22548a315a6dSPyun YongHyeon 		limit = 1;
22558a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
2256e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
22578a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2258e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2259e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2260e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
22618a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
2262e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
226338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
22643f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
2265e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
226695d67482SBill Paul 	}
226795d67482SBill Paul 
226895d67482SBill Paul 	/*
22698a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
22708a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
22718a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
227295d67482SBill Paul 	 */
2273e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2274e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2275e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2276e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
22778a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2278e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2279e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
228095d67482SBill Paul 
228195d67482SBill Paul 	/* Set random backoff seed for TX */
228295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
22830a2cc827SPyun YongHyeon 	    (IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
22844a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
22850a2cc827SPyun YongHyeon 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5]) &
228695d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
228795d67482SBill Paul 
228895d67482SBill Paul 	/* Set inter-packet gap */
228950515680SPyun YongHyeon 	val = 0x2620;
229050515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
229150515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
229250515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
229350515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
229495d67482SBill Paul 
229595d67482SBill Paul 	/*
229695d67482SBill Paul 	 * Specify which ring to use for packets that don't match
229795d67482SBill Paul 	 * any RX rules.
229895d67482SBill Paul 	 */
229995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
230095d67482SBill Paul 
230195d67482SBill Paul 	/*
230295d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
230395d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
230495d67482SBill Paul 	 */
230595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
230695d67482SBill Paul 
230795d67482SBill Paul 	/* Inialize RX list placement stats mask. */
23080c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
230995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
231095d67482SBill Paul 
231195d67482SBill Paul 	/* Disable host coalescing until we get it set up */
231295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
231395d67482SBill Paul 
231495d67482SBill Paul 	/* Poll to make sure it's shut down. */
231595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2316d5d23857SJung-uk Kim 		DELAY(10);
231795d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
231895d67482SBill Paul 			break;
231995d67482SBill Paul 	}
232095d67482SBill Paul 
232195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2322fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2323fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
232495d67482SBill Paul 		return (ENXIO);
232595d67482SBill Paul 	}
232695d67482SBill Paul 
232795d67482SBill Paul 	/* Set up host coalescing defaults */
232895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
232995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
233095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
233195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
23327ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
233395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
233495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
23350434d1b8SBill Paul 	}
2336b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2337b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
233895d67482SBill Paul 
233995d67482SBill Paul 	/* Set up address of statistics block */
23407ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
2341f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2342f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
234395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2344f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
23450434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
234695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
23470434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
23480434d1b8SBill Paul 	}
23490434d1b8SBill Paul 
23500434d1b8SBill Paul 	/* Set up address of status block */
2351f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2352f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
235395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2354f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
235595d67482SBill Paul 
235630f57f61SPyun YongHyeon 	/* Set up status block size. */
235730f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2358864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
235930f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2360864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2361864104feSPyun YongHyeon 	} else {
236230f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2363864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2364864104feSPyun YongHyeon 	}
2365864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2366864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2367864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
236830f57f61SPyun YongHyeon 
236995d67482SBill Paul 	/* Turn on host coalescing state machine */
237030f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
237195d67482SBill Paul 
237295d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
237395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
237495d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
237595d67482SBill Paul 
237695d67482SBill Paul 	/* Turn on RX list placement state machine */
237795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
237895d67482SBill Paul 
237995d67482SBill Paul 	/* Turn on RX list selector state machine. */
23807ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
238195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
238295d67482SBill Paul 
23832246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2384ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2385ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2386ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2387ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2388ea3b4127SPyun YongHyeon 
2389ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2390ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2391ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2392ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2393ea3b4127SPyun YongHyeon 	else
2394ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2395ea3b4127SPyun YongHyeon 
2396548c8f1aSPyun YongHyeon 	/* Allow APE to send/receive frames. */
2397548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2398548c8f1aSPyun YongHyeon 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2399548c8f1aSPyun YongHyeon 
2400ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
24019b80ffe7SPyun YongHyeon 	DELAY(40);
240295d67482SBill Paul 
240395d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
240491bd90d8SPyun YongHyeon 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
240595d67482SBill Paul 
240695d67482SBill Paul #ifdef notdef
240795d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
240895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
240995d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
241095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
241195d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
241295d67482SBill Paul #endif
241395d67482SBill Paul 
241495d67482SBill Paul 	/* Turn on DMA completion state machine */
24157ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
241695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
241795d67482SBill Paul 
24186f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
24196f8718a3SScott Long 
24206f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2421a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
24223889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
24236f8718a3SScott Long 
24247aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
24257aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
24267aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
24277aa4b937SPyun YongHyeon 
242895d67482SBill Paul 	/* Turn on write DMA state machine */
24296f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
24304f09c4c7SMarius Strobl 	DELAY(40);
243195d67482SBill Paul 
243295d67482SBill Paul 	/* Turn on read DMA state machine */
24334f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
24341108273aSPyun YongHyeon 
24351108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
24361108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
24371108273aSPyun YongHyeon 
2438a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2439a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2440a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2441a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2442a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2443a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
24444f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
24454f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
24461108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2447ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
24481108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
24491108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
245055a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
245155a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
245255a24a05SPyun YongHyeon 	}
245350515680SPyun YongHyeon 
2454e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
245550515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
245650515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2457e3215f76SPyun YongHyeon 		/*
2458e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2459e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2460e3215f76SPyun YongHyeon 		 */
2461e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2462e3215f76SPyun YongHyeon 	}
246350515680SPyun YongHyeon 
2464d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2465d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2466d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
24671108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
24681108273aSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc)) {
2469bbe2ca75SPyun YongHyeon 		dmactl = CSR_READ_4(sc, BGE_RDMA_RSRVCTRL);
2470bbe2ca75SPyun YongHyeon 		/*
2471bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2472bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2473bbe2ca75SPyun YongHyeon 		 */
2474f7add34cSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
2475f7add34cSPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
2476bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2477bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2478bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2479bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2480bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2481bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2482bbe2ca75SPyun YongHyeon 		}
2483d255f2a9SPyun YongHyeon 		/*
2484d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2485d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2486d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2487d255f2a9SPyun YongHyeon 		 */
2488bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL, dmactl |
2489d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2490d255f2a9SPyun YongHyeon 	}
2491bbe2ca75SPyun YongHyeon 
2492e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2493bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2494bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2495bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2496bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2497e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2498e3215f76SPyun YongHyeon 		/*
2499e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2500e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2501e3215f76SPyun YongHyeon 		 */
2502e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2503e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2504e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2505e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2506bbe2ca75SPyun YongHyeon 	}
2507bbe2ca75SPyun YongHyeon 
25084f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
25094f09c4c7SMarius Strobl 	DELAY(40);
251095d67482SBill Paul 
2511*29b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
2512*29b44b09SPyun YongHyeon 		for (i = 0; i < BGE_NUM_RDMA_CHANNELS / 2; i++) {
2513*29b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LENGTH + i * 4);
2514*29b44b09SPyun YongHyeon 			if ((val & 0xFFFF) > BGE_FRAMELEN)
2515*29b44b09SPyun YongHyeon 				break;
2516*29b44b09SPyun YongHyeon 			if (((val >> 16) & 0xFFFF) > BGE_FRAMELEN)
2517*29b44b09SPyun YongHyeon 				break;
2518*29b44b09SPyun YongHyeon 		}
2519*29b44b09SPyun YongHyeon 		if (i != BGE_NUM_RDMA_CHANNELS / 2) {
2520*29b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
2521*29b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2522*29b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5719;
2523*29b44b09SPyun YongHyeon 			else
2524*29b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5720;
2525*29b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
2526*29b44b09SPyun YongHyeon 		}
2527*29b44b09SPyun YongHyeon 	}
2528*29b44b09SPyun YongHyeon 
252995d67482SBill Paul 	/* Turn on RX data completion state machine */
253095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
253195d67482SBill Paul 
253295d67482SBill Paul 	/* Turn on RX BD initiator state machine */
253395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
253495d67482SBill Paul 
253595d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
253695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
253795d67482SBill Paul 
253895d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25397ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
254095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
254195d67482SBill Paul 
254295d67482SBill Paul 	/* Turn on send BD completion state machine */
254395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
254495d67482SBill Paul 
254595d67482SBill Paul 	/* Turn on send data completion state machine */
2546a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2547a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2548a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2549a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
255095d67482SBill Paul 
255195d67482SBill Paul 	/* Turn on send data initiator state machine */
25521108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
25531108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
25541108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2555ca3f1187SPyun YongHyeon 	else
255695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
255795d67482SBill Paul 
255895d67482SBill Paul 	/* Turn on send BD initiator state machine */
255995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
256095d67482SBill Paul 
256195d67482SBill Paul 	/* Turn on send BD selector state machine */
256295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
256395d67482SBill Paul 
25640c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
256595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
256695d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
256795d67482SBill Paul 
256895d67482SBill Paul 	/* ack/clear link change events */
256995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
25700434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
25710434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2572f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
257395d67482SBill Paul 
25746ede2cfaSPyun YongHyeon 	/*
25756ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
25766ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
25776ede2cfaSPyun YongHyeon 	 */
2578652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
257995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2580a1d52896SBill Paul 	} else {
25817ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
25827ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
25837ed3f0f0SPyun YongHyeon 			DELAY(80);
25847ed3f0f0SPyun YongHyeon 		}
25851f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
25864c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2587a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2588a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2589a1d52896SBill Paul 	}
259095d67482SBill Paul 
25911f313773SOleg Bulyzhin 	/*
25921f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
25931f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
25941f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
25951f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
25961f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
25971f313773SOleg Bulyzhin 	 */
25981f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
25991f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26001f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
26011f313773SOleg Bulyzhin 
260295d67482SBill Paul 	/* Enable link state change attentions. */
260395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
260495d67482SBill Paul 
260595d67482SBill Paul 	return (0);
260695d67482SBill Paul }
260795d67482SBill Paul 
2608d7acafa1SMarius Strobl static const struct bge_revision *
26094c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
26104c0da0ffSGleb Smirnoff {
26114c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
26124c0da0ffSGleb Smirnoff 
26134c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
26144c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
26154c0da0ffSGleb Smirnoff 			return (br);
26164c0da0ffSGleb Smirnoff 	}
26174c0da0ffSGleb Smirnoff 
26184c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
26194c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
26204c0da0ffSGleb Smirnoff 			return (br);
26214c0da0ffSGleb Smirnoff 	}
26224c0da0ffSGleb Smirnoff 
26234c0da0ffSGleb Smirnoff 	return (NULL);
26244c0da0ffSGleb Smirnoff }
26254c0da0ffSGleb Smirnoff 
2626d7acafa1SMarius Strobl static const struct bge_vendor *
26274c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26284c0da0ffSGleb Smirnoff {
26294c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26304c0da0ffSGleb Smirnoff 
26314c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26324c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26334c0da0ffSGleb Smirnoff 			return (v);
26344c0da0ffSGleb Smirnoff 
26354c0da0ffSGleb Smirnoff 	return (NULL);
26364c0da0ffSGleb Smirnoff }
26374c0da0ffSGleb Smirnoff 
2638d7acafa1SMarius Strobl static uint32_t
2639d7acafa1SMarius Strobl bge_chipid(device_t dev)
264095d67482SBill Paul {
2641978f2704SMarius Strobl 	uint32_t id;
264295d67482SBill Paul 
2643a5779553SStanislav Sedov 	id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2644a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
26451108273aSPyun YongHyeon 	if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
26461108273aSPyun YongHyeon 		/*
2647d7acafa1SMarius Strobl 		 * Find the ASCI revision.  Different chips use different
2648d7acafa1SMarius Strobl 		 * registers.
26491108273aSPyun YongHyeon 		 */
26501108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
26511108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
26521108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2653bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
265450515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
26551108273aSPyun YongHyeon 			id = pci_read_config(dev,
26561108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
26571108273aSPyun YongHyeon 			break;
2658b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2659fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57762:
2660b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2661fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57766:
2662b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
2663b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
2664b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2665b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2666b4a256acSPyun YongHyeon 			id = pci_read_config(dev,
2667b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2668b4a256acSPyun YongHyeon 			break;
26691108273aSPyun YongHyeon 		default:
2670d7acafa1SMarius Strobl 			id = pci_read_config(dev, BGE_PCI_PRODID_ASICREV, 4);
26711108273aSPyun YongHyeon 		}
26721108273aSPyun YongHyeon 	}
2673d7acafa1SMarius Strobl 	return (id);
2674d7acafa1SMarius Strobl }
2675d7acafa1SMarius Strobl 
2676d7acafa1SMarius Strobl /*
2677d7acafa1SMarius Strobl  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2678d7acafa1SMarius Strobl  * against our list and return its name if we find a match.
2679d7acafa1SMarius Strobl  *
2680d7acafa1SMarius Strobl  * Note that since the Broadcom controller contains VPD support, we
2681d7acafa1SMarius Strobl  * try to get the device name string from the controller itself instead
2682d7acafa1SMarius Strobl  * of the compiled-in string. It guarantees we'll always announce the
2683d7acafa1SMarius Strobl  * right product name. We fall back to the compiled-in string when
2684d7acafa1SMarius Strobl  * VPD is unavailable or corrupt.
2685d7acafa1SMarius Strobl  */
2686d7acafa1SMarius Strobl static int
2687d7acafa1SMarius Strobl bge_probe(device_t dev)
2688d7acafa1SMarius Strobl {
2689d7acafa1SMarius Strobl 	char buf[96];
2690d7acafa1SMarius Strobl 	char model[64];
2691d7acafa1SMarius Strobl 	const struct bge_revision *br;
2692d7acafa1SMarius Strobl 	const char *pname;
2693d7acafa1SMarius Strobl 	struct bge_softc *sc;
2694d7acafa1SMarius Strobl 	const struct bge_type *t = bge_devs;
2695d7acafa1SMarius Strobl 	const struct bge_vendor *v;
2696d7acafa1SMarius Strobl 	uint32_t id;
2697d7acafa1SMarius Strobl 	uint16_t did, vid;
2698d7acafa1SMarius Strobl 
2699d7acafa1SMarius Strobl 	sc = device_get_softc(dev);
2700d7acafa1SMarius Strobl 	sc->bge_dev = dev;
2701d7acafa1SMarius Strobl 	vid = pci_get_vendor(dev);
2702d7acafa1SMarius Strobl 	did = pci_get_device(dev);
2703d7acafa1SMarius Strobl 	while(t->bge_vid != 0) {
2704d7acafa1SMarius Strobl 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2705d7acafa1SMarius Strobl 			id = bge_chipid(dev);
27064c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
2707852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2708852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
2709d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s", pname);
2710d7acafa1SMarius Strobl 			else {
2711d7acafa1SMarius Strobl 				v = bge_lookup_vendor(vid);
2712d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s %s",
2713d7acafa1SMarius Strobl 				    v != NULL ? v->v_name : "Unknown",
27147c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
27152ad1b396SMarius Strobl 				    "NetXtreme/NetLink Ethernet Controller");
2716d7acafa1SMarius Strobl 			}
2717d7acafa1SMarius Strobl 			snprintf(buf, sizeof(buf), "%s, %sASIC rev. %#08x",
2718d7acafa1SMarius Strobl 			    model, br != NULL ? "" : "unknown ", id);
27194c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
2720d7acafa1SMarius Strobl 			return (BUS_PROBE_DEFAULT);
272195d67482SBill Paul 		}
272295d67482SBill Paul 		t++;
272395d67482SBill Paul 	}
272495d67482SBill Paul 
272595d67482SBill Paul 	return (ENXIO);
272695d67482SBill Paul }
272795d67482SBill Paul 
2728f41ac2beSBill Paul static void
27293f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2730f41ac2beSBill Paul {
2731f41ac2beSBill Paul 	int i;
2732f41ac2beSBill Paul 
27333f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2734f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2735f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
27360ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2737f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2738f41ac2beSBill Paul 	}
2739943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2740943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2741943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2742f41ac2beSBill Paul 
27433f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2744f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2745f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2746f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2747f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2748f41ac2beSBill Paul 	}
2749943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2750943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2751943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2752f41ac2beSBill Paul 
27533f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2754f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2755f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
27560ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2757f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2758f41ac2beSBill Paul 	}
2759f41ac2beSBill Paul 
27600ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
27610ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2762c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2763c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
27640ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
27650ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2766f41ac2beSBill Paul 
27673f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2768e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2769e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2770e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2771e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2772f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2773f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2774f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2775f41ac2beSBill Paul 
2776f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2777f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2778f41ac2beSBill Paul 
27793f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2780e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2781e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2782e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2783e65bed95SPyun YongHyeon 
2784e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2785e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2786f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2787f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2788f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2789f41ac2beSBill Paul 
2790f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2791f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2792f41ac2beSBill Paul 
27933f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2794e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2795e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2796e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2797e65bed95SPyun YongHyeon 
2798e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2799e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2800f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2801f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2802f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2803f41ac2beSBill Paul 
2804f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2805f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2806f41ac2beSBill Paul 
28073f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2808e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2809e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2810e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2811e65bed95SPyun YongHyeon 
2812e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2813f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2814f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2815f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2816f41ac2beSBill Paul 
2817f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2818f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2819f41ac2beSBill Paul 
28203f74909aSGleb Smirnoff 	/* Destroy status block. */
2821e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2822e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2823e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2824e65bed95SPyun YongHyeon 
2825e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2826f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2827f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2828f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2829f41ac2beSBill Paul 
2830f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2831f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2832f41ac2beSBill Paul 
28333f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2834e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2835e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2836e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2837e65bed95SPyun YongHyeon 
2838e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2839f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2840f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2841f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2842f41ac2beSBill Paul 
2843f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2844f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2845f41ac2beSBill Paul 
28465b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
28475b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
28485b610048SPyun YongHyeon 
28493f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2850f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2851f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2852f41ac2beSBill Paul }
2853f41ac2beSBill Paul 
2854f41ac2beSBill Paul static int
28555b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
28565b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
28575b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2858f41ac2beSBill Paul {
28593f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
28605b610048SPyun YongHyeon 	int error;
2861f41ac2beSBill Paul 
28625b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2863fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
28645b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
28655b610048SPyun YongHyeon 	if (error != 0) {
28665b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28675b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
28685b610048SPyun YongHyeon 		return (ENOMEM);
28695b610048SPyun YongHyeon 	}
28705b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
28715b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
28725b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
28735b610048SPyun YongHyeon 	if (error != 0) {
28745b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28755b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
28765b610048SPyun YongHyeon 		return (ENOMEM);
28775b610048SPyun YongHyeon 	}
28785b610048SPyun YongHyeon 	/* Load the address of the ring. */
28795b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
28805b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
28815b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
28825b610048SPyun YongHyeon 	if (error != 0) {
28835b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28845b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
28855b610048SPyun YongHyeon 		return (ENOMEM);
28865b610048SPyun YongHyeon 	}
28875b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
28885b610048SPyun YongHyeon 	return (0);
28895b610048SPyun YongHyeon }
28905b610048SPyun YongHyeon 
28915b610048SPyun YongHyeon static int
28925b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
28935b610048SPyun YongHyeon {
28945b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2895fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
28965b610048SPyun YongHyeon 	int i, error;
2897f41ac2beSBill Paul 
2898f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2899f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2900f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2901f41ac2beSBill Paul 	/*
2902f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2903f41ac2beSBill Paul 	 */
29044eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2905f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
29064eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
29074eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2908e65bed95SPyun YongHyeon 	if (error != 0) {
2909fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2910fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2911e65bed95SPyun YongHyeon 		return (ENOMEM);
2912e65bed95SPyun YongHyeon 	}
2913e65bed95SPyun YongHyeon 
29145b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
29155b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
29165b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
29175b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
29185b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
29195b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
29205b610048SPyun YongHyeon 	if (error)
29215b610048SPyun YongHyeon 		return (error);
29225b610048SPyun YongHyeon 
29235b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
29245b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
29255b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
29265b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
29275b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
29285b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
29295b610048SPyun YongHyeon 	if (error)
29305b610048SPyun YongHyeon 		return (error);
29315b610048SPyun YongHyeon 
29325b610048SPyun YongHyeon 	/* Create tag for TX ring. */
29335b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
29345b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
29355b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
29365b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
29375b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
29385b610048SPyun YongHyeon 	if (error)
29395b610048SPyun YongHyeon 		return (error);
29405b610048SPyun YongHyeon 
2941f41ac2beSBill Paul 	/*
29425b610048SPyun YongHyeon 	 * Create tag for status block.
29435b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
29445b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
29455b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
29465b610048SPyun YongHyeon 	 * of configured number of ring.
2947f41ac2beSBill Paul 	 */
29485b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
29495b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
29505b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
29515b610048SPyun YongHyeon 	else
29525b610048SPyun YongHyeon 		sbsz = 32;
29535b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
29545b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
29555b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
29565b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
29575b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
29585b610048SPyun YongHyeon 	if (error)
29595b610048SPyun YongHyeon 		return (error);
29605b610048SPyun YongHyeon 
296112c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
296212c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
296312c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
296412c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
296512c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
296612c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
296712c65daeSPyun YongHyeon 	if (error)
296812c65daeSPyun YongHyeon 		return (error);
296912c65daeSPyun YongHyeon 
29705b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
29715b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
29725b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
29735b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
29745b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
29755b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
29765b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
29775b610048SPyun YongHyeon 		if (error)
29785b610048SPyun YongHyeon 			return (error);
29795b610048SPyun YongHyeon 	}
29805b610048SPyun YongHyeon 
29815b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
2982d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
2983d2ffe15aSPyun YongHyeon 		/*
2984d2ffe15aSPyun YongHyeon 		 * XXX
2985d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
2986d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
2987062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
2988062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
2989d2ffe15aSPyun YongHyeon 		 */
2990062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
2991d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
2992d2ffe15aSPyun YongHyeon 	}
2993fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
2994fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
2995fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
2996fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
29975b610048SPyun YongHyeon 	if (error != 0) {
29985b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29995b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
30005b610048SPyun YongHyeon 		return (ENOMEM);
30015b610048SPyun YongHyeon 	}
30025b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
30031108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
3004ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
3005ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
3006ca3f1187SPyun YongHyeon 	} else {
3007ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
3008ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
3009ca3f1187SPyun YongHyeon 	}
30105b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
3011ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
3012ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
3013ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
3014f41ac2beSBill Paul 
3015f41ac2beSBill Paul 	if (error) {
30160ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
30170ac56796SPyun YongHyeon 		return (ENOMEM);
30180ac56796SPyun YongHyeon 	}
30190ac56796SPyun YongHyeon 
30205b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
3021f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
3022f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
3023f5459d4cSPyun YongHyeon 	else
3024f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
30255b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
3026f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
3027f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
30280ac56796SPyun YongHyeon 
30290ac56796SPyun YongHyeon 	if (error) {
30300ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3031f41ac2beSBill Paul 		return (ENOMEM);
3032f41ac2beSBill Paul 	}
3033f41ac2beSBill Paul 
30343f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3035943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3036943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3037943787f3SPyun YongHyeon 	if (error) {
3038943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3039943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3040943787f3SPyun YongHyeon 		return (ENOMEM);
3041943787f3SPyun YongHyeon 	}
3042f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
30430ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3044f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3045f41ac2beSBill Paul 		if (error) {
3046fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3047fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3048f41ac2beSBill Paul 			return (ENOMEM);
3049f41ac2beSBill Paul 		}
3050f41ac2beSBill Paul 	}
3051f41ac2beSBill Paul 
30523f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3053f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
30540ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3055f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3056f41ac2beSBill Paul 		if (error) {
3057fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30580ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3059f41ac2beSBill Paul 			return (ENOMEM);
3060f41ac2beSBill Paul 		}
3061f41ac2beSBill Paul 	}
3062f41ac2beSBill Paul 
30635b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
30644c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30655b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
30668a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
30671be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
30681be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3069f41ac2beSBill Paul 		if (error) {
3070fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30713f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3072f41ac2beSBill Paul 			return (ENOMEM);
3073f41ac2beSBill Paul 		}
30743f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3075943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3076943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3077943787f3SPyun YongHyeon 		if (error) {
3078943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
30791b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3080943787f3SPyun YongHyeon 			return (ENOMEM);
3081943787f3SPyun YongHyeon 		}
3082f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3083f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3084f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3085f41ac2beSBill Paul 			if (error) {
3086fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
30873f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3088f41ac2beSBill Paul 				return (ENOMEM);
3089f41ac2beSBill Paul 			}
3090f41ac2beSBill Paul 		}
3091f41ac2beSBill Paul 	}
3092f41ac2beSBill Paul 
3093f41ac2beSBill Paul 	return (0);
3094f41ac2beSBill Paul }
3095f41ac2beSBill Paul 
3096bf6ef57aSJohn Polstra /*
3097bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3098bf6ef57aSJohn Polstra  */
3099bf6ef57aSJohn Polstra static int
3100bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3101bf6ef57aSJohn Polstra {
3102bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
310355aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3104bf6ef57aSJohn Polstra 
310555aaf894SMarius Strobl 	d = pci_get_domain(dev);
3106bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3107bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3108bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3109bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
311055aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3111bf6ef57aSJohn Polstra 			return (1);
3112bf6ef57aSJohn Polstra 	return (0);
3113bf6ef57aSJohn Polstra }
3114bf6ef57aSJohn Polstra 
3115bf6ef57aSJohn Polstra /*
3116bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3117bf6ef57aSJohn Polstra  */
3118bf6ef57aSJohn Polstra static int
3119bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3120bf6ef57aSJohn Polstra {
3121bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3122bf6ef57aSJohn Polstra 
3123d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
31245c952e8dSPyun YongHyeon 		return (0);
31255c952e8dSPyun YongHyeon 
31261108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
31271108273aSPyun YongHyeon #ifdef DEVICE_POLLING
31281108273aSPyun YongHyeon 	return (0);
31291108273aSPyun YongHyeon #endif
3130bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3131a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3132bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3133bf6ef57aSJohn Polstra 		/*
3134a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3135a8376f70SMarius Strobl 		 * configured in single-port mode.
3136bf6ef57aSJohn Polstra 		 */
3137bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3138bf6ef57aSJohn Polstra 			can_use_msi = 1;
3139bf6ef57aSJohn Polstra 		break;
3140bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3141bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3142bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3143bf6ef57aSJohn Polstra 			can_use_msi = 1;
3144bf6ef57aSJohn Polstra 		break;
3145a8376f70SMarius Strobl 	default:
3146a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3147bf6ef57aSJohn Polstra 			can_use_msi = 1;
3148bf6ef57aSJohn Polstra 	}
3149bf6ef57aSJohn Polstra 	return (can_use_msi);
3150bf6ef57aSJohn Polstra }
3151bf6ef57aSJohn Polstra 
315295d67482SBill Paul static int
3153062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3154062af0b0SPyun YongHyeon {
3155062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3156062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3157062af0b0SPyun YongHyeon 		const uint16_t vendor;
3158062af0b0SPyun YongHyeon 		const uint16_t device;
3159062af0b0SPyun YongHyeon 		const char *desc;
316029658c96SDimitry Andric 	} mbox_reorder_lists[] = {
3161062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3162062af0b0SPyun YongHyeon 	};
3163062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3164062af0b0SPyun YongHyeon 	device_t bus, dev;
316547f4a4dcSMarius Strobl 	int i;
3166062af0b0SPyun YongHyeon 
3167062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3168062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3169062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3170062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3171062af0b0SPyun YongHyeon 	for (;;) {
3172062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3173062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3174062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3175062af0b0SPyun YongHyeon 			break;
317647f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3177062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3178062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3179062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3180062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3181062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3182062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3183062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3184062af0b0SPyun YongHyeon 				return (1);
3185062af0b0SPyun YongHyeon 			}
3186062af0b0SPyun YongHyeon 		}
3187062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
3188062af0b0SPyun YongHyeon 			break;
3189062af0b0SPyun YongHyeon 	}
3190062af0b0SPyun YongHyeon 	return (0);
3191062af0b0SPyun YongHyeon }
3192062af0b0SPyun YongHyeon 
3193ea9c3a30SPyun YongHyeon static void
3194ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3195ea9c3a30SPyun YongHyeon {
3196ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3197ea9c3a30SPyun YongHyeon 
3198ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3199ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3200ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3201ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3202ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3203ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3204ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3205ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3206ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3207ea9c3a30SPyun YongHyeon 			clk = 133;
3208ea9c3a30SPyun YongHyeon 		else {
3209ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3210ea9c3a30SPyun YongHyeon 			switch (clk) {
3211ea9c3a30SPyun YongHyeon 			case 0:
3212ea9c3a30SPyun YongHyeon 				clk = 33;
3213ea9c3a30SPyun YongHyeon 				break;
3214ea9c3a30SPyun YongHyeon 			case 2:
3215ea9c3a30SPyun YongHyeon 				clk = 50;
3216ea9c3a30SPyun YongHyeon 				break;
3217ea9c3a30SPyun YongHyeon 			case 4:
3218ea9c3a30SPyun YongHyeon 				clk = 66;
3219ea9c3a30SPyun YongHyeon 				break;
3220ea9c3a30SPyun YongHyeon 			case 6:
3221ea9c3a30SPyun YongHyeon 				clk = 100;
3222ea9c3a30SPyun YongHyeon 				break;
3223ea9c3a30SPyun YongHyeon 			case 7:
3224ea9c3a30SPyun YongHyeon 				clk = 133;
3225ea9c3a30SPyun YongHyeon 				break;
3226ea9c3a30SPyun YongHyeon 			}
3227ea9c3a30SPyun YongHyeon 		}
3228ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3229ea9c3a30SPyun YongHyeon 	} else {
3230ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3231ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3232ea9c3a30SPyun YongHyeon 		else
3233ea9c3a30SPyun YongHyeon 			printf("PCI ");
3234ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3235ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3236ea9c3a30SPyun YongHyeon 			clk = 66;
3237ea9c3a30SPyun YongHyeon 		else
3238ea9c3a30SPyun YongHyeon 			clk = 33;
3239ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3240ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3241ea9c3a30SPyun YongHyeon 		else
3242ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3243ea9c3a30SPyun YongHyeon 	}
3244ea9c3a30SPyun YongHyeon }
3245ea9c3a30SPyun YongHyeon 
3246062af0b0SPyun YongHyeon static int
32473f74909aSGleb Smirnoff bge_attach(device_t dev)
324895d67482SBill Paul {
324995d67482SBill Paul 	struct ifnet *ifp;
325095d67482SBill Paul 	struct bge_softc *sc;
3251548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
325208013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3253daeeb75cSPyun YongHyeon 	int capmask, error, msicount, reg, rid, trys;
325495d67482SBill Paul 
325595d67482SBill Paul 	sc = device_get_softc(dev);
325695d67482SBill Paul 	sc->bge_dev = dev;
325795d67482SBill Paul 
3258e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3259dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3260e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3261dfe0df9aSPyun YongHyeon 
326295d67482SBill Paul 	/*
326395d67482SBill Paul 	 * Map control/status registers.
326495d67482SBill Paul 	 */
326595d67482SBill Paul 	pci_enable_busmaster(dev);
326695d67482SBill Paul 
3267736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
32685f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
326944f8f2fcSMarius Strobl 	    RF_ACTIVE);
327095d67482SBill Paul 
327195d67482SBill Paul 	if (sc->bge_res == NULL) {
3272548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
327395d67482SBill Paul 		error = ENXIO;
327495d67482SBill Paul 		goto fail;
327595d67482SBill Paul 	}
327695d67482SBill Paul 
32774f09c4c7SMarius Strobl 	/* Save various chip information. */
3278548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3279d7acafa1SMarius Strobl 	sc->bge_chipid = bge_chipid(dev);
3280e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3281e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3282e53d81eeSPaul Saab 
3283a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3284daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
32851108273aSPyun YongHyeon 	 /*
32861108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
32871108273aSPyun YongHyeon 	  *
32881108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
32891108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
32901108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
32911108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
32921108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3293bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
329450515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
32951108273aSPyun YongHyeon 	  *
3296548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3297548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3298548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3299548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3300548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3301548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3302548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3303548c8f1aSPyun YongHyeon 	  *
33041108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
33051108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
33061108273aSPyun YongHyeon 	  */
3307bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
330850515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
330950515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3310548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
33111108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
33121108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3313daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33141108273aSPyun YongHyeon 			else
3315daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3316bbe2ca75SPyun YongHyeon 		} else {
33171108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33181108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3319daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33201108273aSPyun YongHyeon 			else
3321daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
33221108273aSPyun YongHyeon 		}
33231108273aSPyun YongHyeon 	}
3324a813ed78SPyun YongHyeon 
33255fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
33265fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
332708013fd3SMarius Strobl 
33280dae9719SJung-uk Kim 	/* Save chipset family. */
33290dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
3330fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57765:
3331fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57766:
3332fe26ad88SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_57765_PLUS;
3333fe26ad88SPyun YongHyeon 		/* FALLTHROUGH */
33341108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3335bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
333650515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
33371108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
33381108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3339b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
3340*29b44b09SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
3341*29b44b09SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3342*29b44b09SPyun YongHyeon 			/*
3343*29b44b09SPyun YongHyeon 			 * Enable work around for DMA engine miscalculation
3344*29b44b09SPyun YongHyeon 			 * of TXMBUF available space.
3345*29b44b09SPyun YongHyeon 			 */
3346*29b44b09SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_RDMA_BUG;
3347bbe2ca75SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3348bbe2ca75SPyun YongHyeon 			    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3349bbe2ca75SPyun YongHyeon 				/* Jumbo frame on BCM5719 A0 does not work. */
3350463a7e27SPyun YongHyeon 				sc->bge_flags &= ~BGE_FLAG_JUMBO;
3351bbe2ca75SPyun YongHyeon 			}
3352*29b44b09SPyun YongHyeon 		}
33531108273aSPyun YongHyeon 		break;
3354a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3355a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3356a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3357a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3358a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3359a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3360a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3361a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3362a5779553SStanislav Sedov 		break;
33630dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
33640dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
33650dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
33660dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
33677ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
33680dae9719SJung-uk Kim 		break;
33690dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
33700dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
33710dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3372f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
33739fe569d8SXin LI 		/* FALLTHROUGH */
33740dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
33750dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
337638cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
33770dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
33789fe569d8SXin LI 		/* FALLTHROUGH */
33790dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
33800dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
33810dae9719SJung-uk Kim 		break;
33820dae9719SJung-uk Kim 	}
33830dae9719SJung-uk Kim 
3384548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3385548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3386548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3387548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3388548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3389548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
3390548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3391548c8f1aSPyun YongHyeon 		break;
3392548c8f1aSPyun YongHyeon 	}
3393548c8f1aSPyun YongHyeon 
3394548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3395548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3396548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3397548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3398548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3399548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3400548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3401548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3402548c8f1aSPyun YongHyeon 			error = ENXIO;
3403548c8f1aSPyun YongHyeon 			goto fail;
3404548c8f1aSPyun YongHyeon 		}
3405548c8f1aSPyun YongHyeon 
3406548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3407548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3408548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3409548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3410548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3411548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3412548c8f1aSPyun YongHyeon 
3413548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3414548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3415548c8f1aSPyun YongHyeon 	}
3416548c8f1aSPyun YongHyeon 
3417749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3418749a5269SMarius Strobl 	bge_add_sysctls(sc);
3419749a5269SMarius Strobl 
3420a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
34211108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
34221108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3423a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3424a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3425a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3426a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3427a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3428a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3429a813ed78SPyun YongHyeon 	else
3430a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
34317ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
34327ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
34337ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3434a813ed78SPyun YongHyeon 
3435f681b29aSPyun YongHyeon 	/*
3436d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3437f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3438f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3439f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3440f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3441f681b29aSPyun YongHyeon 	 */
3442f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
34434f0794ffSBjoern A. Zeeb 
3444d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3445d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3446d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3447d9820cd8SPyun YongHyeon 
3448a7fcfcf3SPyun YongHyeon 	/*
3449a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3450a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3451a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3452a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3453a7fcfcf3SPyun YongHyeon 	 */
3454a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3455a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3456a7fcfcf3SPyun YongHyeon 
3457ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3458fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
34594f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
34604f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
34614f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
346284ac96f8SPyun YongHyeon 	}
34634f0794ffSBjoern A. Zeeb 
3464fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3465fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3466fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3467fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3468fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3469fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3470fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3471fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3472fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3473fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3474fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3475fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3476fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3477d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57791 ||
3478d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57795 ||
3479fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3480fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3481fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3482d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3483fb772a6cSMarius Strobl 	}
3484fb772a6cSMarius Strobl 
3485e53d81eeSPaul Saab 	/*
3486ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3487ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3488ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3489ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3490d7acafa1SMarius Strobl 	 * known bug which can't handle TSO if Ethernet header + IP/TCP
3491d7acafa1SMarius Strobl 	 * header is greater than 80 bytes. A workaround for the TSO
3492ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3493ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3494ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3495ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3496ca3f1187SPyun YongHyeon 	 */
34971108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
34981108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
34991108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3500bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3501bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3502bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3503bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3504bbe2ca75SPyun YongHyeon 		}
35051108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
35064f4a16e1SPyun YongHyeon 		/*
35074f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
35084f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3509be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
35104f4a16e1SPyun YongHyeon 		 */
35114f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3512be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3513be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3514ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
35154f4a16e1SPyun YongHyeon 	}
3516ca3f1187SPyun YongHyeon 
3517ca3f1187SPyun YongHyeon 	/*
35186f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3519e53d81eeSPaul Saab 	 */
35203b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
35214c0da0ffSGleb Smirnoff 		/*
35226f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
35236f8718a3SScott Long 		 * must be a PCI Express device.
35246f8718a3SScott Long 		 */
35256f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
35260aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
352748630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
352848630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
352948630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
353048630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
353150515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
353250515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
353348630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
353448630d79SPyun YongHyeon 		else
353548630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
353648630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
35376f8718a3SScott Long 	} else {
35386f8718a3SScott Long 		/*
35396f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
35406f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
35414c0da0ffSGleb Smirnoff 		 */
35423b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
35430aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
354490447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
35454c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3546652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
35476f8718a3SScott Long 	}
35484c0da0ffSGleb Smirnoff 
3549bf6ef57aSJohn Polstra 	/*
3550fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3551fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3552fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3553fd4d32feSPyun YongHyeon 	 */
3554fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3555fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3556fd4d32feSPyun YongHyeon 	/*
3557062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3558062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3559062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3560062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3561062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3562062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3563062af0b0SPyun YongHyeon 	 */
3564062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3565062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3566062af0b0SPyun YongHyeon 	/*
3567bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3568bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3569bf6ef57aSJohn Polstra 	 * normal operation.
3570bf6ef57aSJohn Polstra 	 */
35710aaf1057SPyun YongHyeon 	rid = 0;
35723b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
35730aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3574bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
3575bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
3576bf6ef57aSJohn Polstra 			if (msicount > 1)
3577bf6ef57aSJohn Polstra 				msicount = 1;
3578bf6ef57aSJohn Polstra 		} else
3579bf6ef57aSJohn Polstra 			msicount = 0;
3580bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
3581bf6ef57aSJohn Polstra 			rid = 1;
3582bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
35830aaf1057SPyun YongHyeon 		}
35840aaf1057SPyun YongHyeon 	}
3585bf6ef57aSJohn Polstra 
35861108273aSPyun YongHyeon 	/*
35871108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
35881108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
35891108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
35901108273aSPyun YongHyeon 	 */
35911108273aSPyun YongHyeon #ifndef DEVICE_POLLING
35921108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
35931108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
35941108273aSPyun YongHyeon #endif
35951108273aSPyun YongHyeon 
3596bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3597bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
3598bf6ef57aSJohn Polstra 
3599bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3600bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3601bf6ef57aSJohn Polstra 		error = ENXIO;
3602bf6ef57aSJohn Polstra 		goto fail;
3603bf6ef57aSJohn Polstra 	}
3604bf6ef57aSJohn Polstra 
3605ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
36064f09c4c7SMarius Strobl 
36078cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3608548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3609548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3610888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3611888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3612548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3613548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
36148cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
36158cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3616d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
36178cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
36188cb1383cSDoug Ambrisko 			}
36198cb1383cSDoug Ambrisko 		}
3620548c8f1aSPyun YongHyeon 	}
36218cb1383cSDoug Ambrisko 
36228cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
36233dd76c98SPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
36248cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
36258cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
36268cb1383cSDoug Ambrisko 		error = ENXIO;
36278cb1383cSDoug Ambrisko 		goto fail;
36288cb1383cSDoug Ambrisko 	}
36298cb1383cSDoug Ambrisko 
36303dd76c98SPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
36313dd76c98SPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
363295d67482SBill Paul 
363395d67482SBill Paul 	if (bge_chipinit(sc)) {
3634fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
363595d67482SBill Paul 		error = ENXIO;
363695d67482SBill Paul 		goto fail;
363795d67482SBill Paul 	}
363895d67482SBill Paul 
363938cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
364038cc658fSJohn Baldwin 	if (error) {
364108013fd3SMarius Strobl 		device_printf(sc->bge_dev,
364208013fd3SMarius Strobl 		    "failed to read station address\n");
364395d67482SBill Paul 		error = ENXIO;
364495d67482SBill Paul 		goto fail;
364595d67482SBill Paul 	}
364695d67482SBill Paul 
3647f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
36481108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
36491108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
36501108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3651f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3652f41ac2beSBill Paul 	else
3653f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3654f41ac2beSBill Paul 
36555b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3656fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3657fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3658f41ac2beSBill Paul 		error = ENXIO;
3659f41ac2beSBill Paul 		goto fail;
3660f41ac2beSBill Paul 	}
3661f41ac2beSBill Paul 
366295d67482SBill Paul 	/* Set default tuneable values. */
366395d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
366495d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
366595d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
36666f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
36676f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
366895d67482SBill Paul 
366935f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
367035f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
367135f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
367235f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
367335f945cdSPyun YongHyeon 
367495d67482SBill Paul 	/* Set up ifnet structure */
3675fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3676fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3677fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3678fc74a9f9SBrooks Davis 		error = ENXIO;
3679fc74a9f9SBrooks Davis 		goto fail;
3680fc74a9f9SBrooks Davis 	}
368195d67482SBill Paul 	ifp->if_softc = sc;
36829bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
368395d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
368495d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
368595d67482SBill Paul 	ifp->if_start = bge_start;
368695d67482SBill Paul 	ifp->if_init = bge_init;
36874d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
36884d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
36894d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
369035f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
3691d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
36924e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
36931108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3694ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
369504bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
3696ca3f1187SPyun YongHyeon 	}
36974e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
36984e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
36994e35d186SJung-uk Kim #endif
370095d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
370175719184SGleb Smirnoff #ifdef DEVICE_POLLING
370275719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
370375719184SGleb Smirnoff #endif
370495d67482SBill Paul 
3705a1d52896SBill Paul 	/*
3706d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3707d375e524SGleb Smirnoff 	 * to hardware bugs.
3708d375e524SGleb Smirnoff 	 */
3709d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3710d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
37114d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
3712d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
3713d375e524SGleb Smirnoff 	}
3714d375e524SGleb Smirnoff 
3715d375e524SGleb Smirnoff 	/*
3716a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
371741abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
371841abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
371941abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
372041abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
372141abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
372241abcc1bSPaul Saab 	 * SK-9D41.
3723a1d52896SBill Paul 	 */
3724888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3725888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
37265fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
37275fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3728f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3729f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3730fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3731f6789fbaSPyun YongHyeon 			error = ENXIO;
3732f6789fbaSPyun YongHyeon 			goto fail;
3733f6789fbaSPyun YongHyeon 		}
373441abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
373541abcc1bSPaul Saab 	}
373641abcc1bSPaul Saab 
373795d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3738ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3739ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
374070c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3741ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
374270c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
374370c2071bSPyun YongHyeon 		} else
3744652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3745ea3b4127SPyun YongHyeon 	}
374695d67482SBill Paul 
374770c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
374870c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
374970c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
375070c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
375170c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
375270c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
375370c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
375470c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
375570c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
375670c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
375770c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
375870c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
375970c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
376070c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3761fe26ad88SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3762fe26ad88SPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc)) {
376370c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
376470c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
376570c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
376670c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
376770c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
376870c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
376970c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
377070c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
377170c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
377270c2071bSPyun YongHyeon 		} else
377370c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
377470c2071bSPyun YongHyeon 	}
377570c2071bSPyun YongHyeon 
377670c2071bSPyun YongHyeon 	/*
3777d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
377870c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
377970c2071bSPyun YongHyeon 	 */
378070c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
378170c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
378270c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3783d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
378470c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
378570c2071bSPyun YongHyeon 
3786652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
37870c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
37880c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
37890c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
37906098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
37916098821cSJung-uk Kim 		    0, NULL);
379295d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
379395d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3794da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
379595d67482SBill Paul 	} else {
379695d67482SBill Paul 		/*
37978cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
37988cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
37998cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
38008cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
38018cb1383cSDoug Ambrisko 		 * the PHY.
380295d67482SBill Paul 		 */
38034012d104SMarius Strobl 		trys = 0;
38048cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
38058cb1383cSDoug Ambrisko again:
38068cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
38078cb1383cSDoug Ambrisko 
3808fb772a6cSMarius Strobl 		error = mii_attach(dev, &sc->bge_miibus, ifp, bge_ifmedia_upd,
3809daeeb75cSPyun YongHyeon 		    bge_ifmedia_sts, capmask, sc->bge_phy_addr, MII_OFFSET_ANY,
3810fb772a6cSMarius Strobl 		    MIIF_DOPAUSE);
38118e5d93dbSMarius Strobl 		if (error != 0) {
38128cb1383cSDoug Ambrisko 			if (trys++ < 4) {
38138cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3814daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3815daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
38168cb1383cSDoug Ambrisko 				goto again;
38178cb1383cSDoug Ambrisko 			}
38188e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
381995d67482SBill Paul 			goto fail;
382095d67482SBill Paul 		}
38218cb1383cSDoug Ambrisko 
38228cb1383cSDoug Ambrisko 		/*
38238cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
38248cb1383cSDoug Ambrisko 		 */
38258cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
38268cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
382795d67482SBill Paul 	}
382895d67482SBill Paul 
382995d67482SBill Paul 	/*
3830e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3831e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3832e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3833e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3834e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3835e255b776SJohn Polstra 	 * payloads by copying the received packets.
3836e255b776SJohn Polstra 	 */
3837652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3838652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3839652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3840e255b776SJohn Polstra 
3841e255b776SJohn Polstra 	/*
384295d67482SBill Paul 	 * Call MI attach routine.
384395d67482SBill Paul 	 */
3844fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
38450f9bd73bSSam Leffler 
384661ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
384761ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
384861ccb9daSPyun YongHyeon 
38490f9bd73bSSam Leffler 	/*
38500f9bd73bSSam Leffler 	 * Hookup IRQ last.
38510f9bd73bSSam Leffler 	 */
3852dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3853dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
38547e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
38557e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3856dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3857dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3858dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3859dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3860dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3861e010b055SPyun YongHyeon 			error = ENOMEM;
3862dfe0df9aSPyun YongHyeon 			goto fail;
3863dfe0df9aSPyun YongHyeon 		}
3864d7acafa1SMarius Strobl 		error = taskqueue_start_threads(&sc->bge_tq, 1, PI_NET,
3865d7acafa1SMarius Strobl 		    "%s taskq", device_get_nameunit(sc->bge_dev));
3866d7acafa1SMarius Strobl 		if (error != 0) {
3867d7acafa1SMarius Strobl 			device_printf(dev, "could not start threads.\n");
3868d7acafa1SMarius Strobl 			ether_ifdetach(ifp);
3869d7acafa1SMarius Strobl 			goto fail;
3870d7acafa1SMarius Strobl 		}
3871dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3872dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3873dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3874dfe0df9aSPyun YongHyeon 	} else
3875dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3876dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3877dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
38780f9bd73bSSam Leffler 
38790f9bd73bSSam Leffler 	if (error) {
3880e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3881fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
38820f9bd73bSSam Leffler 	}
388395d67482SBill Paul 
388495d67482SBill Paul fail:
3885e010b055SPyun YongHyeon 	if (error)
3886e010b055SPyun YongHyeon 		bge_detach(dev);
388795d67482SBill Paul 	return (error);
388895d67482SBill Paul }
388995d67482SBill Paul 
389095d67482SBill Paul static int
38913f74909aSGleb Smirnoff bge_detach(device_t dev)
389295d67482SBill Paul {
389395d67482SBill Paul 	struct bge_softc *sc;
389495d67482SBill Paul 	struct ifnet *ifp;
389595d67482SBill Paul 
389695d67482SBill Paul 	sc = device_get_softc(dev);
3897fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
389895d67482SBill Paul 
389975719184SGleb Smirnoff #ifdef DEVICE_POLLING
390075719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
390175719184SGleb Smirnoff 		ether_poll_deregister(ifp);
390275719184SGleb Smirnoff #endif
390375719184SGleb Smirnoff 
3904e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3905e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
39060f9bd73bSSam Leffler 		BGE_LOCK(sc);
390795d67482SBill Paul 		bge_stop(sc);
39080f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
39095dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3910e010b055SPyun YongHyeon 	}
39115dda8085SOleg Bulyzhin 
3912dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3913dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
391495d67482SBill Paul 
39150aba72ddSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
391695d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
39170aba72ddSPyun YongHyeon 	else if (sc->bge_miibus != NULL) {
391895d67482SBill Paul 		bus_generic_detach(dev);
391995d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
392095d67482SBill Paul 	}
392195d67482SBill Paul 
392295d67482SBill Paul 	bge_release_resources(sc);
392395d67482SBill Paul 
392495d67482SBill Paul 	return (0);
392595d67482SBill Paul }
392695d67482SBill Paul 
392795d67482SBill Paul static void
39283f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
392995d67482SBill Paul {
393095d67482SBill Paul 	device_t dev;
393195d67482SBill Paul 
393295d67482SBill Paul 	dev = sc->bge_dev;
393395d67482SBill Paul 
3934dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3935dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3936dfe0df9aSPyun YongHyeon 
393795d67482SBill Paul 	if (sc->bge_intrhand != NULL)
393895d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
393995d67482SBill Paul 
394095d67482SBill Paul 	if (sc->bge_irq != NULL)
3941724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3942724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3943724bd939SJohn Polstra 
3944724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3945724bd939SJohn Polstra 		pci_release_msi(dev);
394695d67482SBill Paul 
394795d67482SBill Paul 	if (sc->bge_res != NULL)
394895d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3949736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
395095d67482SBill Paul 
3951548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
3952548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
3953548c8f1aSPyun YongHyeon 		    PCIR_BAR(2), sc->bge_res2);
3954548c8f1aSPyun YongHyeon 
3955ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3956ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3957ad61f896SRuslan Ermilov 
3958f41ac2beSBill Paul 	bge_dma_free(sc);
395995d67482SBill Paul 
39600f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
39610f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
396295d67482SBill Paul }
396395d67482SBill Paul 
39648cb1383cSDoug Ambrisko static int
39653f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
396695d67482SBill Paul {
396795d67482SBill Paul 	device_t dev;
3968cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
39696f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
39700aaf1057SPyun YongHyeon 	uint16_t devctl;
39715fea260fSMarius Strobl 	int i;
397295d67482SBill Paul 
397395d67482SBill Paul 	dev = sc->bge_dev;
397495d67482SBill Paul 
3975cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
3976548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
3977548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
3978cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
3979cc085b36SPyun YongHyeon 
398038cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
398138cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
39826f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
39836f8718a3SScott Long 			write_op = bge_writemem_direct;
39846f8718a3SScott Long 		else
39856f8718a3SScott Long 			write_op = bge_writemem_ind;
39869ba784dbSScott Long 	} else
39876f8718a3SScott Long 		write_op = bge_writereg_ind;
39886f8718a3SScott Long 
39893dd76c98SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
39903dd76c98SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5701) {
39913dd76c98SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
39923dd76c98SPyun YongHyeon 		for (i = 0; i < 8000; i++) {
39933dd76c98SPyun YongHyeon 			if (CSR_READ_4(sc, BGE_NVRAM_SWARB) &
39943dd76c98SPyun YongHyeon 			    BGE_NVRAMSWARB_GNT1)
39953dd76c98SPyun YongHyeon 				break;
39963dd76c98SPyun YongHyeon 			DELAY(20);
39973dd76c98SPyun YongHyeon 		}
39983dd76c98SPyun YongHyeon 		if (i == 8000) {
39993dd76c98SPyun YongHyeon 			if (bootverbose)
40003dd76c98SPyun YongHyeon 				device_printf(dev, "NVRAM lock timedout!\n");
40013dd76c98SPyun YongHyeon 		}
40023dd76c98SPyun YongHyeon 	}
4003548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
4004548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
4005548c8f1aSPyun YongHyeon 
400695d67482SBill Paul 	/* Save some important PCI state. */
400795d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
400895d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
400995d67482SBill Paul 
401095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
401195d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4012e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
401395d67482SBill Paul 
40146f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
40156f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
4016a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
40176f8718a3SScott Long 		if (bootverbose)
4018333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
40196f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
40206f8718a3SScott Long 	}
40216f8718a3SScott Long 
40226f8718a3SScott Long 	/*
40236f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
40246f8718a3SScott Long 	 * When firmware finishes its initialization it will
4025888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
40266f8718a3SScott Long 	 */
4027888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
40286f8718a3SScott Long 
40290c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4030e53d81eeSPaul Saab 
4031e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4032652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4033ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4034ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
40350c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
40360c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4037ad49eccfSPyun YongHyeon 		}
4038e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4039e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
40400c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
40410c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4042e53d81eeSPaul Saab 		}
4043e53d81eeSPaul Saab 	}
4044e53d81eeSPaul Saab 
4045df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4046df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4047df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4048df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4049df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4050df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4051df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4052df4db538SPyun YongHyeon 	}
4053df4db538SPyun YongHyeon 
405421c9e407SDavid Christensen 	/*
40556f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
40566f8718a3SScott Long 	 * powered up in D0 uninitialized.
40576f8718a3SScott Long 	 */
40585512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
40595512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4060caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
40616f8718a3SScott Long 
406295d67482SBill Paul 	/* Issue global reset */
40636f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
406495d67482SBill Paul 
4065cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4066cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4067cc085b36SPyun YongHyeon 	else
406895d67482SBill Paul 		DELAY(1000);
406995d67482SBill Paul 
4070e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4071652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4072e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4073e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
40745fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
40755fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4076e53d81eeSPaul Saab 		}
40770aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4078389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
40790aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4080389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4081389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4082389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
40830aaf1057SPyun YongHyeon 		    devctl, 2);
408448630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
40850aaf1057SPyun YongHyeon 		/* Clear error status. */
4086389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4087389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4088389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4089389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4090e53d81eeSPaul Saab 	}
4091e53d81eeSPaul Saab 
40923f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
409395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
409495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4095e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4096cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4097cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4098cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4099cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4100548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4101548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4102548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4103548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4104cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
410595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
410695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4107cbb2b2feSPyun YongHyeon 	/*
4108cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4109fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4110cbb2b2feSPyun YongHyeon 	 * read stale status block.
4111cbb2b2feSPyun YongHyeon 	 */
4112cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4113cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4114cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4115cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4116cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4117cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4118cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4119cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4120cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4121cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4122cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4123cbb2b2feSPyun YongHyeon 		}
4124cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4125cbb2b2feSPyun YongHyeon 		    devctl, 2);
4126cbb2b2feSPyun YongHyeon 	}
412722a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
41284c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4129bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4130bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
41310aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
41320aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
41330aaf1057SPyun YongHyeon 			pci_write_config(dev,
41340aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4135bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4136bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4137bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4138bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4139bf6ef57aSJohn Polstra 		}
41404c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
41414c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
41424c0da0ffSGleb Smirnoff 	} else
4143a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4144a7b0c314SPaul Saab 
4145cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4146cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4147cc085b36SPyun YongHyeon 
4148cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4149cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4150cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4151cc085b36SPyun YongHyeon 	DELAY(40);
4152cc085b36SPyun YongHyeon 
4153548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4154548c8f1aSPyun YongHyeon 
415538cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
415638cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
415738cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
415838cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
415938cc658fSJohn Baldwin 				break;
416038cc658fSJohn Baldwin 			DELAY(100);
416138cc658fSJohn Baldwin 		}
416238cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4163333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
416438cc658fSJohn Baldwin 			return (1);
416538cc658fSJohn Baldwin 		}
416638cc658fSJohn Baldwin 	} else {
416795d67482SBill Paul 		/*
41686f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
416908013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
41705fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
41715fea260fSMarius Strobl 		 * address is fitted though.
417295d67482SBill Paul 		 */
417395d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4174d5d23857SJung-uk Kim 			DELAY(10);
4175888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4176888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
417795d67482SBill Paul 				break;
417895d67482SBill Paul 		}
417995d67482SBill Paul 
41805fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4181333704a3SPyun YongHyeon 			device_printf(dev,
4182333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4183333704a3SPyun YongHyeon 			    val);
4184b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4185b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4186b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
418738cc658fSJohn Baldwin 	}
418895d67482SBill Paul 
418995d67482SBill Paul 	/*
4190da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4191da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4192da3003f0SBill Paul 	 * to 1.2V.
4193da3003f0SBill Paul 	 */
4194652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4195652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
41965fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
41975fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
41985fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4199da3003f0SBill Paul 	}
4200da3003f0SBill Paul 
4201e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4202652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4203b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4204a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4205a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4206a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
42075fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
42085fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4209e53d81eeSPaul Saab 	}
42108cb1383cSDoug Ambrisko 
421150515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
421250515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
421350515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
421450515680SPyun YongHyeon 
42158cb1383cSDoug Ambrisko 	return (0);
421695d67482SBill Paul }
421795d67482SBill Paul 
4218e0b7b101SPyun YongHyeon static __inline void
4219e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4220e0b7b101SPyun YongHyeon {
4221e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4222e0b7b101SPyun YongHyeon 
4223e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4224e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4225e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4226e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4227e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4228e0b7b101SPyun YongHyeon }
4229e0b7b101SPyun YongHyeon 
4230e0b7b101SPyun YongHyeon static __inline void
4231e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4232e0b7b101SPyun YongHyeon {
4233e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4234e0b7b101SPyun YongHyeon 
4235e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4236e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4237e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4238e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4239e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4240e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4241e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4242e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4243e0b7b101SPyun YongHyeon }
4244e0b7b101SPyun YongHyeon 
424595d67482SBill Paul /*
424695d67482SBill Paul  * Frame reception handling. This is called if there's a frame
424795d67482SBill Paul  * on the receive return list.
424895d67482SBill Paul  *
424995d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
42501be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
425195d67482SBill Paul  * 2) the frame is from the standard receive ring
425295d67482SBill Paul  */
425395d67482SBill Paul 
42541abcdbd1SAttilio Rao static int
4255dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
425695d67482SBill Paul {
425795d67482SBill Paul 	struct ifnet *ifp;
42581abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4259b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
426095d67482SBill Paul 
42617f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
42620f9bd73bSSam Leffler 
42633f74909aSGleb Smirnoff 	/* Nothing to do. */
42647f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
42651abcdbd1SAttilio Rao 		return (rx_npkts);
4266cfcb5025SOleg Bulyzhin 
4267fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
426895d67482SBill Paul 
4269f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4270e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4271f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
427215eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4273f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4274f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4275c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
4276f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
427715eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4278f41ac2beSBill Paul 
42797f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
428095d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
42813f74909aSGleb Smirnoff 		uint32_t		rxidx;
428295d67482SBill Paul 		struct mbuf		*m = NULL;
42833f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
428495d67482SBill Paul 		int			have_tag = 0;
428595d67482SBill Paul 
428675719184SGleb Smirnoff #ifdef DEVICE_POLLING
428775719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
428875719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
428975719184SGleb Smirnoff 				break;
429075719184SGleb Smirnoff 			sc->rxcycles--;
429175719184SGleb Smirnoff 		}
429275719184SGleb Smirnoff #endif
429375719184SGleb Smirnoff 
42947f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
429595d67482SBill Paul 
429695d67482SBill Paul 		rxidx = cur_rx->bge_idx;
42977f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
429895d67482SBill Paul 
4299cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
4300cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
430195d67482SBill Paul 			have_tag = 1;
430295d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
430395d67482SBill Paul 		}
430495d67482SBill Paul 
430595d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
430695d67482SBill Paul 			jumbocnt++;
4307943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
430895d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4309e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
431095d67482SBill Paul 				continue;
431195d67482SBill Paul 			}
4312943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4313e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4314943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
431595d67482SBill Paul 				continue;
431695d67482SBill Paul 			}
431703e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
431895d67482SBill Paul 		} else {
431995d67482SBill Paul 			stdcnt++;
4320e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
432195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4322e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
432395d67482SBill Paul 				continue;
432495d67482SBill Paul 			}
4325943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4326e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4327943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
432895d67482SBill Paul 				continue;
432995d67482SBill Paul 			}
433003e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
433195d67482SBill Paul 		}
433295d67482SBill Paul 
433395d67482SBill Paul 		ifp->if_ipackets++;
4334e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4335e255b776SJohn Polstra 		/*
4336e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4337e65bed95SPyun YongHyeon 		 * the payload is aligned.
4338e255b776SJohn Polstra 		 */
4339652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4340e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4341e255b776SJohn Polstra 			    cur_rx->bge_len);
4342e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4343e255b776SJohn Polstra 		}
4344e255b776SJohn Polstra #endif
4345473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
434695d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
434795d67482SBill Paul 
43481108273aSPyun YongHyeon 		if (ifp->if_capenable & IFCAP_RXCSUM)
43491108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
435095d67482SBill Paul 
435195d67482SBill Paul 		/*
4352673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4353673d9191SSam Leffler 		 * attach that information to the packet.
435495d67482SBill Paul 		 */
4355d147662cSGleb Smirnoff 		if (have_tag) {
435678ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
435778ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4358d147662cSGleb Smirnoff 		}
435995d67482SBill Paul 
4360dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
43610f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4362673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
43630f9bd73bSSam Leffler 			BGE_LOCK(sc);
4364dfe0df9aSPyun YongHyeon 		} else
4365dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
4366d4da719cSAttilio Rao 		rx_npkts++;
436725e13e68SXin LI 
436825e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
43698cf7d13dSAttilio Rao 			return (rx_npkts);
437095d67482SBill Paul 	}
437195d67482SBill Paul 
437215eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
437315eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4374e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4375f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4376e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
43774c0da0ffSGleb Smirnoff 
4378c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4379f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
43804c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4381f41ac2beSBill Paul 
43827f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
438338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
438495d67482SBill Paul 	if (stdcnt)
4385767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4386767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
438795d67482SBill Paul 	if (jumbocnt)
4388767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4389767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4390f5a034f9SPyun YongHyeon #ifdef notyet
4391f5a034f9SPyun YongHyeon 	/*
4392f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4393f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4394f5a034f9SPyun YongHyeon 	 */
4395f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4396f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
4397f5a034f9SPyun YongHyeon #endif
43981abcdbd1SAttilio Rao 	return (rx_npkts);
439995d67482SBill Paul }
440095d67482SBill Paul 
440195d67482SBill Paul static void
44021108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
44031108273aSPyun YongHyeon {
44041108273aSPyun YongHyeon 
44051108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
44061108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
44071108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44081108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44091108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
44101108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
44111108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44121108273aSPyun YongHyeon 			}
44131108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
44141108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
44151108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
44161108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44171108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
44181108273aSPyun YongHyeon 			}
44191108273aSPyun YongHyeon 		}
44201108273aSPyun YongHyeon 	} else {
44211108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44221108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44231108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
44241108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44251108273aSPyun YongHyeon 		}
44261108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
44271108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
44281108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
44291108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
44301108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44311108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
44321108273aSPyun YongHyeon 		}
44331108273aSPyun YongHyeon 	}
44341108273aSPyun YongHyeon }
44351108273aSPyun YongHyeon 
44361108273aSPyun YongHyeon static void
4437b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
443895d67482SBill Paul {
443995a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
444095d67482SBill Paul 	struct ifnet *ifp;
444195d67482SBill Paul 
44420f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
44430f9bd73bSSam Leffler 
44443f74909aSGleb Smirnoff 	/* Nothing to do. */
4445b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4446cfcb5025SOleg Bulyzhin 		return;
4447cfcb5025SOleg Bulyzhin 
4448fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
444995d67482SBill Paul 
4450e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
44515c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
445295d67482SBill Paul 	/*
445395d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
445495d67482SBill Paul 	 * frames that have been sent.
445595d67482SBill Paul 	 */
4456b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
445795a0a340SPyun YongHyeon 		uint32_t		idx;
445895d67482SBill Paul 
445995d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4460f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
446195d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
446295d67482SBill Paul 			ifp->if_opackets++;
446395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
44640ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4465e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4466e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
44670ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4468f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4469e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4470e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
447195d67482SBill Paul 		}
447295d67482SBill Paul 		sc->bge_txcnt--;
447395d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
447495d67482SBill Paul 	}
447595d67482SBill Paul 
447613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
44775b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
44785b01e77cSBruce Evans 		sc->bge_timer = 0;
447995d67482SBill Paul }
448095d67482SBill Paul 
448175719184SGleb Smirnoff #ifdef DEVICE_POLLING
44821abcdbd1SAttilio Rao static int
448375719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
448475719184SGleb Smirnoff {
448575719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
4486b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4487366454f2SOleg Bulyzhin 	uint32_t statusword;
44881abcdbd1SAttilio Rao 	int rx_npkts = 0;
448975719184SGleb Smirnoff 
44903f74909aSGleb Smirnoff 	BGE_LOCK(sc);
44913f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
44923f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
44931abcdbd1SAttilio Rao 		return (rx_npkts);
44943f74909aSGleb Smirnoff 	}
449575719184SGleb Smirnoff 
4496dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4497b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4498b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
44992246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4500b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4501b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4502dab5cd05SOleg Bulyzhin 
4503175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
45042246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4505175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4506dab5cd05SOleg Bulyzhin 
4507dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4508b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4509b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4510366454f2SOleg Bulyzhin 
45110c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4512366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4513366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4514366454f2SOleg Bulyzhin 
4515366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4516366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
45174c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4518652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4519366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4520366454f2SOleg Bulyzhin 
4521366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4522dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
452325e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
452425e13e68SXin LI 		BGE_UNLOCK(sc);
45258cf7d13dSAttilio Rao 		return (rx_npkts);
452625e13e68SXin LI 	}
4527b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4528366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4529366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
45303f74909aSGleb Smirnoff 
45313f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
45321abcdbd1SAttilio Rao 	return (rx_npkts);
453375719184SGleb Smirnoff }
453475719184SGleb Smirnoff #endif /* DEVICE_POLLING */
453575719184SGleb Smirnoff 
4536dfe0df9aSPyun YongHyeon static int
4537dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4538dfe0df9aSPyun YongHyeon {
4539dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4540dfe0df9aSPyun YongHyeon 
4541dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4542dfe0df9aSPyun YongHyeon 	/*
4543dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4544dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4545dfe0df9aSPyun YongHyeon 	 */
4546dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4547dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4548dfe0df9aSPyun YongHyeon }
4549dfe0df9aSPyun YongHyeon 
4550dfe0df9aSPyun YongHyeon static void
4551dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4552dfe0df9aSPyun YongHyeon {
4553dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4554dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
45551108273aSPyun YongHyeon 	uint32_t status, status_tag;
4556dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4557dfe0df9aSPyun YongHyeon 
4558dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4559dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4560dfe0df9aSPyun YongHyeon 
456166151edfSPyun YongHyeon 	BGE_LOCK(sc);
456266151edfSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
456366151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4564dfe0df9aSPyun YongHyeon 		return;
456566151edfSPyun YongHyeon 	}
4566dfe0df9aSPyun YongHyeon 
4567dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4568dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4569dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4570dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4571dfe0df9aSPyun YongHyeon 
45722246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4573dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4574dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4575dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
45761108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
45772246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4578dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4579dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4580dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4581dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
45821108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
45831108273aSPyun YongHyeon 		status_tag = 0;
458466151edfSPyun YongHyeon 
458566151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
458666151edfSPyun YongHyeon 		bge_link_upd(sc);
458766151edfSPyun YongHyeon 
4588dfe0df9aSPyun YongHyeon 	/* Let controller work. */
45891108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4590dfe0df9aSPyun YongHyeon 
459166151edfSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
459266151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4593dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
459466151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4595dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
459666151edfSPyun YongHyeon 		BGE_LOCK(sc);
4597dfe0df9aSPyun YongHyeon 	}
4598dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4599dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4600dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4601dfe0df9aSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4602dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4603dfe0df9aSPyun YongHyeon 	}
460466151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4605dfe0df9aSPyun YongHyeon }
4606dfe0df9aSPyun YongHyeon 
460795d67482SBill Paul static void
46083f74909aSGleb Smirnoff bge_intr(void *xsc)
460995d67482SBill Paul {
461095d67482SBill Paul 	struct bge_softc *sc;
461195d67482SBill Paul 	struct ifnet *ifp;
4612dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4613b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
461495d67482SBill Paul 
461595d67482SBill Paul 	sc = xsc;
4616f41ac2beSBill Paul 
46170f9bd73bSSam Leffler 	BGE_LOCK(sc);
46180f9bd73bSSam Leffler 
4619dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4620dab5cd05SOleg Bulyzhin 
462175719184SGleb Smirnoff #ifdef DEVICE_POLLING
462275719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
462375719184SGleb Smirnoff 		BGE_UNLOCK(sc);
462475719184SGleb Smirnoff 		return;
462575719184SGleb Smirnoff 	}
462675719184SGleb Smirnoff #endif
462775719184SGleb Smirnoff 
4628f30cbfc6SScott Long 	/*
4629b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4630b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4631b848e032SBruce Evans 	 * our current organization this just gives complications and
4632b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4633b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4634b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4635b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4636b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4637b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4638b848e032SBruce Evans 	 *
4639b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4640b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4641b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4642b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4643b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4644b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4645b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4646b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4647b848e032SBruce Evans 	 */
464838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4649b848e032SBruce Evans 
4650f584dfd1SPyun YongHyeon 	/*
4651f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4652f584dfd1SPyun YongHyeon 	 */
4653f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4654f584dfd1SPyun YongHyeon 
4655f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4656f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4657f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4658f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4659f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4660f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4661f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4662f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4663f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4664f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4665f584dfd1SPyun YongHyeon 
46661f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
46674c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4668f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4669dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
467095d67482SBill Paul 
467113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46723f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4673dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
467425e13e68SXin LI 	}
467595d67482SBill Paul 
467625e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46773f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4678b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
467995d67482SBill Paul 	}
468095d67482SBill Paul 
468113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
468213f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
46830f9bd73bSSam Leffler 		bge_start_locked(ifp);
46840f9bd73bSSam Leffler 
46850f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
468695d67482SBill Paul }
468795d67482SBill Paul 
468895d67482SBill Paul static void
46898cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
46908cb1383cSDoug Ambrisko {
46918cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
46928cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
46938cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
46948cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
46958cb1383cSDoug Ambrisko 		else {
4696899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4697888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
46983c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4699888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4700941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4701941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
47023fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
47039931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
47049931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
47058cb1383cSDoug Ambrisko 		}
47068cb1383cSDoug Ambrisko 	}
47078cb1383cSDoug Ambrisko }
47088cb1383cSDoug Ambrisko 
47098cb1383cSDoug Ambrisko static void
4710b74e67fbSGleb Smirnoff bge_tick(void *xsc)
47110f9bd73bSSam Leffler {
4712b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
471395d67482SBill Paul 	struct mii_data *mii = NULL;
471495d67482SBill Paul 
47150f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
471695d67482SBill Paul 
47175dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
47185dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
47195dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
47205dda8085SOleg Bulyzhin 		return;
47215dda8085SOleg Bulyzhin 
47227ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
47230434d1b8SBill Paul 		bge_stats_update_regs(sc);
47240434d1b8SBill Paul 	else
472595d67482SBill Paul 		bge_stats_update(sc);
472695d67482SBill Paul 
4727548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4728548c8f1aSPyun YongHyeon 
4729652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
473095d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
473182b67c01SOleg Bulyzhin 		/*
473282b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
473382b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
473482b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
473582b67c01SOleg Bulyzhin 		 */
473682b67c01SOleg Bulyzhin 		if (!sc->bge_link)
473795d67482SBill Paul 			mii_tick(mii);
47387b97099dSOleg Bulyzhin 	} else {
47397b97099dSOleg Bulyzhin 		/*
47407b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
47417b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
47427b97099dSOleg Bulyzhin 		 * and trigger interrupt.
47437b97099dSOleg Bulyzhin 		 */
47447b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
47453f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
47467b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
47477b97099dSOleg Bulyzhin #endif
47487b97099dSOleg Bulyzhin 		{
47497b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
47504f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
47514f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
47527b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
47534f0794ffSBjoern A. Zeeb 		else
47544f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
47557b97099dSOleg Bulyzhin 		}
4756dab5cd05SOleg Bulyzhin 	}
475795d67482SBill Paul 
47588cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4759b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
47608cb1383cSDoug Ambrisko 
4761dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
476295d67482SBill Paul }
476395d67482SBill Paul 
476495d67482SBill Paul static void
47653f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
47660434d1b8SBill Paul {
47673f74909aSGleb Smirnoff 	struct ifnet *ifp;
47682280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
4769*29b44b09SPyun YongHyeon 	uint32_t val;
47700434d1b8SBill Paul 
4771fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
47722280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
47730434d1b8SBill Paul 
47742280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
47752280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
47762280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
47772280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
47782280c16bSPyun YongHyeon 	stats->outXonSent +=
47792280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
47802280c16bSPyun YongHyeon 	stats->outXoffSent +=
47812280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
47822280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
47832280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
47842280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
47852280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
47862280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
47872280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
47882280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
47892280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
47902280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
47912280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
47922280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
47932280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
47942280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
47952280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
47962280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
47972280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
47982280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
47992280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48007e6e2507SJung-uk Kim 
48012280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
48022280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48032280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
48042280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48052280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
48062280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48072280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
48082280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48092280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
48102280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48112280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
48122280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48132280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
48142280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48152280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
48162280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48172280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
48182280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48192280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
48202280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
48212280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
48222280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
48232280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
48242280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
48252280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
48262280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48272280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
48282280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48292280c16bSPyun YongHyeon 
48302280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
48312280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48322280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
48332280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48342280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
48352280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48362280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
48372280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4838f78094a5SPyun YongHyeon 	/*
4839f78094a5SPyun YongHyeon 	 * XXX
4840f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4841f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4842f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4843f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4844f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4845f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4846f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4847f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4848f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4849f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4850f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4851f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4852f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4853f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4854f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4855f78094a5SPyun YongHyeon 	 * silicon bug.
4856f78094a5SPyun YongHyeon 	 */
4857f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4858f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4859f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
48602280c16bSPyun YongHyeon 		stats->InputDiscards +=
48612280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
48622280c16bSPyun YongHyeon 	stats->InputErrors +=
48632280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
48642280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
48652280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
48662280c16bSPyun YongHyeon 
48672280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
48682280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
48692280c16bSPyun YongHyeon 	    stats->InputErrors);
4870*29b44b09SPyun YongHyeon 
4871*29b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
4872*29b44b09SPyun YongHyeon 		/*
4873*29b44b09SPyun YongHyeon 		 * If controller transmitted more than BGE_NUM_RDMA_CHANNELS
4874*29b44b09SPyun YongHyeon 		 * frames, it's safe to disable workaround for DMA engine's
4875*29b44b09SPyun YongHyeon 		 * miscalculation of TXMBUF space.
4876*29b44b09SPyun YongHyeon 		 */
4877*29b44b09SPyun YongHyeon 		if (stats->ifHCOutUcastPkts + stats->ifHCOutMulticastPkts +
4878*29b44b09SPyun YongHyeon 		    stats->ifHCOutBroadcastPkts > BGE_NUM_RDMA_CHANNELS) {
4879*29b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
4880*29b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
4881*29b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5719;
4882*29b44b09SPyun YongHyeon 			else
4883*29b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5720;
4884*29b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
4885*29b44b09SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_RDMA_BUG;
4886*29b44b09SPyun YongHyeon 		}
4887*29b44b09SPyun YongHyeon 	}
48882280c16bSPyun YongHyeon }
48892280c16bSPyun YongHyeon 
48902280c16bSPyun YongHyeon static void
48912280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
48922280c16bSPyun YongHyeon {
48932280c16bSPyun YongHyeon 
48942280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48952280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48962280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48972280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48982280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48992280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
49002280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
49012280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
49022280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
49032280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
49042280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
49052280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
49062280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
49072280c16bSPyun YongHyeon 
49082280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
49092280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
49102280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
49112280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
49122280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
49132280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
49142280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
49152280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
49162280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
49172280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
49182280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
49192280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
49202280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
49212280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
49222280c16bSPyun YongHyeon 
49232280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
49242280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
49252280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
49262280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
49272280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49282280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49292280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49300434d1b8SBill Paul }
49310434d1b8SBill Paul 
49320434d1b8SBill Paul static void
49333f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
493495d67482SBill Paul {
493595d67482SBill Paul 	struct ifnet *ifp;
4936e907febfSPyun YongHyeon 	bus_size_t stats;
49377e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
493895d67482SBill Paul 
4939fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
494095d67482SBill Paul 
4941e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4942e907febfSPyun YongHyeon 
4943e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4944e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
494595d67482SBill Paul 
49468634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
49476b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
49486fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
49496fb34dd2SOleg Bulyzhin 
495037ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
495137ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_nobds);
495237ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
495337ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
495437ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_inerrs);
495537ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
49566fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
49576b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
49586fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
49596fb34dd2SOleg Bulyzhin 
49606fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
49616b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
49626fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
496395d67482SBill Paul 
4964e907febfSPyun YongHyeon #undef	READ_STAT
496595d67482SBill Paul }
496695d67482SBill Paul 
496795d67482SBill Paul /*
4968d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
4969d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
4970d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
4971d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
4972d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
4973d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
4974d375e524SGleb Smirnoff  */
4975d375e524SGleb Smirnoff static __inline int
4976d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
4977d375e524SGleb Smirnoff {
4978d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
4979d375e524SGleb Smirnoff 	struct mbuf *last;
4980d375e524SGleb Smirnoff 
4981d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
4982d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
4983d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
4984d375e524SGleb Smirnoff 		last = m;
4985d375e524SGleb Smirnoff 	} else {
4986d375e524SGleb Smirnoff 		/*
4987d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
4988d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
4989d375e524SGleb Smirnoff 		 */
4990d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
4991d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
4992d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
4993d375e524SGleb Smirnoff 			struct mbuf *n;
4994d375e524SGleb Smirnoff 
4995c6499eccSGleb Smirnoff 			MGET(n, M_NOWAIT, MT_DATA);
4996d375e524SGleb Smirnoff 			if (n == NULL)
4997d375e524SGleb Smirnoff 				return (ENOBUFS);
4998d375e524SGleb Smirnoff 			n->m_len = 0;
4999d375e524SGleb Smirnoff 			last->m_next = n;
5000d375e524SGleb Smirnoff 			last = n;
5001d375e524SGleb Smirnoff 		}
5002d375e524SGleb Smirnoff 	}
5003d375e524SGleb Smirnoff 
5004d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
5005d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
5006d375e524SGleb Smirnoff 	last->m_len += padlen;
5007d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
5008d375e524SGleb Smirnoff 
5009d375e524SGleb Smirnoff 	return (0);
5010d375e524SGleb Smirnoff }
5011d375e524SGleb Smirnoff 
5012ca3f1187SPyun YongHyeon static struct mbuf *
5013d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
5014d598b626SPyun YongHyeon {
5015d598b626SPyun YongHyeon 	struct mbuf *n;
5016d598b626SPyun YongHyeon 	int found;
5017d598b626SPyun YongHyeon 
5018d598b626SPyun YongHyeon 	/*
5019d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
5020d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
5021d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
5022d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
5023d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
5024d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
5025d598b626SPyun YongHyeon 	 */
5026d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
5027d598b626SPyun YongHyeon 		if (n->m_len < 8) {
5028d598b626SPyun YongHyeon 			found++;
5029d598b626SPyun YongHyeon 			if (found > 1)
5030d598b626SPyun YongHyeon 				break;
5031d598b626SPyun YongHyeon 			continue;
5032d598b626SPyun YongHyeon 		}
5033d598b626SPyun YongHyeon 		found = 0;
5034d598b626SPyun YongHyeon 	}
5035d598b626SPyun YongHyeon 
5036d598b626SPyun YongHyeon 	if (found > 1) {
5037c6499eccSGleb Smirnoff 		n = m_defrag(m, M_NOWAIT);
5038d598b626SPyun YongHyeon 		if (n == NULL)
5039d598b626SPyun YongHyeon 			m_freem(m);
5040d598b626SPyun YongHyeon 	} else
5041d598b626SPyun YongHyeon 		n = m;
5042d598b626SPyun YongHyeon 	return (n);
5043d598b626SPyun YongHyeon }
5044d598b626SPyun YongHyeon 
5045d598b626SPyun YongHyeon static struct mbuf *
50461108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
50471108273aSPyun YongHyeon     uint16_t *flags)
5048ca3f1187SPyun YongHyeon {
5049ca3f1187SPyun YongHyeon 	struct ip *ip;
5050ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
5051ca3f1187SPyun YongHyeon 	struct mbuf *n;
5052ca3f1187SPyun YongHyeon 	uint16_t hlen;
50535b355c4fSPyun YongHyeon 	uint32_t poff;
5054ca3f1187SPyun YongHyeon 
5055ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5056ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5057c6499eccSGleb Smirnoff 		n = m_dup(m, M_NOWAIT);
5058ca3f1187SPyun YongHyeon 		m_freem(m);
5059ca3f1187SPyun YongHyeon 		if (n == NULL)
5060ca3f1187SPyun YongHyeon 			return (NULL);
5061ca3f1187SPyun YongHyeon 		m = n;
5062ca3f1187SPyun YongHyeon 	}
50635b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5064ca3f1187SPyun YongHyeon 	if (m == NULL)
5065ca3f1187SPyun YongHyeon 		return (NULL);
50665b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
50675b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5068ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5069ca3f1187SPyun YongHyeon 	if (m == NULL)
5070ca3f1187SPyun YongHyeon 		return (NULL);
5071ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
50725b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5073ca3f1187SPyun YongHyeon 	if (m == NULL)
5074ca3f1187SPyun YongHyeon 		return (NULL);
5075ca3f1187SPyun YongHyeon 	/*
5076ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5077ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5078ca3f1187SPyun YongHyeon 	 */
5079ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
508096486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5081ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5082ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5083ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
508496486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5085ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5086ca3f1187SPyun YongHyeon 	/*
5087ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5088ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5089ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
50901108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5091ca3f1187SPyun YongHyeon 	 */
50921108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5093ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
50941108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
50951108273aSPyun YongHyeon 		/*
50961108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
50971108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
50981108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
50991108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
51001108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
51011108273aSPyun YongHyeon 		 * frames are supported.
51021108273aSPyun YongHyeon 		 */
51031108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
51041108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
51051108273aSPyun YongHyeon 	} else {
51061108273aSPyun YongHyeon 		/*
51071108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
51081108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
51091108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
51101108273aSPyun YongHyeon 		 * supported.
51111108273aSPyun YongHyeon 		 */
5112ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
51131108273aSPyun YongHyeon 	}
5114ca3f1187SPyun YongHyeon 	return (m);
5115ca3f1187SPyun YongHyeon }
5116ca3f1187SPyun YongHyeon 
5117d375e524SGleb Smirnoff /*
511895d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
511995d67482SBill Paul  * pointers to descriptors.
512095d67482SBill Paul  */
512195d67482SBill Paul static int
5122676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
512395d67482SBill Paul {
51247e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5125f41ac2beSBill Paul 	bus_dmamap_t		map;
5126676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5127676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
51287e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5129ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
51307e27542aSGleb Smirnoff 	int			nsegs, i, error;
513195d67482SBill Paul 
51326909dc43SGleb Smirnoff 	csum_flags = 0;
5133ca3f1187SPyun YongHyeon 	mss = 0;
5134ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5135d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5136d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5137d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5138d598b626SPyun YongHyeon 		if (*m_head == NULL)
5139d598b626SPyun YongHyeon 			return (ENOBUFS);
5140d598b626SPyun YongHyeon 		m = *m_head;
5141d598b626SPyun YongHyeon 	}
5142ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
51431108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5144ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5145ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5146ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5147ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
514835f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
51496909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
51506909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
51516909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
51526909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
51536909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
51546909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
51556909dc43SGleb Smirnoff 				m_freem(m);
51566909dc43SGleb Smirnoff 				*m_head = NULL;
51576909dc43SGleb Smirnoff 				return (error);
51586909dc43SGleb Smirnoff 			}
51596909dc43SGleb Smirnoff 		}
51606909dc43SGleb Smirnoff 	}
51616909dc43SGleb Smirnoff 
51621108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
51631108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
51641108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
51651108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
51661108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5167beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5168d94f2b85SPyun YongHyeon 			/*
5169d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5170d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5171d94f2b85SPyun YongHyeon 			 * DMA read operation.
5172d94f2b85SPyun YongHyeon 			 */
5173beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5174c6499eccSGleb Smirnoff 				m = m_defrag(m, M_NOWAIT);
5175d94f2b85SPyun YongHyeon 			else
5176c6499eccSGleb Smirnoff 				m = m_collapse(m, M_NOWAIT,
51771108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5178261f04d6SPyun YongHyeon 			if (m == NULL)
5179261f04d6SPyun YongHyeon 				m = *m_head;
5180d94f2b85SPyun YongHyeon 			*m_head = m;
5181d94f2b85SPyun YongHyeon 		}
51821108273aSPyun YongHyeon 	}
5183d94f2b85SPyun YongHyeon 
51847e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
51850ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5186676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
51877e27542aSGleb Smirnoff 	if (error == EFBIG) {
5188c6499eccSGleb Smirnoff 		m = m_collapse(m, M_NOWAIT, BGE_NSEG_NEW);
5189676ad2c9SGleb Smirnoff 		if (m == NULL) {
5190676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5191676ad2c9SGleb Smirnoff 			*m_head = NULL;
51927e27542aSGleb Smirnoff 			return (ENOBUFS);
51937e27542aSGleb Smirnoff 		}
5194676ad2c9SGleb Smirnoff 		*m_head = m;
51950ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
51960ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5197676ad2c9SGleb Smirnoff 		if (error) {
5198676ad2c9SGleb Smirnoff 			m_freem(m);
5199676ad2c9SGleb Smirnoff 			*m_head = NULL;
52007e27542aSGleb Smirnoff 			return (error);
52017e27542aSGleb Smirnoff 		}
5202676ad2c9SGleb Smirnoff 	} else if (error != 0)
5203676ad2c9SGleb Smirnoff 		return (error);
52047e27542aSGleb Smirnoff 
5205167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5206167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
52070ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
520895d67482SBill Paul 		return (ENOBUFS);
52097e27542aSGleb Smirnoff 	}
52107e27542aSGleb Smirnoff 
52110ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5212e65bed95SPyun YongHyeon 
5213ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5214ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5215ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5216ca3f1187SPyun YongHyeon 	}
52177e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
52187e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
52197e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
52207e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
52217e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
52227e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
5223ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
5224ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
52257e27542aSGleb Smirnoff 		if (i == nsegs - 1)
52267e27542aSGleb Smirnoff 			break;
52277e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
52287e27542aSGleb Smirnoff 	}
52297e27542aSGleb Smirnoff 
52307e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
52317e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5232676ad2c9SGleb Smirnoff 
5233f41ac2beSBill Paul 	/*
5234f41ac2beSBill Paul 	 * Insure that the map for this transmission
5235f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5236f41ac2beSBill Paul 	 * in this chain.
5237f41ac2beSBill Paul 	 */
52387e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
52397e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5240676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
52417e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
524295d67482SBill Paul 
52437e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
52447e27542aSGleb Smirnoff 	*txidx = idx;
524595d67482SBill Paul 
524695d67482SBill Paul 	return (0);
524795d67482SBill Paul }
524895d67482SBill Paul 
524995d67482SBill Paul /*
525095d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
525195d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
525295d67482SBill Paul  */
525395d67482SBill Paul static void
52543f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
525595d67482SBill Paul {
525695d67482SBill Paul 	struct bge_softc *sc;
5257167fdb62SPyun YongHyeon 	struct mbuf *m_head;
525814bbd30fSGleb Smirnoff 	uint32_t prodidx;
5259167fdb62SPyun YongHyeon 	int count;
526095d67482SBill Paul 
526195d67482SBill Paul 	sc = ifp->if_softc;
5262167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
526395d67482SBill Paul 
5264167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5265167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5266167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
526795d67482SBill Paul 		return;
526895d67482SBill Paul 
526914bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
527095d67482SBill Paul 
5271167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
5272167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5273167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5274167fdb62SPyun YongHyeon 			break;
5275167fdb62SPyun YongHyeon 		}
52764d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
527795d67482SBill Paul 		if (m_head == NULL)
527895d67482SBill Paul 			break;
527995d67482SBill Paul 
528095d67482SBill Paul 		/*
528195d67482SBill Paul 		 * Pack the data into the transmit ring. If we
528295d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
528395d67482SBill Paul 		 * for the NIC to drain the ring.
528495d67482SBill Paul 		 */
5285676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5286676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5287676ad2c9SGleb Smirnoff 				break;
52884d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
528913f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
529095d67482SBill Paul 			break;
529195d67482SBill Paul 		}
5292303a718cSDag-Erling Smørgrav 		++count;
529395d67482SBill Paul 
529495d67482SBill Paul 		/*
529595d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
529695d67482SBill Paul 		 * to him.
529795d67482SBill Paul 		 */
52984e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
529945ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
53004e35d186SJung-uk Kim #else
53014e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
53024e35d186SJung-uk Kim #endif
530395d67482SBill Paul 	}
530495d67482SBill Paul 
5305167fdb62SPyun YongHyeon 	if (count > 0) {
5306aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
53075c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
53083f74909aSGleb Smirnoff 		/* Transmit. */
530938cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
53103927098fSPaul Saab 		/* 5700 b2 errata */
5311e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
531238cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
531395d67482SBill Paul 
531414bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
531514bbd30fSGleb Smirnoff 
531695d67482SBill Paul 		/*
531795d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
531895d67482SBill Paul 		 */
5319b584d2b3SPyun YongHyeon 		sc->bge_timer = BGE_TX_TIMEOUT;
532095d67482SBill Paul 	}
5321167fdb62SPyun YongHyeon }
532295d67482SBill Paul 
53230f9bd73bSSam Leffler /*
53240f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
53250f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
53260f9bd73bSSam Leffler  */
532795d67482SBill Paul static void
53283f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
532995d67482SBill Paul {
53300f9bd73bSSam Leffler 	struct bge_softc *sc;
53310f9bd73bSSam Leffler 
53320f9bd73bSSam Leffler 	sc = ifp->if_softc;
53330f9bd73bSSam Leffler 	BGE_LOCK(sc);
53340f9bd73bSSam Leffler 	bge_start_locked(ifp);
53350f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
53360f9bd73bSSam Leffler }
53370f9bd73bSSam Leffler 
53380f9bd73bSSam Leffler static void
53393f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
53400f9bd73bSSam Leffler {
534195d67482SBill Paul 	struct ifnet *ifp;
53423f74909aSGleb Smirnoff 	uint16_t *m;
5343f6a65488SPyun YongHyeon 	uint32_t mode;
534495d67482SBill Paul 
53450f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
534695d67482SBill Paul 
5347fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
534895d67482SBill Paul 
534913f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
535095d67482SBill Paul 		return;
535195d67482SBill Paul 
535295d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
535395d67482SBill Paul 	bge_stop(sc);
53548cb1383cSDoug Ambrisko 
53558cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
53568cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
535795d67482SBill Paul 	bge_reset(sc);
53588cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
53598cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
53608cb1383cSDoug Ambrisko 
536195d67482SBill Paul 	bge_chipinit(sc);
536295d67482SBill Paul 
536395d67482SBill Paul 	/*
536495d67482SBill Paul 	 * Init the various state machines, ring
536595d67482SBill Paul 	 * control blocks and firmware.
536695d67482SBill Paul 	 */
536795d67482SBill Paul 	if (bge_blockinit(sc)) {
5368fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
536995d67482SBill Paul 		return;
537095d67482SBill Paul 	}
537195d67482SBill Paul 
5372fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
537395d67482SBill Paul 
537495d67482SBill Paul 	/* Specify MTU. */
537595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
5376cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5377cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
537895d67482SBill Paul 
537995d67482SBill Paul 	/* Load our MAC address. */
53803f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
538195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
538295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
538395d67482SBill Paul 
53843e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
53853e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
538695d67482SBill Paul 
538795d67482SBill Paul 	/* Program multicast filter. */
538895d67482SBill Paul 	bge_setmulti(sc);
538995d67482SBill Paul 
5390cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5391cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5392cb2eacc7SYaroslav Tykhiy 
539335f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
539435f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
539535f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
539635f945cdSPyun YongHyeon 	else
539735f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
539835f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
539935f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
540035f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
540135f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
540235f945cdSPyun YongHyeon 	}
540335f945cdSPyun YongHyeon 
540495d67482SBill Paul 	/* Init RX ring. */
54053ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
54063ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
54073ee5d7daSPyun YongHyeon 		bge_stop(sc);
54083ee5d7daSPyun YongHyeon 		return;
54093ee5d7daSPyun YongHyeon 	}
541095d67482SBill Paul 
54110434d1b8SBill Paul 	/*
54120434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
54130434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
54140434d1b8SBill Paul 	 * entry of the ring.
54150434d1b8SBill Paul 	 */
54160434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
54173f74909aSGleb Smirnoff 		uint32_t		v, i;
54180434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
54190434d1b8SBill Paul 			DELAY(20);
54200434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
54210434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
54220434d1b8SBill Paul 				break;
54230434d1b8SBill Paul 		}
54240434d1b8SBill Paul 		if (i == 10)
5425fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5426fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
54270434d1b8SBill Paul 	}
54280434d1b8SBill Paul 
542995d67482SBill Paul 	/* Init jumbo RX ring. */
5430f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5431f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
5432c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
54333ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5434333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5435b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
54363ee5d7daSPyun YongHyeon 			bge_stop(sc);
54373ee5d7daSPyun YongHyeon 			return;
54383ee5d7daSPyun YongHyeon 		}
54393ee5d7daSPyun YongHyeon 	}
544095d67482SBill Paul 
54413f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
544295d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
544395d67482SBill Paul 
54447e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
54457e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
54467e6e2507SJung-uk Kim 
544795d67482SBill Paul 	/* Init TX ring. */
544895d67482SBill Paul 	bge_init_tx_ring(sc);
544995d67482SBill Paul 
5450f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5451f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5452f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5453f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
545450515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
545550515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
545650515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
545750515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
545850515680SPyun YongHyeon 	}
54593f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5460f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5461a6e66cd2SPyun YongHyeon 	DELAY(100);
546295d67482SBill Paul 
54633f74909aSGleb Smirnoff 	/* Turn on receiver. */
5464548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5465548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5466548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
5467548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5468a6e66cd2SPyun YongHyeon 	DELAY(10);
546995d67482SBill Paul 
5470dedcdf57SPyun YongHyeon 	/*
5471dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5472dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5473dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5474dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5475dedcdf57SPyun YongHyeon 	 */
54763fc5fbfbSPyun YongHyeon 	if (BGE_IS_57765_PLUS(sc))
5477b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5478b4a256acSPyun YongHyeon 	else
5479dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5480dedcdf57SPyun YongHyeon 
54812280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
54822280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
54832280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
54842280c16bSPyun YongHyeon 
548595d67482SBill Paul 	/* Tell firmware we're alive. */
548695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
548795d67482SBill Paul 
548875719184SGleb Smirnoff #ifdef DEVICE_POLLING
548975719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
549075719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
549175719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
549275719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
549338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
549475719184SGleb Smirnoff 	} else
549575719184SGleb Smirnoff #endif
549675719184SGleb Smirnoff 
549795d67482SBill Paul 	/* Enable host interrupts. */
549875719184SGleb Smirnoff 	{
549995d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
550095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
550138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
550275719184SGleb Smirnoff 	}
550395d67482SBill Paul 
550413f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
550513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
550695d67482SBill Paul 
5507e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5508e4146b95SPyun YongHyeon 
55090f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
55100f9bd73bSSam Leffler }
55110f9bd73bSSam Leffler 
55120f9bd73bSSam Leffler static void
55133f74909aSGleb Smirnoff bge_init(void *xsc)
55140f9bd73bSSam Leffler {
55150f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
55160f9bd73bSSam Leffler 
55170f9bd73bSSam Leffler 	BGE_LOCK(sc);
55180f9bd73bSSam Leffler 	bge_init_locked(sc);
55190f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
552095d67482SBill Paul }
552195d67482SBill Paul 
552295d67482SBill Paul /*
552395d67482SBill Paul  * Set media options.
552495d67482SBill Paul  */
552595d67482SBill Paul static int
55263f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
552795d67482SBill Paul {
552867d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
552967d5e043SOleg Bulyzhin 	int res;
553067d5e043SOleg Bulyzhin 
553167d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
553267d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
553367d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
553467d5e043SOleg Bulyzhin 
553567d5e043SOleg Bulyzhin 	return (res);
553667d5e043SOleg Bulyzhin }
553767d5e043SOleg Bulyzhin 
553867d5e043SOleg Bulyzhin static int
553967d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
554067d5e043SOleg Bulyzhin {
554167d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
554295d67482SBill Paul 	struct mii_data *mii;
55434f09c4c7SMarius Strobl 	struct mii_softc *miisc;
554495d67482SBill Paul 	struct ifmedia *ifm;
554595d67482SBill Paul 
554667d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
554767d5e043SOleg Bulyzhin 
554895d67482SBill Paul 	ifm = &sc->bge_ifmedia;
554995d67482SBill Paul 
555095d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5551652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
555295d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
555395d67482SBill Paul 			return (EINVAL);
555495d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
555595d67482SBill Paul 		case IFM_AUTO:
5556ff50922bSDoug White 			/*
5557ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5558ff50922bSDoug White 			 * mechanism for programming the autoneg
5559ff50922bSDoug White 			 * advertisement registers in TBI mode.
5560ff50922bSDoug White 			 */
55610f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5562ff50922bSDoug White 				uint32_t sgdig;
55630f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
55640f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5565ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5566ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5567ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5568ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5569ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5570ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5571ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5572ff50922bSDoug White 					DELAY(5);
5573ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5574ff50922bSDoug White 				}
55750f89fde2SJung-uk Kim 			}
557695d67482SBill Paul 			break;
557795d67482SBill Paul 		case IFM_1000_SX:
557895d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
557995d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
558095d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
558195d67482SBill Paul 			} else {
558295d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
558395d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
558495d67482SBill Paul 			}
55859b80ffe7SPyun YongHyeon 			DELAY(40);
558695d67482SBill Paul 			break;
558795d67482SBill Paul 		default:
558895d67482SBill Paul 			return (EINVAL);
558995d67482SBill Paul 		}
559095d67482SBill Paul 		return (0);
559195d67482SBill Paul 	}
559295d67482SBill Paul 
55931493e883SOleg Bulyzhin 	sc->bge_link_evt++;
559495d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
55954f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
55963fcb7a53SMarius Strobl 		PHY_RESET(miisc);
559795d67482SBill Paul 	mii_mediachg(mii);
559895d67482SBill Paul 
5599902827f6SBjoern A. Zeeb 	/*
5600902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5601902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5602902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5603902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5604902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5605902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5606902827f6SBjoern A. Zeeb 	 * get an RX intr.
5607902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5608902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5609902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5610902827f6SBjoern A. Zeeb 	 */
56114f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
56124f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5613902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
56144f0794ffSBjoern A. Zeeb 	else
561563ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5616902827f6SBjoern A. Zeeb 
561795d67482SBill Paul 	return (0);
561895d67482SBill Paul }
561995d67482SBill Paul 
562095d67482SBill Paul /*
562195d67482SBill Paul  * Report current media status.
562295d67482SBill Paul  */
562395d67482SBill Paul static void
56243f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
562595d67482SBill Paul {
562667d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
562795d67482SBill Paul 	struct mii_data *mii;
562895d67482SBill Paul 
562967d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
563095d67482SBill Paul 
5631b9d2edd7SPyun YongHyeon 	if ((ifp->if_flags & IFF_UP) == 0) {
5632b9d2edd7SPyun YongHyeon 		BGE_UNLOCK(sc);
5633b9d2edd7SPyun YongHyeon 		return;
5634b9d2edd7SPyun YongHyeon 	}
5635652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
563695d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
563795d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
563895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
563995d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
564095d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
56414c0da0ffSGleb Smirnoff 		else {
56424c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
564367d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
56444c0da0ffSGleb Smirnoff 			return;
56454c0da0ffSGleb Smirnoff 		}
564695d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
564795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
564895d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
564995d67482SBill Paul 		else
565095d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
565167d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
565295d67482SBill Paul 		return;
565395d67482SBill Paul 	}
565495d67482SBill Paul 
565595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
565695d67482SBill Paul 	mii_pollstat(mii);
565795d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
565895d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
565967d5e043SOleg Bulyzhin 
566067d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
566195d67482SBill Paul }
566295d67482SBill Paul 
566395d67482SBill Paul static int
56643f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
566595d67482SBill Paul {
566695d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
566795d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
566895d67482SBill Paul 	struct mii_data *mii;
5669f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
567095d67482SBill Paul 
567195d67482SBill Paul 	switch (command) {
567295d67482SBill Paul 	case SIOCSIFMTU:
5673f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5674f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
56754c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5676f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
567795d67482SBill Paul 				error = EINVAL;
5678f5459d4cSPyun YongHyeon 				break;
5679f5459d4cSPyun YongHyeon 			}
5680f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5681f5459d4cSPyun YongHyeon 			error = EINVAL;
5682f5459d4cSPyun YongHyeon 			break;
5683f5459d4cSPyun YongHyeon 		}
5684f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5685f5459d4cSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
568695d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
56873a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
568813f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
56893a429c8fSPyun YongHyeon 				bge_init_locked(sc);
569095d67482SBill Paul 			}
56913a429c8fSPyun YongHyeon 		}
56923a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
569395d67482SBill Paul 		break;
569495d67482SBill Paul 	case SIOCSIFFLAGS:
56950f9bd73bSSam Leffler 		BGE_LOCK(sc);
569695d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
569795d67482SBill Paul 			/*
569895d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
569995d67482SBill Paul 			 * then just use the 'set promisc mode' command
570095d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
570195d67482SBill Paul 			 * a full re-init means reloading the firmware and
570295d67482SBill Paul 			 * waiting for it to start up, which may take a
5703d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
570495d67482SBill Paul 			 */
5705f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5706f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
57073e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
57083e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5709f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5710d183af7fSRuslan Ermilov 					bge_setmulti(sc);
571195d67482SBill Paul 			} else
57120f9bd73bSSam Leffler 				bge_init_locked(sc);
571395d67482SBill Paul 		} else {
571413f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
571595d67482SBill Paul 				bge_stop(sc);
571695d67482SBill Paul 			}
571795d67482SBill Paul 		}
571895d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
57190f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
572095d67482SBill Paul 		error = 0;
572195d67482SBill Paul 		break;
572295d67482SBill Paul 	case SIOCADDMULTI:
572395d67482SBill Paul 	case SIOCDELMULTI:
572413f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
57250f9bd73bSSam Leffler 			BGE_LOCK(sc);
572695d67482SBill Paul 			bge_setmulti(sc);
57270f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
572895d67482SBill Paul 			error = 0;
572995d67482SBill Paul 		}
573095d67482SBill Paul 		break;
573195d67482SBill Paul 	case SIOCSIFMEDIA:
573295d67482SBill Paul 	case SIOCGIFMEDIA:
5733652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
573495d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
573595d67482SBill Paul 			    &sc->bge_ifmedia, command);
573695d67482SBill Paul 		} else {
573795d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
573895d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
573995d67482SBill Paul 			    &mii->mii_media, command);
574095d67482SBill Paul 		}
574195d67482SBill Paul 		break;
574295d67482SBill Paul 	case SIOCSIFCAP:
574395d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
574475719184SGleb Smirnoff #ifdef DEVICE_POLLING
574575719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
574675719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
574775719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
574875719184SGleb Smirnoff 				if (error)
574975719184SGleb Smirnoff 					return (error);
575075719184SGleb Smirnoff 				BGE_LOCK(sc);
575175719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
575275719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
575338cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
575475719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
575575719184SGleb Smirnoff 				BGE_UNLOCK(sc);
575675719184SGleb Smirnoff 			} else {
575775719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
575875719184SGleb Smirnoff 				/* Enable interrupt even in error case */
575975719184SGleb Smirnoff 				BGE_LOCK(sc);
576075719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
576175719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
576238cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
576375719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
576475719184SGleb Smirnoff 				BGE_UNLOCK(sc);
576575719184SGleb Smirnoff 			}
576675719184SGleb Smirnoff 		}
576775719184SGleb Smirnoff #endif
5768d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5769d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
5770d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
5771d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
577235f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
577395d67482SBill Paul 			else
577435f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
577595d67482SBill Paul 		}
5776cb2eacc7SYaroslav Tykhiy 
5777d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5778d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
5779d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
5780d8b57f98SPyun YongHyeon 
5781ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5782ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
5783ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
5784ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
5785ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
5786ca3f1187SPyun YongHyeon 			else
5787ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
5788ca3f1187SPyun YongHyeon 		}
5789ca3f1187SPyun YongHyeon 
5790cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5791cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
5792cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5793cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5794cb2eacc7SYaroslav Tykhiy 		}
5795cb2eacc7SYaroslav Tykhiy 
579604bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
579704bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
579804bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
579904bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
580004bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
5801cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
580204bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
580304bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
5804cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5805cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5806cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
580704bde852SPyun YongHyeon 		}
5808cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5809cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
5810cb2eacc7SYaroslav Tykhiy #endif
581195d67482SBill Paul 		break;
581295d67482SBill Paul 	default:
5813673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
581495d67482SBill Paul 		break;
581595d67482SBill Paul 	}
581695d67482SBill Paul 
581795d67482SBill Paul 	return (error);
581895d67482SBill Paul }
581995d67482SBill Paul 
582095d67482SBill Paul static void
5821b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
582295d67482SBill Paul {
5823b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
5824b584d2b3SPyun YongHyeon 	uint32_t status;
582595d67482SBill Paul 
5826b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5827b74e67fbSGleb Smirnoff 
5828b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5829b74e67fbSGleb Smirnoff 		return;
5830b74e67fbSGleb Smirnoff 
5831b584d2b3SPyun YongHyeon 	/* If pause frames are active then don't reset the hardware. */
5832b584d2b3SPyun YongHyeon 	if ((CSR_READ_4(sc, BGE_RX_MODE) & BGE_RXMODE_FLOWCTL_ENABLE) != 0) {
5833b584d2b3SPyun YongHyeon 		status = CSR_READ_4(sc, BGE_RX_STS);
5834b584d2b3SPyun YongHyeon 		if ((status & BGE_RXSTAT_REMOTE_XOFFED) != 0) {
5835b584d2b3SPyun YongHyeon 			/*
5836b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5837b584d2b3SPyun YongHyeon 			 * the condition to clear.
5838b584d2b3SPyun YongHyeon 			 */
5839b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5840b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5841b584d2b3SPyun YongHyeon 			return;
5842b584d2b3SPyun YongHyeon 		} else if ((status & BGE_RXSTAT_RCVD_XOFF) != 0 &&
5843b584d2b3SPyun YongHyeon 		    (status & BGE_RXSTAT_RCVD_XON) != 0) {
5844b584d2b3SPyun YongHyeon 			/*
5845b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5846b584d2b3SPyun YongHyeon 			 * the condition to clear.
5847b584d2b3SPyun YongHyeon 			 */
5848b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5849b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5850b584d2b3SPyun YongHyeon 			return;
5851b584d2b3SPyun YongHyeon 		}
5852b584d2b3SPyun YongHyeon 		/*
5853b584d2b3SPyun YongHyeon 		 * Any other condition is unexpected and the controller
5854b584d2b3SPyun YongHyeon 		 * should be reset.
5855b584d2b3SPyun YongHyeon 		 */
5856b584d2b3SPyun YongHyeon 	}
5857b584d2b3SPyun YongHyeon 
5858b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
585995d67482SBill Paul 
5860fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
586195d67482SBill Paul 
586213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5863426742bfSGleb Smirnoff 	bge_init_locked(sc);
586495d67482SBill Paul 
586595d67482SBill Paul 	ifp->if_oerrors++;
586695d67482SBill Paul }
586795d67482SBill Paul 
58685a147ba6SPyun YongHyeon static void
58695a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
58705a147ba6SPyun YongHyeon {
58715a147ba6SPyun YongHyeon 	int i;
58725a147ba6SPyun YongHyeon 
58735a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
58745a147ba6SPyun YongHyeon 
58755a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
58765a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
58775a147ba6SPyun YongHyeon 			return;
58785a147ba6SPyun YongHyeon 		DELAY(100);
58795a147ba6SPyun YongHyeon         }
58805a147ba6SPyun YongHyeon }
58815a147ba6SPyun YongHyeon 
588295d67482SBill Paul /*
588395d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
588495d67482SBill Paul  * RX and TX lists.
588595d67482SBill Paul  */
588695d67482SBill Paul static void
58873f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
588895d67482SBill Paul {
588995d67482SBill Paul 	struct ifnet *ifp;
589095d67482SBill Paul 
58910f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
58920f9bd73bSSam Leffler 
5893fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
589495d67482SBill Paul 
58950f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
589695d67482SBill Paul 
589744b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
589844b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
589944b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
590044b63691SBjoern A. Zeeb 
590144b63691SBjoern A. Zeeb 	/*
590244b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
590344b63691SBjoern A. Zeeb 	 */
590444b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
5905548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
590644b63691SBjoern A. Zeeb 
590795d67482SBill Paul 	/*
59083f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
590995d67482SBill Paul 	 */
59105a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
59115a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
59125a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
59135a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
59145a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
59155a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
59165a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
59175a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
591895d67482SBill Paul 
591995d67482SBill Paul 	/*
59203f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
592195d67482SBill Paul 	 */
59225a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
59235a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
59245a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
59255a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
59265a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
59275a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
59285a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
59295a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
593095d67482SBill Paul 
593195d67482SBill Paul 	/*
593295d67482SBill Paul 	 * Shut down all of the memory managers and related
593395d67482SBill Paul 	 * state machines.
593495d67482SBill Paul 	 */
59355a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
59365a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
59375a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
59385a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
59395a147ba6SPyun YongHyeon 
59400c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
594195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
59427ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
594395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
594495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
59450434d1b8SBill Paul 	}
59462280c16bSPyun YongHyeon 	/* Update MAC statistics. */
59472280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
59482280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
594995d67482SBill Paul 
59508cb1383cSDoug Ambrisko 	bge_reset(sc);
5951548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
5952548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
59538cb1383cSDoug Ambrisko 
59548cb1383cSDoug Ambrisko 	/*
59558cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
59568cb1383cSDoug Ambrisko 	 */
59578cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
59588cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
59598cb1383cSDoug Ambrisko 	else
596095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
596195d67482SBill Paul 
596295d67482SBill Paul 	/* Free the RX lists. */
596395d67482SBill Paul 	bge_free_rx_ring_std(sc);
596495d67482SBill Paul 
596595d67482SBill Paul 	/* Free jumbo RX list. */
59664c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
596795d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
596895d67482SBill Paul 
596995d67482SBill Paul 	/* Free TX buffers. */
597095d67482SBill Paul 	bge_free_tx_ring(sc);
597195d67482SBill Paul 
597295d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
597395d67482SBill Paul 
59745dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
59751493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
59761493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
59771493e883SOleg Bulyzhin 	sc->bge_link = 0;
597895d67482SBill Paul 
59791493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
598095d67482SBill Paul }
598195d67482SBill Paul 
598295d67482SBill Paul /*
598395d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
598495d67482SBill Paul  * get confused by errant DMAs when rebooting.
598595d67482SBill Paul  */
5986b6c974e8SWarner Losh static int
59873f74909aSGleb Smirnoff bge_shutdown(device_t dev)
598895d67482SBill Paul {
598995d67482SBill Paul 	struct bge_softc *sc;
599095d67482SBill Paul 
599195d67482SBill Paul 	sc = device_get_softc(dev);
59920f9bd73bSSam Leffler 	BGE_LOCK(sc);
599395d67482SBill Paul 	bge_stop(sc);
59940f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
5995b6c974e8SWarner Losh 
5996b6c974e8SWarner Losh 	return (0);
599795d67482SBill Paul }
599814afefa3SPawel Jakub Dawidek 
599914afefa3SPawel Jakub Dawidek static int
600014afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
600114afefa3SPawel Jakub Dawidek {
600214afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
600314afefa3SPawel Jakub Dawidek 
600414afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
600514afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
600614afefa3SPawel Jakub Dawidek 	bge_stop(sc);
600714afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
600814afefa3SPawel Jakub Dawidek 
600914afefa3SPawel Jakub Dawidek 	return (0);
601014afefa3SPawel Jakub Dawidek }
601114afefa3SPawel Jakub Dawidek 
601214afefa3SPawel Jakub Dawidek static int
601314afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
601414afefa3SPawel Jakub Dawidek {
601514afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
601614afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
601714afefa3SPawel Jakub Dawidek 
601814afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
601914afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
602014afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
602114afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
602214afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
602314afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
602414afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
602514afefa3SPawel Jakub Dawidek 	}
602614afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
602714afefa3SPawel Jakub Dawidek 
602814afefa3SPawel Jakub Dawidek 	return (0);
602914afefa3SPawel Jakub Dawidek }
6030dab5cd05SOleg Bulyzhin 
6031dab5cd05SOleg Bulyzhin static void
60323f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
6033dab5cd05SOleg Bulyzhin {
60341f313773SOleg Bulyzhin 	struct mii_data *mii;
60351f313773SOleg Bulyzhin 	uint32_t link, status;
6036dab5cd05SOleg Bulyzhin 
6037dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
60381f313773SOleg Bulyzhin 
60393f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
60407b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
60417b97099dSOleg Bulyzhin 
6042dab5cd05SOleg Bulyzhin 	/*
6043dab5cd05SOleg Bulyzhin 	 * Process link state changes.
6044dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
6045dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
6046dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
6047dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
6048dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
6049dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
6050dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
6051dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
60521f313773SOleg Bulyzhin 	 *
60531f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
60544c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
6055dab5cd05SOleg Bulyzhin 	 */
6056dab5cd05SOleg Bulyzhin 
60571f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
60584c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
6059dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
6060dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
60611f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
60625dda8085SOleg Bulyzhin 			mii_pollstat(mii);
60631f313773SOleg Bulyzhin 			if (!sc->bge_link &&
60641f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
60651f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
60661f313773SOleg Bulyzhin 				sc->bge_link++;
60671f313773SOleg Bulyzhin 				if (bootverbose)
60681f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
60691f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
60701f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
60711f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
60721f313773SOleg Bulyzhin 				sc->bge_link = 0;
60731f313773SOleg Bulyzhin 				if (bootverbose)
60741f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
60751f313773SOleg Bulyzhin 			}
60761f313773SOleg Bulyzhin 
60773f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6078dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6079dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6080daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6081daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6082daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6083daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6084dab5cd05SOleg Bulyzhin 		}
6085dab5cd05SOleg Bulyzhin 		return;
6086dab5cd05SOleg Bulyzhin 	}
6087dab5cd05SOleg Bulyzhin 
6088652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
60891f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
60907b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
60917b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
60921f313773SOleg Bulyzhin 				sc->bge_link++;
60939b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
60941f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
60951f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
60969b80ffe7SPyun YongHyeon 					DELAY(40);
60979b80ffe7SPyun YongHyeon 				}
60980c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
60991f313773SOleg Bulyzhin 				if (bootverbose)
61001f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61013f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
61023f74909aSGleb Smirnoff 				    LINK_STATE_UP);
61037b97099dSOleg Bulyzhin 			}
61041f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6105dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
61061f313773SOleg Bulyzhin 			if (bootverbose)
61071f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
61087b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
61091f313773SOleg Bulyzhin 		}
61106ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
61111f313773SOleg Bulyzhin 		/*
61120c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
61130c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
61140c8aa4eaSJung-uk Kim 		 * PHY link status directly.
61151f313773SOleg Bulyzhin 		 */
61161f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
61171f313773SOleg Bulyzhin 
61181f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
61191f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
61201f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
61215dda8085SOleg Bulyzhin 			mii_pollstat(mii);
61221f313773SOleg Bulyzhin 			if (!sc->bge_link &&
61231f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
61241f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
61251f313773SOleg Bulyzhin 				sc->bge_link++;
61261f313773SOleg Bulyzhin 				if (bootverbose)
61271f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61281f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
61291f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
61301f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
61311f313773SOleg Bulyzhin 				sc->bge_link = 0;
61321f313773SOleg Bulyzhin 				if (bootverbose)
61331f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
61341f313773SOleg Bulyzhin 			}
61351f313773SOleg Bulyzhin 		}
61360c8aa4eaSJung-uk Kim 	} else {
61370c8aa4eaSJung-uk Kim 		/*
61386ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
61396ede2cfaSPyun YongHyeon 		 * link status.
61400c8aa4eaSJung-uk Kim 		 */
61416ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
61426ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
61436ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6144dab5cd05SOleg Bulyzhin 	}
6145dab5cd05SOleg Bulyzhin 
61462246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6147dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6148dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6149dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6150dab5cd05SOleg Bulyzhin }
61516f8718a3SScott Long 
61526f8718a3SScott Long static void
61536f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
61546f8718a3SScott Long {
61556f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
61562280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
61577e32f79aSPyun YongHyeon 	char tn[32];
61587e32f79aSPyun YongHyeon 	int unit;
61596f8718a3SScott Long 
61606f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
61616f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
61626f8718a3SScott Long 
61636f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
61646f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
61656f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
61666f8718a3SScott Long 	    "Debug Information");
61676f8718a3SScott Long 
61686f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
61696f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
6170548c8f1aSPyun YongHyeon 	    "MAC Register Read");
6171548c8f1aSPyun YongHyeon 
6172548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
6173548c8f1aSPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_ape_read, "I",
6174548c8f1aSPyun YongHyeon 	    "APE Register Read");
61756f8718a3SScott Long 
61766f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
61776f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
61786f8718a3SScott Long 	    "Memory Read");
61796f8718a3SScott Long 
61806f8718a3SScott Long #endif
6181763757b2SScott Long 
61827e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
6183beaa2ae1SPyun YongHyeon 	/*
6184beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6185beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6186beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6187beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6188beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6189beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6190beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6191beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6192beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6193beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6194beaa2ae1SPyun YongHyeon 	 */
61957e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
61967e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
61977e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
6198beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6199beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
6200beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6201beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6202beaa2ae1SPyun YongHyeon 
62032ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
62042ae7f64bSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.msi", unit);
62052ae7f64bSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_msi);
62062ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
62072ae7f64bSPyun YongHyeon 	    CTLFLAG_RD, &sc->bge_msi, 0, "Enable MSI");
62085c952e8dSPyun YongHyeon 
620935f945cdSPyun YongHyeon 	/*
621035f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
621135f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
621235f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
621335f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
621435f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
621535f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
621635f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
621735f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
621835f945cdSPyun YongHyeon 	 */
621935f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
622035f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
622135f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
622235f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
622335f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
622435f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
622535f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
622635f945cdSPyun YongHyeon 
6227d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
62282280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
62292280c16bSPyun YongHyeon 	else
62302280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
62312280c16bSPyun YongHyeon }
6232d949071dSJung-uk Kim 
62332280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
62342280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
62352280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
62362280c16bSPyun YongHyeon 	    desc)
62372280c16bSPyun YongHyeon 
62382280c16bSPyun YongHyeon static void
62392280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
62402280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
62412280c16bSPyun YongHyeon {
62422280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
62432280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
62442280c16bSPyun YongHyeon 
62452280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6246763757b2SScott Long 	    NULL, "BGE Statistics");
6247763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6248763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6249763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6250763757b2SScott Long 	    "FramesDroppedDueToFilters");
6251763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6252763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6253763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6254763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6255763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6256763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
625706e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
625806e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
625906e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
626006e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6261763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6262763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6263763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6264763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6265763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6266763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6267763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6268763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6269763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6270763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6271763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6272763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6273763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6274763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6275763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6276763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6277763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6278763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6279763757b2SScott Long 
6280763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
6281763757b2SScott Long 	    NULL, "BGE RX Statistics");
6282763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6283763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
62841cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6285763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6286763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6287763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
62881cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6289763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6290763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6291763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6292763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6293763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6294763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6295763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6296763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6297763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6298763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6299763757b2SScott Long 	    "xoffPauseFramesReceived");
6300763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6301763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6302763757b2SScott Long 	    "ControlFramesReceived");
6303763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6304763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6305763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6306763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6307763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6308763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6309763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6310763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6311763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
631206e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6313763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
631406e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6315763757b2SScott Long 
6316763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
6317763757b2SScott Long 	    NULL, "BGE TX Statistics");
6318763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6319763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
63201cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6321763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6322763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6323763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6324763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6325763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6326763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6327763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6328763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6329763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6330763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6331763757b2SScott Long 	    "InternalMacTransmitErrors");
6332763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6333763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6334763757b2SScott Long 	    "SingleCollisionFrames");
6335763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6336763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6337763757b2SScott Long 	    "MultipleCollisionFrames");
6338763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6339763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6340763757b2SScott Long 	    "DeferredTransmissions");
6341763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6342763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6343763757b2SScott Long 	    "ExcessiveCollisions");
6344763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
634506e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
634606e83c7eSScott Long 	    "LateCollisions");
6347763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
63481cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6349763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6350763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6351763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6352763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6353763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6354763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6355763757b2SScott Long 	    "CarrierSenseErrors");
6356763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6357763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6358763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6359763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6360763757b2SScott Long }
6361763757b2SScott Long 
63622280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
63632280c16bSPyun YongHyeon 
63642280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
63656dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
63662280c16bSPyun YongHyeon 
63672280c16bSPyun YongHyeon static void
63682280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
63692280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
63702280c16bSPyun YongHyeon {
63712280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
63722280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
63732280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
63742280c16bSPyun YongHyeon 
63752280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
63762280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
63772280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
63782280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
63792280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
63802280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
63812280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
63822280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
63832280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
63842280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
63852280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
63862280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
63872280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
63882280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
63892280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
63902280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
63912280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
63922280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
63932280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
63942280c16bSPyun YongHyeon 
63952280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
63962280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
63972280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
63982280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
63992280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
64002280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
64012280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
64021cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
64032280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
64042280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
64052280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
64062280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
64072280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
64082280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
64092280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
64102280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
64112280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
64122280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
64132280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
64142280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
64152280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
64162280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
64172280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
64182280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
64192280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
64202280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
64212280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
64222280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
64232280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
64242280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
64252280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
64262280c16bSPyun YongHyeon 
64272280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
64282280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
64292280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
64301cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
64312280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
64322280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
64332280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
64342280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
64352280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
64362280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
64372280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
64382280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
64392280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
64402280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
64412280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
64422280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
64432280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
64442280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
64452280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
64462280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
64472280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
64482280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
64492280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
64502280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
64512280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
64521cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
64532280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
64541cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
64552280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
64561cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
64572280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
64582280c16bSPyun YongHyeon }
64592280c16bSPyun YongHyeon 
64602280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
64612280c16bSPyun YongHyeon 
6462763757b2SScott Long static int
6463763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6464763757b2SScott Long {
6465763757b2SScott Long 	struct bge_softc *sc;
646606e83c7eSScott Long 	uint32_t result;
6467d949071dSJung-uk Kim 	int offset;
6468763757b2SScott Long 
6469763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6470763757b2SScott Long 	offset = arg2;
6471d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6472d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6473041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
64746f8718a3SScott Long }
64756f8718a3SScott Long 
64766f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
64776f8718a3SScott Long static int
64786f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
64796f8718a3SScott Long {
64806f8718a3SScott Long 	struct bge_softc *sc;
64816f8718a3SScott Long 	uint16_t *sbdata;
648228276ad6SPyun YongHyeon 	int error, result, sbsz;
64836f8718a3SScott Long 	int i, j;
64846f8718a3SScott Long 
64856f8718a3SScott Long 	result = -1;
64866f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
64876f8718a3SScott Long 	if (error || (req->newptr == NULL))
64886f8718a3SScott Long 		return (error);
64896f8718a3SScott Long 
64906f8718a3SScott Long 	if (result == 1) {
64916f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
64926f8718a3SScott Long 
649328276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
649428276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
649528276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
649628276ad6SPyun YongHyeon 		else
649728276ad6SPyun YongHyeon 			sbsz = 32;
64986f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
64996f8718a3SScott Long 		printf("Status Block:\n");
650028276ad6SPyun YongHyeon 		BGE_LOCK(sc);
650128276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
650228276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
650328276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
650428276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
65056f8718a3SScott Long 			printf("%06x:", i);
650628276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
650728276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
65086f8718a3SScott Long 			printf("\n");
65096f8718a3SScott Long 		}
65106f8718a3SScott Long 
65116f8718a3SScott Long 		printf("Registers:\n");
65120c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
65136f8718a3SScott Long 			printf("%06x:", i);
65146f8718a3SScott Long 			for (j = 0; j < 8; j++) {
65156f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
65166f8718a3SScott Long 				i += 4;
65176f8718a3SScott Long 			}
65186f8718a3SScott Long 			printf("\n");
65196f8718a3SScott Long 		}
652028276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
65216f8718a3SScott Long 
65226f8718a3SScott Long 		printf("Hardware Flags:\n");
652328276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
652428276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6525a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6526a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
65275345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
65286f8718a3SScott Long 			printf(" - 575X Plus\n");
65295345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
65306f8718a3SScott Long 			printf(" - 5705 Plus\n");
65315345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
65325345bad0SScott Long 			printf(" - 5714 Family\n");
65335345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
65345345bad0SScott Long 			printf(" - 5700 Family\n");
65356f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
65366f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
65376f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
65386f8718a3SScott Long 			printf(" - PCI-X Bus\n");
65396f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
65406f8718a3SScott Long 			printf(" - PCI Express Bus\n");
65417d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
65426f8718a3SScott Long 			printf(" - No 3 LEDs\n");
65436f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
65446f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
65456f8718a3SScott Long 	}
65466f8718a3SScott Long 
65476f8718a3SScott Long 	return (error);
65486f8718a3SScott Long }
65496f8718a3SScott Long 
65506f8718a3SScott Long static int
65516f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
65526f8718a3SScott Long {
65536f8718a3SScott Long 	struct bge_softc *sc;
65546f8718a3SScott Long 	int error;
65556f8718a3SScott Long 	uint16_t result;
65566f8718a3SScott Long 	uint32_t val;
65576f8718a3SScott Long 
65586f8718a3SScott Long 	result = -1;
65596f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65606f8718a3SScott Long 	if (error || (req->newptr == NULL))
65616f8718a3SScott Long 		return (error);
65626f8718a3SScott Long 
65636f8718a3SScott Long 	if (result < 0x8000) {
65646f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65656f8718a3SScott Long 		val = CSR_READ_4(sc, result);
65666f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
65676f8718a3SScott Long 	}
65686f8718a3SScott Long 
65696f8718a3SScott Long 	return (error);
65706f8718a3SScott Long }
65716f8718a3SScott Long 
65726f8718a3SScott Long static int
6573548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6574548c8f1aSPyun YongHyeon {
6575548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6576548c8f1aSPyun YongHyeon 	int error;
6577548c8f1aSPyun YongHyeon 	uint16_t result;
6578548c8f1aSPyun YongHyeon 	uint32_t val;
6579548c8f1aSPyun YongHyeon 
6580548c8f1aSPyun YongHyeon 	result = -1;
6581548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6582548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6583548c8f1aSPyun YongHyeon 		return (error);
6584548c8f1aSPyun YongHyeon 
6585548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6586548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6587548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6588548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6589548c8f1aSPyun YongHyeon 	}
6590548c8f1aSPyun YongHyeon 
6591548c8f1aSPyun YongHyeon 	return (error);
6592548c8f1aSPyun YongHyeon }
6593548c8f1aSPyun YongHyeon 
6594548c8f1aSPyun YongHyeon static int
65956f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
65966f8718a3SScott Long {
65976f8718a3SScott Long 	struct bge_softc *sc;
65986f8718a3SScott Long 	int error;
65996f8718a3SScott Long 	uint16_t result;
66006f8718a3SScott Long 	uint32_t val;
66016f8718a3SScott Long 
66026f8718a3SScott Long 	result = -1;
66036f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66046f8718a3SScott Long 	if (error || (req->newptr == NULL))
66056f8718a3SScott Long 		return (error);
66066f8718a3SScott Long 
66076f8718a3SScott Long 	if (result < 0x8000) {
66086f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66096f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
66106f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
66116f8718a3SScott Long 	}
66126f8718a3SScott Long 
66136f8718a3SScott Long 	return (error);
66146f8718a3SScott Long }
66156f8718a3SScott Long #endif
661638cc658fSJohn Baldwin 
661738cc658fSJohn Baldwin static int
66185fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
66195fea260fSMarius Strobl {
66205fea260fSMarius Strobl 
66215fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
66225fea260fSMarius Strobl 		return (1);
66235fea260fSMarius Strobl 
66245fea260fSMarius Strobl #ifdef __sparc64__
66255fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
66265fea260fSMarius Strobl 	return (0);
66275fea260fSMarius Strobl #endif
66285fea260fSMarius Strobl 	return (1);
66295fea260fSMarius Strobl }
66305fea260fSMarius Strobl 
66315fea260fSMarius Strobl static int
663238cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
663338cc658fSJohn Baldwin {
663438cc658fSJohn Baldwin 	uint32_t mac_addr;
663538cc658fSJohn Baldwin 
663673635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
663738cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
663838cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
663938cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
664073635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
664138cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
664238cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
664338cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
664438cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
66455fea260fSMarius Strobl 		return (0);
664638cc658fSJohn Baldwin 	}
66475fea260fSMarius Strobl 	return (1);
664838cc658fSJohn Baldwin }
664938cc658fSJohn Baldwin 
665038cc658fSJohn Baldwin static int
665138cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
665238cc658fSJohn Baldwin {
665338cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
665438cc658fSJohn Baldwin 
665538cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
665638cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
665738cc658fSJohn Baldwin 
66585fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
66595fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
666038cc658fSJohn Baldwin }
666138cc658fSJohn Baldwin 
666238cc658fSJohn Baldwin static int
666338cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
666438cc658fSJohn Baldwin {
666538cc658fSJohn Baldwin 
66665fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
66675fea260fSMarius Strobl 		return (1);
66685fea260fSMarius Strobl 
66695fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
66705fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
667138cc658fSJohn Baldwin }
667238cc658fSJohn Baldwin 
667338cc658fSJohn Baldwin static int
667438cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
667538cc658fSJohn Baldwin {
667638cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
667738cc658fSJohn Baldwin 		/* NOTE: Order is critical */
66785fea260fSMarius Strobl 		bge_get_eaddr_fw,
667938cc658fSJohn Baldwin 		bge_get_eaddr_mem,
668038cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
668138cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
668238cc658fSJohn Baldwin 		NULL
668338cc658fSJohn Baldwin 	};
668438cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
668538cc658fSJohn Baldwin 
668638cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
668738cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
668838cc658fSJohn Baldwin 			break;
668938cc658fSJohn Baldwin 	}
669038cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
669138cc658fSJohn Baldwin }
6692