xref: /freebsd/sys/dev/bge/if_bge.c (revision 91bd90d8c21524e651e84ec42ec0fb8e3e68c55c)
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 */
2404*91bd90d8SPyun 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 
251195d67482SBill Paul 	/* Turn on RX data completion state machine */
251295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
251395d67482SBill Paul 
251495d67482SBill Paul 	/* Turn on RX BD initiator state machine */
251595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
251695d67482SBill Paul 
251795d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
251895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
251995d67482SBill Paul 
252095d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25217ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
252295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
252395d67482SBill Paul 
252495d67482SBill Paul 	/* Turn on send BD completion state machine */
252595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
252695d67482SBill Paul 
252795d67482SBill Paul 	/* Turn on send data completion state machine */
2528a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2529a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2530a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2531a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
253295d67482SBill Paul 
253395d67482SBill Paul 	/* Turn on send data initiator state machine */
25341108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
25351108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
25361108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2537ca3f1187SPyun YongHyeon 	else
253895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
253995d67482SBill Paul 
254095d67482SBill Paul 	/* Turn on send BD initiator state machine */
254195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
254295d67482SBill Paul 
254395d67482SBill Paul 	/* Turn on send BD selector state machine */
254495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
254595d67482SBill Paul 
25460c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
254795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
254895d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
254995d67482SBill Paul 
255095d67482SBill Paul 	/* ack/clear link change events */
255195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
25520434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
25530434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2554f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
255595d67482SBill Paul 
25566ede2cfaSPyun YongHyeon 	/*
25576ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
25586ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
25596ede2cfaSPyun YongHyeon 	 */
2560652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
256195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2562a1d52896SBill Paul 	} else {
25637ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
25647ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
25657ed3f0f0SPyun YongHyeon 			DELAY(80);
25667ed3f0f0SPyun YongHyeon 		}
25671f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
25684c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2569a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2570a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2571a1d52896SBill Paul 	}
257295d67482SBill Paul 
25731f313773SOleg Bulyzhin 	/*
25741f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
25751f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
25761f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
25771f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
25781f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
25791f313773SOleg Bulyzhin 	 */
25801f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
25811f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
25821f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
25831f313773SOleg Bulyzhin 
258495d67482SBill Paul 	/* Enable link state change attentions. */
258595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
258695d67482SBill Paul 
258795d67482SBill Paul 	return (0);
258895d67482SBill Paul }
258995d67482SBill Paul 
2590d7acafa1SMarius Strobl static const struct bge_revision *
25914c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
25924c0da0ffSGleb Smirnoff {
25934c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
25944c0da0ffSGleb Smirnoff 
25954c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
25964c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
25974c0da0ffSGleb Smirnoff 			return (br);
25984c0da0ffSGleb Smirnoff 	}
25994c0da0ffSGleb Smirnoff 
26004c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
26014c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
26024c0da0ffSGleb Smirnoff 			return (br);
26034c0da0ffSGleb Smirnoff 	}
26044c0da0ffSGleb Smirnoff 
26054c0da0ffSGleb Smirnoff 	return (NULL);
26064c0da0ffSGleb Smirnoff }
26074c0da0ffSGleb Smirnoff 
2608d7acafa1SMarius Strobl static const struct bge_vendor *
26094c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26104c0da0ffSGleb Smirnoff {
26114c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26124c0da0ffSGleb Smirnoff 
26134c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26144c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26154c0da0ffSGleb Smirnoff 			return (v);
26164c0da0ffSGleb Smirnoff 
26174c0da0ffSGleb Smirnoff 	return (NULL);
26184c0da0ffSGleb Smirnoff }
26194c0da0ffSGleb Smirnoff 
2620d7acafa1SMarius Strobl static uint32_t
2621d7acafa1SMarius Strobl bge_chipid(device_t dev)
262295d67482SBill Paul {
2623978f2704SMarius Strobl 	uint32_t id;
262495d67482SBill Paul 
2625a5779553SStanislav Sedov 	id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2626a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
26271108273aSPyun YongHyeon 	if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
26281108273aSPyun YongHyeon 		/*
2629d7acafa1SMarius Strobl 		 * Find the ASCI revision.  Different chips use different
2630d7acafa1SMarius Strobl 		 * registers.
26311108273aSPyun YongHyeon 		 */
26321108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
26331108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
26341108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2635bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
263650515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
26371108273aSPyun YongHyeon 			id = pci_read_config(dev,
26381108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
26391108273aSPyun YongHyeon 			break;
2640b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2641fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57762:
2642b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2643fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57766:
2644b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
2645b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
2646b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2647b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2648b4a256acSPyun YongHyeon 			id = pci_read_config(dev,
2649b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2650b4a256acSPyun YongHyeon 			break;
26511108273aSPyun YongHyeon 		default:
2652d7acafa1SMarius Strobl 			id = pci_read_config(dev, BGE_PCI_PRODID_ASICREV, 4);
26531108273aSPyun YongHyeon 		}
26541108273aSPyun YongHyeon 	}
2655d7acafa1SMarius Strobl 	return (id);
2656d7acafa1SMarius Strobl }
2657d7acafa1SMarius Strobl 
2658d7acafa1SMarius Strobl /*
2659d7acafa1SMarius Strobl  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2660d7acafa1SMarius Strobl  * against our list and return its name if we find a match.
2661d7acafa1SMarius Strobl  *
2662d7acafa1SMarius Strobl  * Note that since the Broadcom controller contains VPD support, we
2663d7acafa1SMarius Strobl  * try to get the device name string from the controller itself instead
2664d7acafa1SMarius Strobl  * of the compiled-in string. It guarantees we'll always announce the
2665d7acafa1SMarius Strobl  * right product name. We fall back to the compiled-in string when
2666d7acafa1SMarius Strobl  * VPD is unavailable or corrupt.
2667d7acafa1SMarius Strobl  */
2668d7acafa1SMarius Strobl static int
2669d7acafa1SMarius Strobl bge_probe(device_t dev)
2670d7acafa1SMarius Strobl {
2671d7acafa1SMarius Strobl 	char buf[96];
2672d7acafa1SMarius Strobl 	char model[64];
2673d7acafa1SMarius Strobl 	const struct bge_revision *br;
2674d7acafa1SMarius Strobl 	const char *pname;
2675d7acafa1SMarius Strobl 	struct bge_softc *sc;
2676d7acafa1SMarius Strobl 	const struct bge_type *t = bge_devs;
2677d7acafa1SMarius Strobl 	const struct bge_vendor *v;
2678d7acafa1SMarius Strobl 	uint32_t id;
2679d7acafa1SMarius Strobl 	uint16_t did, vid;
2680d7acafa1SMarius Strobl 
2681d7acafa1SMarius Strobl 	sc = device_get_softc(dev);
2682d7acafa1SMarius Strobl 	sc->bge_dev = dev;
2683d7acafa1SMarius Strobl 	vid = pci_get_vendor(dev);
2684d7acafa1SMarius Strobl 	did = pci_get_device(dev);
2685d7acafa1SMarius Strobl 	while(t->bge_vid != 0) {
2686d7acafa1SMarius Strobl 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2687d7acafa1SMarius Strobl 			id = bge_chipid(dev);
26884c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
2689852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2690852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
2691d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s", pname);
2692d7acafa1SMarius Strobl 			else {
2693d7acafa1SMarius Strobl 				v = bge_lookup_vendor(vid);
2694d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s %s",
2695d7acafa1SMarius Strobl 				    v != NULL ? v->v_name : "Unknown",
26967c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
26972ad1b396SMarius Strobl 				    "NetXtreme/NetLink Ethernet Controller");
2698d7acafa1SMarius Strobl 			}
2699d7acafa1SMarius Strobl 			snprintf(buf, sizeof(buf), "%s, %sASIC rev. %#08x",
2700d7acafa1SMarius Strobl 			    model, br != NULL ? "" : "unknown ", id);
27014c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
2702d7acafa1SMarius Strobl 			return (BUS_PROBE_DEFAULT);
270395d67482SBill Paul 		}
270495d67482SBill Paul 		t++;
270595d67482SBill Paul 	}
270695d67482SBill Paul 
270795d67482SBill Paul 	return (ENXIO);
270895d67482SBill Paul }
270995d67482SBill Paul 
2710f41ac2beSBill Paul static void
27113f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2712f41ac2beSBill Paul {
2713f41ac2beSBill Paul 	int i;
2714f41ac2beSBill Paul 
27153f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2716f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2717f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
27180ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2719f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2720f41ac2beSBill Paul 	}
2721943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2722943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2723943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2724f41ac2beSBill Paul 
27253f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2726f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2727f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2728f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2729f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2730f41ac2beSBill Paul 	}
2731943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2732943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2733943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2734f41ac2beSBill Paul 
27353f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2736f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2737f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
27380ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2739f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2740f41ac2beSBill Paul 	}
2741f41ac2beSBill Paul 
27420ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
27430ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2744c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2745c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
27460ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
27470ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2748f41ac2beSBill Paul 
27493f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2750e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2751e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2752e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2753e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2754f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2755f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2756f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2757f41ac2beSBill Paul 
2758f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2759f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2760f41ac2beSBill Paul 
27613f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2762e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2763e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2764e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2765e65bed95SPyun YongHyeon 
2766e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2767e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2768f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2769f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2770f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2771f41ac2beSBill Paul 
2772f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2773f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2774f41ac2beSBill Paul 
27753f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2776e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2777e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2778e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2779e65bed95SPyun YongHyeon 
2780e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2781e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2782f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2783f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2784f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2785f41ac2beSBill Paul 
2786f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2787f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2788f41ac2beSBill Paul 
27893f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2790e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2791e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2792e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2793e65bed95SPyun YongHyeon 
2794e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2795f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2796f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2797f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2798f41ac2beSBill Paul 
2799f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2800f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2801f41ac2beSBill Paul 
28023f74909aSGleb Smirnoff 	/* Destroy status block. */
2803e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2804e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2805e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2806e65bed95SPyun YongHyeon 
2807e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2808f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2809f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2810f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2811f41ac2beSBill Paul 
2812f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2813f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2814f41ac2beSBill Paul 
28153f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2816e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2817e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2818e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2819e65bed95SPyun YongHyeon 
2820e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2821f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2822f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2823f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2824f41ac2beSBill Paul 
2825f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2826f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2827f41ac2beSBill Paul 
28285b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
28295b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
28305b610048SPyun YongHyeon 
28313f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2832f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2833f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2834f41ac2beSBill Paul }
2835f41ac2beSBill Paul 
2836f41ac2beSBill Paul static int
28375b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
28385b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
28395b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2840f41ac2beSBill Paul {
28413f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
28425b610048SPyun YongHyeon 	int error;
2843f41ac2beSBill Paul 
28445b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2845fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
28465b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
28475b610048SPyun YongHyeon 	if (error != 0) {
28485b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28495b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
28505b610048SPyun YongHyeon 		return (ENOMEM);
28515b610048SPyun YongHyeon 	}
28525b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
28535b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
28545b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
28555b610048SPyun YongHyeon 	if (error != 0) {
28565b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28575b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
28585b610048SPyun YongHyeon 		return (ENOMEM);
28595b610048SPyun YongHyeon 	}
28605b610048SPyun YongHyeon 	/* Load the address of the ring. */
28615b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
28625b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
28635b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
28645b610048SPyun YongHyeon 	if (error != 0) {
28655b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28665b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
28675b610048SPyun YongHyeon 		return (ENOMEM);
28685b610048SPyun YongHyeon 	}
28695b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
28705b610048SPyun YongHyeon 	return (0);
28715b610048SPyun YongHyeon }
28725b610048SPyun YongHyeon 
28735b610048SPyun YongHyeon static int
28745b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
28755b610048SPyun YongHyeon {
28765b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2877fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
28785b610048SPyun YongHyeon 	int i, error;
2879f41ac2beSBill Paul 
2880f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2881f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2882f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2883f41ac2beSBill Paul 	/*
2884f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2885f41ac2beSBill Paul 	 */
28864eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2887f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
28884eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
28894eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2890e65bed95SPyun YongHyeon 	if (error != 0) {
2891fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2892fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2893e65bed95SPyun YongHyeon 		return (ENOMEM);
2894e65bed95SPyun YongHyeon 	}
2895e65bed95SPyun YongHyeon 
28965b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
28975b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
28985b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
28995b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
29005b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
29015b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
29025b610048SPyun YongHyeon 	if (error)
29035b610048SPyun YongHyeon 		return (error);
29045b610048SPyun YongHyeon 
29055b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
29065b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
29075b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
29085b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
29095b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
29105b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
29115b610048SPyun YongHyeon 	if (error)
29125b610048SPyun YongHyeon 		return (error);
29135b610048SPyun YongHyeon 
29145b610048SPyun YongHyeon 	/* Create tag for TX ring. */
29155b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
29165b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
29175b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
29185b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
29195b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
29205b610048SPyun YongHyeon 	if (error)
29215b610048SPyun YongHyeon 		return (error);
29225b610048SPyun YongHyeon 
2923f41ac2beSBill Paul 	/*
29245b610048SPyun YongHyeon 	 * Create tag for status block.
29255b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
29265b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
29275b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
29285b610048SPyun YongHyeon 	 * of configured number of ring.
2929f41ac2beSBill Paul 	 */
29305b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
29315b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
29325b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
29335b610048SPyun YongHyeon 	else
29345b610048SPyun YongHyeon 		sbsz = 32;
29355b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
29365b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
29375b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
29385b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
29395b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
29405b610048SPyun YongHyeon 	if (error)
29415b610048SPyun YongHyeon 		return (error);
29425b610048SPyun YongHyeon 
294312c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
294412c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
294512c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
294612c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
294712c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
294812c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
294912c65daeSPyun YongHyeon 	if (error)
295012c65daeSPyun YongHyeon 		return (error);
295112c65daeSPyun YongHyeon 
29525b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
29535b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
29545b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
29555b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
29565b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
29575b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
29585b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
29595b610048SPyun YongHyeon 		if (error)
29605b610048SPyun YongHyeon 			return (error);
29615b610048SPyun YongHyeon 	}
29625b610048SPyun YongHyeon 
29635b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
2964d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
2965d2ffe15aSPyun YongHyeon 		/*
2966d2ffe15aSPyun YongHyeon 		 * XXX
2967d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
2968d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
2969062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
2970062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
2971d2ffe15aSPyun YongHyeon 		 */
2972062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
2973d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
2974d2ffe15aSPyun YongHyeon 	}
2975fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
2976fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
2977fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
2978fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
29795b610048SPyun YongHyeon 	if (error != 0) {
29805b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29815b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
29825b610048SPyun YongHyeon 		return (ENOMEM);
29835b610048SPyun YongHyeon 	}
29845b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
29851108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2986ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
2987ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
2988ca3f1187SPyun YongHyeon 	} else {
2989ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
2990ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
2991ca3f1187SPyun YongHyeon 	}
29925b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
2993ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2994ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
2995ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
2996f41ac2beSBill Paul 
2997f41ac2beSBill Paul 	if (error) {
29980ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
29990ac56796SPyun YongHyeon 		return (ENOMEM);
30000ac56796SPyun YongHyeon 	}
30010ac56796SPyun YongHyeon 
30025b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
3003f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
3004f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
3005f5459d4cSPyun YongHyeon 	else
3006f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
30075b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
3008f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
3009f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
30100ac56796SPyun YongHyeon 
30110ac56796SPyun YongHyeon 	if (error) {
30120ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3013f41ac2beSBill Paul 		return (ENOMEM);
3014f41ac2beSBill Paul 	}
3015f41ac2beSBill Paul 
30163f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3017943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3018943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3019943787f3SPyun YongHyeon 	if (error) {
3020943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3021943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3022943787f3SPyun YongHyeon 		return (ENOMEM);
3023943787f3SPyun YongHyeon 	}
3024f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
30250ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3026f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3027f41ac2beSBill Paul 		if (error) {
3028fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3029fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3030f41ac2beSBill Paul 			return (ENOMEM);
3031f41ac2beSBill Paul 		}
3032f41ac2beSBill Paul 	}
3033f41ac2beSBill Paul 
30343f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3035f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
30360ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3037f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3038f41ac2beSBill Paul 		if (error) {
3039fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30400ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3041f41ac2beSBill Paul 			return (ENOMEM);
3042f41ac2beSBill Paul 		}
3043f41ac2beSBill Paul 	}
3044f41ac2beSBill Paul 
30455b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
30464c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30475b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
30488a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
30491be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
30501be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3051f41ac2beSBill Paul 		if (error) {
3052fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30533f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3054f41ac2beSBill Paul 			return (ENOMEM);
3055f41ac2beSBill Paul 		}
30563f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3057943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3058943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3059943787f3SPyun YongHyeon 		if (error) {
3060943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
30611b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3062943787f3SPyun YongHyeon 			return (ENOMEM);
3063943787f3SPyun YongHyeon 		}
3064f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3065f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3066f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3067f41ac2beSBill Paul 			if (error) {
3068fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
30693f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3070f41ac2beSBill Paul 				return (ENOMEM);
3071f41ac2beSBill Paul 			}
3072f41ac2beSBill Paul 		}
3073f41ac2beSBill Paul 	}
3074f41ac2beSBill Paul 
3075f41ac2beSBill Paul 	return (0);
3076f41ac2beSBill Paul }
3077f41ac2beSBill Paul 
3078bf6ef57aSJohn Polstra /*
3079bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3080bf6ef57aSJohn Polstra  */
3081bf6ef57aSJohn Polstra static int
3082bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3083bf6ef57aSJohn Polstra {
3084bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
308555aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3086bf6ef57aSJohn Polstra 
308755aaf894SMarius Strobl 	d = pci_get_domain(dev);
3088bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3089bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3090bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3091bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
309255aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3093bf6ef57aSJohn Polstra 			return (1);
3094bf6ef57aSJohn Polstra 	return (0);
3095bf6ef57aSJohn Polstra }
3096bf6ef57aSJohn Polstra 
3097bf6ef57aSJohn Polstra /*
3098bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3099bf6ef57aSJohn Polstra  */
3100bf6ef57aSJohn Polstra static int
3101bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3102bf6ef57aSJohn Polstra {
3103bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3104bf6ef57aSJohn Polstra 
3105d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
31065c952e8dSPyun YongHyeon 		return (0);
31075c952e8dSPyun YongHyeon 
31081108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
31091108273aSPyun YongHyeon #ifdef DEVICE_POLLING
31101108273aSPyun YongHyeon 	return (0);
31111108273aSPyun YongHyeon #endif
3112bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3113a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3114bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3115bf6ef57aSJohn Polstra 		/*
3116a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3117a8376f70SMarius Strobl 		 * configured in single-port mode.
3118bf6ef57aSJohn Polstra 		 */
3119bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3120bf6ef57aSJohn Polstra 			can_use_msi = 1;
3121bf6ef57aSJohn Polstra 		break;
3122bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3123bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3124bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3125bf6ef57aSJohn Polstra 			can_use_msi = 1;
3126bf6ef57aSJohn Polstra 		break;
3127a8376f70SMarius Strobl 	default:
3128a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3129bf6ef57aSJohn Polstra 			can_use_msi = 1;
3130bf6ef57aSJohn Polstra 	}
3131bf6ef57aSJohn Polstra 	return (can_use_msi);
3132bf6ef57aSJohn Polstra }
3133bf6ef57aSJohn Polstra 
313495d67482SBill Paul static int
3135062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3136062af0b0SPyun YongHyeon {
3137062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3138062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3139062af0b0SPyun YongHyeon 		const uint16_t vendor;
3140062af0b0SPyun YongHyeon 		const uint16_t device;
3141062af0b0SPyun YongHyeon 		const char *desc;
314229658c96SDimitry Andric 	} mbox_reorder_lists[] = {
3143062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3144062af0b0SPyun YongHyeon 	};
3145062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3146062af0b0SPyun YongHyeon 	device_t bus, dev;
314747f4a4dcSMarius Strobl 	int i;
3148062af0b0SPyun YongHyeon 
3149062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3150062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3151062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3152062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3153062af0b0SPyun YongHyeon 	for (;;) {
3154062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3155062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3156062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3157062af0b0SPyun YongHyeon 			break;
315847f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3159062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3160062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3161062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3162062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3163062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3164062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3165062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3166062af0b0SPyun YongHyeon 				return (1);
3167062af0b0SPyun YongHyeon 			}
3168062af0b0SPyun YongHyeon 		}
3169062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
3170062af0b0SPyun YongHyeon 			break;
3171062af0b0SPyun YongHyeon 	}
3172062af0b0SPyun YongHyeon 	return (0);
3173062af0b0SPyun YongHyeon }
3174062af0b0SPyun YongHyeon 
3175ea9c3a30SPyun YongHyeon static void
3176ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3177ea9c3a30SPyun YongHyeon {
3178ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3179ea9c3a30SPyun YongHyeon 
3180ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3181ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3182ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3183ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3184ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3185ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3186ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3187ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3188ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3189ea9c3a30SPyun YongHyeon 			clk = 133;
3190ea9c3a30SPyun YongHyeon 		else {
3191ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3192ea9c3a30SPyun YongHyeon 			switch (clk) {
3193ea9c3a30SPyun YongHyeon 			case 0:
3194ea9c3a30SPyun YongHyeon 				clk = 33;
3195ea9c3a30SPyun YongHyeon 				break;
3196ea9c3a30SPyun YongHyeon 			case 2:
3197ea9c3a30SPyun YongHyeon 				clk = 50;
3198ea9c3a30SPyun YongHyeon 				break;
3199ea9c3a30SPyun YongHyeon 			case 4:
3200ea9c3a30SPyun YongHyeon 				clk = 66;
3201ea9c3a30SPyun YongHyeon 				break;
3202ea9c3a30SPyun YongHyeon 			case 6:
3203ea9c3a30SPyun YongHyeon 				clk = 100;
3204ea9c3a30SPyun YongHyeon 				break;
3205ea9c3a30SPyun YongHyeon 			case 7:
3206ea9c3a30SPyun YongHyeon 				clk = 133;
3207ea9c3a30SPyun YongHyeon 				break;
3208ea9c3a30SPyun YongHyeon 			}
3209ea9c3a30SPyun YongHyeon 		}
3210ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3211ea9c3a30SPyun YongHyeon 	} else {
3212ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3213ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3214ea9c3a30SPyun YongHyeon 		else
3215ea9c3a30SPyun YongHyeon 			printf("PCI ");
3216ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3217ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3218ea9c3a30SPyun YongHyeon 			clk = 66;
3219ea9c3a30SPyun YongHyeon 		else
3220ea9c3a30SPyun YongHyeon 			clk = 33;
3221ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3222ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3223ea9c3a30SPyun YongHyeon 		else
3224ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3225ea9c3a30SPyun YongHyeon 	}
3226ea9c3a30SPyun YongHyeon }
3227ea9c3a30SPyun YongHyeon 
3228062af0b0SPyun YongHyeon static int
32293f74909aSGleb Smirnoff bge_attach(device_t dev)
323095d67482SBill Paul {
323195d67482SBill Paul 	struct ifnet *ifp;
323295d67482SBill Paul 	struct bge_softc *sc;
3233548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
323408013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3235daeeb75cSPyun YongHyeon 	int capmask, error, msicount, reg, rid, trys;
323695d67482SBill Paul 
323795d67482SBill Paul 	sc = device_get_softc(dev);
323895d67482SBill Paul 	sc->bge_dev = dev;
323995d67482SBill Paul 
3240e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3241dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3242e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3243dfe0df9aSPyun YongHyeon 
324495d67482SBill Paul 	/*
324595d67482SBill Paul 	 * Map control/status registers.
324695d67482SBill Paul 	 */
324795d67482SBill Paul 	pci_enable_busmaster(dev);
324895d67482SBill Paul 
3249736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
32505f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
325144f8f2fcSMarius Strobl 	    RF_ACTIVE);
325295d67482SBill Paul 
325395d67482SBill Paul 	if (sc->bge_res == NULL) {
3254548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
325595d67482SBill Paul 		error = ENXIO;
325695d67482SBill Paul 		goto fail;
325795d67482SBill Paul 	}
325895d67482SBill Paul 
32594f09c4c7SMarius Strobl 	/* Save various chip information. */
3260548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3261d7acafa1SMarius Strobl 	sc->bge_chipid = bge_chipid(dev);
3262e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3263e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3264e53d81eeSPaul Saab 
3265a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3266daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
32671108273aSPyun YongHyeon 	 /*
32681108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
32691108273aSPyun YongHyeon 	  *
32701108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
32711108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
32721108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
32731108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
32741108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3275bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
327650515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
32771108273aSPyun YongHyeon 	  *
3278548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3279548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3280548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3281548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3282548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3283548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3284548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3285548c8f1aSPyun YongHyeon 	  *
32861108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
32871108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
32881108273aSPyun YongHyeon 	  */
3289bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
329050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
329150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3292548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
32931108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
32941108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3295daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
32961108273aSPyun YongHyeon 			else
3297daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3298bbe2ca75SPyun YongHyeon 		} else {
32991108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33001108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3301daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33021108273aSPyun YongHyeon 			else
3303daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
33041108273aSPyun YongHyeon 		}
33051108273aSPyun YongHyeon 	}
3306a813ed78SPyun YongHyeon 
33075fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
33085fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
330908013fd3SMarius Strobl 
33100dae9719SJung-uk Kim 	/* Save chipset family. */
33110dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
3312fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57765:
3313fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57766:
3314fe26ad88SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_57765_PLUS;
3315fe26ad88SPyun YongHyeon 		/* FALLTHROUGH */
33161108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3317bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
331850515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
33191108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
33201108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3321b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
3322bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3323bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3324bbe2ca75SPyun YongHyeon 			/* Jumbo frame on BCM5719 A0 does not work. */
3325463a7e27SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_JUMBO;
3326bbe2ca75SPyun YongHyeon 		}
33271108273aSPyun YongHyeon 		break;
3328a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3329a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3330a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3331a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3332a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3333a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3334a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3335a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3336a5779553SStanislav Sedov 		break;
33370dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
33380dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
33390dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
33400dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
33417ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
33420dae9719SJung-uk Kim 		break;
33430dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
33440dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
33450dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3346f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
33479fe569d8SXin LI 		/* FALLTHROUGH */
33480dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
33490dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
335038cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
33510dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
33529fe569d8SXin LI 		/* FALLTHROUGH */
33530dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
33540dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
33550dae9719SJung-uk Kim 		break;
33560dae9719SJung-uk Kim 	}
33570dae9719SJung-uk Kim 
3358548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3359548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3360548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3361548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3362548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3363548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
3364548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3365548c8f1aSPyun YongHyeon 		break;
3366548c8f1aSPyun YongHyeon 	}
3367548c8f1aSPyun YongHyeon 
3368548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3369548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3370548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3371548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3372548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3373548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3374548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3375548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3376548c8f1aSPyun YongHyeon 			error = ENXIO;
3377548c8f1aSPyun YongHyeon 			goto fail;
3378548c8f1aSPyun YongHyeon 		}
3379548c8f1aSPyun YongHyeon 
3380548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3381548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3382548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3383548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3384548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3385548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3386548c8f1aSPyun YongHyeon 
3387548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3388548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3389548c8f1aSPyun YongHyeon 	}
3390548c8f1aSPyun YongHyeon 
3391749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3392749a5269SMarius Strobl 	bge_add_sysctls(sc);
3393749a5269SMarius Strobl 
3394a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
33951108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
33961108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3397a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3398a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3399a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3400a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3401a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3402a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3403a813ed78SPyun YongHyeon 	else
3404a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
34057ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
34067ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
34077ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3408a813ed78SPyun YongHyeon 
3409f681b29aSPyun YongHyeon 	/*
3410d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3411f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3412f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3413f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3414f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3415f681b29aSPyun YongHyeon 	 */
3416f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
34174f0794ffSBjoern A. Zeeb 
3418d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3419d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3420d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3421d9820cd8SPyun YongHyeon 
3422a7fcfcf3SPyun YongHyeon 	/*
3423a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3424a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3425a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3426a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3427a7fcfcf3SPyun YongHyeon 	 */
3428a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3429a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3430a7fcfcf3SPyun YongHyeon 
3431ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3432fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
34334f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
34344f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
34354f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
343684ac96f8SPyun YongHyeon 	}
34374f0794ffSBjoern A. Zeeb 
3438fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3439fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3440fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3441fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3442fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3443fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3444fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3445fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3446fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3447fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3448fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3449fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3450fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3451d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57791 ||
3452d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57795 ||
3453fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3454fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3455fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3456d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3457fb772a6cSMarius Strobl 	}
3458fb772a6cSMarius Strobl 
3459e53d81eeSPaul Saab 	/*
3460ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3461ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3462ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3463ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3464d7acafa1SMarius Strobl 	 * known bug which can't handle TSO if Ethernet header + IP/TCP
3465d7acafa1SMarius Strobl 	 * header is greater than 80 bytes. A workaround for the TSO
3466ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3467ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3468ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3469ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3470ca3f1187SPyun YongHyeon 	 */
34711108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
34721108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
34731108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3474bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3475bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3476bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3477bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3478bbe2ca75SPyun YongHyeon 		}
34791108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
34804f4a16e1SPyun YongHyeon 		/*
34814f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
34824f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3483be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
34844f4a16e1SPyun YongHyeon 		 */
34854f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3486be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3487be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3488ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
34894f4a16e1SPyun YongHyeon 	}
3490ca3f1187SPyun YongHyeon 
3491ca3f1187SPyun YongHyeon 	/*
34926f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3493e53d81eeSPaul Saab 	 */
34943b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
34954c0da0ffSGleb Smirnoff 		/*
34966f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
34976f8718a3SScott Long 		 * must be a PCI Express device.
34986f8718a3SScott Long 		 */
34996f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
35000aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
350148630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
350248630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
350348630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
350448630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
350550515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
350650515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
350748630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
350848630d79SPyun YongHyeon 		else
350948630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
351048630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
35116f8718a3SScott Long 	} else {
35126f8718a3SScott Long 		/*
35136f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
35146f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
35154c0da0ffSGleb Smirnoff 		 */
35163b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
35170aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
351890447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
35194c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3520652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
35216f8718a3SScott Long 	}
35224c0da0ffSGleb Smirnoff 
3523bf6ef57aSJohn Polstra 	/*
3524fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3525fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3526fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3527fd4d32feSPyun YongHyeon 	 */
3528fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3529fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3530fd4d32feSPyun YongHyeon 	/*
3531062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3532062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3533062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3534062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3535062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3536062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3537062af0b0SPyun YongHyeon 	 */
3538062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3539062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3540062af0b0SPyun YongHyeon 	/*
3541bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3542bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3543bf6ef57aSJohn Polstra 	 * normal operation.
3544bf6ef57aSJohn Polstra 	 */
35450aaf1057SPyun YongHyeon 	rid = 0;
35463b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
35470aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3548bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
3549bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
3550bf6ef57aSJohn Polstra 			if (msicount > 1)
3551bf6ef57aSJohn Polstra 				msicount = 1;
3552bf6ef57aSJohn Polstra 		} else
3553bf6ef57aSJohn Polstra 			msicount = 0;
3554bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
3555bf6ef57aSJohn Polstra 			rid = 1;
3556bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
35570aaf1057SPyun YongHyeon 		}
35580aaf1057SPyun YongHyeon 	}
3559bf6ef57aSJohn Polstra 
35601108273aSPyun YongHyeon 	/*
35611108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
35621108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
35631108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
35641108273aSPyun YongHyeon 	 */
35651108273aSPyun YongHyeon #ifndef DEVICE_POLLING
35661108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
35671108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
35681108273aSPyun YongHyeon #endif
35691108273aSPyun YongHyeon 
3570bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3571bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
3572bf6ef57aSJohn Polstra 
3573bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3574bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3575bf6ef57aSJohn Polstra 		error = ENXIO;
3576bf6ef57aSJohn Polstra 		goto fail;
3577bf6ef57aSJohn Polstra 	}
3578bf6ef57aSJohn Polstra 
3579ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
35804f09c4c7SMarius Strobl 
35818cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3582548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3583548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3584888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3585888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3586548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3587548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
35888cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
35898cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3590d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
35918cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
35928cb1383cSDoug Ambrisko 			}
35938cb1383cSDoug Ambrisko 		}
3594548c8f1aSPyun YongHyeon 	}
35958cb1383cSDoug Ambrisko 
35968cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
35973dd76c98SPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
35988cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
35998cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
36008cb1383cSDoug Ambrisko 		error = ENXIO;
36018cb1383cSDoug Ambrisko 		goto fail;
36028cb1383cSDoug Ambrisko 	}
36038cb1383cSDoug Ambrisko 
36043dd76c98SPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
36053dd76c98SPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
360695d67482SBill Paul 
360795d67482SBill Paul 	if (bge_chipinit(sc)) {
3608fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
360995d67482SBill Paul 		error = ENXIO;
361095d67482SBill Paul 		goto fail;
361195d67482SBill Paul 	}
361295d67482SBill Paul 
361338cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
361438cc658fSJohn Baldwin 	if (error) {
361508013fd3SMarius Strobl 		device_printf(sc->bge_dev,
361608013fd3SMarius Strobl 		    "failed to read station address\n");
361795d67482SBill Paul 		error = ENXIO;
361895d67482SBill Paul 		goto fail;
361995d67482SBill Paul 	}
362095d67482SBill Paul 
3621f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
36221108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
36231108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
36241108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3625f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3626f41ac2beSBill Paul 	else
3627f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3628f41ac2beSBill Paul 
36295b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3630fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3631fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3632f41ac2beSBill Paul 		error = ENXIO;
3633f41ac2beSBill Paul 		goto fail;
3634f41ac2beSBill Paul 	}
3635f41ac2beSBill Paul 
363695d67482SBill Paul 	/* Set default tuneable values. */
363795d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
363895d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
363995d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
36406f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
36416f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
364295d67482SBill Paul 
364335f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
364435f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
364535f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
364635f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
364735f945cdSPyun YongHyeon 
364895d67482SBill Paul 	/* Set up ifnet structure */
3649fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3650fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3651fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3652fc74a9f9SBrooks Davis 		error = ENXIO;
3653fc74a9f9SBrooks Davis 		goto fail;
3654fc74a9f9SBrooks Davis 	}
365595d67482SBill Paul 	ifp->if_softc = sc;
36569bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
365795d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
365895d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
365995d67482SBill Paul 	ifp->if_start = bge_start;
366095d67482SBill Paul 	ifp->if_init = bge_init;
36614d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
36624d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
36634d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
366435f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
3665d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
36664e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
36671108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3668ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
366904bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
3670ca3f1187SPyun YongHyeon 	}
36714e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
36724e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
36734e35d186SJung-uk Kim #endif
367495d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
367575719184SGleb Smirnoff #ifdef DEVICE_POLLING
367675719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
367775719184SGleb Smirnoff #endif
367895d67482SBill Paul 
3679a1d52896SBill Paul 	/*
3680d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3681d375e524SGleb Smirnoff 	 * to hardware bugs.
3682d375e524SGleb Smirnoff 	 */
3683d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3684d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
36854d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
3686d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
3687d375e524SGleb Smirnoff 	}
3688d375e524SGleb Smirnoff 
3689d375e524SGleb Smirnoff 	/*
3690a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
369141abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
369241abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
369341abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
369441abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
369541abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
369641abcc1bSPaul Saab 	 * SK-9D41.
3697a1d52896SBill Paul 	 */
3698888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3699888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
37005fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
37015fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3702f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3703f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3704fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3705f6789fbaSPyun YongHyeon 			error = ENXIO;
3706f6789fbaSPyun YongHyeon 			goto fail;
3707f6789fbaSPyun YongHyeon 		}
370841abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
370941abcc1bSPaul Saab 	}
371041abcc1bSPaul Saab 
371195d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3712ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3713ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
371470c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3715ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
371670c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
371770c2071bSPyun YongHyeon 		} else
3718652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3719ea3b4127SPyun YongHyeon 	}
372095d67482SBill Paul 
372170c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
372270c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
372370c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
372470c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
372570c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
372670c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
372770c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
372870c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
372970c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
373070c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
373170c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
373270c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
373370c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
373470c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3735fe26ad88SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3736fe26ad88SPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc)) {
373770c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
373870c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
373970c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
374070c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
374170c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
374270c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
374370c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
374470c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
374570c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
374670c2071bSPyun YongHyeon 		} else
374770c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
374870c2071bSPyun YongHyeon 	}
374970c2071bSPyun YongHyeon 
375070c2071bSPyun YongHyeon 	/*
3751d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
375270c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
375370c2071bSPyun YongHyeon 	 */
375470c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
375570c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
375670c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3757d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
375870c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
375970c2071bSPyun YongHyeon 
3760652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
37610c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
37620c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
37630c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
37646098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
37656098821cSJung-uk Kim 		    0, NULL);
376695d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
376795d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3768da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
376995d67482SBill Paul 	} else {
377095d67482SBill Paul 		/*
37718cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
37728cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
37738cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
37748cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
37758cb1383cSDoug Ambrisko 		 * the PHY.
377695d67482SBill Paul 		 */
37774012d104SMarius Strobl 		trys = 0;
37788cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
37798cb1383cSDoug Ambrisko again:
37808cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
37818cb1383cSDoug Ambrisko 
3782fb772a6cSMarius Strobl 		error = mii_attach(dev, &sc->bge_miibus, ifp, bge_ifmedia_upd,
3783daeeb75cSPyun YongHyeon 		    bge_ifmedia_sts, capmask, sc->bge_phy_addr, MII_OFFSET_ANY,
3784fb772a6cSMarius Strobl 		    MIIF_DOPAUSE);
37858e5d93dbSMarius Strobl 		if (error != 0) {
37868cb1383cSDoug Ambrisko 			if (trys++ < 4) {
37878cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3788daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3789daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
37908cb1383cSDoug Ambrisko 				goto again;
37918cb1383cSDoug Ambrisko 			}
37928e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
379395d67482SBill Paul 			goto fail;
379495d67482SBill Paul 		}
37958cb1383cSDoug Ambrisko 
37968cb1383cSDoug Ambrisko 		/*
37978cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
37988cb1383cSDoug Ambrisko 		 */
37998cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
38008cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
380195d67482SBill Paul 	}
380295d67482SBill Paul 
380395d67482SBill Paul 	/*
3804e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3805e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3806e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3807e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3808e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3809e255b776SJohn Polstra 	 * payloads by copying the received packets.
3810e255b776SJohn Polstra 	 */
3811652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3812652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3813652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3814e255b776SJohn Polstra 
3815e255b776SJohn Polstra 	/*
381695d67482SBill Paul 	 * Call MI attach routine.
381795d67482SBill Paul 	 */
3818fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
38190f9bd73bSSam Leffler 
382061ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
382161ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
382261ccb9daSPyun YongHyeon 
38230f9bd73bSSam Leffler 	/*
38240f9bd73bSSam Leffler 	 * Hookup IRQ last.
38250f9bd73bSSam Leffler 	 */
3826dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3827dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
38287e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
38297e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3830dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3831dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3832dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3833dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3834dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3835e010b055SPyun YongHyeon 			error = ENOMEM;
3836dfe0df9aSPyun YongHyeon 			goto fail;
3837dfe0df9aSPyun YongHyeon 		}
3838d7acafa1SMarius Strobl 		error = taskqueue_start_threads(&sc->bge_tq, 1, PI_NET,
3839d7acafa1SMarius Strobl 		    "%s taskq", device_get_nameunit(sc->bge_dev));
3840d7acafa1SMarius Strobl 		if (error != 0) {
3841d7acafa1SMarius Strobl 			device_printf(dev, "could not start threads.\n");
3842d7acafa1SMarius Strobl 			ether_ifdetach(ifp);
3843d7acafa1SMarius Strobl 			goto fail;
3844d7acafa1SMarius Strobl 		}
3845dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3846dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3847dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3848dfe0df9aSPyun YongHyeon 	} else
3849dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3850dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3851dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
38520f9bd73bSSam Leffler 
38530f9bd73bSSam Leffler 	if (error) {
3854e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3855fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
38560f9bd73bSSam Leffler 	}
385795d67482SBill Paul 
385895d67482SBill Paul fail:
3859e010b055SPyun YongHyeon 	if (error)
3860e010b055SPyun YongHyeon 		bge_detach(dev);
386195d67482SBill Paul 	return (error);
386295d67482SBill Paul }
386395d67482SBill Paul 
386495d67482SBill Paul static int
38653f74909aSGleb Smirnoff bge_detach(device_t dev)
386695d67482SBill Paul {
386795d67482SBill Paul 	struct bge_softc *sc;
386895d67482SBill Paul 	struct ifnet *ifp;
386995d67482SBill Paul 
387095d67482SBill Paul 	sc = device_get_softc(dev);
3871fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
387295d67482SBill Paul 
387375719184SGleb Smirnoff #ifdef DEVICE_POLLING
387475719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
387575719184SGleb Smirnoff 		ether_poll_deregister(ifp);
387675719184SGleb Smirnoff #endif
387775719184SGleb Smirnoff 
3878e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3879e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
38800f9bd73bSSam Leffler 		BGE_LOCK(sc);
388195d67482SBill Paul 		bge_stop(sc);
38820f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
38835dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3884e010b055SPyun YongHyeon 	}
38855dda8085SOleg Bulyzhin 
3886dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3887dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
388895d67482SBill Paul 
38890aba72ddSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
389095d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
38910aba72ddSPyun YongHyeon 	else if (sc->bge_miibus != NULL) {
389295d67482SBill Paul 		bus_generic_detach(dev);
389395d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
389495d67482SBill Paul 	}
389595d67482SBill Paul 
389695d67482SBill Paul 	bge_release_resources(sc);
389795d67482SBill Paul 
389895d67482SBill Paul 	return (0);
389995d67482SBill Paul }
390095d67482SBill Paul 
390195d67482SBill Paul static void
39023f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
390395d67482SBill Paul {
390495d67482SBill Paul 	device_t dev;
390595d67482SBill Paul 
390695d67482SBill Paul 	dev = sc->bge_dev;
390795d67482SBill Paul 
3908dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3909dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3910dfe0df9aSPyun YongHyeon 
391195d67482SBill Paul 	if (sc->bge_intrhand != NULL)
391295d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
391395d67482SBill Paul 
391495d67482SBill Paul 	if (sc->bge_irq != NULL)
3915724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3916724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3917724bd939SJohn Polstra 
3918724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3919724bd939SJohn Polstra 		pci_release_msi(dev);
392095d67482SBill Paul 
392195d67482SBill Paul 	if (sc->bge_res != NULL)
392295d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3923736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
392495d67482SBill Paul 
3925548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
3926548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
3927548c8f1aSPyun YongHyeon 		    PCIR_BAR(2), sc->bge_res2);
3928548c8f1aSPyun YongHyeon 
3929ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3930ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3931ad61f896SRuslan Ermilov 
3932f41ac2beSBill Paul 	bge_dma_free(sc);
393395d67482SBill Paul 
39340f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
39350f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
393695d67482SBill Paul }
393795d67482SBill Paul 
39388cb1383cSDoug Ambrisko static int
39393f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
394095d67482SBill Paul {
394195d67482SBill Paul 	device_t dev;
3942cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
39436f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
39440aaf1057SPyun YongHyeon 	uint16_t devctl;
39455fea260fSMarius Strobl 	int i;
394695d67482SBill Paul 
394795d67482SBill Paul 	dev = sc->bge_dev;
394895d67482SBill Paul 
3949cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
3950548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
3951548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
3952cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
3953cc085b36SPyun YongHyeon 
395438cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
395538cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
39566f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
39576f8718a3SScott Long 			write_op = bge_writemem_direct;
39586f8718a3SScott Long 		else
39596f8718a3SScott Long 			write_op = bge_writemem_ind;
39609ba784dbSScott Long 	} else
39616f8718a3SScott Long 		write_op = bge_writereg_ind;
39626f8718a3SScott Long 
39633dd76c98SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
39643dd76c98SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5701) {
39653dd76c98SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
39663dd76c98SPyun YongHyeon 		for (i = 0; i < 8000; i++) {
39673dd76c98SPyun YongHyeon 			if (CSR_READ_4(sc, BGE_NVRAM_SWARB) &
39683dd76c98SPyun YongHyeon 			    BGE_NVRAMSWARB_GNT1)
39693dd76c98SPyun YongHyeon 				break;
39703dd76c98SPyun YongHyeon 			DELAY(20);
39713dd76c98SPyun YongHyeon 		}
39723dd76c98SPyun YongHyeon 		if (i == 8000) {
39733dd76c98SPyun YongHyeon 			if (bootverbose)
39743dd76c98SPyun YongHyeon 				device_printf(dev, "NVRAM lock timedout!\n");
39753dd76c98SPyun YongHyeon 		}
39763dd76c98SPyun YongHyeon 	}
3977548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
3978548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
3979548c8f1aSPyun YongHyeon 
398095d67482SBill Paul 	/* Save some important PCI state. */
398195d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
398295d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
398395d67482SBill Paul 
398495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
398595d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3986e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
398795d67482SBill Paul 
39886f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
39896f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
3990a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
39916f8718a3SScott Long 		if (bootverbose)
3992333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
39936f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
39946f8718a3SScott Long 	}
39956f8718a3SScott Long 
39966f8718a3SScott Long 	/*
39976f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
39986f8718a3SScott Long 	 * When firmware finishes its initialization it will
3999888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
40006f8718a3SScott Long 	 */
4001888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
40026f8718a3SScott Long 
40030c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4004e53d81eeSPaul Saab 
4005e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4006652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4007ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4008ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
40090c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
40100c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4011ad49eccfSPyun YongHyeon 		}
4012e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4013e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
40140c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
40150c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4016e53d81eeSPaul Saab 		}
4017e53d81eeSPaul Saab 	}
4018e53d81eeSPaul Saab 
4019df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4020df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4021df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4022df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4023df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4024df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4025df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4026df4db538SPyun YongHyeon 	}
4027df4db538SPyun YongHyeon 
402821c9e407SDavid Christensen 	/*
40296f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
40306f8718a3SScott Long 	 * powered up in D0 uninitialized.
40316f8718a3SScott Long 	 */
40325512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
40335512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4034caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
40356f8718a3SScott Long 
403695d67482SBill Paul 	/* Issue global reset */
40376f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
403895d67482SBill Paul 
4039cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4040cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4041cc085b36SPyun YongHyeon 	else
404295d67482SBill Paul 		DELAY(1000);
404395d67482SBill Paul 
4044e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4045652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4046e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4047e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
40485fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
40495fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4050e53d81eeSPaul Saab 		}
40510aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4052389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
40530aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4054389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4055389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4056389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
40570aaf1057SPyun YongHyeon 		    devctl, 2);
405848630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
40590aaf1057SPyun YongHyeon 		/* Clear error status. */
4060389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4061389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4062389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4063389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4064e53d81eeSPaul Saab 	}
4065e53d81eeSPaul Saab 
40663f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
406795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
406895d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4069e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4070cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4071cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4072cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4073cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4074548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4075548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4076548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4077548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4078cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
407995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
408095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4081cbb2b2feSPyun YongHyeon 	/*
4082cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4083fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4084cbb2b2feSPyun YongHyeon 	 * read stale status block.
4085cbb2b2feSPyun YongHyeon 	 */
4086cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4087cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4088cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4089cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4090cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4091cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4092cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4093cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4094cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4095cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4096cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4097cbb2b2feSPyun YongHyeon 		}
4098cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4099cbb2b2feSPyun YongHyeon 		    devctl, 2);
4100cbb2b2feSPyun YongHyeon 	}
410122a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
41024c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4103bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4104bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
41050aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
41060aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
41070aaf1057SPyun YongHyeon 			pci_write_config(dev,
41080aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4109bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4110bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4111bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4112bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4113bf6ef57aSJohn Polstra 		}
41144c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
41154c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
41164c0da0ffSGleb Smirnoff 	} else
4117a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4118a7b0c314SPaul Saab 
4119cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4120cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4121cc085b36SPyun YongHyeon 
4122cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4123cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4124cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4125cc085b36SPyun YongHyeon 	DELAY(40);
4126cc085b36SPyun YongHyeon 
4127548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4128548c8f1aSPyun YongHyeon 
412938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
413038cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
413138cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
413238cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
413338cc658fSJohn Baldwin 				break;
413438cc658fSJohn Baldwin 			DELAY(100);
413538cc658fSJohn Baldwin 		}
413638cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4137333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
413838cc658fSJohn Baldwin 			return (1);
413938cc658fSJohn Baldwin 		}
414038cc658fSJohn Baldwin 	} else {
414195d67482SBill Paul 		/*
41426f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
414308013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
41445fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
41455fea260fSMarius Strobl 		 * address is fitted though.
414695d67482SBill Paul 		 */
414795d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4148d5d23857SJung-uk Kim 			DELAY(10);
4149888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4150888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
415195d67482SBill Paul 				break;
415295d67482SBill Paul 		}
415395d67482SBill Paul 
41545fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4155333704a3SPyun YongHyeon 			device_printf(dev,
4156333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4157333704a3SPyun YongHyeon 			    val);
4158b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4159b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4160b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
416138cc658fSJohn Baldwin 	}
416295d67482SBill Paul 
416395d67482SBill Paul 	/*
4164da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4165da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4166da3003f0SBill Paul 	 * to 1.2V.
4167da3003f0SBill Paul 	 */
4168652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4169652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
41705fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
41715fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
41725fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4173da3003f0SBill Paul 	}
4174da3003f0SBill Paul 
4175e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4176652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4177b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4178a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4179a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4180a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
41815fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
41825fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4183e53d81eeSPaul Saab 	}
41848cb1383cSDoug Ambrisko 
418550515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
418650515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
418750515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
418850515680SPyun YongHyeon 
41898cb1383cSDoug Ambrisko 	return (0);
419095d67482SBill Paul }
419195d67482SBill Paul 
4192e0b7b101SPyun YongHyeon static __inline void
4193e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4194e0b7b101SPyun YongHyeon {
4195e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4196e0b7b101SPyun YongHyeon 
4197e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4198e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4199e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4200e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4201e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4202e0b7b101SPyun YongHyeon }
4203e0b7b101SPyun YongHyeon 
4204e0b7b101SPyun YongHyeon static __inline void
4205e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4206e0b7b101SPyun YongHyeon {
4207e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4208e0b7b101SPyun YongHyeon 
4209e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4210e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4211e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4212e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4213e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4214e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4215e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4216e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4217e0b7b101SPyun YongHyeon }
4218e0b7b101SPyun YongHyeon 
421995d67482SBill Paul /*
422095d67482SBill Paul  * Frame reception handling. This is called if there's a frame
422195d67482SBill Paul  * on the receive return list.
422295d67482SBill Paul  *
422395d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
42241be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
422595d67482SBill Paul  * 2) the frame is from the standard receive ring
422695d67482SBill Paul  */
422795d67482SBill Paul 
42281abcdbd1SAttilio Rao static int
4229dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
423095d67482SBill Paul {
423195d67482SBill Paul 	struct ifnet *ifp;
42321abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4233b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
423495d67482SBill Paul 
42357f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
42360f9bd73bSSam Leffler 
42373f74909aSGleb Smirnoff 	/* Nothing to do. */
42387f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
42391abcdbd1SAttilio Rao 		return (rx_npkts);
4240cfcb5025SOleg Bulyzhin 
4241fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
424295d67482SBill Paul 
4243f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4244e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4245f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
424615eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4247f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4248f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4249c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
4250f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
425115eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4252f41ac2beSBill Paul 
42537f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
425495d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
42553f74909aSGleb Smirnoff 		uint32_t		rxidx;
425695d67482SBill Paul 		struct mbuf		*m = NULL;
42573f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
425895d67482SBill Paul 		int			have_tag = 0;
425995d67482SBill Paul 
426075719184SGleb Smirnoff #ifdef DEVICE_POLLING
426175719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
426275719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
426375719184SGleb Smirnoff 				break;
426475719184SGleb Smirnoff 			sc->rxcycles--;
426575719184SGleb Smirnoff 		}
426675719184SGleb Smirnoff #endif
426775719184SGleb Smirnoff 
42687f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
426995d67482SBill Paul 
427095d67482SBill Paul 		rxidx = cur_rx->bge_idx;
42717f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
427295d67482SBill Paul 
4273cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
4274cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
427595d67482SBill Paul 			have_tag = 1;
427695d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
427795d67482SBill Paul 		}
427895d67482SBill Paul 
427995d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
428095d67482SBill Paul 			jumbocnt++;
4281943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
428295d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4283e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
428495d67482SBill Paul 				continue;
428595d67482SBill Paul 			}
4286943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4287e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4288943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
428995d67482SBill Paul 				continue;
429095d67482SBill Paul 			}
429103e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
429295d67482SBill Paul 		} else {
429395d67482SBill Paul 			stdcnt++;
4294e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
429595d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4296e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
429795d67482SBill Paul 				continue;
429895d67482SBill Paul 			}
4299943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4300e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4301943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
430295d67482SBill Paul 				continue;
430395d67482SBill Paul 			}
430403e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
430595d67482SBill Paul 		}
430695d67482SBill Paul 
430795d67482SBill Paul 		ifp->if_ipackets++;
4308e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4309e255b776SJohn Polstra 		/*
4310e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4311e65bed95SPyun YongHyeon 		 * the payload is aligned.
4312e255b776SJohn Polstra 		 */
4313652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4314e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4315e255b776SJohn Polstra 			    cur_rx->bge_len);
4316e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4317e255b776SJohn Polstra 		}
4318e255b776SJohn Polstra #endif
4319473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
432095d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
432195d67482SBill Paul 
43221108273aSPyun YongHyeon 		if (ifp->if_capenable & IFCAP_RXCSUM)
43231108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
432495d67482SBill Paul 
432595d67482SBill Paul 		/*
4326673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4327673d9191SSam Leffler 		 * attach that information to the packet.
432895d67482SBill Paul 		 */
4329d147662cSGleb Smirnoff 		if (have_tag) {
433078ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
433178ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4332d147662cSGleb Smirnoff 		}
433395d67482SBill Paul 
4334dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
43350f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4336673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
43370f9bd73bSSam Leffler 			BGE_LOCK(sc);
4338dfe0df9aSPyun YongHyeon 		} else
4339dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
4340d4da719cSAttilio Rao 		rx_npkts++;
434125e13e68SXin LI 
434225e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
43438cf7d13dSAttilio Rao 			return (rx_npkts);
434495d67482SBill Paul 	}
434595d67482SBill Paul 
434615eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
434715eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4348e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4349f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4350e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
43514c0da0ffSGleb Smirnoff 
4352c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4353f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
43544c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4355f41ac2beSBill Paul 
43567f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
435738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
435895d67482SBill Paul 	if (stdcnt)
4359767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4360767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
436195d67482SBill Paul 	if (jumbocnt)
4362767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4363767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4364f5a034f9SPyun YongHyeon #ifdef notyet
4365f5a034f9SPyun YongHyeon 	/*
4366f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4367f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4368f5a034f9SPyun YongHyeon 	 */
4369f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4370f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
4371f5a034f9SPyun YongHyeon #endif
43721abcdbd1SAttilio Rao 	return (rx_npkts);
437395d67482SBill Paul }
437495d67482SBill Paul 
437595d67482SBill Paul static void
43761108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
43771108273aSPyun YongHyeon {
43781108273aSPyun YongHyeon 
43791108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
43801108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
43811108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
43821108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
43831108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
43841108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
43851108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
43861108273aSPyun YongHyeon 			}
43871108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
43881108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
43891108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
43901108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
43911108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
43921108273aSPyun YongHyeon 			}
43931108273aSPyun YongHyeon 		}
43941108273aSPyun YongHyeon 	} else {
43951108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
43961108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
43971108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
43981108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
43991108273aSPyun YongHyeon 		}
44001108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
44011108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
44021108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
44031108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
44041108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44051108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
44061108273aSPyun YongHyeon 		}
44071108273aSPyun YongHyeon 	}
44081108273aSPyun YongHyeon }
44091108273aSPyun YongHyeon 
44101108273aSPyun YongHyeon static void
4411b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
441295d67482SBill Paul {
441395a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
441495d67482SBill Paul 	struct ifnet *ifp;
441595d67482SBill Paul 
44160f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
44170f9bd73bSSam Leffler 
44183f74909aSGleb Smirnoff 	/* Nothing to do. */
4419b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4420cfcb5025SOleg Bulyzhin 		return;
4421cfcb5025SOleg Bulyzhin 
4422fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
442395d67482SBill Paul 
4424e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
44255c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
442695d67482SBill Paul 	/*
442795d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
442895d67482SBill Paul 	 * frames that have been sent.
442995d67482SBill Paul 	 */
4430b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
443195a0a340SPyun YongHyeon 		uint32_t		idx;
443295d67482SBill Paul 
443395d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4434f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
443595d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
443695d67482SBill Paul 			ifp->if_opackets++;
443795d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
44380ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4439e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4440e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
44410ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4442f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4443e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4444e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
444595d67482SBill Paul 		}
444695d67482SBill Paul 		sc->bge_txcnt--;
444795d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
444895d67482SBill Paul 	}
444995d67482SBill Paul 
445013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
44515b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
44525b01e77cSBruce Evans 		sc->bge_timer = 0;
445395d67482SBill Paul }
445495d67482SBill Paul 
445575719184SGleb Smirnoff #ifdef DEVICE_POLLING
44561abcdbd1SAttilio Rao static int
445775719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
445875719184SGleb Smirnoff {
445975719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
4460b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4461366454f2SOleg Bulyzhin 	uint32_t statusword;
44621abcdbd1SAttilio Rao 	int rx_npkts = 0;
446375719184SGleb Smirnoff 
44643f74909aSGleb Smirnoff 	BGE_LOCK(sc);
44653f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
44663f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
44671abcdbd1SAttilio Rao 		return (rx_npkts);
44683f74909aSGleb Smirnoff 	}
446975719184SGleb Smirnoff 
4470dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4471b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4472b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
44732246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4474b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4475b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4476dab5cd05SOleg Bulyzhin 
4477175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
44782246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4479175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4480dab5cd05SOleg Bulyzhin 
4481dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4482b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4483b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4484366454f2SOleg Bulyzhin 
44850c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4486366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4487366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4488366454f2SOleg Bulyzhin 
4489366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4490366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
44914c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4492652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4493366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4494366454f2SOleg Bulyzhin 
4495366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4496dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
449725e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
449825e13e68SXin LI 		BGE_UNLOCK(sc);
44998cf7d13dSAttilio Rao 		return (rx_npkts);
450025e13e68SXin LI 	}
4501b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4502366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4503366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
45043f74909aSGleb Smirnoff 
45053f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
45061abcdbd1SAttilio Rao 	return (rx_npkts);
450775719184SGleb Smirnoff }
450875719184SGleb Smirnoff #endif /* DEVICE_POLLING */
450975719184SGleb Smirnoff 
4510dfe0df9aSPyun YongHyeon static int
4511dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4512dfe0df9aSPyun YongHyeon {
4513dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4514dfe0df9aSPyun YongHyeon 
4515dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4516dfe0df9aSPyun YongHyeon 	/*
4517dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4518dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4519dfe0df9aSPyun YongHyeon 	 */
4520dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4521dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4522dfe0df9aSPyun YongHyeon }
4523dfe0df9aSPyun YongHyeon 
4524dfe0df9aSPyun YongHyeon static void
4525dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4526dfe0df9aSPyun YongHyeon {
4527dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4528dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
45291108273aSPyun YongHyeon 	uint32_t status, status_tag;
4530dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4531dfe0df9aSPyun YongHyeon 
4532dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4533dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4534dfe0df9aSPyun YongHyeon 
453566151edfSPyun YongHyeon 	BGE_LOCK(sc);
453666151edfSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
453766151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4538dfe0df9aSPyun YongHyeon 		return;
453966151edfSPyun YongHyeon 	}
4540dfe0df9aSPyun YongHyeon 
4541dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4542dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4543dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4544dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4545dfe0df9aSPyun YongHyeon 
45462246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4547dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4548dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4549dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
45501108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
45512246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4552dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4553dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4554dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4555dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
45561108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
45571108273aSPyun YongHyeon 		status_tag = 0;
455866151edfSPyun YongHyeon 
455966151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
456066151edfSPyun YongHyeon 		bge_link_upd(sc);
456166151edfSPyun YongHyeon 
4562dfe0df9aSPyun YongHyeon 	/* Let controller work. */
45631108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4564dfe0df9aSPyun YongHyeon 
456566151edfSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
456666151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4567dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
456866151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4569dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
457066151edfSPyun YongHyeon 		BGE_LOCK(sc);
4571dfe0df9aSPyun YongHyeon 	}
4572dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4573dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4574dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4575dfe0df9aSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4576dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4577dfe0df9aSPyun YongHyeon 	}
457866151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4579dfe0df9aSPyun YongHyeon }
4580dfe0df9aSPyun YongHyeon 
458195d67482SBill Paul static void
45823f74909aSGleb Smirnoff bge_intr(void *xsc)
458395d67482SBill Paul {
458495d67482SBill Paul 	struct bge_softc *sc;
458595d67482SBill Paul 	struct ifnet *ifp;
4586dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4587b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
458895d67482SBill Paul 
458995d67482SBill Paul 	sc = xsc;
4590f41ac2beSBill Paul 
45910f9bd73bSSam Leffler 	BGE_LOCK(sc);
45920f9bd73bSSam Leffler 
4593dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4594dab5cd05SOleg Bulyzhin 
459575719184SGleb Smirnoff #ifdef DEVICE_POLLING
459675719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
459775719184SGleb Smirnoff 		BGE_UNLOCK(sc);
459875719184SGleb Smirnoff 		return;
459975719184SGleb Smirnoff 	}
460075719184SGleb Smirnoff #endif
460175719184SGleb Smirnoff 
4602f30cbfc6SScott Long 	/*
4603b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4604b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4605b848e032SBruce Evans 	 * our current organization this just gives complications and
4606b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4607b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4608b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4609b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4610b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4611b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4612b848e032SBruce Evans 	 *
4613b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4614b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4615b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4616b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4617b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4618b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4619b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4620b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4621b848e032SBruce Evans 	 */
462238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4623b848e032SBruce Evans 
4624f584dfd1SPyun YongHyeon 	/*
4625f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4626f584dfd1SPyun YongHyeon 	 */
4627f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4628f584dfd1SPyun YongHyeon 
4629f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4630f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4631f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4632f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4633f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4634f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4635f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4636f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4637f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4638f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4639f584dfd1SPyun YongHyeon 
46401f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
46414c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4642f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4643dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
464495d67482SBill Paul 
464513f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46463f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4647dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
464825e13e68SXin LI 	}
464995d67482SBill Paul 
465025e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46513f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4652b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
465395d67482SBill Paul 	}
465495d67482SBill Paul 
465513f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
465613f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
46570f9bd73bSSam Leffler 		bge_start_locked(ifp);
46580f9bd73bSSam Leffler 
46590f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
466095d67482SBill Paul }
466195d67482SBill Paul 
466295d67482SBill Paul static void
46638cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
46648cb1383cSDoug Ambrisko {
46658cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
46668cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
46678cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
46688cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
46698cb1383cSDoug Ambrisko 		else {
4670899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4671888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
46723c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4673888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4674941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4675941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
46763fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
46779931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
46789931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
46798cb1383cSDoug Ambrisko 		}
46808cb1383cSDoug Ambrisko 	}
46818cb1383cSDoug Ambrisko }
46828cb1383cSDoug Ambrisko 
46838cb1383cSDoug Ambrisko static void
4684b74e67fbSGleb Smirnoff bge_tick(void *xsc)
46850f9bd73bSSam Leffler {
4686b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
468795d67482SBill Paul 	struct mii_data *mii = NULL;
468895d67482SBill Paul 
46890f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
469095d67482SBill Paul 
46915dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
46925dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
46935dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
46945dda8085SOleg Bulyzhin 		return;
46955dda8085SOleg Bulyzhin 
46967ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
46970434d1b8SBill Paul 		bge_stats_update_regs(sc);
46980434d1b8SBill Paul 	else
469995d67482SBill Paul 		bge_stats_update(sc);
470095d67482SBill Paul 
4701548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4702548c8f1aSPyun YongHyeon 
4703652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
470495d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
470582b67c01SOleg Bulyzhin 		/*
470682b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
470782b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
470882b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
470982b67c01SOleg Bulyzhin 		 */
471082b67c01SOleg Bulyzhin 		if (!sc->bge_link)
471195d67482SBill Paul 			mii_tick(mii);
47127b97099dSOleg Bulyzhin 	} else {
47137b97099dSOleg Bulyzhin 		/*
47147b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
47157b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
47167b97099dSOleg Bulyzhin 		 * and trigger interrupt.
47177b97099dSOleg Bulyzhin 		 */
47187b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
47193f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
47207b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
47217b97099dSOleg Bulyzhin #endif
47227b97099dSOleg Bulyzhin 		{
47237b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
47244f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
47254f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
47267b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
47274f0794ffSBjoern A. Zeeb 		else
47284f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
47297b97099dSOleg Bulyzhin 		}
4730dab5cd05SOleg Bulyzhin 	}
473195d67482SBill Paul 
47328cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4733b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
47348cb1383cSDoug Ambrisko 
4735dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
473695d67482SBill Paul }
473795d67482SBill Paul 
473895d67482SBill Paul static void
47393f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
47400434d1b8SBill Paul {
47413f74909aSGleb Smirnoff 	struct ifnet *ifp;
47422280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
47430434d1b8SBill Paul 
4744fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
47452280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
47460434d1b8SBill Paul 
47472280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
47482280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
47492280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
47502280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
47512280c16bSPyun YongHyeon 	stats->outXonSent +=
47522280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
47532280c16bSPyun YongHyeon 	stats->outXoffSent +=
47542280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
47552280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
47562280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
47572280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
47582280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
47592280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
47602280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
47612280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
47622280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
47632280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
47642280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
47652280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
47662280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
47672280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
47682280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
47692280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
47702280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
47712280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
47722280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
47737e6e2507SJung-uk Kim 
47742280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
47752280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
47762280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
47772280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
47782280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
47792280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
47802280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
47812280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
47822280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
47832280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
47842280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
47852280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
47862280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
47872280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
47882280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
47892280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
47902280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
47912280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
47922280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
47932280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
47942280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
47952280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
47962280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
47972280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
47982280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
47992280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48002280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
48012280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48022280c16bSPyun YongHyeon 
48032280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
48042280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48052280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
48062280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48072280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
48082280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48092280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
48102280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4811f78094a5SPyun YongHyeon 	/*
4812f78094a5SPyun YongHyeon 	 * XXX
4813f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4814f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4815f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4816f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4817f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4818f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4819f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4820f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4821f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4822f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4823f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4824f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4825f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4826f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4827f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4828f78094a5SPyun YongHyeon 	 * silicon bug.
4829f78094a5SPyun YongHyeon 	 */
4830f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4831f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4832f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
48332280c16bSPyun YongHyeon 		stats->InputDiscards +=
48342280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
48352280c16bSPyun YongHyeon 	stats->InputErrors +=
48362280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
48372280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
48382280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
48392280c16bSPyun YongHyeon 
48402280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
48412280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
48422280c16bSPyun YongHyeon 	    stats->InputErrors);
48432280c16bSPyun YongHyeon }
48442280c16bSPyun YongHyeon 
48452280c16bSPyun YongHyeon static void
48462280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
48472280c16bSPyun YongHyeon {
48482280c16bSPyun YongHyeon 
48492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
48552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
48562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
48572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
48582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
48592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
48602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
48612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48622280c16bSPyun YongHyeon 
48632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
48732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
48742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
48752280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48762280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48772280c16bSPyun YongHyeon 
48782280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48792280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48802280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48812280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
48822280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
48832280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
48842280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
48850434d1b8SBill Paul }
48860434d1b8SBill Paul 
48870434d1b8SBill Paul static void
48883f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
488995d67482SBill Paul {
489095d67482SBill Paul 	struct ifnet *ifp;
4891e907febfSPyun YongHyeon 	bus_size_t stats;
48927e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
489395d67482SBill Paul 
4894fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
489595d67482SBill Paul 
4896e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4897e907febfSPyun YongHyeon 
4898e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4899e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
490095d67482SBill Paul 
49018634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
49026b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
49036fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
49046fb34dd2SOleg Bulyzhin 
490537ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
490637ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_nobds);
490737ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
490837ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
490937ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_inerrs);
491037ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
49116fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
49126b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
49136fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
49146fb34dd2SOleg Bulyzhin 
49156fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
49166b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
49176fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
491895d67482SBill Paul 
4919e907febfSPyun YongHyeon #undef	READ_STAT
492095d67482SBill Paul }
492195d67482SBill Paul 
492295d67482SBill Paul /*
4923d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
4924d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
4925d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
4926d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
4927d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
4928d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
4929d375e524SGleb Smirnoff  */
4930d375e524SGleb Smirnoff static __inline int
4931d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
4932d375e524SGleb Smirnoff {
4933d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
4934d375e524SGleb Smirnoff 	struct mbuf *last;
4935d375e524SGleb Smirnoff 
4936d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
4937d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
4938d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
4939d375e524SGleb Smirnoff 		last = m;
4940d375e524SGleb Smirnoff 	} else {
4941d375e524SGleb Smirnoff 		/*
4942d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
4943d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
4944d375e524SGleb Smirnoff 		 */
4945d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
4946d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
4947d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
4948d375e524SGleb Smirnoff 			struct mbuf *n;
4949d375e524SGleb Smirnoff 
4950c6499eccSGleb Smirnoff 			MGET(n, M_NOWAIT, MT_DATA);
4951d375e524SGleb Smirnoff 			if (n == NULL)
4952d375e524SGleb Smirnoff 				return (ENOBUFS);
4953d375e524SGleb Smirnoff 			n->m_len = 0;
4954d375e524SGleb Smirnoff 			last->m_next = n;
4955d375e524SGleb Smirnoff 			last = n;
4956d375e524SGleb Smirnoff 		}
4957d375e524SGleb Smirnoff 	}
4958d375e524SGleb Smirnoff 
4959d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
4960d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
4961d375e524SGleb Smirnoff 	last->m_len += padlen;
4962d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
4963d375e524SGleb Smirnoff 
4964d375e524SGleb Smirnoff 	return (0);
4965d375e524SGleb Smirnoff }
4966d375e524SGleb Smirnoff 
4967ca3f1187SPyun YongHyeon static struct mbuf *
4968d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
4969d598b626SPyun YongHyeon {
4970d598b626SPyun YongHyeon 	struct mbuf *n;
4971d598b626SPyun YongHyeon 	int found;
4972d598b626SPyun YongHyeon 
4973d598b626SPyun YongHyeon 	/*
4974d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
4975d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
4976d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
4977d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
4978d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
4979d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
4980d598b626SPyun YongHyeon 	 */
4981d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
4982d598b626SPyun YongHyeon 		if (n->m_len < 8) {
4983d598b626SPyun YongHyeon 			found++;
4984d598b626SPyun YongHyeon 			if (found > 1)
4985d598b626SPyun YongHyeon 				break;
4986d598b626SPyun YongHyeon 			continue;
4987d598b626SPyun YongHyeon 		}
4988d598b626SPyun YongHyeon 		found = 0;
4989d598b626SPyun YongHyeon 	}
4990d598b626SPyun YongHyeon 
4991d598b626SPyun YongHyeon 	if (found > 1) {
4992c6499eccSGleb Smirnoff 		n = m_defrag(m, M_NOWAIT);
4993d598b626SPyun YongHyeon 		if (n == NULL)
4994d598b626SPyun YongHyeon 			m_freem(m);
4995d598b626SPyun YongHyeon 	} else
4996d598b626SPyun YongHyeon 		n = m;
4997d598b626SPyun YongHyeon 	return (n);
4998d598b626SPyun YongHyeon }
4999d598b626SPyun YongHyeon 
5000d598b626SPyun YongHyeon static struct mbuf *
50011108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
50021108273aSPyun YongHyeon     uint16_t *flags)
5003ca3f1187SPyun YongHyeon {
5004ca3f1187SPyun YongHyeon 	struct ip *ip;
5005ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
5006ca3f1187SPyun YongHyeon 	struct mbuf *n;
5007ca3f1187SPyun YongHyeon 	uint16_t hlen;
50085b355c4fSPyun YongHyeon 	uint32_t poff;
5009ca3f1187SPyun YongHyeon 
5010ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5011ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5012c6499eccSGleb Smirnoff 		n = m_dup(m, M_NOWAIT);
5013ca3f1187SPyun YongHyeon 		m_freem(m);
5014ca3f1187SPyun YongHyeon 		if (n == NULL)
5015ca3f1187SPyun YongHyeon 			return (NULL);
5016ca3f1187SPyun YongHyeon 		m = n;
5017ca3f1187SPyun YongHyeon 	}
50185b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5019ca3f1187SPyun YongHyeon 	if (m == NULL)
5020ca3f1187SPyun YongHyeon 		return (NULL);
50215b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
50225b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5023ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5024ca3f1187SPyun YongHyeon 	if (m == NULL)
5025ca3f1187SPyun YongHyeon 		return (NULL);
5026ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
50275b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5028ca3f1187SPyun YongHyeon 	if (m == NULL)
5029ca3f1187SPyun YongHyeon 		return (NULL);
5030ca3f1187SPyun YongHyeon 	/*
5031ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5032ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5033ca3f1187SPyun YongHyeon 	 */
5034ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
503596486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5036ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5037ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5038ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
503996486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5040ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5041ca3f1187SPyun YongHyeon 	/*
5042ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5043ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5044ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
50451108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5046ca3f1187SPyun YongHyeon 	 */
50471108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5048ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
50491108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
50501108273aSPyun YongHyeon 		/*
50511108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
50521108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
50531108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
50541108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
50551108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
50561108273aSPyun YongHyeon 		 * frames are supported.
50571108273aSPyun YongHyeon 		 */
50581108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
50591108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
50601108273aSPyun YongHyeon 	} else {
50611108273aSPyun YongHyeon 		/*
50621108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
50631108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
50641108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
50651108273aSPyun YongHyeon 		 * supported.
50661108273aSPyun YongHyeon 		 */
5067ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
50681108273aSPyun YongHyeon 	}
5069ca3f1187SPyun YongHyeon 	return (m);
5070ca3f1187SPyun YongHyeon }
5071ca3f1187SPyun YongHyeon 
5072d375e524SGleb Smirnoff /*
507395d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
507495d67482SBill Paul  * pointers to descriptors.
507595d67482SBill Paul  */
507695d67482SBill Paul static int
5077676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
507895d67482SBill Paul {
50797e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5080f41ac2beSBill Paul 	bus_dmamap_t		map;
5081676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5082676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
50837e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5084ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
50857e27542aSGleb Smirnoff 	int			nsegs, i, error;
508695d67482SBill Paul 
50876909dc43SGleb Smirnoff 	csum_flags = 0;
5088ca3f1187SPyun YongHyeon 	mss = 0;
5089ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5090d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5091d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5092d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5093d598b626SPyun YongHyeon 		if (*m_head == NULL)
5094d598b626SPyun YongHyeon 			return (ENOBUFS);
5095d598b626SPyun YongHyeon 		m = *m_head;
5096d598b626SPyun YongHyeon 	}
5097ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
50981108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5099ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5100ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5101ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5102ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
510335f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
51046909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
51056909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
51066909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
51076909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
51086909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
51096909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
51106909dc43SGleb Smirnoff 				m_freem(m);
51116909dc43SGleb Smirnoff 				*m_head = NULL;
51126909dc43SGleb Smirnoff 				return (error);
51136909dc43SGleb Smirnoff 			}
51146909dc43SGleb Smirnoff 		}
51156909dc43SGleb Smirnoff 	}
51166909dc43SGleb Smirnoff 
51171108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
51181108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
51191108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
51201108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
51211108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5122beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5123d94f2b85SPyun YongHyeon 			/*
5124d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5125d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5126d94f2b85SPyun YongHyeon 			 * DMA read operation.
5127d94f2b85SPyun YongHyeon 			 */
5128beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5129c6499eccSGleb Smirnoff 				m = m_defrag(m, M_NOWAIT);
5130d94f2b85SPyun YongHyeon 			else
5131c6499eccSGleb Smirnoff 				m = m_collapse(m, M_NOWAIT,
51321108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5133261f04d6SPyun YongHyeon 			if (m == NULL)
5134261f04d6SPyun YongHyeon 				m = *m_head;
5135d94f2b85SPyun YongHyeon 			*m_head = m;
5136d94f2b85SPyun YongHyeon 		}
51371108273aSPyun YongHyeon 	}
5138d94f2b85SPyun YongHyeon 
51397e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
51400ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5141676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
51427e27542aSGleb Smirnoff 	if (error == EFBIG) {
5143c6499eccSGleb Smirnoff 		m = m_collapse(m, M_NOWAIT, BGE_NSEG_NEW);
5144676ad2c9SGleb Smirnoff 		if (m == NULL) {
5145676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5146676ad2c9SGleb Smirnoff 			*m_head = NULL;
51477e27542aSGleb Smirnoff 			return (ENOBUFS);
51487e27542aSGleb Smirnoff 		}
5149676ad2c9SGleb Smirnoff 		*m_head = m;
51500ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
51510ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5152676ad2c9SGleb Smirnoff 		if (error) {
5153676ad2c9SGleb Smirnoff 			m_freem(m);
5154676ad2c9SGleb Smirnoff 			*m_head = NULL;
51557e27542aSGleb Smirnoff 			return (error);
51567e27542aSGleb Smirnoff 		}
5157676ad2c9SGleb Smirnoff 	} else if (error != 0)
5158676ad2c9SGleb Smirnoff 		return (error);
51597e27542aSGleb Smirnoff 
5160167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5161167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
51620ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
516395d67482SBill Paul 		return (ENOBUFS);
51647e27542aSGleb Smirnoff 	}
51657e27542aSGleb Smirnoff 
51660ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5167e65bed95SPyun YongHyeon 
5168ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5169ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5170ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5171ca3f1187SPyun YongHyeon 	}
51727e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
51737e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
51747e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
51757e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
51767e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
51777e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
5178ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
5179ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
51807e27542aSGleb Smirnoff 		if (i == nsegs - 1)
51817e27542aSGleb Smirnoff 			break;
51827e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
51837e27542aSGleb Smirnoff 	}
51847e27542aSGleb Smirnoff 
51857e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
51867e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5187676ad2c9SGleb Smirnoff 
5188f41ac2beSBill Paul 	/*
5189f41ac2beSBill Paul 	 * Insure that the map for this transmission
5190f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5191f41ac2beSBill Paul 	 * in this chain.
5192f41ac2beSBill Paul 	 */
51937e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
51947e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5195676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
51967e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
519795d67482SBill Paul 
51987e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
51997e27542aSGleb Smirnoff 	*txidx = idx;
520095d67482SBill Paul 
520195d67482SBill Paul 	return (0);
520295d67482SBill Paul }
520395d67482SBill Paul 
520495d67482SBill Paul /*
520595d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
520695d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
520795d67482SBill Paul  */
520895d67482SBill Paul static void
52093f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
521095d67482SBill Paul {
521195d67482SBill Paul 	struct bge_softc *sc;
5212167fdb62SPyun YongHyeon 	struct mbuf *m_head;
521314bbd30fSGleb Smirnoff 	uint32_t prodidx;
5214167fdb62SPyun YongHyeon 	int count;
521595d67482SBill Paul 
521695d67482SBill Paul 	sc = ifp->if_softc;
5217167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
521895d67482SBill Paul 
5219167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5220167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5221167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
522295d67482SBill Paul 		return;
522395d67482SBill Paul 
522414bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
522595d67482SBill Paul 
5226167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
5227167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5228167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5229167fdb62SPyun YongHyeon 			break;
5230167fdb62SPyun YongHyeon 		}
52314d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
523295d67482SBill Paul 		if (m_head == NULL)
523395d67482SBill Paul 			break;
523495d67482SBill Paul 
523595d67482SBill Paul 		/*
523695d67482SBill Paul 		 * Pack the data into the transmit ring. If we
523795d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
523895d67482SBill Paul 		 * for the NIC to drain the ring.
523995d67482SBill Paul 		 */
5240676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5241676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5242676ad2c9SGleb Smirnoff 				break;
52434d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
524413f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
524595d67482SBill Paul 			break;
524695d67482SBill Paul 		}
5247303a718cSDag-Erling Smørgrav 		++count;
524895d67482SBill Paul 
524995d67482SBill Paul 		/*
525095d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
525195d67482SBill Paul 		 * to him.
525295d67482SBill Paul 		 */
52534e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
525445ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
52554e35d186SJung-uk Kim #else
52564e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
52574e35d186SJung-uk Kim #endif
525895d67482SBill Paul 	}
525995d67482SBill Paul 
5260167fdb62SPyun YongHyeon 	if (count > 0) {
5261aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
52625c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
52633f74909aSGleb Smirnoff 		/* Transmit. */
526438cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
52653927098fSPaul Saab 		/* 5700 b2 errata */
5266e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
526738cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
526895d67482SBill Paul 
526914bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
527014bbd30fSGleb Smirnoff 
527195d67482SBill Paul 		/*
527295d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
527395d67482SBill Paul 		 */
5274b74e67fbSGleb Smirnoff 		sc->bge_timer = 5;
527595d67482SBill Paul 	}
5276167fdb62SPyun YongHyeon }
527795d67482SBill Paul 
52780f9bd73bSSam Leffler /*
52790f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
52800f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
52810f9bd73bSSam Leffler  */
528295d67482SBill Paul static void
52833f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
528495d67482SBill Paul {
52850f9bd73bSSam Leffler 	struct bge_softc *sc;
52860f9bd73bSSam Leffler 
52870f9bd73bSSam Leffler 	sc = ifp->if_softc;
52880f9bd73bSSam Leffler 	BGE_LOCK(sc);
52890f9bd73bSSam Leffler 	bge_start_locked(ifp);
52900f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
52910f9bd73bSSam Leffler }
52920f9bd73bSSam Leffler 
52930f9bd73bSSam Leffler static void
52943f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
52950f9bd73bSSam Leffler {
529695d67482SBill Paul 	struct ifnet *ifp;
52973f74909aSGleb Smirnoff 	uint16_t *m;
5298f6a65488SPyun YongHyeon 	uint32_t mode;
529995d67482SBill Paul 
53000f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
530195d67482SBill Paul 
5302fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
530395d67482SBill Paul 
530413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
530595d67482SBill Paul 		return;
530695d67482SBill Paul 
530795d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
530895d67482SBill Paul 	bge_stop(sc);
53098cb1383cSDoug Ambrisko 
53108cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
53118cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
531295d67482SBill Paul 	bge_reset(sc);
53138cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
53148cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
53158cb1383cSDoug Ambrisko 
531695d67482SBill Paul 	bge_chipinit(sc);
531795d67482SBill Paul 
531895d67482SBill Paul 	/*
531995d67482SBill Paul 	 * Init the various state machines, ring
532095d67482SBill Paul 	 * control blocks and firmware.
532195d67482SBill Paul 	 */
532295d67482SBill Paul 	if (bge_blockinit(sc)) {
5323fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
532495d67482SBill Paul 		return;
532595d67482SBill Paul 	}
532695d67482SBill Paul 
5327fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
532895d67482SBill Paul 
532995d67482SBill Paul 	/* Specify MTU. */
533095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
5331cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5332cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
533395d67482SBill Paul 
533495d67482SBill Paul 	/* Load our MAC address. */
53353f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
533695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
533795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
533895d67482SBill Paul 
53393e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
53403e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
534195d67482SBill Paul 
534295d67482SBill Paul 	/* Program multicast filter. */
534395d67482SBill Paul 	bge_setmulti(sc);
534495d67482SBill Paul 
5345cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5346cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5347cb2eacc7SYaroslav Tykhiy 
534835f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
534935f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
535035f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
535135f945cdSPyun YongHyeon 	else
535235f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
535335f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
535435f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
535535f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
535635f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
535735f945cdSPyun YongHyeon 	}
535835f945cdSPyun YongHyeon 
535995d67482SBill Paul 	/* Init RX ring. */
53603ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
53613ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
53623ee5d7daSPyun YongHyeon 		bge_stop(sc);
53633ee5d7daSPyun YongHyeon 		return;
53643ee5d7daSPyun YongHyeon 	}
536595d67482SBill Paul 
53660434d1b8SBill Paul 	/*
53670434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
53680434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
53690434d1b8SBill Paul 	 * entry of the ring.
53700434d1b8SBill Paul 	 */
53710434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
53723f74909aSGleb Smirnoff 		uint32_t		v, i;
53730434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
53740434d1b8SBill Paul 			DELAY(20);
53750434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
53760434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
53770434d1b8SBill Paul 				break;
53780434d1b8SBill Paul 		}
53790434d1b8SBill Paul 		if (i == 10)
5380fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5381fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
53820434d1b8SBill Paul 	}
53830434d1b8SBill Paul 
538495d67482SBill Paul 	/* Init jumbo RX ring. */
5385f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5386f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
5387c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
53883ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5389333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5390b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
53913ee5d7daSPyun YongHyeon 			bge_stop(sc);
53923ee5d7daSPyun YongHyeon 			return;
53933ee5d7daSPyun YongHyeon 		}
53943ee5d7daSPyun YongHyeon 	}
539595d67482SBill Paul 
53963f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
539795d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
539895d67482SBill Paul 
53997e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
54007e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
54017e6e2507SJung-uk Kim 
540295d67482SBill Paul 	/* Init TX ring. */
540395d67482SBill Paul 	bge_init_tx_ring(sc);
540495d67482SBill Paul 
5405f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5406f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5407f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5408f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
540950515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
541050515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
541150515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
541250515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
541350515680SPyun YongHyeon 	}
54143f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5415f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5416a6e66cd2SPyun YongHyeon 	DELAY(100);
541795d67482SBill Paul 
54183f74909aSGleb Smirnoff 	/* Turn on receiver. */
5419548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5420548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5421548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
5422548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5423a6e66cd2SPyun YongHyeon 	DELAY(10);
542495d67482SBill Paul 
5425dedcdf57SPyun YongHyeon 	/*
5426dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5427dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5428dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5429dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5430dedcdf57SPyun YongHyeon 	 */
54313fc5fbfbSPyun YongHyeon 	if (BGE_IS_57765_PLUS(sc))
5432b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5433b4a256acSPyun YongHyeon 	else
5434dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5435dedcdf57SPyun YongHyeon 
54362280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
54372280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
54382280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
54392280c16bSPyun YongHyeon 
544095d67482SBill Paul 	/* Tell firmware we're alive. */
544195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
544295d67482SBill Paul 
544375719184SGleb Smirnoff #ifdef DEVICE_POLLING
544475719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
544575719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
544675719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
544775719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
544838cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
544975719184SGleb Smirnoff 	} else
545075719184SGleb Smirnoff #endif
545175719184SGleb Smirnoff 
545295d67482SBill Paul 	/* Enable host interrupts. */
545375719184SGleb Smirnoff 	{
545495d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
545595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
545638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
545775719184SGleb Smirnoff 	}
545895d67482SBill Paul 
545913f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
546013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
546195d67482SBill Paul 
5462e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5463e4146b95SPyun YongHyeon 
54640f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
54650f9bd73bSSam Leffler }
54660f9bd73bSSam Leffler 
54670f9bd73bSSam Leffler static void
54683f74909aSGleb Smirnoff bge_init(void *xsc)
54690f9bd73bSSam Leffler {
54700f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
54710f9bd73bSSam Leffler 
54720f9bd73bSSam Leffler 	BGE_LOCK(sc);
54730f9bd73bSSam Leffler 	bge_init_locked(sc);
54740f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
547595d67482SBill Paul }
547695d67482SBill Paul 
547795d67482SBill Paul /*
547895d67482SBill Paul  * Set media options.
547995d67482SBill Paul  */
548095d67482SBill Paul static int
54813f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
548295d67482SBill Paul {
548367d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
548467d5e043SOleg Bulyzhin 	int res;
548567d5e043SOleg Bulyzhin 
548667d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
548767d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
548867d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
548967d5e043SOleg Bulyzhin 
549067d5e043SOleg Bulyzhin 	return (res);
549167d5e043SOleg Bulyzhin }
549267d5e043SOleg Bulyzhin 
549367d5e043SOleg Bulyzhin static int
549467d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
549567d5e043SOleg Bulyzhin {
549667d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
549795d67482SBill Paul 	struct mii_data *mii;
54984f09c4c7SMarius Strobl 	struct mii_softc *miisc;
549995d67482SBill Paul 	struct ifmedia *ifm;
550095d67482SBill Paul 
550167d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
550267d5e043SOleg Bulyzhin 
550395d67482SBill Paul 	ifm = &sc->bge_ifmedia;
550495d67482SBill Paul 
550595d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5506652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
550795d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
550895d67482SBill Paul 			return (EINVAL);
550995d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
551095d67482SBill Paul 		case IFM_AUTO:
5511ff50922bSDoug White 			/*
5512ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5513ff50922bSDoug White 			 * mechanism for programming the autoneg
5514ff50922bSDoug White 			 * advertisement registers in TBI mode.
5515ff50922bSDoug White 			 */
55160f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5517ff50922bSDoug White 				uint32_t sgdig;
55180f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
55190f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5520ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5521ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5522ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5523ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5524ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5525ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5526ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5527ff50922bSDoug White 					DELAY(5);
5528ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5529ff50922bSDoug White 				}
55300f89fde2SJung-uk Kim 			}
553195d67482SBill Paul 			break;
553295d67482SBill Paul 		case IFM_1000_SX:
553395d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
553495d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
553595d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
553695d67482SBill Paul 			} else {
553795d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
553895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
553995d67482SBill Paul 			}
55409b80ffe7SPyun YongHyeon 			DELAY(40);
554195d67482SBill Paul 			break;
554295d67482SBill Paul 		default:
554395d67482SBill Paul 			return (EINVAL);
554495d67482SBill Paul 		}
554595d67482SBill Paul 		return (0);
554695d67482SBill Paul 	}
554795d67482SBill Paul 
55481493e883SOleg Bulyzhin 	sc->bge_link_evt++;
554995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
55504f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
55513fcb7a53SMarius Strobl 		PHY_RESET(miisc);
555295d67482SBill Paul 	mii_mediachg(mii);
555395d67482SBill Paul 
5554902827f6SBjoern A. Zeeb 	/*
5555902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5556902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5557902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5558902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5559902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5560902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5561902827f6SBjoern A. Zeeb 	 * get an RX intr.
5562902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5563902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5564902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5565902827f6SBjoern A. Zeeb 	 */
55664f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
55674f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5568902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
55694f0794ffSBjoern A. Zeeb 	else
557063ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5571902827f6SBjoern A. Zeeb 
557295d67482SBill Paul 	return (0);
557395d67482SBill Paul }
557495d67482SBill Paul 
557595d67482SBill Paul /*
557695d67482SBill Paul  * Report current media status.
557795d67482SBill Paul  */
557895d67482SBill Paul static void
55793f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
558095d67482SBill Paul {
558167d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
558295d67482SBill Paul 	struct mii_data *mii;
558395d67482SBill Paul 
558467d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
558595d67482SBill Paul 
5586b9d2edd7SPyun YongHyeon 	if ((ifp->if_flags & IFF_UP) == 0) {
5587b9d2edd7SPyun YongHyeon 		BGE_UNLOCK(sc);
5588b9d2edd7SPyun YongHyeon 		return;
5589b9d2edd7SPyun YongHyeon 	}
5590652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
559195d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
559295d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
559395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
559495d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
559595d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
55964c0da0ffSGleb Smirnoff 		else {
55974c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
559867d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
55994c0da0ffSGleb Smirnoff 			return;
56004c0da0ffSGleb Smirnoff 		}
560195d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
560295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
560395d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
560495d67482SBill Paul 		else
560595d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
560667d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
560795d67482SBill Paul 		return;
560895d67482SBill Paul 	}
560995d67482SBill Paul 
561095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
561195d67482SBill Paul 	mii_pollstat(mii);
561295d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
561395d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
561467d5e043SOleg Bulyzhin 
561567d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
561695d67482SBill Paul }
561795d67482SBill Paul 
561895d67482SBill Paul static int
56193f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
562095d67482SBill Paul {
562195d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
562295d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
562395d67482SBill Paul 	struct mii_data *mii;
5624f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
562595d67482SBill Paul 
562695d67482SBill Paul 	switch (command) {
562795d67482SBill Paul 	case SIOCSIFMTU:
5628f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5629f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
56304c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5631f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
563295d67482SBill Paul 				error = EINVAL;
5633f5459d4cSPyun YongHyeon 				break;
5634f5459d4cSPyun YongHyeon 			}
5635f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5636f5459d4cSPyun YongHyeon 			error = EINVAL;
5637f5459d4cSPyun YongHyeon 			break;
5638f5459d4cSPyun YongHyeon 		}
5639f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5640f5459d4cSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
564195d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
56423a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
564313f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
56443a429c8fSPyun YongHyeon 				bge_init_locked(sc);
564595d67482SBill Paul 			}
56463a429c8fSPyun YongHyeon 		}
56473a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
564895d67482SBill Paul 		break;
564995d67482SBill Paul 	case SIOCSIFFLAGS:
56500f9bd73bSSam Leffler 		BGE_LOCK(sc);
565195d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
565295d67482SBill Paul 			/*
565395d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
565495d67482SBill Paul 			 * then just use the 'set promisc mode' command
565595d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
565695d67482SBill Paul 			 * a full re-init means reloading the firmware and
565795d67482SBill Paul 			 * waiting for it to start up, which may take a
5658d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
565995d67482SBill Paul 			 */
5660f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5661f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
56623e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
56633e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5664f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5665d183af7fSRuslan Ermilov 					bge_setmulti(sc);
566695d67482SBill Paul 			} else
56670f9bd73bSSam Leffler 				bge_init_locked(sc);
566895d67482SBill Paul 		} else {
566913f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
567095d67482SBill Paul 				bge_stop(sc);
567195d67482SBill Paul 			}
567295d67482SBill Paul 		}
567395d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
56740f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
567595d67482SBill Paul 		error = 0;
567695d67482SBill Paul 		break;
567795d67482SBill Paul 	case SIOCADDMULTI:
567895d67482SBill Paul 	case SIOCDELMULTI:
567913f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
56800f9bd73bSSam Leffler 			BGE_LOCK(sc);
568195d67482SBill Paul 			bge_setmulti(sc);
56820f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
568395d67482SBill Paul 			error = 0;
568495d67482SBill Paul 		}
568595d67482SBill Paul 		break;
568695d67482SBill Paul 	case SIOCSIFMEDIA:
568795d67482SBill Paul 	case SIOCGIFMEDIA:
5688652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
568995d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
569095d67482SBill Paul 			    &sc->bge_ifmedia, command);
569195d67482SBill Paul 		} else {
569295d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
569395d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
569495d67482SBill Paul 			    &mii->mii_media, command);
569595d67482SBill Paul 		}
569695d67482SBill Paul 		break;
569795d67482SBill Paul 	case SIOCSIFCAP:
569895d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
569975719184SGleb Smirnoff #ifdef DEVICE_POLLING
570075719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
570175719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
570275719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
570375719184SGleb Smirnoff 				if (error)
570475719184SGleb Smirnoff 					return (error);
570575719184SGleb Smirnoff 				BGE_LOCK(sc);
570675719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
570775719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
570838cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
570975719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
571075719184SGleb Smirnoff 				BGE_UNLOCK(sc);
571175719184SGleb Smirnoff 			} else {
571275719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
571375719184SGleb Smirnoff 				/* Enable interrupt even in error case */
571475719184SGleb Smirnoff 				BGE_LOCK(sc);
571575719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
571675719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
571738cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
571875719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
571975719184SGleb Smirnoff 				BGE_UNLOCK(sc);
572075719184SGleb Smirnoff 			}
572175719184SGleb Smirnoff 		}
572275719184SGleb Smirnoff #endif
5723d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5724d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
5725d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
5726d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
572735f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
572895d67482SBill Paul 			else
572935f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
573095d67482SBill Paul 		}
5731cb2eacc7SYaroslav Tykhiy 
5732d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5733d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
5734d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
5735d8b57f98SPyun YongHyeon 
5736ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5737ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
5738ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
5739ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
5740ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
5741ca3f1187SPyun YongHyeon 			else
5742ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
5743ca3f1187SPyun YongHyeon 		}
5744ca3f1187SPyun YongHyeon 
5745cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5746cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
5747cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5748cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5749cb2eacc7SYaroslav Tykhiy 		}
5750cb2eacc7SYaroslav Tykhiy 
575104bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
575204bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
575304bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
575404bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
575504bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
5756cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
575704bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
575804bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
5759cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5760cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5761cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
576204bde852SPyun YongHyeon 		}
5763cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5764cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
5765cb2eacc7SYaroslav Tykhiy #endif
576695d67482SBill Paul 		break;
576795d67482SBill Paul 	default:
5768673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
576995d67482SBill Paul 		break;
577095d67482SBill Paul 	}
577195d67482SBill Paul 
577295d67482SBill Paul 	return (error);
577395d67482SBill Paul }
577495d67482SBill Paul 
577595d67482SBill Paul static void
5776b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
577795d67482SBill Paul {
5778b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
577995d67482SBill Paul 
5780b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5781b74e67fbSGleb Smirnoff 
5782b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5783b74e67fbSGleb Smirnoff 		return;
5784b74e67fbSGleb Smirnoff 
5785b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
578695d67482SBill Paul 
5787fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
578895d67482SBill Paul 
578913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5790426742bfSGleb Smirnoff 	bge_init_locked(sc);
579195d67482SBill Paul 
579295d67482SBill Paul 	ifp->if_oerrors++;
579395d67482SBill Paul }
579495d67482SBill Paul 
57955a147ba6SPyun YongHyeon static void
57965a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
57975a147ba6SPyun YongHyeon {
57985a147ba6SPyun YongHyeon 	int i;
57995a147ba6SPyun YongHyeon 
58005a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
58015a147ba6SPyun YongHyeon 
58025a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
58035a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
58045a147ba6SPyun YongHyeon 			return;
58055a147ba6SPyun YongHyeon 		DELAY(100);
58065a147ba6SPyun YongHyeon         }
58075a147ba6SPyun YongHyeon }
58085a147ba6SPyun YongHyeon 
580995d67482SBill Paul /*
581095d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
581195d67482SBill Paul  * RX and TX lists.
581295d67482SBill Paul  */
581395d67482SBill Paul static void
58143f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
581595d67482SBill Paul {
581695d67482SBill Paul 	struct ifnet *ifp;
581795d67482SBill Paul 
58180f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
58190f9bd73bSSam Leffler 
5820fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
582195d67482SBill Paul 
58220f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
582395d67482SBill Paul 
582444b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
582544b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
582644b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
582744b63691SBjoern A. Zeeb 
582844b63691SBjoern A. Zeeb 	/*
582944b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
583044b63691SBjoern A. Zeeb 	 */
583144b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
5832548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
583344b63691SBjoern A. Zeeb 
583495d67482SBill Paul 	/*
58353f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
583695d67482SBill Paul 	 */
58375a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
58385a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
58395a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
58405a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
58415a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
58425a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
58435a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
58445a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
584595d67482SBill Paul 
584695d67482SBill Paul 	/*
58473f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
584895d67482SBill Paul 	 */
58495a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
58505a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
58515a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
58525a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
58535a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
58545a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
58555a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
58565a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
585795d67482SBill Paul 
585895d67482SBill Paul 	/*
585995d67482SBill Paul 	 * Shut down all of the memory managers and related
586095d67482SBill Paul 	 * state machines.
586195d67482SBill Paul 	 */
58625a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
58635a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
58645a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
58655a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
58665a147ba6SPyun YongHyeon 
58670c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
586895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
58697ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
587095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
587195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
58720434d1b8SBill Paul 	}
58732280c16bSPyun YongHyeon 	/* Update MAC statistics. */
58742280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
58752280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
587695d67482SBill Paul 
58778cb1383cSDoug Ambrisko 	bge_reset(sc);
5878548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
5879548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
58808cb1383cSDoug Ambrisko 
58818cb1383cSDoug Ambrisko 	/*
58828cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
58838cb1383cSDoug Ambrisko 	 */
58848cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
58858cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
58868cb1383cSDoug Ambrisko 	else
588795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
588895d67482SBill Paul 
588995d67482SBill Paul 	/* Free the RX lists. */
589095d67482SBill Paul 	bge_free_rx_ring_std(sc);
589195d67482SBill Paul 
589295d67482SBill Paul 	/* Free jumbo RX list. */
58934c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
589495d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
589595d67482SBill Paul 
589695d67482SBill Paul 	/* Free TX buffers. */
589795d67482SBill Paul 	bge_free_tx_ring(sc);
589895d67482SBill Paul 
589995d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
590095d67482SBill Paul 
59015dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
59021493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
59031493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
59041493e883SOleg Bulyzhin 	sc->bge_link = 0;
590595d67482SBill Paul 
59061493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
590795d67482SBill Paul }
590895d67482SBill Paul 
590995d67482SBill Paul /*
591095d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
591195d67482SBill Paul  * get confused by errant DMAs when rebooting.
591295d67482SBill Paul  */
5913b6c974e8SWarner Losh static int
59143f74909aSGleb Smirnoff bge_shutdown(device_t dev)
591595d67482SBill Paul {
591695d67482SBill Paul 	struct bge_softc *sc;
591795d67482SBill Paul 
591895d67482SBill Paul 	sc = device_get_softc(dev);
59190f9bd73bSSam Leffler 	BGE_LOCK(sc);
592095d67482SBill Paul 	bge_stop(sc);
59210f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
5922b6c974e8SWarner Losh 
5923b6c974e8SWarner Losh 	return (0);
592495d67482SBill Paul }
592514afefa3SPawel Jakub Dawidek 
592614afefa3SPawel Jakub Dawidek static int
592714afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
592814afefa3SPawel Jakub Dawidek {
592914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
593014afefa3SPawel Jakub Dawidek 
593114afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
593214afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
593314afefa3SPawel Jakub Dawidek 	bge_stop(sc);
593414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
593514afefa3SPawel Jakub Dawidek 
593614afefa3SPawel Jakub Dawidek 	return (0);
593714afefa3SPawel Jakub Dawidek }
593814afefa3SPawel Jakub Dawidek 
593914afefa3SPawel Jakub Dawidek static int
594014afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
594114afefa3SPawel Jakub Dawidek {
594214afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
594314afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
594414afefa3SPawel Jakub Dawidek 
594514afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
594614afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
594714afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
594814afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
594914afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
595014afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
595114afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
595214afefa3SPawel Jakub Dawidek 	}
595314afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
595414afefa3SPawel Jakub Dawidek 
595514afefa3SPawel Jakub Dawidek 	return (0);
595614afefa3SPawel Jakub Dawidek }
5957dab5cd05SOleg Bulyzhin 
5958dab5cd05SOleg Bulyzhin static void
59593f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
5960dab5cd05SOleg Bulyzhin {
59611f313773SOleg Bulyzhin 	struct mii_data *mii;
59621f313773SOleg Bulyzhin 	uint32_t link, status;
5963dab5cd05SOleg Bulyzhin 
5964dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
59651f313773SOleg Bulyzhin 
59663f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
59677b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
59687b97099dSOleg Bulyzhin 
5969dab5cd05SOleg Bulyzhin 	/*
5970dab5cd05SOleg Bulyzhin 	 * Process link state changes.
5971dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
5972dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
5973dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
5974dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
5975dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
5976dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
5977dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
5978dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
59791f313773SOleg Bulyzhin 	 *
59801f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
59814c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
5982dab5cd05SOleg Bulyzhin 	 */
5983dab5cd05SOleg Bulyzhin 
59841f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
59854c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
5986dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
5987dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
59881f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
59895dda8085SOleg Bulyzhin 			mii_pollstat(mii);
59901f313773SOleg Bulyzhin 			if (!sc->bge_link &&
59911f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
59921f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
59931f313773SOleg Bulyzhin 				sc->bge_link++;
59941f313773SOleg Bulyzhin 				if (bootverbose)
59951f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
59961f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
59971f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
59981f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
59991f313773SOleg Bulyzhin 				sc->bge_link = 0;
60001f313773SOleg Bulyzhin 				if (bootverbose)
60011f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
60021f313773SOleg Bulyzhin 			}
60031f313773SOleg Bulyzhin 
60043f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6005dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6006dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6007daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6008daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6009daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6010daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6011dab5cd05SOleg Bulyzhin 		}
6012dab5cd05SOleg Bulyzhin 		return;
6013dab5cd05SOleg Bulyzhin 	}
6014dab5cd05SOleg Bulyzhin 
6015652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
60161f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
60177b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
60187b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
60191f313773SOleg Bulyzhin 				sc->bge_link++;
60209b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
60211f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
60221f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
60239b80ffe7SPyun YongHyeon 					DELAY(40);
60249b80ffe7SPyun YongHyeon 				}
60250c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
60261f313773SOleg Bulyzhin 				if (bootverbose)
60271f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
60283f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
60293f74909aSGleb Smirnoff 				    LINK_STATE_UP);
60307b97099dSOleg Bulyzhin 			}
60311f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6032dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
60331f313773SOleg Bulyzhin 			if (bootverbose)
60341f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
60357b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
60361f313773SOleg Bulyzhin 		}
60376ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
60381f313773SOleg Bulyzhin 		/*
60390c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
60400c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
60410c8aa4eaSJung-uk Kim 		 * PHY link status directly.
60421f313773SOleg Bulyzhin 		 */
60431f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
60441f313773SOleg Bulyzhin 
60451f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
60461f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
60471f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
60485dda8085SOleg Bulyzhin 			mii_pollstat(mii);
60491f313773SOleg Bulyzhin 			if (!sc->bge_link &&
60501f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
60511f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
60521f313773SOleg Bulyzhin 				sc->bge_link++;
60531f313773SOleg Bulyzhin 				if (bootverbose)
60541f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
60551f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
60561f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
60571f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
60581f313773SOleg Bulyzhin 				sc->bge_link = 0;
60591f313773SOleg Bulyzhin 				if (bootverbose)
60601f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
60611f313773SOleg Bulyzhin 			}
60621f313773SOleg Bulyzhin 		}
60630c8aa4eaSJung-uk Kim 	} else {
60640c8aa4eaSJung-uk Kim 		/*
60656ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
60666ede2cfaSPyun YongHyeon 		 * link status.
60670c8aa4eaSJung-uk Kim 		 */
60686ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
60696ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
60706ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6071dab5cd05SOleg Bulyzhin 	}
6072dab5cd05SOleg Bulyzhin 
60732246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6074dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6075dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6076dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6077dab5cd05SOleg Bulyzhin }
60786f8718a3SScott Long 
60796f8718a3SScott Long static void
60806f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
60816f8718a3SScott Long {
60826f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
60832280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
60847e32f79aSPyun YongHyeon 	char tn[32];
60857e32f79aSPyun YongHyeon 	int unit;
60866f8718a3SScott Long 
60876f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
60886f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
60896f8718a3SScott Long 
60906f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
60916f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
60926f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
60936f8718a3SScott Long 	    "Debug Information");
60946f8718a3SScott Long 
60956f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
60966f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
6097548c8f1aSPyun YongHyeon 	    "MAC Register Read");
6098548c8f1aSPyun YongHyeon 
6099548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
6100548c8f1aSPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_ape_read, "I",
6101548c8f1aSPyun YongHyeon 	    "APE Register Read");
61026f8718a3SScott Long 
61036f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
61046f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
61056f8718a3SScott Long 	    "Memory Read");
61066f8718a3SScott Long 
61076f8718a3SScott Long #endif
6108763757b2SScott Long 
61097e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
6110beaa2ae1SPyun YongHyeon 	/*
6111beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6112beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6113beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6114beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6115beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6116beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6117beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6118beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6119beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6120beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6121beaa2ae1SPyun YongHyeon 	 */
61227e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
61237e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
61247e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
6125beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6126beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
6127beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6128beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6129beaa2ae1SPyun YongHyeon 
61302ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
61312ae7f64bSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.msi", unit);
61322ae7f64bSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_msi);
61332ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
61342ae7f64bSPyun YongHyeon 	    CTLFLAG_RD, &sc->bge_msi, 0, "Enable MSI");
61355c952e8dSPyun YongHyeon 
613635f945cdSPyun YongHyeon 	/*
613735f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
613835f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
613935f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
614035f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
614135f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
614235f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
614335f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
614435f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
614535f945cdSPyun YongHyeon 	 */
614635f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
614735f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
614835f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
614935f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
615035f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
615135f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
615235f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
615335f945cdSPyun YongHyeon 
6154d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
61552280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
61562280c16bSPyun YongHyeon 	else
61572280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
61582280c16bSPyun YongHyeon }
6159d949071dSJung-uk Kim 
61602280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
61612280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
61622280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
61632280c16bSPyun YongHyeon 	    desc)
61642280c16bSPyun YongHyeon 
61652280c16bSPyun YongHyeon static void
61662280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
61672280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
61682280c16bSPyun YongHyeon {
61692280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
61702280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
61712280c16bSPyun YongHyeon 
61722280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6173763757b2SScott Long 	    NULL, "BGE Statistics");
6174763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6175763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6176763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6177763757b2SScott Long 	    "FramesDroppedDueToFilters");
6178763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6179763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6180763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6181763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6182763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6183763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
618406e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
618506e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
618606e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
618706e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6188763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6189763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6190763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6191763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6192763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6193763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6194763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6195763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6196763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6197763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6198763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6199763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6200763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6201763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6202763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6203763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6204763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6205763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6206763757b2SScott Long 
6207763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
6208763757b2SScott Long 	    NULL, "BGE RX Statistics");
6209763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6210763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
62111cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6212763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6213763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6214763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
62151cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6216763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6217763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6218763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6219763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6220763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6221763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6222763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6223763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6224763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6225763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6226763757b2SScott Long 	    "xoffPauseFramesReceived");
6227763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6228763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6229763757b2SScott Long 	    "ControlFramesReceived");
6230763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6231763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6232763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6233763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6234763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6235763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6236763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6237763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6238763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
623906e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6240763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
624106e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6242763757b2SScott Long 
6243763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
6244763757b2SScott Long 	    NULL, "BGE TX Statistics");
6245763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6246763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
62471cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6248763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6249763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6250763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6251763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6252763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6253763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6254763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6255763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6256763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6257763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6258763757b2SScott Long 	    "InternalMacTransmitErrors");
6259763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6260763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6261763757b2SScott Long 	    "SingleCollisionFrames");
6262763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6263763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6264763757b2SScott Long 	    "MultipleCollisionFrames");
6265763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6266763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6267763757b2SScott Long 	    "DeferredTransmissions");
6268763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6269763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6270763757b2SScott Long 	    "ExcessiveCollisions");
6271763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
627206e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
627306e83c7eSScott Long 	    "LateCollisions");
6274763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
62751cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6276763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6277763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6278763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6279763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6280763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6281763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6282763757b2SScott Long 	    "CarrierSenseErrors");
6283763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6284763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6285763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6286763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6287763757b2SScott Long }
6288763757b2SScott Long 
62892280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
62902280c16bSPyun YongHyeon 
62912280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
62926dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
62932280c16bSPyun YongHyeon 
62942280c16bSPyun YongHyeon static void
62952280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
62962280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
62972280c16bSPyun YongHyeon {
62982280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
62992280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
63002280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
63012280c16bSPyun YongHyeon 
63022280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
63032280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
63042280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
63052280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
63062280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
63072280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
63082280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
63092280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
63102280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
63112280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
63122280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
63132280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
63142280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
63152280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
63162280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
63172280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
63182280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
63192280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
63202280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
63212280c16bSPyun YongHyeon 
63222280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
63232280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
63242280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
63252280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
63262280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
63272280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
63282280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
63291cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
63302280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
63312280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
63322280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
63332280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
63342280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
63352280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
63362280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
63372280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
63382280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
63392280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
63402280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
63412280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
63422280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
63432280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
63442280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
63452280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
63462280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
63472280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
63482280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
63492280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
63502280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
63512280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
63522280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
63532280c16bSPyun YongHyeon 
63542280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
63552280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
63562280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
63571cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
63582280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
63592280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
63602280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
63612280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
63622280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
63632280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
63642280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
63652280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
63662280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
63672280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
63682280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
63692280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
63702280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
63712280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
63722280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
63732280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
63742280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
63752280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
63762280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
63772280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
63782280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
63791cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
63802280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
63811cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
63822280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
63831cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
63842280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
63852280c16bSPyun YongHyeon }
63862280c16bSPyun YongHyeon 
63872280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
63882280c16bSPyun YongHyeon 
6389763757b2SScott Long static int
6390763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6391763757b2SScott Long {
6392763757b2SScott Long 	struct bge_softc *sc;
639306e83c7eSScott Long 	uint32_t result;
6394d949071dSJung-uk Kim 	int offset;
6395763757b2SScott Long 
6396763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6397763757b2SScott Long 	offset = arg2;
6398d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6399d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6400041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
64016f8718a3SScott Long }
64026f8718a3SScott Long 
64036f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
64046f8718a3SScott Long static int
64056f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
64066f8718a3SScott Long {
64076f8718a3SScott Long 	struct bge_softc *sc;
64086f8718a3SScott Long 	uint16_t *sbdata;
640928276ad6SPyun YongHyeon 	int error, result, sbsz;
64106f8718a3SScott Long 	int i, j;
64116f8718a3SScott Long 
64126f8718a3SScott Long 	result = -1;
64136f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
64146f8718a3SScott Long 	if (error || (req->newptr == NULL))
64156f8718a3SScott Long 		return (error);
64166f8718a3SScott Long 
64176f8718a3SScott Long 	if (result == 1) {
64186f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
64196f8718a3SScott Long 
642028276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
642128276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
642228276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
642328276ad6SPyun YongHyeon 		else
642428276ad6SPyun YongHyeon 			sbsz = 32;
64256f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
64266f8718a3SScott Long 		printf("Status Block:\n");
642728276ad6SPyun YongHyeon 		BGE_LOCK(sc);
642828276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
642928276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
643028276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
643128276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
64326f8718a3SScott Long 			printf("%06x:", i);
643328276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
643428276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
64356f8718a3SScott Long 			printf("\n");
64366f8718a3SScott Long 		}
64376f8718a3SScott Long 
64386f8718a3SScott Long 		printf("Registers:\n");
64390c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
64406f8718a3SScott Long 			printf("%06x:", i);
64416f8718a3SScott Long 			for (j = 0; j < 8; j++) {
64426f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
64436f8718a3SScott Long 				i += 4;
64446f8718a3SScott Long 			}
64456f8718a3SScott Long 			printf("\n");
64466f8718a3SScott Long 		}
644728276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
64486f8718a3SScott Long 
64496f8718a3SScott Long 		printf("Hardware Flags:\n");
645028276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
645128276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6452a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6453a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
64545345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
64556f8718a3SScott Long 			printf(" - 575X Plus\n");
64565345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
64576f8718a3SScott Long 			printf(" - 5705 Plus\n");
64585345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
64595345bad0SScott Long 			printf(" - 5714 Family\n");
64605345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
64615345bad0SScott Long 			printf(" - 5700 Family\n");
64626f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
64636f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
64646f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
64656f8718a3SScott Long 			printf(" - PCI-X Bus\n");
64666f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
64676f8718a3SScott Long 			printf(" - PCI Express Bus\n");
64687d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
64696f8718a3SScott Long 			printf(" - No 3 LEDs\n");
64706f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
64716f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
64726f8718a3SScott Long 	}
64736f8718a3SScott Long 
64746f8718a3SScott Long 	return (error);
64756f8718a3SScott Long }
64766f8718a3SScott Long 
64776f8718a3SScott Long static int
64786f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
64796f8718a3SScott Long {
64806f8718a3SScott Long 	struct bge_softc *sc;
64816f8718a3SScott Long 	int error;
64826f8718a3SScott Long 	uint16_t result;
64836f8718a3SScott Long 	uint32_t val;
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 < 0x8000) {
64916f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
64926f8718a3SScott Long 		val = CSR_READ_4(sc, result);
64936f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
64946f8718a3SScott Long 	}
64956f8718a3SScott Long 
64966f8718a3SScott Long 	return (error);
64976f8718a3SScott Long }
64986f8718a3SScott Long 
64996f8718a3SScott Long static int
6500548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6501548c8f1aSPyun YongHyeon {
6502548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6503548c8f1aSPyun YongHyeon 	int error;
6504548c8f1aSPyun YongHyeon 	uint16_t result;
6505548c8f1aSPyun YongHyeon 	uint32_t val;
6506548c8f1aSPyun YongHyeon 
6507548c8f1aSPyun YongHyeon 	result = -1;
6508548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6509548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6510548c8f1aSPyun YongHyeon 		return (error);
6511548c8f1aSPyun YongHyeon 
6512548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6513548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6514548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6515548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6516548c8f1aSPyun YongHyeon 	}
6517548c8f1aSPyun YongHyeon 
6518548c8f1aSPyun YongHyeon 	return (error);
6519548c8f1aSPyun YongHyeon }
6520548c8f1aSPyun YongHyeon 
6521548c8f1aSPyun YongHyeon static int
65226f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
65236f8718a3SScott Long {
65246f8718a3SScott Long 	struct bge_softc *sc;
65256f8718a3SScott Long 	int error;
65266f8718a3SScott Long 	uint16_t result;
65276f8718a3SScott Long 	uint32_t val;
65286f8718a3SScott Long 
65296f8718a3SScott Long 	result = -1;
65306f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65316f8718a3SScott Long 	if (error || (req->newptr == NULL))
65326f8718a3SScott Long 		return (error);
65336f8718a3SScott Long 
65346f8718a3SScott Long 	if (result < 0x8000) {
65356f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65366f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
65376f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
65386f8718a3SScott Long 	}
65396f8718a3SScott Long 
65406f8718a3SScott Long 	return (error);
65416f8718a3SScott Long }
65426f8718a3SScott Long #endif
654338cc658fSJohn Baldwin 
654438cc658fSJohn Baldwin static int
65455fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
65465fea260fSMarius Strobl {
65475fea260fSMarius Strobl 
65485fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
65495fea260fSMarius Strobl 		return (1);
65505fea260fSMarius Strobl 
65515fea260fSMarius Strobl #ifdef __sparc64__
65525fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
65535fea260fSMarius Strobl 	return (0);
65545fea260fSMarius Strobl #endif
65555fea260fSMarius Strobl 	return (1);
65565fea260fSMarius Strobl }
65575fea260fSMarius Strobl 
65585fea260fSMarius Strobl static int
655938cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
656038cc658fSJohn Baldwin {
656138cc658fSJohn Baldwin 	uint32_t mac_addr;
656238cc658fSJohn Baldwin 
656373635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
656438cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
656538cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
656638cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
656773635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
656838cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
656938cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
657038cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
657138cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
65725fea260fSMarius Strobl 		return (0);
657338cc658fSJohn Baldwin 	}
65745fea260fSMarius Strobl 	return (1);
657538cc658fSJohn Baldwin }
657638cc658fSJohn Baldwin 
657738cc658fSJohn Baldwin static int
657838cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
657938cc658fSJohn Baldwin {
658038cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
658138cc658fSJohn Baldwin 
658238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
658338cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
658438cc658fSJohn Baldwin 
65855fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
65865fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
658738cc658fSJohn Baldwin }
658838cc658fSJohn Baldwin 
658938cc658fSJohn Baldwin static int
659038cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
659138cc658fSJohn Baldwin {
659238cc658fSJohn Baldwin 
65935fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
65945fea260fSMarius Strobl 		return (1);
65955fea260fSMarius Strobl 
65965fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
65975fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
659838cc658fSJohn Baldwin }
659938cc658fSJohn Baldwin 
660038cc658fSJohn Baldwin static int
660138cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
660238cc658fSJohn Baldwin {
660338cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
660438cc658fSJohn Baldwin 		/* NOTE: Order is critical */
66055fea260fSMarius Strobl 		bge_get_eaddr_fw,
660638cc658fSJohn Baldwin 		bge_get_eaddr_mem,
660738cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
660838cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
660938cc658fSJohn Baldwin 		NULL
661038cc658fSJohn Baldwin 	};
661138cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
661238cc658fSJohn Baldwin 
661338cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
661438cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
661538cc658fSJohn Baldwin 			break;
661638cc658fSJohn Baldwin 	}
661738cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
661838cc658fSJohn Baldwin }
6619