xref: /freebsd/sys/dev/bge/if_bge.c (revision fe26ad88e33ada61803bf9febac60c5f62418cc3)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
3895d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4195d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4222a4ecedSMarius Strobl  * MAC chips. The BCM5700, sometimes referred to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
82f1a7e6d5SScott Long #include <sys/sysctl.h>
83dfe0df9aSPyun YongHyeon #include <sys/taskqueue.h>
8495d67482SBill Paul 
8595d67482SBill Paul #include <net/if.h>
8695d67482SBill Paul #include <net/if_arp.h>
8795d67482SBill Paul #include <net/ethernet.h>
8895d67482SBill Paul #include <net/if_dl.h>
8995d67482SBill Paul #include <net/if_media.h>
9095d67482SBill Paul 
9195d67482SBill Paul #include <net/bpf.h>
9295d67482SBill Paul 
9395d67482SBill Paul #include <net/if_types.h>
9495d67482SBill Paul #include <net/if_vlan_var.h>
9595d67482SBill Paul 
9695d67482SBill Paul #include <netinet/in_systm.h>
9795d67482SBill Paul #include <netinet/in.h>
9895d67482SBill Paul #include <netinet/ip.h>
99ca3f1187SPyun YongHyeon #include <netinet/tcp.h>
10095d67482SBill Paul 
10195d67482SBill Paul #include <machine/bus.h>
10295d67482SBill Paul #include <machine/resource.h>
10395d67482SBill Paul #include <sys/bus.h>
10495d67482SBill Paul #include <sys/rman.h>
10595d67482SBill Paul 
10695d67482SBill Paul #include <dev/mii/mii.h>
10795d67482SBill Paul #include <dev/mii/miivar.h>
1082d3ce713SDavid E. O'Brien #include "miidevs.h"
10995d67482SBill Paul #include <dev/mii/brgphyreg.h>
11095d67482SBill Paul 
11108013fd3SMarius Strobl #ifdef __sparc64__
11208013fd3SMarius Strobl #include <dev/ofw/ofw_bus.h>
11308013fd3SMarius Strobl #include <dev/ofw/openfirm.h>
11408013fd3SMarius Strobl #include <machine/ofw_machdep.h>
11508013fd3SMarius Strobl #include <machine/ver.h>
11608013fd3SMarius Strobl #endif
11708013fd3SMarius Strobl 
1184fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1194fbd232cSWarner Losh #include <dev/pci/pcivar.h>
12095d67482SBill Paul 
12195d67482SBill Paul #include <dev/bge/if_bgereg.h>
12295d67482SBill Paul 
12335f945cdSPyun YongHyeon #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP)
124d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12595d67482SBill Paul 
126f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
127f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12895d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12995d67482SBill Paul 
1307b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
13195d67482SBill Paul #include "miibus_if.h"
13295d67482SBill Paul 
13395d67482SBill Paul /*
13495d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13595d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13695d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13795d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13895d67482SBill Paul  */
139852c67f9SMarius Strobl static const struct bge_type {
1404c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1414c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
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 },
219*fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57762 },
220b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57765 },
221*fe26ad88SPyun 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" },
352*fe26ad88SPyun 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)
368*fe26ad88SPyun YongHyeon #define	BGE_IS_57765_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_57765_PLUS)
3694c0da0ffSGleb Smirnoff 
3704c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3714c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
37238cc658fSJohn Baldwin 
37338cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
37438cc658fSJohn Baldwin 
375e51a25f8SAlfred Perlstein static int bge_probe(device_t);
376e51a25f8SAlfred Perlstein static int bge_attach(device_t);
377e51a25f8SAlfred Perlstein static int bge_detach(device_t);
37814afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
37914afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3803f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
381f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3825b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
383f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
3845b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
3855b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
386f41ac2beSBill Paul 
387ea9c3a30SPyun YongHyeon static void bge_devinfo(struct bge_softc *);
388062af0b0SPyun YongHyeon static int bge_mbox_reorder(struct bge_softc *);
389062af0b0SPyun YongHyeon 
3905fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
39138cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
39238cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
39338cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
39438cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
39538cc658fSJohn Baldwin 
396b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
3971108273aSPyun YongHyeon static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
398dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
39995d67482SBill Paul 
4008cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
401e51a25f8SAlfred Perlstein static void bge_tick(void *);
4022280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
403e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
4043f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
405d598b626SPyun YongHyeon static struct mbuf *bge_check_short_dma(struct mbuf *);
4062e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
4071108273aSPyun YongHyeon     uint16_t *, uint16_t *);
408676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
40995d67482SBill Paul 
410e51a25f8SAlfred Perlstein static void bge_intr(void *);
411dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
412dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
4130f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
414e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
415e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
4160f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
417e51a25f8SAlfred Perlstein static void bge_init(void *);
4185a147ba6SPyun YongHyeon static void bge_stop_block(struct bge_softc *, bus_size_t, uint32_t);
419e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
420b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
421b6c974e8SWarner Losh static int bge_shutdown(device_t);
42267d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
423e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
424e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
42595d67482SBill Paul 
42638cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
42738cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
42838cc658fSJohn Baldwin 
4293f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
430e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
43195d67482SBill Paul 
4323e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
433e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
434cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
43595d67482SBill Paul 
436e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
437e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
438943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
439943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
440e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
441e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
442e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
443e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
444e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
445e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
44695d67482SBill Paul 
447e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
448e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
44950515680SPyun YongHyeon static uint32_t bge_dma_swap_options(struct bge_softc *);
45095d67482SBill Paul 
4515fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4523f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
453e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
45438cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
45595d67482SBill Paul #ifdef notdef
4563f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
45795d67482SBill Paul #endif
4589ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
459e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
46095d67482SBill Paul 
461e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
462e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
463e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
46475719184SGleb Smirnoff #ifdef DEVICE_POLLING
4651abcdbd1SAttilio Rao static int bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
46675719184SGleb Smirnoff #endif
46795d67482SBill Paul 
468548c8f1aSPyun YongHyeon #define	BGE_RESET_SHUTDOWN	0
4698cb1383cSDoug Ambrisko #define	BGE_RESET_START		1
470548c8f1aSPyun YongHyeon #define	BGE_RESET_SUSPEND	2
4718cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4728cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4738cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
474797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4758cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
476dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
47795d67482SBill Paul 
478548c8f1aSPyun YongHyeon static void bge_ape_lock_init(struct bge_softc *);
479548c8f1aSPyun YongHyeon static void bge_ape_read_fw_ver(struct bge_softc *);
480548c8f1aSPyun YongHyeon static int bge_ape_lock(struct bge_softc *, int);
481548c8f1aSPyun YongHyeon static void bge_ape_unlock(struct bge_softc *, int);
482548c8f1aSPyun YongHyeon static void bge_ape_send_event(struct bge_softc *, uint32_t);
483548c8f1aSPyun YongHyeon static void bge_ape_driver_state_change(struct bge_softc *, int);
484548c8f1aSPyun YongHyeon 
4856f8718a3SScott Long /*
4866f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4876f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4886f8718a3SScott Long  * traps on certain architectures.
4896f8718a3SScott Long  */
4906f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4916f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4926f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
493548c8f1aSPyun YongHyeon static int bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS);
4946f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
4956f8718a3SScott Long #endif
4966f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
4972280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
4982280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
4992280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
5002280c16bSPyun YongHyeon     struct sysctl_oid_list *);
501763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
5026f8718a3SScott Long 
50395d67482SBill Paul static device_method_t bge_methods[] = {
50495d67482SBill Paul 	/* Device interface */
50595d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
50695d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
50795d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
50895d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
50914afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
51014afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
51195d67482SBill Paul 
51295d67482SBill Paul 	/* MII interface */
51395d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
51495d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
51595d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
51695d67482SBill Paul 
5174b7ec270SMarius Strobl 	DEVMETHOD_END
51895d67482SBill Paul };
51995d67482SBill Paul 
52095d67482SBill Paul static driver_t bge_driver = {
52195d67482SBill Paul 	"bge",
52295d67482SBill Paul 	bge_methods,
52395d67482SBill Paul 	sizeof(struct bge_softc)
52495d67482SBill Paul };
52595d67482SBill Paul 
52695d67482SBill Paul static devclass_t bge_devclass;
52795d67482SBill Paul 
528f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
52995d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
53095d67482SBill Paul 
531f1a7e6d5SScott Long static int bge_allow_asf = 1;
532f1a7e6d5SScott Long 
533f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
534f1a7e6d5SScott Long 
5356472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
536f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
537f1a7e6d5SScott Long 	"Allow ASF mode if available");
538c4529f41SMichael Reifenberger 
53908013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
54008013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
54108013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
54208013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
54308013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
54408013fd3SMarius Strobl 
54508013fd3SMarius Strobl static int
5465fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
54708013fd3SMarius Strobl {
54808013fd3SMarius Strobl #ifdef __sparc64__
54908013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
55008013fd3SMarius Strobl 	device_t dev;
55108013fd3SMarius Strobl 	uint32_t subvendor;
55208013fd3SMarius Strobl 
55308013fd3SMarius Strobl 	dev = sc->bge_dev;
55408013fd3SMarius Strobl 
55508013fd3SMarius Strobl 	/*
55608013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
55708013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
55808013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
55908013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
56008013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
56108013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
56208013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
56308013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
56408013fd3SMarius Strobl 	 * there.
56508013fd3SMarius Strobl 	 */
56608013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
56708013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5682d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
56908013fd3SMarius Strobl 		return (0);
57008013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
57108013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
57208013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
57308013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
57408013fd3SMarius Strobl 			return (0);
57508013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
57608013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
57708013fd3SMarius Strobl 			return (0);
57808013fd3SMarius Strobl 	}
57908013fd3SMarius Strobl #endif
58008013fd3SMarius Strobl 	return (1);
58108013fd3SMarius Strobl }
58208013fd3SMarius Strobl 
5833f74909aSGleb Smirnoff static uint32_t
5843f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
58595d67482SBill Paul {
58695d67482SBill Paul 	device_t dev;
5876f8718a3SScott Long 	uint32_t val;
58895d67482SBill Paul 
589a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
590a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
591a4431ebaSPyun YongHyeon 		return (0);
592a4431ebaSPyun YongHyeon 
59395d67482SBill Paul 	dev = sc->bge_dev;
59495d67482SBill Paul 
59595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5966f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5976f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5986f8718a3SScott Long 	return (val);
59995d67482SBill Paul }
60095d67482SBill Paul 
60195d67482SBill Paul static void
6023f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
60395d67482SBill Paul {
60495d67482SBill Paul 	device_t dev;
60595d67482SBill Paul 
606a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
607a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
608a4431ebaSPyun YongHyeon 		return;
609a4431ebaSPyun YongHyeon 
61095d67482SBill Paul 	dev = sc->bge_dev;
61195d67482SBill Paul 
61295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
61395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
6146f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
61595d67482SBill Paul }
61695d67482SBill Paul 
61795d67482SBill Paul #ifdef notdef
6183f74909aSGleb Smirnoff static uint32_t
6193f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
62095d67482SBill Paul {
62195d67482SBill Paul 	device_t dev;
62295d67482SBill Paul 
62395d67482SBill Paul 	dev = sc->bge_dev;
62495d67482SBill Paul 
62595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
62695d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
62795d67482SBill Paul }
62895d67482SBill Paul #endif
62995d67482SBill Paul 
63095d67482SBill Paul static void
6313f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
63295d67482SBill Paul {
63395d67482SBill Paul 	device_t dev;
63495d67482SBill Paul 
63595d67482SBill Paul 	dev = sc->bge_dev;
63695d67482SBill Paul 
63795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
63895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
63995d67482SBill Paul }
64095d67482SBill Paul 
6416f8718a3SScott Long static void
6426f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6436f8718a3SScott Long {
6446f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6456f8718a3SScott Long }
6466f8718a3SScott Long 
64738cc658fSJohn Baldwin static void
64838cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
64938cc658fSJohn Baldwin {
65038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
65138cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
65238cc658fSJohn Baldwin 
65338cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
654062af0b0SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
655062af0b0SPyun YongHyeon 		CSR_READ_4(sc, off);
65638cc658fSJohn Baldwin }
65738cc658fSJohn Baldwin 
658f41ac2beSBill Paul /*
659548c8f1aSPyun YongHyeon  * Clear all stale locks and select the lock for this driver instance.
660548c8f1aSPyun YongHyeon  */
661548c8f1aSPyun YongHyeon static void
662548c8f1aSPyun YongHyeon bge_ape_lock_init(struct bge_softc *sc)
663548c8f1aSPyun YongHyeon {
664548c8f1aSPyun YongHyeon 	uint32_t bit, regbase;
665548c8f1aSPyun YongHyeon 	int i;
666548c8f1aSPyun YongHyeon 
667548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
668548c8f1aSPyun YongHyeon 		regbase = BGE_APE_LOCK_GRANT;
669548c8f1aSPyun YongHyeon 	else
670548c8f1aSPyun YongHyeon 		regbase = BGE_APE_PER_LOCK_GRANT;
671548c8f1aSPyun YongHyeon 
672548c8f1aSPyun YongHyeon 	/* Clear any stale locks. */
673548c8f1aSPyun YongHyeon 	for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
674548c8f1aSPyun YongHyeon 		switch (i) {
675548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY0:
676548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY1:
677548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY2:
678548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY3:
679548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
680548c8f1aSPyun YongHyeon 			break;
681548c8f1aSPyun YongHyeon 		default:
682548c8f1aSPyun YongHyeon 			if (sc->bge_func_addr != 0)
683548c8f1aSPyun YongHyeon 				bit = BGE_APE_LOCK_GRANT_DRIVER0;
684548c8f1aSPyun YongHyeon 			else
685548c8f1aSPyun YongHyeon 				bit = (1 << sc->bge_func_addr);
686548c8f1aSPyun YongHyeon 		}
687548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, regbase + 4 * i, bit);
688548c8f1aSPyun YongHyeon 	}
689548c8f1aSPyun YongHyeon 
690548c8f1aSPyun YongHyeon 	/* Select the PHY lock based on the device's function number. */
691548c8f1aSPyun YongHyeon 	switch (sc->bge_func_addr) {
692548c8f1aSPyun YongHyeon 	case 0:
693548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY0;
694548c8f1aSPyun YongHyeon 		break;
695548c8f1aSPyun YongHyeon 	case 1:
696548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY1;
697548c8f1aSPyun YongHyeon 		break;
698548c8f1aSPyun YongHyeon 	case 2:
699548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY2;
700548c8f1aSPyun YongHyeon 		break;
701548c8f1aSPyun YongHyeon 	case 3:
702548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY3;
703548c8f1aSPyun YongHyeon 		break;
704548c8f1aSPyun YongHyeon 	default:
705548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev,
706548c8f1aSPyun YongHyeon 		    "PHY lock not supported on this function\n");
707548c8f1aSPyun YongHyeon 	}
708548c8f1aSPyun YongHyeon }
709548c8f1aSPyun YongHyeon 
710548c8f1aSPyun YongHyeon /*
711548c8f1aSPyun YongHyeon  * Check for APE firmware, set flags, and print version info.
712548c8f1aSPyun YongHyeon  */
713548c8f1aSPyun YongHyeon static void
714548c8f1aSPyun YongHyeon bge_ape_read_fw_ver(struct bge_softc *sc)
715548c8f1aSPyun YongHyeon {
716548c8f1aSPyun YongHyeon 	const char *fwtype;
717548c8f1aSPyun YongHyeon 	uint32_t apedata, features;
718548c8f1aSPyun YongHyeon 
719548c8f1aSPyun YongHyeon 	/* Check for a valid APE signature in shared memory. */
720548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
721548c8f1aSPyun YongHyeon 	if (apedata != BGE_APE_SEG_SIG_MAGIC) {
722548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags &= ~ BGE_MFW_ON_APE;
723548c8f1aSPyun YongHyeon 		return;
724548c8f1aSPyun YongHyeon 	}
725548c8f1aSPyun YongHyeon 
726548c8f1aSPyun YongHyeon 	/* Check if APE firmware is running. */
727548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
728548c8f1aSPyun YongHyeon 	if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
729548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE signature found "
730548c8f1aSPyun YongHyeon 		    "but FW status not ready! 0x%08x\n", apedata);
731548c8f1aSPyun YongHyeon 		return;
732548c8f1aSPyun YongHyeon 	}
733548c8f1aSPyun YongHyeon 
734548c8f1aSPyun YongHyeon 	sc->bge_mfw_flags |= BGE_MFW_ON_APE;
735548c8f1aSPyun YongHyeon 
736548c8f1aSPyun YongHyeon 	/* Fetch the APE firwmare type and version. */
737548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
738548c8f1aSPyun YongHyeon 	features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
739548c8f1aSPyun YongHyeon 	if ((features & BGE_APE_FW_FEATURE_NCSI) != 0) {
740548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_NCSI;
741548c8f1aSPyun YongHyeon 		fwtype = "NCSI";
742548c8f1aSPyun YongHyeon 	} else if ((features & BGE_APE_FW_FEATURE_DASH) != 0) {
743548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_DASH;
744548c8f1aSPyun YongHyeon 		fwtype = "DASH";
745548c8f1aSPyun YongHyeon 	} else
746548c8f1aSPyun YongHyeon 		fwtype = "UNKN";
747548c8f1aSPyun YongHyeon 
748548c8f1aSPyun YongHyeon 	/* Print the APE firmware version. */
749548c8f1aSPyun YongHyeon 	device_printf(sc->bge_dev, "APE FW version: %s v%d.%d.%d.%d\n",
750548c8f1aSPyun YongHyeon 	    fwtype,
751548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
752548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
753548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
754548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_BLDMSK));
755548c8f1aSPyun YongHyeon }
756548c8f1aSPyun YongHyeon 
757548c8f1aSPyun YongHyeon static int
758548c8f1aSPyun YongHyeon bge_ape_lock(struct bge_softc *sc, int locknum)
759548c8f1aSPyun YongHyeon {
760548c8f1aSPyun YongHyeon 	uint32_t bit, gnt, req, status;
761548c8f1aSPyun YongHyeon 	int i, off;
762548c8f1aSPyun YongHyeon 
763548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
764548c8f1aSPyun YongHyeon 		return (0);
765548c8f1aSPyun YongHyeon 
766548c8f1aSPyun YongHyeon 	/* Lock request/grant registers have different bases. */
767548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761) {
768548c8f1aSPyun YongHyeon 		req = BGE_APE_LOCK_REQ;
769548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
770548c8f1aSPyun YongHyeon 	} else {
771548c8f1aSPyun YongHyeon 		req = BGE_APE_PER_LOCK_REQ;
772548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
773548c8f1aSPyun YongHyeon 	}
774548c8f1aSPyun YongHyeon 
775548c8f1aSPyun YongHyeon 	off = 4 * locknum;
776548c8f1aSPyun YongHyeon 
777548c8f1aSPyun YongHyeon 	switch (locknum) {
778548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
779548c8f1aSPyun YongHyeon 		/* Lock required when using GPIO. */
780548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
781548c8f1aSPyun YongHyeon 			return (0);
782548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
783548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
784548c8f1aSPyun YongHyeon 		else
785548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
786548c8f1aSPyun YongHyeon 		break;
787548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
788548c8f1aSPyun YongHyeon 		/* Lock required to reset the device. */
789548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
790548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
791548c8f1aSPyun YongHyeon 		else
792548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
793548c8f1aSPyun YongHyeon 		break;
794548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
795548c8f1aSPyun YongHyeon 		/* Lock required when accessing certain APE memory. */
796548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
797548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
798548c8f1aSPyun YongHyeon 		else
799548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
800548c8f1aSPyun YongHyeon 		break;
801548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
802548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
803548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
804548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
805548c8f1aSPyun YongHyeon 		/* Lock required when accessing PHYs. */
806548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_REQ_DRIVER0;
807548c8f1aSPyun YongHyeon 		break;
808548c8f1aSPyun YongHyeon 	default:
809548c8f1aSPyun YongHyeon 		return (EINVAL);
810548c8f1aSPyun YongHyeon 	}
811548c8f1aSPyun YongHyeon 
812548c8f1aSPyun YongHyeon 	/* Request a lock. */
813548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, req + off, bit);
814548c8f1aSPyun YongHyeon 
815548c8f1aSPyun YongHyeon 	/* Wait up to 1 second to acquire lock. */
816548c8f1aSPyun YongHyeon 	for (i = 0; i < 20000; i++) {
817548c8f1aSPyun YongHyeon 		status = APE_READ_4(sc, gnt + off);
818548c8f1aSPyun YongHyeon 		if (status == bit)
819548c8f1aSPyun YongHyeon 			break;
820548c8f1aSPyun YongHyeon 		DELAY(50);
821548c8f1aSPyun YongHyeon 	}
822548c8f1aSPyun YongHyeon 
823548c8f1aSPyun YongHyeon 	/* Handle any errors. */
824548c8f1aSPyun YongHyeon 	if (status != bit) {
825548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE lock %d request failed! "
826548c8f1aSPyun YongHyeon 		    "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
827548c8f1aSPyun YongHyeon 		    locknum, req + off, bit & 0xFFFF, gnt + off,
828548c8f1aSPyun YongHyeon 		    status & 0xFFFF);
829548c8f1aSPyun YongHyeon 		/* Revoke the lock request. */
830548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, gnt + off, bit);
831548c8f1aSPyun YongHyeon 		return (EBUSY);
832548c8f1aSPyun YongHyeon 	}
833548c8f1aSPyun YongHyeon 
834548c8f1aSPyun YongHyeon 	return (0);
835548c8f1aSPyun YongHyeon }
836548c8f1aSPyun YongHyeon 
837548c8f1aSPyun YongHyeon static void
838548c8f1aSPyun YongHyeon bge_ape_unlock(struct bge_softc *sc, int locknum)
839548c8f1aSPyun YongHyeon {
840548c8f1aSPyun YongHyeon 	uint32_t bit, gnt;
841548c8f1aSPyun YongHyeon 	int off;
842548c8f1aSPyun YongHyeon 
843548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
844548c8f1aSPyun YongHyeon 		return;
845548c8f1aSPyun YongHyeon 
846548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
847548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
848548c8f1aSPyun YongHyeon 	else
849548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
850548c8f1aSPyun YongHyeon 
851548c8f1aSPyun YongHyeon 	off = 4 * locknum;
852548c8f1aSPyun YongHyeon 
853548c8f1aSPyun YongHyeon 	switch (locknum) {
854548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
855548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
856548c8f1aSPyun YongHyeon 			return;
857548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
858548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
859548c8f1aSPyun YongHyeon 		else
860548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
861548c8f1aSPyun YongHyeon 		break;
862548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
863548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
864548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
865548c8f1aSPyun YongHyeon 		else
866548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
867548c8f1aSPyun YongHyeon 		break;
868548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
869548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
870548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
871548c8f1aSPyun YongHyeon 		else
872548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
873548c8f1aSPyun YongHyeon 		break;
874548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
875548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
876548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
877548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
878548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_GRANT_DRIVER0;
879548c8f1aSPyun YongHyeon 		break;
880548c8f1aSPyun YongHyeon 	default:
881548c8f1aSPyun YongHyeon 		return;
882548c8f1aSPyun YongHyeon 	}
883548c8f1aSPyun YongHyeon 
884548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, gnt + off, bit);
885548c8f1aSPyun YongHyeon }
886548c8f1aSPyun YongHyeon 
887548c8f1aSPyun YongHyeon /*
888548c8f1aSPyun YongHyeon  * Send an event to the APE firmware.
889548c8f1aSPyun YongHyeon  */
890548c8f1aSPyun YongHyeon static void
891548c8f1aSPyun YongHyeon bge_ape_send_event(struct bge_softc *sc, uint32_t event)
892548c8f1aSPyun YongHyeon {
893548c8f1aSPyun YongHyeon 	uint32_t apedata;
894548c8f1aSPyun YongHyeon 	int i;
895548c8f1aSPyun YongHyeon 
896548c8f1aSPyun YongHyeon 	/* NCSI does not support APE events. */
897548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
898548c8f1aSPyun YongHyeon 		return;
899548c8f1aSPyun YongHyeon 
900548c8f1aSPyun YongHyeon 	/* Wait up to 1ms for APE to service previous event. */
901548c8f1aSPyun YongHyeon 	for (i = 10; i > 0; i--) {
902548c8f1aSPyun YongHyeon 		if (bge_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
903548c8f1aSPyun YongHyeon 			break;
904548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
905548c8f1aSPyun YongHyeon 		if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
906548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
907548c8f1aSPyun YongHyeon 			    BGE_APE_EVENT_STATUS_EVENT_PENDING);
908548c8f1aSPyun YongHyeon 			bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
909548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
910548c8f1aSPyun YongHyeon 			break;
911548c8f1aSPyun YongHyeon 		}
912548c8f1aSPyun YongHyeon 		bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
913548c8f1aSPyun YongHyeon 		DELAY(100);
914548c8f1aSPyun YongHyeon 	}
915548c8f1aSPyun YongHyeon 	if (i == 0)
916548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE event 0x%08x send timed out\n",
917548c8f1aSPyun YongHyeon 		    event);
918548c8f1aSPyun YongHyeon }
919548c8f1aSPyun YongHyeon 
920548c8f1aSPyun YongHyeon static void
921548c8f1aSPyun YongHyeon bge_ape_driver_state_change(struct bge_softc *sc, int kind)
922548c8f1aSPyun YongHyeon {
923548c8f1aSPyun YongHyeon 	uint32_t apedata, event;
924548c8f1aSPyun YongHyeon 
925548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
926548c8f1aSPyun YongHyeon 		return;
927548c8f1aSPyun YongHyeon 
928548c8f1aSPyun YongHyeon 	switch (kind) {
929548c8f1aSPyun YongHyeon 	case BGE_RESET_START:
930548c8f1aSPyun YongHyeon 		/* If this is the first load, clear the load counter. */
931548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
932548c8f1aSPyun YongHyeon 		if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC)
933548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
934548c8f1aSPyun YongHyeon 		else {
935548c8f1aSPyun YongHyeon 			apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
936548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
937548c8f1aSPyun YongHyeon 		}
938548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
939548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_SIG_MAGIC);
940548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
941548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_LEN_MAGIC);
942548c8f1aSPyun YongHyeon 
943548c8f1aSPyun YongHyeon 		/* Add some version info if bge(4) supports it. */
944548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
945548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
946548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
947548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_BEHAV_NO_PHYLOCK);
948548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
949548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
950548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
951548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_START);
952548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_START;
953548c8f1aSPyun YongHyeon 		break;
954548c8f1aSPyun YongHyeon 	case BGE_RESET_SHUTDOWN:
955548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
956548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_UNLOAD);
957548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
958548c8f1aSPyun YongHyeon 		break;
959548c8f1aSPyun YongHyeon 	case BGE_RESET_SUSPEND:
960548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
961548c8f1aSPyun YongHyeon 		break;
962548c8f1aSPyun YongHyeon 	default:
963548c8f1aSPyun YongHyeon 		return;
964548c8f1aSPyun YongHyeon 	}
965548c8f1aSPyun YongHyeon 
966548c8f1aSPyun YongHyeon 	bge_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
967548c8f1aSPyun YongHyeon 	    BGE_APE_EVENT_STATUS_STATE_CHNGE);
968548c8f1aSPyun YongHyeon }
969548c8f1aSPyun YongHyeon 
970548c8f1aSPyun YongHyeon /*
971f41ac2beSBill Paul  * Map a single buffer address.
972f41ac2beSBill Paul  */
973f41ac2beSBill Paul 
974f41ac2beSBill Paul static void
9753f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
976f41ac2beSBill Paul {
977f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
978f41ac2beSBill Paul 
979f41ac2beSBill Paul 	if (error)
980f41ac2beSBill Paul 		return;
981f41ac2beSBill Paul 
9825b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
9835b610048SPyun YongHyeon 
984f41ac2beSBill Paul 	ctx = arg;
985f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
986f41ac2beSBill Paul }
987f41ac2beSBill Paul 
98838cc658fSJohn Baldwin static uint8_t
98938cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
99038cc658fSJohn Baldwin {
99138cc658fSJohn Baldwin 	uint32_t access, byte = 0;
99238cc658fSJohn Baldwin 	int i;
99338cc658fSJohn Baldwin 
99438cc658fSJohn Baldwin 	/* Lock. */
99538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
99638cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
99738cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
99838cc658fSJohn Baldwin 			break;
99938cc658fSJohn Baldwin 		DELAY(20);
100038cc658fSJohn Baldwin 	}
100138cc658fSJohn Baldwin 	if (i == 8000)
100238cc658fSJohn Baldwin 		return (1);
100338cc658fSJohn Baldwin 
100438cc658fSJohn Baldwin 	/* Enable access. */
100538cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
100638cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
100738cc658fSJohn Baldwin 
100838cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
100938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
101038cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
101138cc658fSJohn Baldwin 		DELAY(10);
101238cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
101338cc658fSJohn Baldwin 			DELAY(10);
101438cc658fSJohn Baldwin 			break;
101538cc658fSJohn Baldwin 		}
101638cc658fSJohn Baldwin 	}
101738cc658fSJohn Baldwin 
101838cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
101938cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
102038cc658fSJohn Baldwin 		return (1);
102138cc658fSJohn Baldwin 	}
102238cc658fSJohn Baldwin 
102338cc658fSJohn Baldwin 	/* Get result. */
102438cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
102538cc658fSJohn Baldwin 
102638cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
102738cc658fSJohn Baldwin 
102838cc658fSJohn Baldwin 	/* Disable access. */
102938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
103038cc658fSJohn Baldwin 
103138cc658fSJohn Baldwin 	/* Unlock. */
103238cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
103338cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
103438cc658fSJohn Baldwin 
103538cc658fSJohn Baldwin 	return (0);
103638cc658fSJohn Baldwin }
103738cc658fSJohn Baldwin 
103838cc658fSJohn Baldwin /*
103938cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
104038cc658fSJohn Baldwin  */
104138cc658fSJohn Baldwin static int
104238cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
104338cc658fSJohn Baldwin {
104438cc658fSJohn Baldwin 	int err = 0, i;
104538cc658fSJohn Baldwin 	uint8_t byte = 0;
104638cc658fSJohn Baldwin 
104738cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
104838cc658fSJohn Baldwin 		return (1);
104938cc658fSJohn Baldwin 
105038cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
105138cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
105238cc658fSJohn Baldwin 		if (err)
105338cc658fSJohn Baldwin 			break;
105438cc658fSJohn Baldwin 		*(dest + i) = byte;
105538cc658fSJohn Baldwin 	}
105638cc658fSJohn Baldwin 
105738cc658fSJohn Baldwin 	return (err ? 1 : 0);
105838cc658fSJohn Baldwin }
105938cc658fSJohn Baldwin 
106095d67482SBill Paul /*
106195d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
106295d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
106395d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
106495d67482SBill Paul  * access method.
106595d67482SBill Paul  */
10663f74909aSGleb Smirnoff static uint8_t
10673f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
106895d67482SBill Paul {
106995d67482SBill Paul 	int i;
10703f74909aSGleb Smirnoff 	uint32_t byte = 0;
107195d67482SBill Paul 
107295d67482SBill Paul 	/*
107395d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
107495d67482SBill Paul 	 * having to use the bitbang method.
107595d67482SBill Paul 	 */
107695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
107795d67482SBill Paul 
107895d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
107995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
108095d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
108195d67482SBill Paul 	DELAY(20);
108295d67482SBill Paul 
108395d67482SBill Paul 	/* Issue the read EEPROM command. */
108495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
108595d67482SBill Paul 
108695d67482SBill Paul 	/* Wait for completion */
108795d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
108895d67482SBill Paul 		DELAY(10);
108995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
109095d67482SBill Paul 			break;
109195d67482SBill Paul 	}
109295d67482SBill Paul 
1093d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
1094fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
1095f6789fbaSPyun YongHyeon 		return (1);
109695d67482SBill Paul 	}
109795d67482SBill Paul 
109895d67482SBill Paul 	/* Get result. */
109995d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
110095d67482SBill Paul 
11010c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
110295d67482SBill Paul 
110395d67482SBill Paul 	return (0);
110495d67482SBill Paul }
110595d67482SBill Paul 
110695d67482SBill Paul /*
110795d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
110895d67482SBill Paul  */
110995d67482SBill Paul static int
11103f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
111195d67482SBill Paul {
11123f74909aSGleb Smirnoff 	int i, error = 0;
11133f74909aSGleb Smirnoff 	uint8_t byte = 0;
111495d67482SBill Paul 
111595d67482SBill Paul 	for (i = 0; i < cnt; i++) {
11163f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
11173f74909aSGleb Smirnoff 		if (error)
111895d67482SBill Paul 			break;
111995d67482SBill Paul 		*(dest + i) = byte;
112095d67482SBill Paul 	}
112195d67482SBill Paul 
11223f74909aSGleb Smirnoff 	return (error ? 1 : 0);
112395d67482SBill Paul }
112495d67482SBill Paul 
112595d67482SBill Paul static int
11263f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
112795d67482SBill Paul {
112895d67482SBill Paul 	struct bge_softc *sc;
1129a813ed78SPyun YongHyeon 	uint32_t val;
113095d67482SBill Paul 	int i;
113195d67482SBill Paul 
113295d67482SBill Paul 	sc = device_get_softc(dev);
113395d67482SBill Paul 
1134548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1135548c8f1aSPyun YongHyeon 		return (0);
1136548c8f1aSPyun YongHyeon 
1137a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1138a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1139a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1140a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1141a813ed78SPyun YongHyeon 		DELAY(80);
114237ceeb4dSPaul Saab 	}
114337ceeb4dSPaul Saab 
114495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
114595d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
114695d67482SBill Paul 
1147a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
114895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1149d5d23857SJung-uk Kim 		DELAY(10);
115095d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
1151a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
1152a813ed78SPyun YongHyeon 			DELAY(5);
1153a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
115495d67482SBill Paul 			break;
115595d67482SBill Paul 		}
1156a813ed78SPyun YongHyeon 	}
115795d67482SBill Paul 
115895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
11595fea260fSMarius Strobl 		device_printf(sc->bge_dev,
11605fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
11615fea260fSMarius Strobl 		    phy, reg, val);
116237ceeb4dSPaul Saab 		val = 0;
116395d67482SBill Paul 	}
116495d67482SBill Paul 
1165a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1166a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1167a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1168a813ed78SPyun YongHyeon 		DELAY(80);
116937ceeb4dSPaul Saab 	}
117037ceeb4dSPaul Saab 
1171548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1172548c8f1aSPyun YongHyeon 
117395d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
117495d67482SBill Paul 		return (0);
117595d67482SBill Paul 
11760c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
117795d67482SBill Paul }
117895d67482SBill Paul 
117995d67482SBill Paul static int
11803f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
118195d67482SBill Paul {
118295d67482SBill Paul 	struct bge_softc *sc;
118395d67482SBill Paul 	int i;
118495d67482SBill Paul 
118595d67482SBill Paul 	sc = device_get_softc(dev);
118695d67482SBill Paul 
118738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
118838cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
118938cc658fSJohn Baldwin 		return (0);
119038cc658fSJohn Baldwin 
1191548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1192548c8f1aSPyun YongHyeon 		return (0);
1193548c8f1aSPyun YongHyeon 
1194a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1195a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1196a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1197a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1198a813ed78SPyun YongHyeon 		DELAY(80);
119937ceeb4dSPaul Saab 	}
120037ceeb4dSPaul Saab 
120195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
120295d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
120395d67482SBill Paul 
120495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1205d5d23857SJung-uk Kim 		DELAY(10);
120638cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
120738cc658fSJohn Baldwin 			DELAY(5);
120838cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
120995d67482SBill Paul 			break;
1210d5d23857SJung-uk Kim 		}
121138cc658fSJohn Baldwin 	}
1212d5d23857SJung-uk Kim 
1213a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1214a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1215a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1216a813ed78SPyun YongHyeon 		DELAY(80);
1217a813ed78SPyun YongHyeon 	}
1218a813ed78SPyun YongHyeon 
1219548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1220548c8f1aSPyun YongHyeon 
1221a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
122238cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
12232246e8c6SPyun YongHyeon 		    "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
122438cc658fSJohn Baldwin 		    phy, reg, val);
122537ceeb4dSPaul Saab 
122695d67482SBill Paul 	return (0);
122795d67482SBill Paul }
122895d67482SBill Paul 
122995d67482SBill Paul static void
12303f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
123195d67482SBill Paul {
123295d67482SBill Paul 	struct bge_softc *sc;
123395d67482SBill Paul 	struct mii_data *mii;
1234a0a03d1eSPyun YongHyeon 	uint32_t mac_mode, rx_mode, tx_mode;
1235e4146b95SPyun YongHyeon 
123695d67482SBill Paul 	sc = device_get_softc(dev);
1237e4146b95SPyun YongHyeon 	if ((sc->bge_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1238e4146b95SPyun YongHyeon 		return;
123995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
124095d67482SBill Paul 
1241d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1242d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
1243d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
1244d4f5240aSPyun YongHyeon 		case IFM_10_T:
1245d4f5240aSPyun YongHyeon 		case IFM_100_TX:
1246d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
1247d4f5240aSPyun YongHyeon 			break;
1248d4f5240aSPyun YongHyeon 		case IFM_1000_T:
1249d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
1250d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
1251d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1252d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
1253d4f5240aSPyun YongHyeon 			else
1254d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
1255d4f5240aSPyun YongHyeon 			break;
1256d4f5240aSPyun YongHyeon 		default:
1257d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
1258d4f5240aSPyun YongHyeon 			break;
1259d4f5240aSPyun YongHyeon 		}
1260d4f5240aSPyun YongHyeon 	} else
1261d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
1262d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
1263d4f5240aSPyun YongHyeon 		return;
1264a0a03d1eSPyun YongHyeon 
1265a0a03d1eSPyun YongHyeon 	/*
1266a0a03d1eSPyun YongHyeon 	 * APE firmware touches these registers to keep the MAC
1267a0a03d1eSPyun YongHyeon 	 * connected to the outside world.  Try to keep the
1268a0a03d1eSPyun YongHyeon 	 * accesses atomic.
1269a0a03d1eSPyun YongHyeon 	 */
1270a0a03d1eSPyun YongHyeon 
1271a0a03d1eSPyun YongHyeon 	/* Set the port mode (MII/GMII) to match the link speed. */
1272a0a03d1eSPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
1273a0a03d1eSPyun YongHyeon 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
1274a0a03d1eSPyun YongHyeon 	tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
1275a0a03d1eSPyun YongHyeon 	rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
1276a0a03d1eSPyun YongHyeon 
1277ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1278ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1279a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_GMII;
12803f74909aSGleb Smirnoff 	else
1281a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_MII;
128295d67482SBill Paul 
1283a0a03d1eSPyun YongHyeon 	/* Set MAC flow control behavior to match link flow control settings. */
1284a0a03d1eSPyun YongHyeon 	tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
1285a0a03d1eSPyun YongHyeon 	rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
12866854be25SPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0) {
1287a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1288a0a03d1eSPyun YongHyeon 			tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
1289a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1290a0a03d1eSPyun YongHyeon 			rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
1291a0a03d1eSPyun YongHyeon 	} else
1292a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
1293a0a03d1eSPyun YongHyeon 
1294a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
12959b80ffe7SPyun YongHyeon 	DELAY(40);
1296a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
1297a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
129895d67482SBill Paul }
129995d67482SBill Paul 
130095d67482SBill Paul /*
130195d67482SBill Paul  * Intialize a standard receive ring descriptor.
130295d67482SBill Paul  */
130395d67482SBill Paul static int
1304943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
130595d67482SBill Paul {
1306943787f3SPyun YongHyeon 	struct mbuf *m;
130795d67482SBill Paul 	struct bge_rx_bd *r;
1308a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
1309943787f3SPyun YongHyeon 	bus_dmamap_t map;
1310a23634a1SPyun YongHyeon 	int error, nsegs;
131195d67482SBill Paul 
1312f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
1313f5459d4cSPyun YongHyeon 	    (sc->bge_ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
1314f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
1315f5459d4cSPyun YongHyeon 		m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1316f5459d4cSPyun YongHyeon 		if (m == NULL)
1317f5459d4cSPyun YongHyeon 			return (ENOBUFS);
1318f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1319f5459d4cSPyun YongHyeon 	} else {
1320943787f3SPyun YongHyeon 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1321943787f3SPyun YongHyeon 		if (m == NULL)
132295d67482SBill Paul 			return (ENOBUFS);
1323943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
1324f5459d4cSPyun YongHyeon 	}
1325652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1326943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
1327943787f3SPyun YongHyeon 
13280ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
1329943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
1330a23634a1SPyun YongHyeon 	if (error != 0) {
1331943787f3SPyun YongHyeon 		m_freem(m);
1332a23634a1SPyun YongHyeon 		return (error);
1333f41ac2beSBill Paul 	}
1334943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1335943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1336943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1337943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1338943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
1339943787f3SPyun YongHyeon 	}
1340943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
1341943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1342943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
1343943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
1344e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1345943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1346a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1347a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1348e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1349a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1350e907febfSPyun YongHyeon 	r->bge_idx = i;
1351f41ac2beSBill Paul 
13520ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1353943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
135495d67482SBill Paul 
135595d67482SBill Paul 	return (0);
135695d67482SBill Paul }
135795d67482SBill Paul 
135895d67482SBill Paul /*
135995d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
136095d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
136195d67482SBill Paul  */
136295d67482SBill Paul static int
1363943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
136495d67482SBill Paul {
13651be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1366943787f3SPyun YongHyeon 	bus_dmamap_t map;
13671be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1368943787f3SPyun YongHyeon 	struct mbuf *m;
1369943787f3SPyun YongHyeon 	int error, nsegs;
137095d67482SBill Paul 
1371943787f3SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1372943787f3SPyun YongHyeon 	if (m == NULL)
137395d67482SBill Paul 		return (ENOBUFS);
137495d67482SBill Paul 
1375943787f3SPyun YongHyeon 	m_cljget(m, M_DONTWAIT, MJUM9BYTES);
1376943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
1377943787f3SPyun YongHyeon 		m_freem(m);
137895d67482SBill Paul 		return (ENOBUFS);
137995d67482SBill Paul 	}
1380943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1381652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1382943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
13831be6acb7SGleb Smirnoff 
13841be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1385943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1386943787f3SPyun YongHyeon 	if (error != 0) {
1387943787f3SPyun YongHyeon 		m_freem(m);
13881be6acb7SGleb Smirnoff 		return (error);
1389f7cea149SGleb Smirnoff 	}
13901be6acb7SGleb Smirnoff 
1391aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1392943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1393943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1394943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1395943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1396943787f3SPyun YongHyeon 	}
1397943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1398943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1399943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1400943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1401943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1402e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1403e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1404e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1405e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1406e0b7b101SPyun YongHyeon 
14071be6acb7SGleb Smirnoff 	/*
14081be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
14091be6acb7SGleb Smirnoff 	 */
1410943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
14114e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
14124e7ba1abSGleb Smirnoff 	r->bge_idx = i;
14134e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
14144e7ba1abSGleb Smirnoff 	switch (nsegs) {
14154e7ba1abSGleb Smirnoff 	case 4:
14164e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
14174e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
14184e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1419e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
14204e7ba1abSGleb Smirnoff 	case 3:
1421e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1422e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1423e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1424e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
14254e7ba1abSGleb Smirnoff 	case 2:
14264e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
14274e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
14284e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1429e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
14304e7ba1abSGleb Smirnoff 	case 1:
14314e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
14324e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
14334e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1434e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
14354e7ba1abSGleb Smirnoff 		break;
14364e7ba1abSGleb Smirnoff 	default:
14374e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
14384e7ba1abSGleb Smirnoff 	}
1439f41ac2beSBill Paul 
1440a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1441943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
144295d67482SBill Paul 
144395d67482SBill Paul 	return (0);
144495d67482SBill Paul }
144595d67482SBill Paul 
144695d67482SBill Paul static int
14473f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
144895d67482SBill Paul {
14493ee5d7daSPyun YongHyeon 	int error, i;
145095d67482SBill Paul 
1451e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
145203e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1453e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1454943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
14553ee5d7daSPyun YongHyeon 			return (error);
145603e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
14571888f324SPyun YongHyeon 	}
145895d67482SBill Paul 
1459f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1460d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1461f41ac2beSBill Paul 
1462e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1463e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
146495d67482SBill Paul 
146595d67482SBill Paul 	return (0);
146695d67482SBill Paul }
146795d67482SBill Paul 
146895d67482SBill Paul static void
14693f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
147095d67482SBill Paul {
147195d67482SBill Paul 	int i;
147295d67482SBill Paul 
147395d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
147495d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
14750ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1476e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1477e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
14780ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1479f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1480e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1481e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
148295d67482SBill Paul 		}
1483f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
148495d67482SBill Paul 		    sizeof(struct bge_rx_bd));
148595d67482SBill Paul 	}
148695d67482SBill Paul }
148795d67482SBill Paul 
148895d67482SBill Paul static int
14893f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
149095d67482SBill Paul {
149195d67482SBill Paul 	struct bge_rcb *rcb;
14923ee5d7daSPyun YongHyeon 	int error, i;
149395d67482SBill Paul 
1494e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
149503e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
149695d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1497943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
14983ee5d7daSPyun YongHyeon 			return (error);
149903e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
15001888f324SPyun YongHyeon 	}
150195d67482SBill Paul 
1502f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1503d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1504f41ac2beSBill Paul 
1505e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
150695d67482SBill Paul 
15078a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1508f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
15098a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
15108a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
151167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
151295d67482SBill Paul 
1513e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
151495d67482SBill Paul 
151595d67482SBill Paul 	return (0);
151695d67482SBill Paul }
151795d67482SBill Paul 
151895d67482SBill Paul static void
15193f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
152095d67482SBill Paul {
152195d67482SBill Paul 	int i;
152295d67482SBill Paul 
152395d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
152495d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1525e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1526e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1527e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1528f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1529f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1530e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1531e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
153295d67482SBill Paul 		}
1533f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
15341be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
153595d67482SBill Paul 	}
153695d67482SBill Paul }
153795d67482SBill Paul 
153895d67482SBill Paul static void
15393f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
154095d67482SBill Paul {
154195d67482SBill Paul 	int i;
154295d67482SBill Paul 
1543f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
154495d67482SBill Paul 		return;
154595d67482SBill Paul 
154695d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
154795d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
15480ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1549e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1550e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15510ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1552f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1553e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1554e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
155595d67482SBill Paul 		}
1556f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
155795d67482SBill Paul 		    sizeof(struct bge_tx_bd));
155895d67482SBill Paul 	}
155995d67482SBill Paul }
156095d67482SBill Paul 
156195d67482SBill Paul static int
15623f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
156395d67482SBill Paul {
156495d67482SBill Paul 	sc->bge_txcnt = 0;
156595d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
15663927098fSPaul Saab 
1567e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1568e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
15695c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1570e6bf277eSPyun YongHyeon 
157114bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
157214bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
157338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
157414bbd30fSGleb Smirnoff 
15753927098fSPaul Saab 	/* 5700 b2 errata */
1576e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
157738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
15783927098fSPaul Saab 
157914bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
158038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
15813927098fSPaul Saab 	/* 5700 b2 errata */
1582e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
158338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
158495d67482SBill Paul 
158595d67482SBill Paul 	return (0);
158695d67482SBill Paul }
158795d67482SBill Paul 
158895d67482SBill Paul static void
15893e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
15903e9b1bcaSJung-uk Kim {
15913e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
15923e9b1bcaSJung-uk Kim 
15933e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
15943e9b1bcaSJung-uk Kim 
15953e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
15963e9b1bcaSJung-uk Kim 
159745ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
15983e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
159945ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16003e9b1bcaSJung-uk Kim 	else
160145ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16023e9b1bcaSJung-uk Kim }
16033e9b1bcaSJung-uk Kim 
16043e9b1bcaSJung-uk Kim static void
16053f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
160695d67482SBill Paul {
160795d67482SBill Paul 	struct ifnet *ifp;
160895d67482SBill Paul 	struct ifmultiaddr *ifma;
16093f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
161095d67482SBill Paul 	int h, i;
161195d67482SBill Paul 
16120f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
16130f9bd73bSSam Leffler 
1614fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
161595d67482SBill Paul 
161695d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
161795d67482SBill Paul 		for (i = 0; i < 4; i++)
16180c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
161995d67482SBill Paul 		return;
162095d67482SBill Paul 	}
162195d67482SBill Paul 
162295d67482SBill Paul 	/* First, zot all the existing filters. */
162395d67482SBill Paul 	for (i = 0; i < 4; i++)
162495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
162595d67482SBill Paul 
162695d67482SBill Paul 	/* Now program new ones. */
1627eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
162895d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
162995d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
163095d67482SBill Paul 			continue;
16310e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
16320c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
16330c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
163495d67482SBill Paul 	}
1635eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
163695d67482SBill Paul 
163795d67482SBill Paul 	for (i = 0; i < 4; i++)
163895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
163995d67482SBill Paul }
164095d67482SBill Paul 
16418cb1383cSDoug Ambrisko static void
1642cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1643cb2eacc7SYaroslav Tykhiy {
1644cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1645cb2eacc7SYaroslav Tykhiy 
1646cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1647cb2eacc7SYaroslav Tykhiy 
1648cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1649cb2eacc7SYaroslav Tykhiy 
1650cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1651cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1652cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1653cb2eacc7SYaroslav Tykhiy 	else
1654cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1655cb2eacc7SYaroslav Tykhiy }
1656cb2eacc7SYaroslav Tykhiy 
1657cb2eacc7SYaroslav Tykhiy static void
1658797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
16598cb1383cSDoug Ambrisko {
1660797ab05eSPyun YongHyeon 
16618cb1383cSDoug Ambrisko 	/*
16628cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
16638cb1383cSDoug Ambrisko 	 */
16648cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1665888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
16668cb1383cSDoug Ambrisko 
16678cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16688cb1383cSDoug Ambrisko 		switch (type) {
16698cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1670224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1671224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
16728cb1383cSDoug Ambrisko 			break;
1673548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1674224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1675224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
16768cb1383cSDoug Ambrisko 			break;
1677548c8f1aSPyun YongHyeon 		case BGE_RESET_SUSPEND:
1678548c8f1aSPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1679548c8f1aSPyun YongHyeon 			    BGE_FW_DRV_STATE_SUSPEND);
1680548c8f1aSPyun YongHyeon 			break;
16818cb1383cSDoug Ambrisko 		}
16828cb1383cSDoug Ambrisko 	}
1683548c8f1aSPyun YongHyeon 
1684548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1685548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
16868cb1383cSDoug Ambrisko }
16878cb1383cSDoug Ambrisko 
16888cb1383cSDoug Ambrisko static void
1689797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
16908cb1383cSDoug Ambrisko {
1691797ab05eSPyun YongHyeon 
16928cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16938cb1383cSDoug Ambrisko 		switch (type) {
16948cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1695224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1696224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
16978cb1383cSDoug Ambrisko 			/* START DONE */
16988cb1383cSDoug Ambrisko 			break;
1699548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1700224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1701224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
17028cb1383cSDoug Ambrisko 			break;
17038cb1383cSDoug Ambrisko 		}
17048cb1383cSDoug Ambrisko 	}
1705548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_SHUTDOWN)
1706548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17078cb1383cSDoug Ambrisko }
17088cb1383cSDoug Ambrisko 
17098cb1383cSDoug Ambrisko static void
1710797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
17118cb1383cSDoug Ambrisko {
1712797ab05eSPyun YongHyeon 
17138cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17148cb1383cSDoug Ambrisko 		switch (type) {
17158cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1716224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1717224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
17188cb1383cSDoug Ambrisko 			break;
1719548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1720224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1721224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
17228cb1383cSDoug Ambrisko 			break;
17238cb1383cSDoug Ambrisko 		}
17248cb1383cSDoug Ambrisko 	}
17258cb1383cSDoug Ambrisko }
17268cb1383cSDoug Ambrisko 
1727797ab05eSPyun YongHyeon static void
1728797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
17298cb1383cSDoug Ambrisko {
17308cb1383cSDoug Ambrisko 	int i;
17318cb1383cSDoug Ambrisko 
17328cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17333c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
17343fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
17359931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
17368cb1383cSDoug Ambrisko 
17378cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
17389931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
17399931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
17408cb1383cSDoug Ambrisko 				break;
17418cb1383cSDoug Ambrisko 			DELAY(10);
17428cb1383cSDoug Ambrisko 		}
17438cb1383cSDoug Ambrisko 	}
17448cb1383cSDoug Ambrisko }
17458cb1383cSDoug Ambrisko 
174650515680SPyun YongHyeon static uint32_t
174750515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
174850515680SPyun YongHyeon {
174950515680SPyun YongHyeon 	uint32_t dma_options;
175050515680SPyun YongHyeon 
175150515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
175250515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
175350515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
175450515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
175550515680SPyun YongHyeon #endif
175650515680SPyun YongHyeon 	return (dma_options);
175750515680SPyun YongHyeon }
175850515680SPyun YongHyeon 
175995d67482SBill Paul /*
1760c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
176195d67482SBill Paul  */
176295d67482SBill Paul static int
17633f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
176495d67482SBill Paul {
176550515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1766fbc374afSPyun YongHyeon 	uint16_t val;
176795d67482SBill Paul 	int i;
176895d67482SBill Paul 
17698cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
17701108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
17711108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
17721108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
17731108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
177495d67482SBill Paul 
177595d67482SBill Paul 	/*
177695d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
177795d67482SBill Paul 	 * internal memory.
177895d67482SBill Paul 	 */
177995d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
17803f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
178195d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
178295d67482SBill Paul 
178395d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
17843f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
178595d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
178695d67482SBill Paul 
1787fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1788fbc374afSPyun YongHyeon 		/*
1789d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1790fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1791fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1792fbc374afSPyun YongHyeon 		 */
1793fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1794fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1795fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1796fbc374afSPyun YongHyeon 	}
1797fbc374afSPyun YongHyeon 
1798186f842bSJung-uk Kim 	/*
1799186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1800186f842bSJung-uk Kim 	 */
1801186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1802186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1803652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
180448630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
180548630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
180648630d79SPyun YongHyeon 		else
1807186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1808652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
18094c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1810186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1811186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1812186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1813186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1814186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1815186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1816cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1817cbb2b2feSPyun YongHyeon 			/*
1818cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1819cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1820cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1821cbb2b2feSPyun YongHyeon 			 * register.
1822cbb2b2feSPyun YongHyeon 			 */
1823cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1824cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1825186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1826186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1827186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1828186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1829186f842bSJung-uk Kim 		} else {
1830186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1831186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1832186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
18330c8aa4eaSJung-uk Kim 			    0x0F;
1834186f842bSJung-uk Kim 		}
1835e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1836e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
18373f74909aSGleb Smirnoff 			uint32_t tmp;
18385cba12d3SPaul Saab 
1839186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
18400c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1841186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1842186f842bSJung-uk Kim 				dma_rw_ctl |=
1843186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
18445cba12d3SPaul Saab 
1845186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1846186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1847186f842bSJung-uk Kim 		}
1848186f842bSJung-uk Kim 	} else {
1849186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1850186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1851186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1852186f842bSJung-uk Kim 
1853186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1854186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1855186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1856186f842bSJung-uk Kim 	}
1857186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1858186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1859186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1860186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1861e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1862186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
18635cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1864b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18651108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1866b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1867b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1868bbe2ca75SPyun YongHyeon 		/*
1869bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1870bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1871bbe2ca75SPyun YongHyeon 		 * disabled.
1872bbe2ca75SPyun YongHyeon 		 */
1873bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
1874bbe2ca75SPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM57765)
1875bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1876b4a256acSPyun YongHyeon 	}
18775cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
187895d67482SBill Paul 
187995d67482SBill Paul 	/*
188095d67482SBill Paul 	 * Set up general mode register.
188195d67482SBill Paul 	 */
1882548c8f1aSPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc);
1883548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
1884548c8f1aSPyun YongHyeon 		/* Retain Host-2-BMC settings written by APE firmware. */
1885548c8f1aSPyun YongHyeon 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1886548c8f1aSPyun YongHyeon 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1887548c8f1aSPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1888548c8f1aSPyun YongHyeon 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1889548c8f1aSPyun YongHyeon 	}
1890548c8f1aSPyun YongHyeon 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1891548c8f1aSPyun YongHyeon 	    BGE_MODECTL_TX_NO_PHDR_CSUM;
189295d67482SBill Paul 
189395d67482SBill Paul 	/*
189490447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
189590447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
189690447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
189790447aadSMarius Strobl 	 * certain bridges.
189890447aadSMarius Strobl 	 */
189990447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
190090447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
190150515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
190290447aadSMarius Strobl 
190390447aadSMarius Strobl 	/*
19048cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
19058cb1383cSDoug Ambrisko 	 */
19068cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
190750515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
190850515680SPyun YongHyeon 
190950515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
19108cb1383cSDoug Ambrisko 
19118cb1383cSDoug Ambrisko 	/*
1912ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1913c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1914c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
191595d67482SBill Paul 	 */
1916c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1917c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
191895d67482SBill Paul 
191995d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
19200c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
192195d67482SBill Paul 
192238cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
192338cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
192438cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
192538cc658fSJohn Baldwin 
192638cc658fSJohn Baldwin 		/* Put PHY into ready state */
192738cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
192838cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
192938cc658fSJohn Baldwin 		DELAY(40);
193038cc658fSJohn Baldwin 	}
193138cc658fSJohn Baldwin 
193295d67482SBill Paul 	return (0);
193395d67482SBill Paul }
193495d67482SBill Paul 
193595d67482SBill Paul static int
19363f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
193795d67482SBill Paul {
193895d67482SBill Paul 	struct bge_rcb *rcb;
1939e907febfSPyun YongHyeon 	bus_size_t vrcb;
1940e907febfSPyun YongHyeon 	bge_hostaddr taddr;
1941bbe2ca75SPyun YongHyeon 	uint32_t dmactl, val;
19428a315a6dSPyun YongHyeon 	int i, limit;
194395d67482SBill Paul 
194495d67482SBill Paul 	/*
194595d67482SBill Paul 	 * Initialize the memory window pointer register so that
194695d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
194795d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
194895d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
194995d67482SBill Paul 	 */
195095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
195195d67482SBill Paul 
1952822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1953822f63fcSBill Paul 
19547ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
195595d67482SBill Paul 		/* Configure mbuf memory pool */
19560dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1957822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1958822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1959822f63fcSBill Paul 		else
196095d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
196195d67482SBill Paul 
196295d67482SBill Paul 		/* Configure DMA resource pool */
19630434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
19640434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
196595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
19660434d1b8SBill Paul 	}
196795d67482SBill Paul 
196895d67482SBill Paul 	/* Configure mbuf pool watermarks */
196950515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
19701108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
19711108273aSPyun YongHyeon 		if (sc->bge_ifp->if_mtu > ETHERMTU) {
19721108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
19731108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
19741108273aSPyun YongHyeon 		} else {
19751108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
19761108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
19771108273aSPyun YongHyeon 		}
19781108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
1979fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1980fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1981fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
198238cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
198338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
198438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
198538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
198638cc658fSJohn Baldwin 	} else {
198738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
198838cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
198938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
199038cc658fSJohn Baldwin 	}
199195d67482SBill Paul 
199295d67482SBill Paul 	/* Configure DMA resource watermarks */
199395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
199495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
199595d67482SBill Paul 
199695d67482SBill Paul 	/* Enable buffer manager */
1997bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1998bbe2ca75SPyun YongHyeon 	/*
1999bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
2000bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
2001bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
2002bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
2003bbe2ca75SPyun YongHyeon 	 */
2004bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2005bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
2006bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
200795d67482SBill Paul 
200895d67482SBill Paul 	/* Poll for buffer manager start indication */
200995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2010d5d23857SJung-uk Kim 		DELAY(10);
20110c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
201295d67482SBill Paul 			break;
201395d67482SBill Paul 	}
201495d67482SBill Paul 
201595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
20165a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
201795d67482SBill Paul 		return (ENXIO);
201895d67482SBill Paul 	}
201995d67482SBill Paul 
202095d67482SBill Paul 	/* Enable flow-through queues */
20210c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
202295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
202395d67482SBill Paul 
202495d67482SBill Paul 	/* Wait until queue initialization is complete */
202595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2026d5d23857SJung-uk Kim 		DELAY(10);
202795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
202895d67482SBill Paul 			break;
202995d67482SBill Paul 	}
203095d67482SBill Paul 
203195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2032fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
203395d67482SBill Paul 		return (ENXIO);
203495d67482SBill Paul 	}
203595d67482SBill Paul 
20368a315a6dSPyun YongHyeon 	/*
20378a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
20388a315a6dSPyun YongHyeon 	 *
20398a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
20408a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
20418a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
20428a315a6dSPyun YongHyeon 	 *
20438a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
20448a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
20458a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
20468a315a6dSPyun YongHyeon 	 *   to the controller.
20478a315a6dSPyun YongHyeon 	 *
20488a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
20498a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
20508a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
20518a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
20528a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
20538a315a6dSPyun YongHyeon 	 *   be disabled.
20548a315a6dSPyun YongHyeon 	 *
20558a315a6dSPyun YongHyeon 	 * Receive Return Ring
20568a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
20578a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
20588a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
20598a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
20608a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
20618a315a6dSPyun YongHyeon 	 *
20628a315a6dSPyun YongHyeon 	 * Send Ring
20638a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
20648a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
20658a315a6dSPyun YongHyeon 	 */
20668a315a6dSPyun YongHyeon 
20678a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
2068f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2069f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
2070f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2071f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
2072f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2073f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2074f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
20751108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
20761108273aSPyun YongHyeon 		/*
20771108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
20781108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
20791108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
20801108273aSPyun YongHyeon 		 * Bit 0     : Reserved
20811108273aSPyun YongHyeon 		 */
20821108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
20831108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
20841108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
20858a315a6dSPyun YongHyeon 		/*
20868a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
20878a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
20888a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
20898a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
20908a315a6dSPyun YongHyeon 		 */
20910434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
20928a315a6dSPyun YongHyeon 	} else {
20938a315a6dSPyun YongHyeon 		/*
20948a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
20958a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
20968a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
20978a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
20988a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
20998a315a6dSPyun YongHyeon 		 */
21000434d1b8SBill Paul 		rcb->bge_maxlen_flags =
21010434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
21028a315a6dSPyun YongHyeon 	}
2103bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
210450515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
210550515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21061108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
21071108273aSPyun YongHyeon 	else
210895d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
21098a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
21100c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
21110c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
211267111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
211367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
211495d67482SBill Paul 
21158a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
21168a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
21178a315a6dSPyun YongHyeon 
211895d67482SBill Paul 	/*
21198a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
21208a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
21218a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
212295d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
212395d67482SBill Paul 	 * high enough to require it).
212495d67482SBill Paul 	 */
21254c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2126f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
21278a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
2128f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
2129f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2130f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
2131f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2132f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2133f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2134f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
21351be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
21361be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2137bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
213850515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
213950515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21401108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
21411108273aSPyun YongHyeon 		else
214295d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
214367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
214467111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
214567111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
214667111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
21478a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
21480434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
21490434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
215067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
21518a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
21528a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
21538a315a6dSPyun YongHyeon 	}
215495d67482SBill Paul 
21558a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
21565e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
2157f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
215867111612SJohn Polstra 		rcb->bge_maxlen_flags =
215967111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
21600434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
21610434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
21628a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
21638a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
21640434d1b8SBill Paul 	}
216595d67482SBill Paul 
2166ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2167ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2168427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2169427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2170427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
21718d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
21728d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2173ca4f8986SPyun YongHyeon 	}
217495d67482SBill Paul 	/*
21758a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
21768a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
21778a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
21788a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
21798a315a6dSPyun YongHyeon 	 *
218095d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
218195d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
218295d67482SBill Paul 	 * each ring.
21839ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
21849ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
21859ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
21869ba784dbSScott Long 	 * are reports that it might not need to be so strict.
218738cc658fSJohn Baldwin 	 *
218838cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
218938cc658fSJohn Baldwin 	 * well.
219095d67482SBill Paul 	 */
21915345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
21926f8718a3SScott Long 		val = 8;
21936f8718a3SScott Long 	else
21946f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
21956f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
21962a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
21972a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
21982a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
21991108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
22001108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
22011108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
22021108273aSPyun YongHyeon 	}
220395d67482SBill Paul 
220495d67482SBill Paul 	/*
22058a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
22068a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
22078a315a6dSPyun YongHyeon 	 * located in NIC memory.
220895d67482SBill Paul 	 */
22098a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
22108a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
22118a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
22128a315a6dSPyun YongHyeon 	else
22138a315a6dSPyun YongHyeon 		limit = 1;
2214e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
22158a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2216e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2217e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2218e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2219e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
222095d67482SBill Paul 	}
222195d67482SBill Paul 
22228a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
2223e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2224e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2225e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2226e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2227bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
222850515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
222950515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
22301108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
22311108273aSPyun YongHyeon 	else
2232e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2233e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2234e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2235e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
223695d67482SBill Paul 
22378a315a6dSPyun YongHyeon 	/*
22388a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
22398a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
22408a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
22418a315a6dSPyun YongHyeon 	 */
2242bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
224350515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
224450515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
22451108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
22461108273aSPyun YongHyeon 		limit = 16;
22471108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
22488a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
2249b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2250*fe26ad88SPyun YongHyeon 	    BGE_IS_57765_PLUS(sc))
22518a315a6dSPyun YongHyeon 		limit = 4;
22528a315a6dSPyun YongHyeon 	else
22538a315a6dSPyun YongHyeon 		limit = 1;
22548a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
2255e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
22568a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2257e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2258e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2259e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
22608a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
2261e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
226238cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
22633f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
2264e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
226595d67482SBill Paul 	}
226695d67482SBill Paul 
226795d67482SBill Paul 	/*
22688a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
22698a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
22708a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
227195d67482SBill Paul 	 */
2272e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2273e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2274e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2275e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
22768a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2277e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2278e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
227995d67482SBill Paul 
228095d67482SBill Paul 	/* Set random backoff seed for TX */
228195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
22824a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
22834a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
22844a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
228595d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
228695d67482SBill Paul 
228795d67482SBill Paul 	/* Set inter-packet gap */
228850515680SPyun YongHyeon 	val = 0x2620;
228950515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
229050515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
229150515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
229250515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
229395d67482SBill Paul 
229495d67482SBill Paul 	/*
229595d67482SBill Paul 	 * Specify which ring to use for packets that don't match
229695d67482SBill Paul 	 * any RX rules.
229795d67482SBill Paul 	 */
229895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
229995d67482SBill Paul 
230095d67482SBill Paul 	/*
230195d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
230295d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
230395d67482SBill Paul 	 */
230495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
230595d67482SBill Paul 
230695d67482SBill Paul 	/* Inialize RX list placement stats mask. */
23070c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
230895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
230995d67482SBill Paul 
231095d67482SBill Paul 	/* Disable host coalescing until we get it set up */
231195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
231295d67482SBill Paul 
231395d67482SBill Paul 	/* Poll to make sure it's shut down. */
231495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2315d5d23857SJung-uk Kim 		DELAY(10);
231695d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
231795d67482SBill Paul 			break;
231895d67482SBill Paul 	}
231995d67482SBill Paul 
232095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2321fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2322fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
232395d67482SBill Paul 		return (ENXIO);
232495d67482SBill Paul 	}
232595d67482SBill Paul 
232695d67482SBill Paul 	/* Set up host coalescing defaults */
232795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
232895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
232995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
233095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
23317ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
233295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
233395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
23340434d1b8SBill Paul 	}
2335b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2336b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
233795d67482SBill Paul 
233895d67482SBill Paul 	/* Set up address of statistics block */
23397ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
2340f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2341f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
234295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2343f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
23440434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
234595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
23460434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
23470434d1b8SBill Paul 	}
23480434d1b8SBill Paul 
23490434d1b8SBill Paul 	/* Set up address of status block */
2350f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2351f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
235295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2353f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
235495d67482SBill Paul 
235530f57f61SPyun YongHyeon 	/* Set up status block size. */
235630f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2357864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
235830f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2359864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2360864104feSPyun YongHyeon 	} else {
236130f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2362864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2363864104feSPyun YongHyeon 	}
2364864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2365864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2366864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
236730f57f61SPyun YongHyeon 
236895d67482SBill Paul 	/* Turn on host coalescing state machine */
236930f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
237095d67482SBill Paul 
237195d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
237295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
237395d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
237495d67482SBill Paul 
237595d67482SBill Paul 	/* Turn on RX list placement state machine */
237695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
237795d67482SBill Paul 
237895d67482SBill Paul 	/* Turn on RX list selector state machine. */
23797ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
238095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
238195d67482SBill Paul 
23822246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2383ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2384ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2385ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2386ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2387ea3b4127SPyun YongHyeon 
2388ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2389ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2390ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2391ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2392ea3b4127SPyun YongHyeon 	else
2393ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2394ea3b4127SPyun YongHyeon 
2395548c8f1aSPyun YongHyeon 	/* Allow APE to send/receive frames. */
2396548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2397548c8f1aSPyun YongHyeon 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2398548c8f1aSPyun YongHyeon 
2399ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
24009b80ffe7SPyun YongHyeon 	DELAY(40);
240195d67482SBill Paul 
240295d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
240395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
240495d67482SBill Paul 
240595d67482SBill Paul #ifdef notdef
240695d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
240795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
240895d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
240995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
241095d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
241195d67482SBill Paul #endif
241295d67482SBill Paul 
241395d67482SBill Paul 	/* Turn on DMA completion state machine */
24147ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
241595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
241695d67482SBill Paul 
24176f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
24186f8718a3SScott Long 
24196f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2420a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
24213889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
24226f8718a3SScott Long 
24237aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
24247aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
24257aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
24267aa4b937SPyun YongHyeon 
242795d67482SBill Paul 	/* Turn on write DMA state machine */
24286f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
24294f09c4c7SMarius Strobl 	DELAY(40);
243095d67482SBill Paul 
243195d67482SBill Paul 	/* Turn on read DMA state machine */
24324f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
24331108273aSPyun YongHyeon 
24341108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
24351108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
24361108273aSPyun YongHyeon 
2437a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2438a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2439a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2440a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2441a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2442a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
24434f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
24444f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
24451108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2446ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
24471108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
24481108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
244955a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
245055a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
245155a24a05SPyun YongHyeon 	}
245250515680SPyun YongHyeon 
2453e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
245450515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
245550515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2456e3215f76SPyun YongHyeon 		/*
2457e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2458e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2459e3215f76SPyun YongHyeon 		 */
2460e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2461e3215f76SPyun YongHyeon 	}
246250515680SPyun YongHyeon 
2463d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2464d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2465d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
24661108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
24671108273aSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc)) {
2468bbe2ca75SPyun YongHyeon 		dmactl = CSR_READ_4(sc, BGE_RDMA_RSRVCTRL);
2469bbe2ca75SPyun YongHyeon 		/*
2470bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2471bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2472bbe2ca75SPyun YongHyeon 		 */
2473f7add34cSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
2474f7add34cSPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
2475bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2476bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2477bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2478bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2479bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2480bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2481bbe2ca75SPyun YongHyeon 		}
2482d255f2a9SPyun YongHyeon 		/*
2483d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2484d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2485d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2486d255f2a9SPyun YongHyeon 		 */
2487bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL, dmactl |
2488d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2489d255f2a9SPyun YongHyeon 	}
2490bbe2ca75SPyun YongHyeon 
2491e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2492bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2493bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2494bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2495bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2496e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2497e3215f76SPyun YongHyeon 		/*
2498e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2499e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2500e3215f76SPyun YongHyeon 		 */
2501e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2502e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2503e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2504e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2505bbe2ca75SPyun YongHyeon 	}
2506bbe2ca75SPyun YongHyeon 
25074f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
25084f09c4c7SMarius Strobl 	DELAY(40);
250995d67482SBill Paul 
251095d67482SBill Paul 	/* Turn on RX data completion state machine */
251195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
251295d67482SBill Paul 
251395d67482SBill Paul 	/* Turn on RX BD initiator state machine */
251495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
251595d67482SBill Paul 
251695d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
251795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
251895d67482SBill Paul 
251995d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25207ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
252195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
252295d67482SBill Paul 
252395d67482SBill Paul 	/* Turn on send BD completion state machine */
252495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
252595d67482SBill Paul 
252695d67482SBill Paul 	/* Turn on send data completion state machine */
2527a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2528a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2529a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2530a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
253195d67482SBill Paul 
253295d67482SBill Paul 	/* Turn on send data initiator state machine */
25331108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
25341108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
25351108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2536ca3f1187SPyun YongHyeon 	else
253795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
253895d67482SBill Paul 
253995d67482SBill Paul 	/* Turn on send BD initiator state machine */
254095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
254195d67482SBill Paul 
254295d67482SBill Paul 	/* Turn on send BD selector state machine */
254395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
254495d67482SBill Paul 
25450c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
254695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
254795d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
254895d67482SBill Paul 
254995d67482SBill Paul 	/* ack/clear link change events */
255095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
25510434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
25520434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2553f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
255495d67482SBill Paul 
25556ede2cfaSPyun YongHyeon 	/*
25566ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
25576ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
25586ede2cfaSPyun YongHyeon 	 */
2559652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
256095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2561a1d52896SBill Paul 	} else {
25627ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
25637ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
25647ed3f0f0SPyun YongHyeon 			DELAY(80);
25657ed3f0f0SPyun YongHyeon 		}
25661f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
25674c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2568a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2569a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2570a1d52896SBill Paul 	}
257195d67482SBill Paul 
25721f313773SOleg Bulyzhin 	/*
25731f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
25741f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
25751f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
25761f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
25771f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
25781f313773SOleg Bulyzhin 	 */
25791f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
25801f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
25811f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
25821f313773SOleg Bulyzhin 
258395d67482SBill Paul 	/* Enable link state change attentions. */
258495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
258595d67482SBill Paul 
258695d67482SBill Paul 	return (0);
258795d67482SBill Paul }
258895d67482SBill Paul 
25894c0da0ffSGleb Smirnoff const struct bge_revision *
25904c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
25914c0da0ffSGleb Smirnoff {
25924c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
25934c0da0ffSGleb Smirnoff 
25944c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
25954c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
25964c0da0ffSGleb Smirnoff 			return (br);
25974c0da0ffSGleb Smirnoff 	}
25984c0da0ffSGleb Smirnoff 
25994c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
26004c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
26014c0da0ffSGleb Smirnoff 			return (br);
26024c0da0ffSGleb Smirnoff 	}
26034c0da0ffSGleb Smirnoff 
26044c0da0ffSGleb Smirnoff 	return (NULL);
26054c0da0ffSGleb Smirnoff }
26064c0da0ffSGleb Smirnoff 
26074c0da0ffSGleb Smirnoff const struct bge_vendor *
26084c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26094c0da0ffSGleb Smirnoff {
26104c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26114c0da0ffSGleb Smirnoff 
26124c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26134c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26144c0da0ffSGleb Smirnoff 			return (v);
26154c0da0ffSGleb Smirnoff 
26164c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
26174c0da0ffSGleb Smirnoff 	return (NULL);
26184c0da0ffSGleb Smirnoff }
26194c0da0ffSGleb Smirnoff 
262095d67482SBill Paul /*
262195d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
26224c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
26234c0da0ffSGleb Smirnoff  *
26244c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
26257c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
26267c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
26277c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
26287c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
262995d67482SBill Paul  */
263095d67482SBill Paul static int
26313f74909aSGleb Smirnoff bge_probe(device_t dev)
263295d67482SBill Paul {
2633978f2704SMarius Strobl 	char buf[96];
2634978f2704SMarius Strobl 	char model[64];
2635978f2704SMarius Strobl 	const struct bge_revision *br;
2636978f2704SMarius Strobl 	const char *pname;
26374c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
2638978f2704SMarius Strobl 	const struct bge_type *t = bge_devs;
2639978f2704SMarius Strobl 	const struct bge_vendor *v;
2640978f2704SMarius Strobl 	uint32_t id;
2641978f2704SMarius Strobl 	uint16_t did, vid;
264295d67482SBill Paul 
264395d67482SBill Paul 	sc->bge_dev = dev;
26447c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
26457c929cf9SJung-uk Kim 	did = pci_get_device(dev);
26464c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
26477c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2648a5779553SStanislav Sedov 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2649a5779553SStanislav Sedov 			    BGE_PCIMISCCTL_ASICREV_SHIFT;
26501108273aSPyun YongHyeon 			if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
26511108273aSPyun YongHyeon 				/*
26521108273aSPyun YongHyeon 				 * Find the ASCI revision.  Different chips
26531108273aSPyun YongHyeon 				 * use different registers.
26541108273aSPyun YongHyeon 				 */
26551108273aSPyun YongHyeon 				switch (pci_get_device(dev)) {
26561108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5717:
26571108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5718:
2658bbe2ca75SPyun YongHyeon 				case BCOM_DEVICEID_BCM5719:
265950515680SPyun YongHyeon 				case BCOM_DEVICEID_BCM5720:
26601108273aSPyun YongHyeon 					id = pci_read_config(dev,
26611108273aSPyun YongHyeon 					    BGE_PCI_GEN2_PRODID_ASICREV, 4);
26621108273aSPyun YongHyeon 					break;
2663b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57761:
2664*fe26ad88SPyun YongHyeon 				case BCOM_DEVICEID_BCM57762:
2665b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57765:
2666*fe26ad88SPyun YongHyeon 				case BCOM_DEVICEID_BCM57766:
2667b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57781:
2668b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57785:
2669b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57791:
2670b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57795:
2671b4a256acSPyun YongHyeon 					id = pci_read_config(dev,
2672b4a256acSPyun YongHyeon 					    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2673b4a256acSPyun YongHyeon 					break;
26741108273aSPyun YongHyeon 				default:
2675a5779553SStanislav Sedov 					id = pci_read_config(dev,
2676a5779553SStanislav Sedov 					    BGE_PCI_PRODID_ASICREV, 4);
26771108273aSPyun YongHyeon 				}
26781108273aSPyun YongHyeon 			}
26794c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
26807c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
2681852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2682852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
26834e35d186SJung-uk Kim 				snprintf(model, 64, "%s", pname);
26844e35d186SJung-uk Kim 			else
2685978f2704SMarius Strobl 				snprintf(model, 64, "%s %s", v->v_name,
26867c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
26877c929cf9SJung-uk Kim 				    "NetXtreme Ethernet Controller");
2688a5779553SStanislav Sedov 			snprintf(buf, 96, "%s, %sASIC rev. %#08x", model,
2689a5779553SStanislav Sedov 			    br != NULL ? "" : "unknown ", id);
26904c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
269195d67482SBill Paul 			return (0);
269295d67482SBill Paul 		}
269395d67482SBill Paul 		t++;
269495d67482SBill Paul 	}
269595d67482SBill Paul 
269695d67482SBill Paul 	return (ENXIO);
269795d67482SBill Paul }
269895d67482SBill Paul 
2699f41ac2beSBill Paul static void
27003f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2701f41ac2beSBill Paul {
2702f41ac2beSBill Paul 	int i;
2703f41ac2beSBill Paul 
27043f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2705f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2706f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
27070ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2708f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2709f41ac2beSBill Paul 	}
2710943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2711943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2712943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2713f41ac2beSBill Paul 
27143f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2715f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2716f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2717f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2718f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2719f41ac2beSBill Paul 	}
2720943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2721943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2722943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2723f41ac2beSBill Paul 
27243f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2725f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2726f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
27270ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2728f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2729f41ac2beSBill Paul 	}
2730f41ac2beSBill Paul 
27310ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
27320ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2733c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2734c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
27350ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
27360ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2737f41ac2beSBill Paul 
27383f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2739e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2740e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2741e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2742e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2743f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2744f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2745f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2746f41ac2beSBill Paul 
2747f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2748f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2749f41ac2beSBill Paul 
27503f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2751e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2752e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2753e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2754e65bed95SPyun YongHyeon 
2755e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2756e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2757f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2758f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2759f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2760f41ac2beSBill Paul 
2761f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2762f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2763f41ac2beSBill Paul 
27643f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2765e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2766e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2767e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2768e65bed95SPyun YongHyeon 
2769e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2770e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2771f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2772f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2773f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2774f41ac2beSBill Paul 
2775f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2776f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2777f41ac2beSBill Paul 
27783f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2779e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2780e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2781e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2782e65bed95SPyun YongHyeon 
2783e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2784f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2785f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2786f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2787f41ac2beSBill Paul 
2788f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2789f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2790f41ac2beSBill Paul 
27913f74909aSGleb Smirnoff 	/* Destroy status block. */
2792e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2793e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2794e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2795e65bed95SPyun YongHyeon 
2796e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2797f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2798f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2799f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2800f41ac2beSBill Paul 
2801f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2802f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2803f41ac2beSBill Paul 
28043f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2805e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2806e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2807e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2808e65bed95SPyun YongHyeon 
2809e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2810f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2811f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2812f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2813f41ac2beSBill Paul 
2814f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2815f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2816f41ac2beSBill Paul 
28175b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
28185b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
28195b610048SPyun YongHyeon 
28203f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2821f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2822f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2823f41ac2beSBill Paul }
2824f41ac2beSBill Paul 
2825f41ac2beSBill Paul static int
28265b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
28275b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
28285b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2829f41ac2beSBill Paul {
28303f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
28315b610048SPyun YongHyeon 	int error;
2832f41ac2beSBill Paul 
28335b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2834fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
28355b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
28365b610048SPyun YongHyeon 	if (error != 0) {
28375b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28385b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
28395b610048SPyun YongHyeon 		return (ENOMEM);
28405b610048SPyun YongHyeon 	}
28415b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
28425b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
28435b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
28445b610048SPyun YongHyeon 	if (error != 0) {
28455b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28465b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
28475b610048SPyun YongHyeon 		return (ENOMEM);
28485b610048SPyun YongHyeon 	}
28495b610048SPyun YongHyeon 	/* Load the address of the ring. */
28505b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
28515b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
28525b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
28535b610048SPyun YongHyeon 	if (error != 0) {
28545b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28555b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
28565b610048SPyun YongHyeon 		return (ENOMEM);
28575b610048SPyun YongHyeon 	}
28585b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
28595b610048SPyun YongHyeon 	return (0);
28605b610048SPyun YongHyeon }
28615b610048SPyun YongHyeon 
28625b610048SPyun YongHyeon static int
28635b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
28645b610048SPyun YongHyeon {
28655b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2866fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
28675b610048SPyun YongHyeon 	int i, error;
2868f41ac2beSBill Paul 
2869f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2870f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2871f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2872f41ac2beSBill Paul 	/*
2873f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2874f41ac2beSBill Paul 	 */
28754eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2876f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
28774eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
28784eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2879e65bed95SPyun YongHyeon 	if (error != 0) {
2880fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2881fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2882e65bed95SPyun YongHyeon 		return (ENOMEM);
2883e65bed95SPyun YongHyeon 	}
2884e65bed95SPyun YongHyeon 
28855b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
28865b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
28875b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
28885b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
28895b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
28905b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
28915b610048SPyun YongHyeon 	if (error)
28925b610048SPyun YongHyeon 		return (error);
28935b610048SPyun YongHyeon 
28945b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
28955b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
28965b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
28975b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
28985b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
28995b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
29005b610048SPyun YongHyeon 	if (error)
29015b610048SPyun YongHyeon 		return (error);
29025b610048SPyun YongHyeon 
29035b610048SPyun YongHyeon 	/* Create tag for TX ring. */
29045b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
29055b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
29065b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
29075b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
29085b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
29095b610048SPyun YongHyeon 	if (error)
29105b610048SPyun YongHyeon 		return (error);
29115b610048SPyun YongHyeon 
2912f41ac2beSBill Paul 	/*
29135b610048SPyun YongHyeon 	 * Create tag for status block.
29145b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
29155b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
29165b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
29175b610048SPyun YongHyeon 	 * of configured number of ring.
2918f41ac2beSBill Paul 	 */
29195b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
29205b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
29215b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
29225b610048SPyun YongHyeon 	else
29235b610048SPyun YongHyeon 		sbsz = 32;
29245b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
29255b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
29265b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
29275b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
29285b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
29295b610048SPyun YongHyeon 	if (error)
29305b610048SPyun YongHyeon 		return (error);
29315b610048SPyun YongHyeon 
293212c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
293312c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
293412c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
293512c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
293612c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
293712c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
293812c65daeSPyun YongHyeon 	if (error)
293912c65daeSPyun YongHyeon 		return (error);
294012c65daeSPyun YongHyeon 
29415b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
29425b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
29435b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
29445b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
29455b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
29465b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
29475b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
29485b610048SPyun YongHyeon 		if (error)
29495b610048SPyun YongHyeon 			return (error);
29505b610048SPyun YongHyeon 	}
29515b610048SPyun YongHyeon 
29525b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
2953d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
2954d2ffe15aSPyun YongHyeon 		/*
2955d2ffe15aSPyun YongHyeon 		 * XXX
2956d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
2957d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
2958062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
2959062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
2960d2ffe15aSPyun YongHyeon 		 */
2961062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
2962d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
2963d2ffe15aSPyun YongHyeon 	}
2964fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
2965fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
2966fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
2967fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
29685b610048SPyun YongHyeon 	if (error != 0) {
29695b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29705b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
29715b610048SPyun YongHyeon 		return (ENOMEM);
29725b610048SPyun YongHyeon 	}
29735b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
29741108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2975ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
2976ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
2977ca3f1187SPyun YongHyeon 	} else {
2978ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
2979ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
2980ca3f1187SPyun YongHyeon 	}
29815b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
2982ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2983ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
2984ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
2985f41ac2beSBill Paul 
2986f41ac2beSBill Paul 	if (error) {
29870ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
29880ac56796SPyun YongHyeon 		return (ENOMEM);
29890ac56796SPyun YongHyeon 	}
29900ac56796SPyun YongHyeon 
29915b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
2992f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
2993f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
2994f5459d4cSPyun YongHyeon 	else
2995f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
29965b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
2997f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
2998f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
29990ac56796SPyun YongHyeon 
30000ac56796SPyun YongHyeon 	if (error) {
30010ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3002f41ac2beSBill Paul 		return (ENOMEM);
3003f41ac2beSBill Paul 	}
3004f41ac2beSBill Paul 
30053f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3006943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3007943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3008943787f3SPyun YongHyeon 	if (error) {
3009943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3010943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3011943787f3SPyun YongHyeon 		return (ENOMEM);
3012943787f3SPyun YongHyeon 	}
3013f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
30140ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3015f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3016f41ac2beSBill Paul 		if (error) {
3017fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3018fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3019f41ac2beSBill Paul 			return (ENOMEM);
3020f41ac2beSBill Paul 		}
3021f41ac2beSBill Paul 	}
3022f41ac2beSBill Paul 
30233f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3024f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
30250ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3026f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3027f41ac2beSBill Paul 		if (error) {
3028fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30290ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3030f41ac2beSBill Paul 			return (ENOMEM);
3031f41ac2beSBill Paul 		}
3032f41ac2beSBill Paul 	}
3033f41ac2beSBill Paul 
30345b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
30354c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30365b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
30378a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
30381be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
30391be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3040f41ac2beSBill Paul 		if (error) {
3041fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30423f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3043f41ac2beSBill Paul 			return (ENOMEM);
3044f41ac2beSBill Paul 		}
30453f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3046943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3047943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3048943787f3SPyun YongHyeon 		if (error) {
3049943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
30501b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3051943787f3SPyun YongHyeon 			return (ENOMEM);
3052943787f3SPyun YongHyeon 		}
3053f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3054f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3055f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3056f41ac2beSBill Paul 			if (error) {
3057fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
30583f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3059f41ac2beSBill Paul 				return (ENOMEM);
3060f41ac2beSBill Paul 			}
3061f41ac2beSBill Paul 		}
3062f41ac2beSBill Paul 	}
3063f41ac2beSBill Paul 
3064f41ac2beSBill Paul 	return (0);
3065f41ac2beSBill Paul }
3066f41ac2beSBill Paul 
3067bf6ef57aSJohn Polstra /*
3068bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3069bf6ef57aSJohn Polstra  */
3070bf6ef57aSJohn Polstra static int
3071bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3072bf6ef57aSJohn Polstra {
3073bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
307455aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3075bf6ef57aSJohn Polstra 
307655aaf894SMarius Strobl 	d = pci_get_domain(dev);
3077bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3078bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3079bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3080bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
308155aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3082bf6ef57aSJohn Polstra 			return (1);
3083bf6ef57aSJohn Polstra 	return (0);
3084bf6ef57aSJohn Polstra }
3085bf6ef57aSJohn Polstra 
3086bf6ef57aSJohn Polstra /*
3087bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3088bf6ef57aSJohn Polstra  */
3089bf6ef57aSJohn Polstra static int
3090bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3091bf6ef57aSJohn Polstra {
3092bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3093bf6ef57aSJohn Polstra 
3094d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
30955c952e8dSPyun YongHyeon 		return (0);
30965c952e8dSPyun YongHyeon 
30971108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
30981108273aSPyun YongHyeon #ifdef DEVICE_POLLING
30991108273aSPyun YongHyeon 	return (0);
31001108273aSPyun YongHyeon #endif
3101bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3102a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3103bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3104bf6ef57aSJohn Polstra 		/*
3105a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3106a8376f70SMarius Strobl 		 * configured in single-port mode.
3107bf6ef57aSJohn Polstra 		 */
3108bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3109bf6ef57aSJohn Polstra 			can_use_msi = 1;
3110bf6ef57aSJohn Polstra 		break;
3111bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3112bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3113bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3114bf6ef57aSJohn Polstra 			can_use_msi = 1;
3115bf6ef57aSJohn Polstra 		break;
3116a8376f70SMarius Strobl 	default:
3117a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3118bf6ef57aSJohn Polstra 			can_use_msi = 1;
3119bf6ef57aSJohn Polstra 	}
3120bf6ef57aSJohn Polstra 	return (can_use_msi);
3121bf6ef57aSJohn Polstra }
3122bf6ef57aSJohn Polstra 
312395d67482SBill Paul static int
3124062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3125062af0b0SPyun YongHyeon {
3126062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3127062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3128062af0b0SPyun YongHyeon 		const uint16_t vendor;
3129062af0b0SPyun YongHyeon 		const uint16_t device;
3130062af0b0SPyun YongHyeon 		const char *desc;
313129658c96SDimitry Andric 	} mbox_reorder_lists[] = {
3132062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3133062af0b0SPyun YongHyeon 	};
3134062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3135062af0b0SPyun YongHyeon 	device_t bus, dev;
313647f4a4dcSMarius Strobl 	int i;
3137062af0b0SPyun YongHyeon 
3138062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3139062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3140062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3141062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3142062af0b0SPyun YongHyeon 	for (;;) {
3143062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3144062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3145062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3146062af0b0SPyun YongHyeon 			break;
314747f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3148062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3149062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3150062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3151062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3152062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3153062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3154062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3155062af0b0SPyun YongHyeon 				return (1);
3156062af0b0SPyun YongHyeon 			}
3157062af0b0SPyun YongHyeon 		}
3158062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
3159062af0b0SPyun YongHyeon 			break;
3160062af0b0SPyun YongHyeon 	}
3161062af0b0SPyun YongHyeon 	return (0);
3162062af0b0SPyun YongHyeon }
3163062af0b0SPyun YongHyeon 
3164ea9c3a30SPyun YongHyeon static void
3165ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3166ea9c3a30SPyun YongHyeon {
3167ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3168ea9c3a30SPyun YongHyeon 
3169ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3170ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3171ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3172ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3173ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3174ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3175ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3176ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3177ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3178ea9c3a30SPyun YongHyeon 			clk = 133;
3179ea9c3a30SPyun YongHyeon 		else {
3180ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3181ea9c3a30SPyun YongHyeon 			switch (clk) {
3182ea9c3a30SPyun YongHyeon 			case 0:
3183ea9c3a30SPyun YongHyeon 				clk = 33;
3184ea9c3a30SPyun YongHyeon 				break;
3185ea9c3a30SPyun YongHyeon 			case 2:
3186ea9c3a30SPyun YongHyeon 				clk = 50;
3187ea9c3a30SPyun YongHyeon 				break;
3188ea9c3a30SPyun YongHyeon 			case 4:
3189ea9c3a30SPyun YongHyeon 				clk = 66;
3190ea9c3a30SPyun YongHyeon 				break;
3191ea9c3a30SPyun YongHyeon 			case 6:
3192ea9c3a30SPyun YongHyeon 				clk = 100;
3193ea9c3a30SPyun YongHyeon 				break;
3194ea9c3a30SPyun YongHyeon 			case 7:
3195ea9c3a30SPyun YongHyeon 				clk = 133;
3196ea9c3a30SPyun YongHyeon 				break;
3197ea9c3a30SPyun YongHyeon 			}
3198ea9c3a30SPyun YongHyeon 		}
3199ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3200ea9c3a30SPyun YongHyeon 	} else {
3201ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3202ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3203ea9c3a30SPyun YongHyeon 		else
3204ea9c3a30SPyun YongHyeon 			printf("PCI ");
3205ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3206ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3207ea9c3a30SPyun YongHyeon 			clk = 66;
3208ea9c3a30SPyun YongHyeon 		else
3209ea9c3a30SPyun YongHyeon 			clk = 33;
3210ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3211ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3212ea9c3a30SPyun YongHyeon 		else
3213ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3214ea9c3a30SPyun YongHyeon 	}
3215ea9c3a30SPyun YongHyeon }
3216ea9c3a30SPyun YongHyeon 
3217062af0b0SPyun YongHyeon static int
32183f74909aSGleb Smirnoff bge_attach(device_t dev)
321995d67482SBill Paul {
322095d67482SBill Paul 	struct ifnet *ifp;
322195d67482SBill Paul 	struct bge_softc *sc;
3222548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
322308013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3224daeeb75cSPyun YongHyeon 	int capmask, error, msicount, reg, rid, trys;
322595d67482SBill Paul 
322695d67482SBill Paul 	sc = device_get_softc(dev);
322795d67482SBill Paul 	sc->bge_dev = dev;
322895d67482SBill Paul 
3229e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3230dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3231e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3232dfe0df9aSPyun YongHyeon 
323395d67482SBill Paul 	/*
323495d67482SBill Paul 	 * Map control/status registers.
323595d67482SBill Paul 	 */
323695d67482SBill Paul 	pci_enable_busmaster(dev);
323795d67482SBill Paul 
3238736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
32395f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
324044f8f2fcSMarius Strobl 	    RF_ACTIVE);
324195d67482SBill Paul 
324295d67482SBill Paul 	if (sc->bge_res == NULL) {
3243548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
324495d67482SBill Paul 		error = ENXIO;
324595d67482SBill Paul 		goto fail;
324695d67482SBill Paul 	}
324795d67482SBill Paul 
32484f09c4c7SMarius Strobl 	/* Save various chip information. */
3249548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3250e53d81eeSPaul Saab 	sc->bge_chipid =
3251a5779553SStanislav Sedov 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
3252a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
32531108273aSPyun YongHyeon 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG) {
32541108273aSPyun YongHyeon 		/*
32551108273aSPyun YongHyeon 		 * Find the ASCI revision.  Different chips use different
32561108273aSPyun YongHyeon 		 * registers.
32571108273aSPyun YongHyeon 		 */
32581108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
32591108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
32601108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
3261bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
326250515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
32631108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
32641108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
32651108273aSPyun YongHyeon 			break;
3266b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
3267*fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57762:
3268b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
3269*fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57766:
3270b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
3271b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
3272b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
3273b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
3274b4a256acSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
3275b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
3276b4a256acSPyun YongHyeon 			break;
32771108273aSPyun YongHyeon 		default:
32781108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
32791108273aSPyun YongHyeon 			    BGE_PCI_PRODID_ASICREV, 4);
32801108273aSPyun YongHyeon 		}
32811108273aSPyun YongHyeon 	}
3282e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3283e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3284e53d81eeSPaul Saab 
3285a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3286daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
32871108273aSPyun YongHyeon 	 /*
32881108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
32891108273aSPyun YongHyeon 	  *
32901108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
32911108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
32921108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
32931108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
32941108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3295bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
329650515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
32971108273aSPyun YongHyeon 	  *
3298548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3299548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3300548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3301548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3302548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3303548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3304548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3305548c8f1aSPyun YongHyeon 	  *
33061108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
33071108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
33081108273aSPyun YongHyeon 	  */
3309bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
331050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
331150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3312548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
33131108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
33141108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3315daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33161108273aSPyun YongHyeon 			else
3317daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3318bbe2ca75SPyun YongHyeon 		} else {
33191108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33201108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3321daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33221108273aSPyun YongHyeon 			else
3323daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
33241108273aSPyun YongHyeon 		}
33251108273aSPyun YongHyeon 	}
3326a813ed78SPyun YongHyeon 
33275fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
33285fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
332908013fd3SMarius Strobl 
33300dae9719SJung-uk Kim 	/* Save chipset family. */
33310dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
3332*fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57765:
3333*fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57766:
3334*fe26ad88SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_57765_PLUS;
3335*fe26ad88SPyun YongHyeon 		/* FALLTHROUGH */
33361108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3337bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
333850515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
33391108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
33401108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3341b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
3342bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3343bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3344bbe2ca75SPyun YongHyeon 			/* Jumbo frame on BCM5719 A0 does not work. */
3345463a7e27SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_JUMBO;
3346bbe2ca75SPyun YongHyeon 		}
33471108273aSPyun YongHyeon 		break;
3348a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3349a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3350a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3351a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3352a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3353a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3354a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3355a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3356a5779553SStanislav Sedov 		break;
33570dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
33580dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
33590dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
33600dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
33617ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
33620dae9719SJung-uk Kim 		break;
33630dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
33640dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
33650dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3366f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
33679fe569d8SXin LI 		/* FALLTHROUGH */
33680dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
33690dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
337038cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
33710dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
33729fe569d8SXin LI 		/* FALLTHROUGH */
33730dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
33740dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
33750dae9719SJung-uk Kim 		break;
33760dae9719SJung-uk Kim 	}
33770dae9719SJung-uk Kim 
3378548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3379548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3380548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3381548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3382548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3383548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
3384548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3385548c8f1aSPyun YongHyeon 		break;
3386548c8f1aSPyun YongHyeon 	}
3387548c8f1aSPyun YongHyeon 
3388548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3389548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3390548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3391548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3392548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3393548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3394548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3395548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3396548c8f1aSPyun YongHyeon 			error = ENXIO;
3397548c8f1aSPyun YongHyeon 			goto fail;
3398548c8f1aSPyun YongHyeon 		}
3399548c8f1aSPyun YongHyeon 
3400548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3401548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3402548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3403548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3404548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3405548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3406548c8f1aSPyun YongHyeon 
3407548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3408548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3409548c8f1aSPyun YongHyeon 	}
3410548c8f1aSPyun YongHyeon 
3411749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3412749a5269SMarius Strobl 	bge_add_sysctls(sc);
3413749a5269SMarius Strobl 
3414a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
34151108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
34161108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3417a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3418a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3419a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3420a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3421a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3422a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3423a813ed78SPyun YongHyeon 	else
3424a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
34257ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
34267ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
34277ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3428a813ed78SPyun YongHyeon 
3429f681b29aSPyun YongHyeon 	/*
3430d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3431f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3432f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3433f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3434f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3435f681b29aSPyun YongHyeon 	 */
3436f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
34374f0794ffSBjoern A. Zeeb 
3438d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3439d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3440d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3441d9820cd8SPyun YongHyeon 
3442a7fcfcf3SPyun YongHyeon 	/*
3443a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3444a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3445a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3446a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3447a7fcfcf3SPyun YongHyeon 	 */
3448a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3449a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3450a7fcfcf3SPyun YongHyeon 
3451ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3452fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
34534f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
34544f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
34554f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
345684ac96f8SPyun YongHyeon 	}
34574f0794ffSBjoern A. Zeeb 
3458fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3459fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3460fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3461fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3462fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3463fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3464fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3465fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3466fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3467fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3468fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3469fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3470fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3471fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3472fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3473fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3474d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3475fb772a6cSMarius Strobl 	}
3476fb772a6cSMarius Strobl 
3477e53d81eeSPaul Saab 	/*
3478ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3479ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3480ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3481ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3482ca3f1187SPyun YongHyeon 	 * known bug which can't handle TSO if ethernet header + IP/TCP
3483ca3f1187SPyun YongHyeon 	 * header is greater than 80 bytes. The workaround for the TSO
3484ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3485ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3486ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3487ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3488ca3f1187SPyun YongHyeon 	 */
34891108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
34901108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
34911108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3492bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3493bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3494bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3495bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3496bbe2ca75SPyun YongHyeon 		}
34971108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
34984f4a16e1SPyun YongHyeon 		/*
34994f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
35004f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3501be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
35024f4a16e1SPyun YongHyeon 		 */
35034f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3504be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3505be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3506ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
35074f4a16e1SPyun YongHyeon 	}
3508ca3f1187SPyun YongHyeon 
3509ca3f1187SPyun YongHyeon 	/*
35106f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3511e53d81eeSPaul Saab 	 */
35123b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
35134c0da0ffSGleb Smirnoff 		/*
35146f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
35156f8718a3SScott Long 		 * must be a PCI Express device.
35166f8718a3SScott Long 		 */
35176f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
35180aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
351948630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
352048630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
352148630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
352248630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
352350515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
352450515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
352548630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
352648630d79SPyun YongHyeon 		else
352748630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
352848630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
35296f8718a3SScott Long 	} else {
35306f8718a3SScott Long 		/*
35316f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
35326f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
35334c0da0ffSGleb Smirnoff 		 */
35343b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
35350aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
353690447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
35374c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3538652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
35396f8718a3SScott Long 	}
35404c0da0ffSGleb Smirnoff 
3541bf6ef57aSJohn Polstra 	/*
3542fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3543fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3544fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3545fd4d32feSPyun YongHyeon 	 */
3546fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3547fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3548fd4d32feSPyun YongHyeon 	/*
3549062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3550062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3551062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3552062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3553062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3554062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3555062af0b0SPyun YongHyeon 	 */
3556062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3557062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3558062af0b0SPyun YongHyeon 	/*
3559bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3560bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3561bf6ef57aSJohn Polstra 	 * normal operation.
3562bf6ef57aSJohn Polstra 	 */
35630aaf1057SPyun YongHyeon 	rid = 0;
35643b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
35650aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3566bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
3567bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
3568bf6ef57aSJohn Polstra 			if (msicount > 1)
3569bf6ef57aSJohn Polstra 				msicount = 1;
3570bf6ef57aSJohn Polstra 		} else
3571bf6ef57aSJohn Polstra 			msicount = 0;
3572bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
3573bf6ef57aSJohn Polstra 			rid = 1;
3574bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
35750aaf1057SPyun YongHyeon 		}
35760aaf1057SPyun YongHyeon 	}
3577bf6ef57aSJohn Polstra 
35781108273aSPyun YongHyeon 	/*
35791108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
35801108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
35811108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
35821108273aSPyun YongHyeon 	 */
35831108273aSPyun YongHyeon #ifndef DEVICE_POLLING
35841108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
35851108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
35861108273aSPyun YongHyeon #endif
35871108273aSPyun YongHyeon 
3588bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3589bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
3590bf6ef57aSJohn Polstra 
3591bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3592bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3593bf6ef57aSJohn Polstra 		error = ENXIO;
3594bf6ef57aSJohn Polstra 		goto fail;
3595bf6ef57aSJohn Polstra 	}
3596bf6ef57aSJohn Polstra 
3597ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
35984f09c4c7SMarius Strobl 
35998cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3600548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3601548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3602888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3603888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3604548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3605548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
36068cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
36078cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3608d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
36098cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
36108cb1383cSDoug Ambrisko 			}
36118cb1383cSDoug Ambrisko 		}
3612548c8f1aSPyun YongHyeon 	}
36138cb1383cSDoug Ambrisko 
36148cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
3615548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_START);
36168cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
36178cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
36188cb1383cSDoug Ambrisko 		error = ENXIO;
36198cb1383cSDoug Ambrisko 		goto fail;
36208cb1383cSDoug Ambrisko 	}
36218cb1383cSDoug Ambrisko 
3622548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_START);
3623548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_START);
362495d67482SBill Paul 
362595d67482SBill Paul 	if (bge_chipinit(sc)) {
3626fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
362795d67482SBill Paul 		error = ENXIO;
362895d67482SBill Paul 		goto fail;
362995d67482SBill Paul 	}
363095d67482SBill Paul 
363138cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
363238cc658fSJohn Baldwin 	if (error) {
363308013fd3SMarius Strobl 		device_printf(sc->bge_dev,
363408013fd3SMarius Strobl 		    "failed to read station address\n");
363595d67482SBill Paul 		error = ENXIO;
363695d67482SBill Paul 		goto fail;
363795d67482SBill Paul 	}
363895d67482SBill Paul 
3639f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
36401108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
36411108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
36421108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3643f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3644f41ac2beSBill Paul 	else
3645f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3646f41ac2beSBill Paul 
36475b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3648fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3649fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3650f41ac2beSBill Paul 		error = ENXIO;
3651f41ac2beSBill Paul 		goto fail;
3652f41ac2beSBill Paul 	}
3653f41ac2beSBill Paul 
365495d67482SBill Paul 	/* Set default tuneable values. */
365595d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
365695d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
365795d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
36586f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
36596f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
366095d67482SBill Paul 
366135f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
366235f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
366335f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
366435f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
366535f945cdSPyun YongHyeon 
366695d67482SBill Paul 	/* Set up ifnet structure */
3667fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3668fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3669fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3670fc74a9f9SBrooks Davis 		error = ENXIO;
3671fc74a9f9SBrooks Davis 		goto fail;
3672fc74a9f9SBrooks Davis 	}
367395d67482SBill Paul 	ifp->if_softc = sc;
36749bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
367595d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
367695d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
367795d67482SBill Paul 	ifp->if_start = bge_start;
367895d67482SBill Paul 	ifp->if_init = bge_init;
36794d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
36804d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
36814d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
368235f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
3683d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
36844e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
36851108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3686ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
368704bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
3688ca3f1187SPyun YongHyeon 	}
36894e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
36904e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
36914e35d186SJung-uk Kim #endif
369295d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
369375719184SGleb Smirnoff #ifdef DEVICE_POLLING
369475719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
369575719184SGleb Smirnoff #endif
369695d67482SBill Paul 
3697a1d52896SBill Paul 	/*
3698d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3699d375e524SGleb Smirnoff 	 * to hardware bugs.
3700d375e524SGleb Smirnoff 	 */
3701d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3702d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
37034d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
3704d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
3705d375e524SGleb Smirnoff 	}
3706d375e524SGleb Smirnoff 
3707d375e524SGleb Smirnoff 	/*
3708a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
370941abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
371041abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
371141abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
371241abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
371341abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
371441abcc1bSPaul Saab 	 * SK-9D41.
3715a1d52896SBill Paul 	 */
3716888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3717888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
37185fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
37195fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3720f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3721f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3722fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3723f6789fbaSPyun YongHyeon 			error = ENXIO;
3724f6789fbaSPyun YongHyeon 			goto fail;
3725f6789fbaSPyun YongHyeon 		}
372641abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
372741abcc1bSPaul Saab 	}
372841abcc1bSPaul Saab 
372995d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3730ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3731ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
373270c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3733ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
373470c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
373570c2071bSPyun YongHyeon 		} else
3736652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3737ea3b4127SPyun YongHyeon 	}
373895d67482SBill Paul 
373970c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
374070c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
374170c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
374270c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
374370c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
374470c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
374570c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
374670c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
374770c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
374870c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
374970c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
375070c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
375170c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
375270c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3753*fe26ad88SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3754*fe26ad88SPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc)) {
375570c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
375670c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
375770c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
375870c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
375970c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
376070c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
376170c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
376270c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
376370c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
376470c2071bSPyun YongHyeon 		} else
376570c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
376670c2071bSPyun YongHyeon 	}
376770c2071bSPyun YongHyeon 
376870c2071bSPyun YongHyeon 	/*
3769d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
377070c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
377170c2071bSPyun YongHyeon 	 */
377270c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
377370c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
377470c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3775d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
377670c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
377770c2071bSPyun YongHyeon 
3778652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
37790c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
37800c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
37810c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
37826098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
37836098821cSJung-uk Kim 		    0, NULL);
378495d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
378595d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3786da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
378795d67482SBill Paul 	} else {
378895d67482SBill Paul 		/*
37898cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
37908cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
37918cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
37928cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
37938cb1383cSDoug Ambrisko 		 * the PHY.
379495d67482SBill Paul 		 */
37954012d104SMarius Strobl 		trys = 0;
37968cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
37978cb1383cSDoug Ambrisko again:
37988cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
37998cb1383cSDoug Ambrisko 
3800fb772a6cSMarius Strobl 		error = mii_attach(dev, &sc->bge_miibus, ifp, bge_ifmedia_upd,
3801daeeb75cSPyun YongHyeon 		    bge_ifmedia_sts, capmask, sc->bge_phy_addr, MII_OFFSET_ANY,
3802fb772a6cSMarius Strobl 		    MIIF_DOPAUSE);
38038e5d93dbSMarius Strobl 		if (error != 0) {
38048cb1383cSDoug Ambrisko 			if (trys++ < 4) {
38058cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3806daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3807daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
38088cb1383cSDoug Ambrisko 				goto again;
38098cb1383cSDoug Ambrisko 			}
38108e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
381195d67482SBill Paul 			goto fail;
381295d67482SBill Paul 		}
38138cb1383cSDoug Ambrisko 
38148cb1383cSDoug Ambrisko 		/*
38158cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
38168cb1383cSDoug Ambrisko 		 */
38178cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
38188cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
381995d67482SBill Paul 	}
382095d67482SBill Paul 
382195d67482SBill Paul 	/*
3822e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3823e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3824e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3825e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3826e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3827e255b776SJohn Polstra 	 * payloads by copying the received packets.
3828e255b776SJohn Polstra 	 */
3829652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3830652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3831652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3832e255b776SJohn Polstra 
3833e255b776SJohn Polstra 	/*
383495d67482SBill Paul 	 * Call MI attach routine.
383595d67482SBill Paul 	 */
3836fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
38370f9bd73bSSam Leffler 
383861ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
383961ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
384061ccb9daSPyun YongHyeon 
38410f9bd73bSSam Leffler 	/*
38420f9bd73bSSam Leffler 	 * Hookup IRQ last.
38430f9bd73bSSam Leffler 	 */
3844dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3845dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
38467e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
38477e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3848dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3849dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3850dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3851dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3852dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3853e010b055SPyun YongHyeon 			error = ENOMEM;
3854dfe0df9aSPyun YongHyeon 			goto fail;
3855dfe0df9aSPyun YongHyeon 		}
3856dfe0df9aSPyun YongHyeon 		taskqueue_start_threads(&sc->bge_tq, 1, PI_NET, "%s taskq",
3857dfe0df9aSPyun YongHyeon 		    device_get_nameunit(sc->bge_dev));
3858dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3859dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3860dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3861dfe0df9aSPyun YongHyeon 	} else
3862dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3863dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3864dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
38650f9bd73bSSam Leffler 
38660f9bd73bSSam Leffler 	if (error) {
3867e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3868fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
38690f9bd73bSSam Leffler 	}
387095d67482SBill Paul 
387195d67482SBill Paul fail:
3872e010b055SPyun YongHyeon 	if (error)
3873e010b055SPyun YongHyeon 		bge_detach(dev);
387495d67482SBill Paul 	return (error);
387595d67482SBill Paul }
387695d67482SBill Paul 
387795d67482SBill Paul static int
38783f74909aSGleb Smirnoff bge_detach(device_t dev)
387995d67482SBill Paul {
388095d67482SBill Paul 	struct bge_softc *sc;
388195d67482SBill Paul 	struct ifnet *ifp;
388295d67482SBill Paul 
388395d67482SBill Paul 	sc = device_get_softc(dev);
3884fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
388595d67482SBill Paul 
388675719184SGleb Smirnoff #ifdef DEVICE_POLLING
388775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
388875719184SGleb Smirnoff 		ether_poll_deregister(ifp);
388975719184SGleb Smirnoff #endif
389075719184SGleb Smirnoff 
3891e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3892e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
38930f9bd73bSSam Leffler 		BGE_LOCK(sc);
389495d67482SBill Paul 		bge_stop(sc);
38950f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
38965dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3897e010b055SPyun YongHyeon 	}
38985dda8085SOleg Bulyzhin 
3899dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3900dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
390195d67482SBill Paul 
3902652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
390395d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
390495d67482SBill Paul 	} else {
390595d67482SBill Paul 		bus_generic_detach(dev);
390695d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
390795d67482SBill Paul 	}
390895d67482SBill Paul 
390995d67482SBill Paul 	bge_release_resources(sc);
391095d67482SBill Paul 
391195d67482SBill Paul 	return (0);
391295d67482SBill Paul }
391395d67482SBill Paul 
391495d67482SBill Paul static void
39153f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
391695d67482SBill Paul {
391795d67482SBill Paul 	device_t dev;
391895d67482SBill Paul 
391995d67482SBill Paul 	dev = sc->bge_dev;
392095d67482SBill Paul 
3921dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3922dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3923dfe0df9aSPyun YongHyeon 
392495d67482SBill Paul 	if (sc->bge_intrhand != NULL)
392595d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
392695d67482SBill Paul 
392795d67482SBill Paul 	if (sc->bge_irq != NULL)
3928724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3929724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3930724bd939SJohn Polstra 
3931724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3932724bd939SJohn Polstra 		pci_release_msi(dev);
393395d67482SBill Paul 
393495d67482SBill Paul 	if (sc->bge_res != NULL)
393595d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3936736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
393795d67482SBill Paul 
3938548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
3939548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
3940548c8f1aSPyun YongHyeon 		    PCIR_BAR(2), sc->bge_res2);
3941548c8f1aSPyun YongHyeon 
3942ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3943ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3944ad61f896SRuslan Ermilov 
3945f41ac2beSBill Paul 	bge_dma_free(sc);
394695d67482SBill Paul 
39470f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
39480f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
394995d67482SBill Paul }
395095d67482SBill Paul 
39518cb1383cSDoug Ambrisko static int
39523f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
395395d67482SBill Paul {
395495d67482SBill Paul 	device_t dev;
3955cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
39566f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
39570aaf1057SPyun YongHyeon 	uint16_t devctl;
39585fea260fSMarius Strobl 	int i;
395995d67482SBill Paul 
396095d67482SBill Paul 	dev = sc->bge_dev;
396195d67482SBill Paul 
3962cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
3963548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
3964548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
3965cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
3966cc085b36SPyun YongHyeon 
396738cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
396838cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
39696f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
39706f8718a3SScott Long 			write_op = bge_writemem_direct;
39716f8718a3SScott Long 		else
39726f8718a3SScott Long 			write_op = bge_writemem_ind;
39739ba784dbSScott Long 	} else
39746f8718a3SScott Long 		write_op = bge_writereg_ind;
39756f8718a3SScott Long 
3976548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
3977548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
3978548c8f1aSPyun YongHyeon 
397995d67482SBill Paul 	/* Save some important PCI state. */
398095d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
398195d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
398295d67482SBill Paul 
398395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
398495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3985e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
398695d67482SBill Paul 
39876f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
39886f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
3989a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
39906f8718a3SScott Long 		if (bootverbose)
3991333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
39926f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
39936f8718a3SScott Long 	}
39946f8718a3SScott Long 
39956f8718a3SScott Long 	/*
39966f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
39976f8718a3SScott Long 	 * When firmware finishes its initialization it will
3998888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
39996f8718a3SScott Long 	 */
4000888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
40016f8718a3SScott Long 
40020c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4003e53d81eeSPaul Saab 
4004e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4005652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4006ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4007ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
40080c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
40090c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4010ad49eccfSPyun YongHyeon 		}
4011e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4012e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
40130c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
40140c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4015e53d81eeSPaul Saab 		}
4016e53d81eeSPaul Saab 	}
4017e53d81eeSPaul Saab 
4018df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4019df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4020df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4021df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4022df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4023df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4024df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4025df4db538SPyun YongHyeon 	}
4026df4db538SPyun YongHyeon 
402721c9e407SDavid Christensen 	/*
40286f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
40296f8718a3SScott Long 	 * powered up in D0 uninitialized.
40306f8718a3SScott Long 	 */
40315512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
40325512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4033caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
40346f8718a3SScott Long 
403595d67482SBill Paul 	/* Issue global reset */
40366f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
403795d67482SBill Paul 
4038cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4039cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4040cc085b36SPyun YongHyeon 	else
404195d67482SBill Paul 		DELAY(1000);
404295d67482SBill Paul 
4043e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4044652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4045e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4046e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
40475fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
40485fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4049e53d81eeSPaul Saab 		}
40500aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4051389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
40520aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4053389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4054389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4055389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
40560aaf1057SPyun YongHyeon 		    devctl, 2);
405748630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
40580aaf1057SPyun YongHyeon 		/* Clear error status. */
4059389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4060389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4061389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4062389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4063e53d81eeSPaul Saab 	}
4064e53d81eeSPaul Saab 
40653f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
406695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
406795d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4068e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4069cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4070cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4071cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4072cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4073548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4074548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4075548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4076548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4077cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
407895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
407995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4080cbb2b2feSPyun YongHyeon 	/*
4081cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4082fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4083cbb2b2feSPyun YongHyeon 	 * read stale status block.
4084cbb2b2feSPyun YongHyeon 	 */
4085cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4086cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4087cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4088cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4089cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4090cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4091cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4092cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4093cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4094cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4095cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4096cbb2b2feSPyun YongHyeon 		}
4097cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4098cbb2b2feSPyun YongHyeon 		    devctl, 2);
4099cbb2b2feSPyun YongHyeon 	}
410022a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
41014c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4102bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4103bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
41040aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
41050aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
41060aaf1057SPyun YongHyeon 			pci_write_config(dev,
41070aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4108bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4109bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4110bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4111bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4112bf6ef57aSJohn Polstra 		}
41134c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
41144c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
41154c0da0ffSGleb Smirnoff 	} else
4116a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4117a7b0c314SPaul Saab 
4118cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4119cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4120cc085b36SPyun YongHyeon 
4121cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4122cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4123cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4124cc085b36SPyun YongHyeon 	DELAY(40);
4125cc085b36SPyun YongHyeon 
4126548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4127548c8f1aSPyun YongHyeon 
412838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
412938cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
413038cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
413138cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
413238cc658fSJohn Baldwin 				break;
413338cc658fSJohn Baldwin 			DELAY(100);
413438cc658fSJohn Baldwin 		}
413538cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4136333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
413738cc658fSJohn Baldwin 			return (1);
413838cc658fSJohn Baldwin 		}
413938cc658fSJohn Baldwin 	} else {
414095d67482SBill Paul 		/*
41416f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
414208013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
41435fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
41445fea260fSMarius Strobl 		 * address is fitted though.
414595d67482SBill Paul 		 */
414695d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4147d5d23857SJung-uk Kim 			DELAY(10);
4148888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4149888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
415095d67482SBill Paul 				break;
415195d67482SBill Paul 		}
415295d67482SBill Paul 
41535fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4154333704a3SPyun YongHyeon 			device_printf(dev,
4155333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4156333704a3SPyun YongHyeon 			    val);
4157b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4158b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4159b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
416038cc658fSJohn Baldwin 	}
416195d67482SBill Paul 
416295d67482SBill Paul 	/*
4163da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4164da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4165da3003f0SBill Paul 	 * to 1.2V.
4166da3003f0SBill Paul 	 */
4167652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4168652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
41695fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
41705fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
41715fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4172da3003f0SBill Paul 	}
4173da3003f0SBill Paul 
4174e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4175652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4176b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4177a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4178a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4179a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
41805fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
41815fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4182e53d81eeSPaul Saab 	}
41838cb1383cSDoug Ambrisko 
418450515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
418550515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
418650515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
418750515680SPyun YongHyeon 
41888cb1383cSDoug Ambrisko 	return (0);
418995d67482SBill Paul }
419095d67482SBill Paul 
4191e0b7b101SPyun YongHyeon static __inline void
4192e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4193e0b7b101SPyun YongHyeon {
4194e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4195e0b7b101SPyun YongHyeon 
4196e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4197e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4198e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4199e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4200e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4201e0b7b101SPyun YongHyeon }
4202e0b7b101SPyun YongHyeon 
4203e0b7b101SPyun YongHyeon static __inline void
4204e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4205e0b7b101SPyun YongHyeon {
4206e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4207e0b7b101SPyun YongHyeon 
4208e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4209e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4210e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4211e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4212e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4213e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4214e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4215e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4216e0b7b101SPyun YongHyeon }
4217e0b7b101SPyun YongHyeon 
421895d67482SBill Paul /*
421995d67482SBill Paul  * Frame reception handling. This is called if there's a frame
422095d67482SBill Paul  * on the receive return list.
422195d67482SBill Paul  *
422295d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
42231be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
422495d67482SBill Paul  * 2) the frame is from the standard receive ring
422595d67482SBill Paul  */
422695d67482SBill Paul 
42271abcdbd1SAttilio Rao static int
4228dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
422995d67482SBill Paul {
423095d67482SBill Paul 	struct ifnet *ifp;
42311abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4232b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
423395d67482SBill Paul 
42347f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
42350f9bd73bSSam Leffler 
42363f74909aSGleb Smirnoff 	/* Nothing to do. */
42377f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
42381abcdbd1SAttilio Rao 		return (rx_npkts);
4239cfcb5025SOleg Bulyzhin 
4240fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
424195d67482SBill Paul 
4242f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4243e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4244f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
424515eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4246f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4247f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4248c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
4249f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
425015eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4251f41ac2beSBill Paul 
42527f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
425395d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
42543f74909aSGleb Smirnoff 		uint32_t		rxidx;
425595d67482SBill Paul 		struct mbuf		*m = NULL;
42563f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
425795d67482SBill Paul 		int			have_tag = 0;
425895d67482SBill Paul 
425975719184SGleb Smirnoff #ifdef DEVICE_POLLING
426075719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
426175719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
426275719184SGleb Smirnoff 				break;
426375719184SGleb Smirnoff 			sc->rxcycles--;
426475719184SGleb Smirnoff 		}
426575719184SGleb Smirnoff #endif
426675719184SGleb Smirnoff 
42677f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
426895d67482SBill Paul 
426995d67482SBill Paul 		rxidx = cur_rx->bge_idx;
42707f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
427195d67482SBill Paul 
4272cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
4273cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
427495d67482SBill Paul 			have_tag = 1;
427595d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
427695d67482SBill Paul 		}
427795d67482SBill Paul 
427895d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
427995d67482SBill Paul 			jumbocnt++;
4280943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
428195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4282e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
428395d67482SBill Paul 				continue;
428495d67482SBill Paul 			}
4285943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4286e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4287943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
428895d67482SBill Paul 				continue;
428995d67482SBill Paul 			}
429003e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
429195d67482SBill Paul 		} else {
429295d67482SBill Paul 			stdcnt++;
4293e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
429495d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4295e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
429695d67482SBill Paul 				continue;
429795d67482SBill Paul 			}
4298943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4299e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4300943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
430195d67482SBill Paul 				continue;
430295d67482SBill Paul 			}
430303e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
430495d67482SBill Paul 		}
430595d67482SBill Paul 
430695d67482SBill Paul 		ifp->if_ipackets++;
4307e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4308e255b776SJohn Polstra 		/*
4309e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4310e65bed95SPyun YongHyeon 		 * the payload is aligned.
4311e255b776SJohn Polstra 		 */
4312652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4313e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4314e255b776SJohn Polstra 			    cur_rx->bge_len);
4315e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4316e255b776SJohn Polstra 		}
4317e255b776SJohn Polstra #endif
4318473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
431995d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
432095d67482SBill Paul 
43211108273aSPyun YongHyeon 		if (ifp->if_capenable & IFCAP_RXCSUM)
43221108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
432395d67482SBill Paul 
432495d67482SBill Paul 		/*
4325673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4326673d9191SSam Leffler 		 * attach that information to the packet.
432795d67482SBill Paul 		 */
4328d147662cSGleb Smirnoff 		if (have_tag) {
432978ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
433078ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4331d147662cSGleb Smirnoff 		}
433295d67482SBill Paul 
4333dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
43340f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4335673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
43360f9bd73bSSam Leffler 			BGE_LOCK(sc);
4337dfe0df9aSPyun YongHyeon 		} else
4338dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
4339d4da719cSAttilio Rao 		rx_npkts++;
434025e13e68SXin LI 
434125e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
43428cf7d13dSAttilio Rao 			return (rx_npkts);
434395d67482SBill Paul 	}
434495d67482SBill Paul 
434515eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
434615eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4347e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4348f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4349e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
43504c0da0ffSGleb Smirnoff 
4351c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4352f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
43534c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4354f41ac2beSBill Paul 
43557f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
435638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
435795d67482SBill Paul 	if (stdcnt)
4358767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4359767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
436095d67482SBill Paul 	if (jumbocnt)
4361767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4362767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4363f5a034f9SPyun YongHyeon #ifdef notyet
4364f5a034f9SPyun YongHyeon 	/*
4365f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4366f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4367f5a034f9SPyun YongHyeon 	 */
4368f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4369f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
4370f5a034f9SPyun YongHyeon #endif
43711abcdbd1SAttilio Rao 	return (rx_npkts);
437295d67482SBill Paul }
437395d67482SBill Paul 
437495d67482SBill Paul static void
43751108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
43761108273aSPyun YongHyeon {
43771108273aSPyun YongHyeon 
43781108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
43791108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
43801108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
43811108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
43821108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
43831108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
43841108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
43851108273aSPyun YongHyeon 			}
43861108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
43871108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
43881108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
43891108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
43901108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
43911108273aSPyun YongHyeon 			}
43921108273aSPyun YongHyeon 		}
43931108273aSPyun YongHyeon 	} else {
43941108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
43951108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
43961108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
43971108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
43981108273aSPyun YongHyeon 		}
43991108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
44001108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
44011108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
44021108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
44031108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44041108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
44051108273aSPyun YongHyeon 		}
44061108273aSPyun YongHyeon 	}
44071108273aSPyun YongHyeon }
44081108273aSPyun YongHyeon 
44091108273aSPyun YongHyeon static void
4410b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
441195d67482SBill Paul {
441295a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
441395d67482SBill Paul 	struct ifnet *ifp;
441495d67482SBill Paul 
44150f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
44160f9bd73bSSam Leffler 
44173f74909aSGleb Smirnoff 	/* Nothing to do. */
4418b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4419cfcb5025SOleg Bulyzhin 		return;
4420cfcb5025SOleg Bulyzhin 
4421fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
442295d67482SBill Paul 
4423e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
44245c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
442595d67482SBill Paul 	/*
442695d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
442795d67482SBill Paul 	 * frames that have been sent.
442895d67482SBill Paul 	 */
4429b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
443095a0a340SPyun YongHyeon 		uint32_t		idx;
443195d67482SBill Paul 
443295d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4433f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
443495d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
443595d67482SBill Paul 			ifp->if_opackets++;
443695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
44370ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4438e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4439e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
44400ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4441f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4442e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4443e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
444495d67482SBill Paul 		}
444595d67482SBill Paul 		sc->bge_txcnt--;
444695d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
444795d67482SBill Paul 	}
444895d67482SBill Paul 
444913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
44505b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
44515b01e77cSBruce Evans 		sc->bge_timer = 0;
445295d67482SBill Paul }
445395d67482SBill Paul 
445475719184SGleb Smirnoff #ifdef DEVICE_POLLING
44551abcdbd1SAttilio Rao static int
445675719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
445775719184SGleb Smirnoff {
445875719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
4459b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4460366454f2SOleg Bulyzhin 	uint32_t statusword;
44611abcdbd1SAttilio Rao 	int rx_npkts = 0;
446275719184SGleb Smirnoff 
44633f74909aSGleb Smirnoff 	BGE_LOCK(sc);
44643f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
44653f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
44661abcdbd1SAttilio Rao 		return (rx_npkts);
44673f74909aSGleb Smirnoff 	}
446875719184SGleb Smirnoff 
4469dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4470b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4471b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
44722246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4473b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4474b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4475dab5cd05SOleg Bulyzhin 
4476175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
44772246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4478175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4479dab5cd05SOleg Bulyzhin 
4480dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4481b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4482b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4483366454f2SOleg Bulyzhin 
44840c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4485366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4486366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4487366454f2SOleg Bulyzhin 
4488366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4489366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
44904c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4491652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4492366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4493366454f2SOleg Bulyzhin 
4494366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4495dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
449625e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
449725e13e68SXin LI 		BGE_UNLOCK(sc);
44988cf7d13dSAttilio Rao 		return (rx_npkts);
449925e13e68SXin LI 	}
4500b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4501366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4502366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
45033f74909aSGleb Smirnoff 
45043f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
45051abcdbd1SAttilio Rao 	return (rx_npkts);
450675719184SGleb Smirnoff }
450775719184SGleb Smirnoff #endif /* DEVICE_POLLING */
450875719184SGleb Smirnoff 
4509dfe0df9aSPyun YongHyeon static int
4510dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4511dfe0df9aSPyun YongHyeon {
4512dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4513dfe0df9aSPyun YongHyeon 
4514dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4515dfe0df9aSPyun YongHyeon 	/*
4516dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4517dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4518dfe0df9aSPyun YongHyeon 	 */
4519dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4520dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4521dfe0df9aSPyun YongHyeon }
4522dfe0df9aSPyun YongHyeon 
4523dfe0df9aSPyun YongHyeon static void
4524dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4525dfe0df9aSPyun YongHyeon {
4526dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4527dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
45281108273aSPyun YongHyeon 	uint32_t status, status_tag;
4529dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4530dfe0df9aSPyun YongHyeon 
4531dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4532dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4533dfe0df9aSPyun YongHyeon 
453466151edfSPyun YongHyeon 	BGE_LOCK(sc);
453566151edfSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
453666151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4537dfe0df9aSPyun YongHyeon 		return;
453866151edfSPyun YongHyeon 	}
4539dfe0df9aSPyun YongHyeon 
4540dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4541dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4542dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4543dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4544dfe0df9aSPyun YongHyeon 
45452246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4546dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4547dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4548dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
45491108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
45502246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4551dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4552dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4553dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4554dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
45551108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
45561108273aSPyun YongHyeon 		status_tag = 0;
455766151edfSPyun YongHyeon 
455866151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
455966151edfSPyun YongHyeon 		bge_link_upd(sc);
456066151edfSPyun YongHyeon 
4561dfe0df9aSPyun YongHyeon 	/* Let controller work. */
45621108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4563dfe0df9aSPyun YongHyeon 
456466151edfSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
456566151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4566dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
456766151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4568dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
456966151edfSPyun YongHyeon 		BGE_LOCK(sc);
4570dfe0df9aSPyun YongHyeon 	}
4571dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4572dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4573dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4574dfe0df9aSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4575dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4576dfe0df9aSPyun YongHyeon 	}
457766151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4578dfe0df9aSPyun YongHyeon }
4579dfe0df9aSPyun YongHyeon 
458095d67482SBill Paul static void
45813f74909aSGleb Smirnoff bge_intr(void *xsc)
458295d67482SBill Paul {
458395d67482SBill Paul 	struct bge_softc *sc;
458495d67482SBill Paul 	struct ifnet *ifp;
4585dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4586b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
458795d67482SBill Paul 
458895d67482SBill Paul 	sc = xsc;
4589f41ac2beSBill Paul 
45900f9bd73bSSam Leffler 	BGE_LOCK(sc);
45910f9bd73bSSam Leffler 
4592dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4593dab5cd05SOleg Bulyzhin 
459475719184SGleb Smirnoff #ifdef DEVICE_POLLING
459575719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
459675719184SGleb Smirnoff 		BGE_UNLOCK(sc);
459775719184SGleb Smirnoff 		return;
459875719184SGleb Smirnoff 	}
459975719184SGleb Smirnoff #endif
460075719184SGleb Smirnoff 
4601f30cbfc6SScott Long 	/*
4602b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4603b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4604b848e032SBruce Evans 	 * our current organization this just gives complications and
4605b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4606b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4607b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4608b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4609b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4610b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4611b848e032SBruce Evans 	 *
4612b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4613b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4614b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4615b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4616b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4617b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4618b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4619b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4620b848e032SBruce Evans 	 */
462138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4622b848e032SBruce Evans 
4623f584dfd1SPyun YongHyeon 	/*
4624f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4625f584dfd1SPyun YongHyeon 	 */
4626f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4627f584dfd1SPyun YongHyeon 
4628f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4629f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4630f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4631f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4632f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4633f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4634f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4635f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4636f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4637f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4638f584dfd1SPyun YongHyeon 
46391f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
46404c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4641f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4642dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
464395d67482SBill Paul 
464413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46453f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4646dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
464725e13e68SXin LI 	}
464895d67482SBill Paul 
464925e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46503f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4651b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
465295d67482SBill Paul 	}
465395d67482SBill Paul 
465413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
465513f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
46560f9bd73bSSam Leffler 		bge_start_locked(ifp);
46570f9bd73bSSam Leffler 
46580f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
465995d67482SBill Paul }
466095d67482SBill Paul 
466195d67482SBill Paul static void
46628cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
46638cb1383cSDoug Ambrisko {
46648cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
46658cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
46668cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
46678cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
46688cb1383cSDoug Ambrisko 		else {
4669899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4670888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
46713c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4672888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4673941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4674941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
46753fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
46769931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
46779931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
46788cb1383cSDoug Ambrisko 		}
46798cb1383cSDoug Ambrisko 	}
46808cb1383cSDoug Ambrisko }
46818cb1383cSDoug Ambrisko 
46828cb1383cSDoug Ambrisko static void
4683b74e67fbSGleb Smirnoff bge_tick(void *xsc)
46840f9bd73bSSam Leffler {
4685b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
468695d67482SBill Paul 	struct mii_data *mii = NULL;
468795d67482SBill Paul 
46880f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
468995d67482SBill Paul 
46905dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
46915dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
46925dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
46935dda8085SOleg Bulyzhin 		return;
46945dda8085SOleg Bulyzhin 
46957ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
46960434d1b8SBill Paul 		bge_stats_update_regs(sc);
46970434d1b8SBill Paul 	else
469895d67482SBill Paul 		bge_stats_update(sc);
469995d67482SBill Paul 
4700548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4701548c8f1aSPyun YongHyeon 
4702652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
470395d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
470482b67c01SOleg Bulyzhin 		/*
470582b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
470682b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
470782b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
470882b67c01SOleg Bulyzhin 		 */
470982b67c01SOleg Bulyzhin 		if (!sc->bge_link)
471095d67482SBill Paul 			mii_tick(mii);
47117b97099dSOleg Bulyzhin 	} else {
47127b97099dSOleg Bulyzhin 		/*
47137b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
47147b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
47157b97099dSOleg Bulyzhin 		 * and trigger interrupt.
47167b97099dSOleg Bulyzhin 		 */
47177b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
47183f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
47197b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
47207b97099dSOleg Bulyzhin #endif
47217b97099dSOleg Bulyzhin 		{
47227b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
47234f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
47244f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
47257b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
47264f0794ffSBjoern A. Zeeb 		else
47274f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
47287b97099dSOleg Bulyzhin 		}
4729dab5cd05SOleg Bulyzhin 	}
473095d67482SBill Paul 
47318cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4732b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
47338cb1383cSDoug Ambrisko 
4734dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
473595d67482SBill Paul }
473695d67482SBill Paul 
473795d67482SBill Paul static void
47383f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
47390434d1b8SBill Paul {
47403f74909aSGleb Smirnoff 	struct ifnet *ifp;
47412280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
47420434d1b8SBill Paul 
4743fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
47442280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
47450434d1b8SBill Paul 
47462280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
47472280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
47482280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
47492280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
47502280c16bSPyun YongHyeon 	stats->outXonSent +=
47512280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
47522280c16bSPyun YongHyeon 	stats->outXoffSent +=
47532280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
47542280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
47552280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
47562280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
47572280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
47582280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
47592280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
47602280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
47612280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
47622280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
47632280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
47642280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
47652280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
47662280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
47672280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
47682280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
47692280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
47702280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
47712280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
47727e6e2507SJung-uk Kim 
47732280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
47742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
47752280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
47762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
47772280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
47782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
47792280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
47802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
47812280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
47822280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
47832280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
47842280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
47852280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
47862280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
47872280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
47882280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
47892280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
47902280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
47912280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
47922280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
47932280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
47942280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
47952280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
47962280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
47972280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
47982280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
47992280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
48002280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48012280c16bSPyun YongHyeon 
48022280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
48032280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48042280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
48052280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48062280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
48072280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48082280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
48092280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4810f78094a5SPyun YongHyeon 	/*
4811f78094a5SPyun YongHyeon 	 * XXX
4812f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4813f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4814f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4815f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4816f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4817f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4818f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4819f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4820f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4821f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4822f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4823f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4824f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4825f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4826f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4827f78094a5SPyun YongHyeon 	 * silicon bug.
4828f78094a5SPyun YongHyeon 	 */
4829f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4830f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4831f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
48322280c16bSPyun YongHyeon 		stats->InputDiscards +=
48332280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
48342280c16bSPyun YongHyeon 	stats->InputErrors +=
48352280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
48362280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
48372280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
48382280c16bSPyun YongHyeon 
48392280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
48402280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
48412280c16bSPyun YongHyeon 	    stats->InputErrors);
48422280c16bSPyun YongHyeon }
48432280c16bSPyun YongHyeon 
48442280c16bSPyun YongHyeon static void
48452280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
48462280c16bSPyun YongHyeon {
48472280c16bSPyun YongHyeon 
48482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
48542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
48552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
48562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
48572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
48582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
48592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
48602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48612280c16bSPyun YongHyeon 
48622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
48722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
48732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
48742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48752280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48762280c16bSPyun YongHyeon 
48772280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48782280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48792280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48802280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
48812280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
48822280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
48832280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
48840434d1b8SBill Paul }
48850434d1b8SBill Paul 
48860434d1b8SBill Paul static void
48873f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
488895d67482SBill Paul {
488995d67482SBill Paul 	struct ifnet *ifp;
4890e907febfSPyun YongHyeon 	bus_size_t stats;
48917e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
489295d67482SBill Paul 
4893fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
489495d67482SBill Paul 
4895e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4896e907febfSPyun YongHyeon 
4897e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4898e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
489995d67482SBill Paul 
49008634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
49016b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
49026fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
49036fb34dd2SOleg Bulyzhin 
490437ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
490537ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_nobds);
490637ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
490737ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
490837ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_inerrs);
490937ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
49106fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
49116b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
49126fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
49136fb34dd2SOleg Bulyzhin 
49146fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
49156b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
49166fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
491795d67482SBill Paul 
4918e907febfSPyun YongHyeon #undef	READ_STAT
491995d67482SBill Paul }
492095d67482SBill Paul 
492195d67482SBill Paul /*
4922d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
4923d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
4924d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
4925d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
4926d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
4927d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
4928d375e524SGleb Smirnoff  */
4929d375e524SGleb Smirnoff static __inline int
4930d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
4931d375e524SGleb Smirnoff {
4932d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
4933d375e524SGleb Smirnoff 	struct mbuf *last;
4934d375e524SGleb Smirnoff 
4935d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
4936d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
4937d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
4938d375e524SGleb Smirnoff 		last = m;
4939d375e524SGleb Smirnoff 	} else {
4940d375e524SGleb Smirnoff 		/*
4941d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
4942d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
4943d375e524SGleb Smirnoff 		 */
4944d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
4945d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
4946d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
4947d375e524SGleb Smirnoff 			struct mbuf *n;
4948d375e524SGleb Smirnoff 
4949d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
4950d375e524SGleb Smirnoff 			if (n == NULL)
4951d375e524SGleb Smirnoff 				return (ENOBUFS);
4952d375e524SGleb Smirnoff 			n->m_len = 0;
4953d375e524SGleb Smirnoff 			last->m_next = n;
4954d375e524SGleb Smirnoff 			last = n;
4955d375e524SGleb Smirnoff 		}
4956d375e524SGleb Smirnoff 	}
4957d375e524SGleb Smirnoff 
4958d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
4959d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
4960d375e524SGleb Smirnoff 	last->m_len += padlen;
4961d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
4962d375e524SGleb Smirnoff 
4963d375e524SGleb Smirnoff 	return (0);
4964d375e524SGleb Smirnoff }
4965d375e524SGleb Smirnoff 
4966ca3f1187SPyun YongHyeon static struct mbuf *
4967d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
4968d598b626SPyun YongHyeon {
4969d598b626SPyun YongHyeon 	struct mbuf *n;
4970d598b626SPyun YongHyeon 	int found;
4971d598b626SPyun YongHyeon 
4972d598b626SPyun YongHyeon 	/*
4973d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
4974d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
4975d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
4976d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
4977d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
4978d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
4979d598b626SPyun YongHyeon 	 */
4980d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
4981d598b626SPyun YongHyeon 		if (n->m_len < 8) {
4982d598b626SPyun YongHyeon 			found++;
4983d598b626SPyun YongHyeon 			if (found > 1)
4984d598b626SPyun YongHyeon 				break;
4985d598b626SPyun YongHyeon 			continue;
4986d598b626SPyun YongHyeon 		}
4987d598b626SPyun YongHyeon 		found = 0;
4988d598b626SPyun YongHyeon 	}
4989d598b626SPyun YongHyeon 
4990d598b626SPyun YongHyeon 	if (found > 1) {
4991d598b626SPyun YongHyeon 		n = m_defrag(m, M_DONTWAIT);
4992d598b626SPyun YongHyeon 		if (n == NULL)
4993d598b626SPyun YongHyeon 			m_freem(m);
4994d598b626SPyun YongHyeon 	} else
4995d598b626SPyun YongHyeon 		n = m;
4996d598b626SPyun YongHyeon 	return (n);
4997d598b626SPyun YongHyeon }
4998d598b626SPyun YongHyeon 
4999d598b626SPyun YongHyeon static struct mbuf *
50001108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
50011108273aSPyun YongHyeon     uint16_t *flags)
5002ca3f1187SPyun YongHyeon {
5003ca3f1187SPyun YongHyeon 	struct ip *ip;
5004ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
5005ca3f1187SPyun YongHyeon 	struct mbuf *n;
5006ca3f1187SPyun YongHyeon 	uint16_t hlen;
50075b355c4fSPyun YongHyeon 	uint32_t poff;
5008ca3f1187SPyun YongHyeon 
5009ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5010ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5011ca3f1187SPyun YongHyeon 		n = m_dup(m, M_DONTWAIT);
5012ca3f1187SPyun YongHyeon 		m_freem(m);
5013ca3f1187SPyun YongHyeon 		if (n == NULL)
5014ca3f1187SPyun YongHyeon 			return (NULL);
5015ca3f1187SPyun YongHyeon 		m = n;
5016ca3f1187SPyun YongHyeon 	}
50175b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5018ca3f1187SPyun YongHyeon 	if (m == NULL)
5019ca3f1187SPyun YongHyeon 		return (NULL);
50205b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
50215b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5022ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5023ca3f1187SPyun YongHyeon 	if (m == NULL)
5024ca3f1187SPyun YongHyeon 		return (NULL);
5025ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
50265b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5027ca3f1187SPyun YongHyeon 	if (m == NULL)
5028ca3f1187SPyun YongHyeon 		return (NULL);
5029ca3f1187SPyun YongHyeon 	/*
5030ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5031ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5032ca3f1187SPyun YongHyeon 	 */
5033ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
503496486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5035ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5036ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5037ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
503896486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5039ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5040ca3f1187SPyun YongHyeon 	/*
5041ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5042ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5043ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
50441108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5045ca3f1187SPyun YongHyeon 	 */
50461108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5047ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
50481108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
50491108273aSPyun YongHyeon 		/*
50501108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
50511108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
50521108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
50531108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
50541108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
50551108273aSPyun YongHyeon 		 * frames are supported.
50561108273aSPyun YongHyeon 		 */
50571108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
50581108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
50591108273aSPyun YongHyeon 	} else {
50601108273aSPyun YongHyeon 		/*
50611108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
50621108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
50631108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
50641108273aSPyun YongHyeon 		 * supported.
50651108273aSPyun YongHyeon 		 */
5066ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
50671108273aSPyun YongHyeon 	}
5068ca3f1187SPyun YongHyeon 	return (m);
5069ca3f1187SPyun YongHyeon }
5070ca3f1187SPyun YongHyeon 
5071d375e524SGleb Smirnoff /*
507295d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
507395d67482SBill Paul  * pointers to descriptors.
507495d67482SBill Paul  */
507595d67482SBill Paul static int
5076676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
507795d67482SBill Paul {
50787e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5079f41ac2beSBill Paul 	bus_dmamap_t		map;
5080676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5081676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
50827e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5083ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
50847e27542aSGleb Smirnoff 	int			nsegs, i, error;
508595d67482SBill Paul 
50866909dc43SGleb Smirnoff 	csum_flags = 0;
5087ca3f1187SPyun YongHyeon 	mss = 0;
5088ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5089d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5090d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5091d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5092d598b626SPyun YongHyeon 		if (*m_head == NULL)
5093d598b626SPyun YongHyeon 			return (ENOBUFS);
5094d598b626SPyun YongHyeon 		m = *m_head;
5095d598b626SPyun YongHyeon 	}
5096ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
50971108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5098ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5099ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5100ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5101ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
510235f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
51036909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
51046909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
51056909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
51066909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
51076909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
51086909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
51096909dc43SGleb Smirnoff 				m_freem(m);
51106909dc43SGleb Smirnoff 				*m_head = NULL;
51116909dc43SGleb Smirnoff 				return (error);
51126909dc43SGleb Smirnoff 			}
51136909dc43SGleb Smirnoff 		}
51146909dc43SGleb Smirnoff 	}
51156909dc43SGleb Smirnoff 
51161108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
51171108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
51181108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
51191108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
51201108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5121beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5122d94f2b85SPyun YongHyeon 			/*
5123d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5124d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5125d94f2b85SPyun YongHyeon 			 * DMA read operation.
5126d94f2b85SPyun YongHyeon 			 */
5127beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5128d94f2b85SPyun YongHyeon 				m = m_defrag(m, M_DONTWAIT);
5129d94f2b85SPyun YongHyeon 			else
51301108273aSPyun YongHyeon 				m = m_collapse(m, M_DONTWAIT,
51311108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5132261f04d6SPyun YongHyeon 			if (m == NULL)
5133261f04d6SPyun YongHyeon 				m = *m_head;
5134d94f2b85SPyun YongHyeon 			*m_head = m;
5135d94f2b85SPyun YongHyeon 		}
51361108273aSPyun YongHyeon 	}
5137d94f2b85SPyun YongHyeon 
51387e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
51390ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5140676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
51417e27542aSGleb Smirnoff 	if (error == EFBIG) {
51424eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
5143676ad2c9SGleb Smirnoff 		if (m == NULL) {
5144676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5145676ad2c9SGleb Smirnoff 			*m_head = NULL;
51467e27542aSGleb Smirnoff 			return (ENOBUFS);
51477e27542aSGleb Smirnoff 		}
5148676ad2c9SGleb Smirnoff 		*m_head = m;
51490ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
51500ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5151676ad2c9SGleb Smirnoff 		if (error) {
5152676ad2c9SGleb Smirnoff 			m_freem(m);
5153676ad2c9SGleb Smirnoff 			*m_head = NULL;
51547e27542aSGleb Smirnoff 			return (error);
51557e27542aSGleb Smirnoff 		}
5156676ad2c9SGleb Smirnoff 	} else if (error != 0)
5157676ad2c9SGleb Smirnoff 		return (error);
51587e27542aSGleb Smirnoff 
5159167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5160167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
51610ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
516295d67482SBill Paul 		return (ENOBUFS);
51637e27542aSGleb Smirnoff 	}
51647e27542aSGleb Smirnoff 
51650ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5166e65bed95SPyun YongHyeon 
5167ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5168ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5169ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5170ca3f1187SPyun YongHyeon 	}
51717e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
51727e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
51737e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
51747e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
51757e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
51767e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
5177ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
5178ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
51797e27542aSGleb Smirnoff 		if (i == nsegs - 1)
51807e27542aSGleb Smirnoff 			break;
51817e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
51827e27542aSGleb Smirnoff 	}
51837e27542aSGleb Smirnoff 
51847e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
51857e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5186676ad2c9SGleb Smirnoff 
5187f41ac2beSBill Paul 	/*
5188f41ac2beSBill Paul 	 * Insure that the map for this transmission
5189f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5190f41ac2beSBill Paul 	 * in this chain.
5191f41ac2beSBill Paul 	 */
51927e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
51937e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5194676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
51957e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
519695d67482SBill Paul 
51977e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
51987e27542aSGleb Smirnoff 	*txidx = idx;
519995d67482SBill Paul 
520095d67482SBill Paul 	return (0);
520195d67482SBill Paul }
520295d67482SBill Paul 
520395d67482SBill Paul /*
520495d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
520595d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
520695d67482SBill Paul  */
520795d67482SBill Paul static void
52083f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
520995d67482SBill Paul {
521095d67482SBill Paul 	struct bge_softc *sc;
5211167fdb62SPyun YongHyeon 	struct mbuf *m_head;
521214bbd30fSGleb Smirnoff 	uint32_t prodidx;
5213167fdb62SPyun YongHyeon 	int count;
521495d67482SBill Paul 
521595d67482SBill Paul 	sc = ifp->if_softc;
5216167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
521795d67482SBill Paul 
5218167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5219167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5220167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
522195d67482SBill Paul 		return;
522295d67482SBill Paul 
522314bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
522495d67482SBill Paul 
5225167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
5226167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5227167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5228167fdb62SPyun YongHyeon 			break;
5229167fdb62SPyun YongHyeon 		}
52304d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
523195d67482SBill Paul 		if (m_head == NULL)
523295d67482SBill Paul 			break;
523395d67482SBill Paul 
523495d67482SBill Paul 		/*
523595d67482SBill Paul 		 * Pack the data into the transmit ring. If we
523695d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
523795d67482SBill Paul 		 * for the NIC to drain the ring.
523895d67482SBill Paul 		 */
5239676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5240676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5241676ad2c9SGleb Smirnoff 				break;
52424d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
524313f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
524495d67482SBill Paul 			break;
524595d67482SBill Paul 		}
5246303a718cSDag-Erling Smørgrav 		++count;
524795d67482SBill Paul 
524895d67482SBill Paul 		/*
524995d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
525095d67482SBill Paul 		 * to him.
525195d67482SBill Paul 		 */
52524e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
525345ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
52544e35d186SJung-uk Kim #else
52554e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
52564e35d186SJung-uk Kim #endif
525795d67482SBill Paul 	}
525895d67482SBill Paul 
5259167fdb62SPyun YongHyeon 	if (count > 0) {
5260aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
52615c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
52623f74909aSGleb Smirnoff 		/* Transmit. */
526338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
52643927098fSPaul Saab 		/* 5700 b2 errata */
5265e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
526638cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
526795d67482SBill Paul 
526814bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
526914bbd30fSGleb Smirnoff 
527095d67482SBill Paul 		/*
527195d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
527295d67482SBill Paul 		 */
5273b74e67fbSGleb Smirnoff 		sc->bge_timer = 5;
527495d67482SBill Paul 	}
5275167fdb62SPyun YongHyeon }
527695d67482SBill Paul 
52770f9bd73bSSam Leffler /*
52780f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
52790f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
52800f9bd73bSSam Leffler  */
528195d67482SBill Paul static void
52823f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
528395d67482SBill Paul {
52840f9bd73bSSam Leffler 	struct bge_softc *sc;
52850f9bd73bSSam Leffler 
52860f9bd73bSSam Leffler 	sc = ifp->if_softc;
52870f9bd73bSSam Leffler 	BGE_LOCK(sc);
52880f9bd73bSSam Leffler 	bge_start_locked(ifp);
52890f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
52900f9bd73bSSam Leffler }
52910f9bd73bSSam Leffler 
52920f9bd73bSSam Leffler static void
52933f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
52940f9bd73bSSam Leffler {
529595d67482SBill Paul 	struct ifnet *ifp;
52963f74909aSGleb Smirnoff 	uint16_t *m;
5297f6a65488SPyun YongHyeon 	uint32_t mode;
529895d67482SBill Paul 
52990f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
530095d67482SBill Paul 
5301fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
530295d67482SBill Paul 
530313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
530495d67482SBill Paul 		return;
530595d67482SBill Paul 
530695d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
530795d67482SBill Paul 	bge_stop(sc);
53088cb1383cSDoug Ambrisko 
53098cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
53108cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
531195d67482SBill Paul 	bge_reset(sc);
53128cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
53138cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
53148cb1383cSDoug Ambrisko 
531595d67482SBill Paul 	bge_chipinit(sc);
531695d67482SBill Paul 
531795d67482SBill Paul 	/*
531895d67482SBill Paul 	 * Init the various state machines, ring
531995d67482SBill Paul 	 * control blocks and firmware.
532095d67482SBill Paul 	 */
532195d67482SBill Paul 	if (bge_blockinit(sc)) {
5322fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
532395d67482SBill Paul 		return;
532495d67482SBill Paul 	}
532595d67482SBill Paul 
5326fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
532795d67482SBill Paul 
532895d67482SBill Paul 	/* Specify MTU. */
532995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
5330cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5331cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
533295d67482SBill Paul 
533395d67482SBill Paul 	/* Load our MAC address. */
53343f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
533595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
533695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
533795d67482SBill Paul 
53383e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
53393e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
534095d67482SBill Paul 
534195d67482SBill Paul 	/* Program multicast filter. */
534295d67482SBill Paul 	bge_setmulti(sc);
534395d67482SBill Paul 
5344cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5345cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5346cb2eacc7SYaroslav Tykhiy 
534735f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
534835f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
534935f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
535035f945cdSPyun YongHyeon 	else
535135f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
535235f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
535335f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
535435f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
535535f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
535635f945cdSPyun YongHyeon 	}
535735f945cdSPyun YongHyeon 
535895d67482SBill Paul 	/* Init RX ring. */
53593ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
53603ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
53613ee5d7daSPyun YongHyeon 		bge_stop(sc);
53623ee5d7daSPyun YongHyeon 		return;
53633ee5d7daSPyun YongHyeon 	}
536495d67482SBill Paul 
53650434d1b8SBill Paul 	/*
53660434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
53670434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
53680434d1b8SBill Paul 	 * entry of the ring.
53690434d1b8SBill Paul 	 */
53700434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
53713f74909aSGleb Smirnoff 		uint32_t		v, i;
53720434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
53730434d1b8SBill Paul 			DELAY(20);
53740434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
53750434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
53760434d1b8SBill Paul 				break;
53770434d1b8SBill Paul 		}
53780434d1b8SBill Paul 		if (i == 10)
5379fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5380fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
53810434d1b8SBill Paul 	}
53820434d1b8SBill Paul 
538395d67482SBill Paul 	/* Init jumbo RX ring. */
5384f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5385f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
5386c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
53873ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5388333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5389b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
53903ee5d7daSPyun YongHyeon 			bge_stop(sc);
53913ee5d7daSPyun YongHyeon 			return;
53923ee5d7daSPyun YongHyeon 		}
53933ee5d7daSPyun YongHyeon 	}
539495d67482SBill Paul 
53953f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
539695d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
539795d67482SBill Paul 
53987e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
53997e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
54007e6e2507SJung-uk Kim 
540195d67482SBill Paul 	/* Init TX ring. */
540295d67482SBill Paul 	bge_init_tx_ring(sc);
540395d67482SBill Paul 
5404f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5405f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5406f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5407f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
540850515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
540950515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
541050515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
541150515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
541250515680SPyun YongHyeon 	}
54133f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5414f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5415a6e66cd2SPyun YongHyeon 	DELAY(100);
541695d67482SBill Paul 
54173f74909aSGleb Smirnoff 	/* Turn on receiver. */
5418548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5419548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5420548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
5421548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5422a6e66cd2SPyun YongHyeon 	DELAY(10);
542395d67482SBill Paul 
5424dedcdf57SPyun YongHyeon 	/*
5425dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5426dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5427dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5428dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5429dedcdf57SPyun YongHyeon 	 */
5430b4a256acSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765)
5431b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5432b4a256acSPyun YongHyeon 	else
5433dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5434dedcdf57SPyun YongHyeon 
54352280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
54362280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
54372280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
54382280c16bSPyun YongHyeon 
543995d67482SBill Paul 	/* Tell firmware we're alive. */
544095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
544195d67482SBill Paul 
544275719184SGleb Smirnoff #ifdef DEVICE_POLLING
544375719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
544475719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
544575719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
544675719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
544738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
544875719184SGleb Smirnoff 	} else
544975719184SGleb Smirnoff #endif
545075719184SGleb Smirnoff 
545195d67482SBill Paul 	/* Enable host interrupts. */
545275719184SGleb Smirnoff 	{
545395d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
545495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
545538cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
545675719184SGleb Smirnoff 	}
545795d67482SBill Paul 
545813f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
545913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
546095d67482SBill Paul 
5461e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5462e4146b95SPyun YongHyeon 
54630f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
54640f9bd73bSSam Leffler }
54650f9bd73bSSam Leffler 
54660f9bd73bSSam Leffler static void
54673f74909aSGleb Smirnoff bge_init(void *xsc)
54680f9bd73bSSam Leffler {
54690f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
54700f9bd73bSSam Leffler 
54710f9bd73bSSam Leffler 	BGE_LOCK(sc);
54720f9bd73bSSam Leffler 	bge_init_locked(sc);
54730f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
547495d67482SBill Paul }
547595d67482SBill Paul 
547695d67482SBill Paul /*
547795d67482SBill Paul  * Set media options.
547895d67482SBill Paul  */
547995d67482SBill Paul static int
54803f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
548195d67482SBill Paul {
548267d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
548367d5e043SOleg Bulyzhin 	int res;
548467d5e043SOleg Bulyzhin 
548567d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
548667d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
548767d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
548867d5e043SOleg Bulyzhin 
548967d5e043SOleg Bulyzhin 	return (res);
549067d5e043SOleg Bulyzhin }
549167d5e043SOleg Bulyzhin 
549267d5e043SOleg Bulyzhin static int
549367d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
549467d5e043SOleg Bulyzhin {
549567d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
549695d67482SBill Paul 	struct mii_data *mii;
54974f09c4c7SMarius Strobl 	struct mii_softc *miisc;
549895d67482SBill Paul 	struct ifmedia *ifm;
549995d67482SBill Paul 
550067d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
550167d5e043SOleg Bulyzhin 
550295d67482SBill Paul 	ifm = &sc->bge_ifmedia;
550395d67482SBill Paul 
550495d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5505652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
550695d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
550795d67482SBill Paul 			return (EINVAL);
550895d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
550995d67482SBill Paul 		case IFM_AUTO:
5510ff50922bSDoug White 			/*
5511ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5512ff50922bSDoug White 			 * mechanism for programming the autoneg
5513ff50922bSDoug White 			 * advertisement registers in TBI mode.
5514ff50922bSDoug White 			 */
55150f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5516ff50922bSDoug White 				uint32_t sgdig;
55170f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
55180f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5519ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5520ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5521ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5522ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5523ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5524ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5525ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5526ff50922bSDoug White 					DELAY(5);
5527ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5528ff50922bSDoug White 				}
55290f89fde2SJung-uk Kim 			}
553095d67482SBill Paul 			break;
553195d67482SBill Paul 		case IFM_1000_SX:
553295d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
553395d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
553495d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
553595d67482SBill Paul 			} else {
553695d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
553795d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
553895d67482SBill Paul 			}
55399b80ffe7SPyun YongHyeon 			DELAY(40);
554095d67482SBill Paul 			break;
554195d67482SBill Paul 		default:
554295d67482SBill Paul 			return (EINVAL);
554395d67482SBill Paul 		}
554495d67482SBill Paul 		return (0);
554595d67482SBill Paul 	}
554695d67482SBill Paul 
55471493e883SOleg Bulyzhin 	sc->bge_link_evt++;
554895d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
55494f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
55503fcb7a53SMarius Strobl 		PHY_RESET(miisc);
555195d67482SBill Paul 	mii_mediachg(mii);
555295d67482SBill Paul 
5553902827f6SBjoern A. Zeeb 	/*
5554902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5555902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5556902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5557902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5558902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5559902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5560902827f6SBjoern A. Zeeb 	 * get an RX intr.
5561902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5562902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5563902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5564902827f6SBjoern A. Zeeb 	 */
55654f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
55664f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5567902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
55684f0794ffSBjoern A. Zeeb 	else
556963ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5570902827f6SBjoern A. Zeeb 
557195d67482SBill Paul 	return (0);
557295d67482SBill Paul }
557395d67482SBill Paul 
557495d67482SBill Paul /*
557595d67482SBill Paul  * Report current media status.
557695d67482SBill Paul  */
557795d67482SBill Paul static void
55783f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
557995d67482SBill Paul {
558067d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
558195d67482SBill Paul 	struct mii_data *mii;
558295d67482SBill Paul 
558367d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
558495d67482SBill Paul 
5585652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
558695d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
558795d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
558895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
558995d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
559095d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
55914c0da0ffSGleb Smirnoff 		else {
55924c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
559367d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
55944c0da0ffSGleb Smirnoff 			return;
55954c0da0ffSGleb Smirnoff 		}
559695d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
559795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
559895d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
559995d67482SBill Paul 		else
560095d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
560167d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
560295d67482SBill Paul 		return;
560395d67482SBill Paul 	}
560495d67482SBill Paul 
560595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
560695d67482SBill Paul 	mii_pollstat(mii);
560795d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
560895d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
560967d5e043SOleg Bulyzhin 
561067d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
561195d67482SBill Paul }
561295d67482SBill Paul 
561395d67482SBill Paul static int
56143f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
561595d67482SBill Paul {
561695d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
561795d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
561895d67482SBill Paul 	struct mii_data *mii;
5619f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
562095d67482SBill Paul 
562195d67482SBill Paul 	switch (command) {
562295d67482SBill Paul 	case SIOCSIFMTU:
5623f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5624f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
56254c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5626f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
562795d67482SBill Paul 				error = EINVAL;
5628f5459d4cSPyun YongHyeon 				break;
5629f5459d4cSPyun YongHyeon 			}
5630f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5631f5459d4cSPyun YongHyeon 			error = EINVAL;
5632f5459d4cSPyun YongHyeon 			break;
5633f5459d4cSPyun YongHyeon 		}
5634f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5635f5459d4cSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
563695d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
56373a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
563813f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
56393a429c8fSPyun YongHyeon 				bge_init_locked(sc);
564095d67482SBill Paul 			}
56413a429c8fSPyun YongHyeon 		}
56423a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
564395d67482SBill Paul 		break;
564495d67482SBill Paul 	case SIOCSIFFLAGS:
56450f9bd73bSSam Leffler 		BGE_LOCK(sc);
564695d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
564795d67482SBill Paul 			/*
564895d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
564995d67482SBill Paul 			 * then just use the 'set promisc mode' command
565095d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
565195d67482SBill Paul 			 * a full re-init means reloading the firmware and
565295d67482SBill Paul 			 * waiting for it to start up, which may take a
5653d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
565495d67482SBill Paul 			 */
5655f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5656f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
56573e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
56583e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5659f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5660d183af7fSRuslan Ermilov 					bge_setmulti(sc);
566195d67482SBill Paul 			} else
56620f9bd73bSSam Leffler 				bge_init_locked(sc);
566395d67482SBill Paul 		} else {
566413f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
566595d67482SBill Paul 				bge_stop(sc);
566695d67482SBill Paul 			}
566795d67482SBill Paul 		}
566895d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
56690f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
567095d67482SBill Paul 		error = 0;
567195d67482SBill Paul 		break;
567295d67482SBill Paul 	case SIOCADDMULTI:
567395d67482SBill Paul 	case SIOCDELMULTI:
567413f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
56750f9bd73bSSam Leffler 			BGE_LOCK(sc);
567695d67482SBill Paul 			bge_setmulti(sc);
56770f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
567895d67482SBill Paul 			error = 0;
567995d67482SBill Paul 		}
568095d67482SBill Paul 		break;
568195d67482SBill Paul 	case SIOCSIFMEDIA:
568295d67482SBill Paul 	case SIOCGIFMEDIA:
5683652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
568495d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
568595d67482SBill Paul 			    &sc->bge_ifmedia, command);
568695d67482SBill Paul 		} else {
568795d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
568895d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
568995d67482SBill Paul 			    &mii->mii_media, command);
569095d67482SBill Paul 		}
569195d67482SBill Paul 		break;
569295d67482SBill Paul 	case SIOCSIFCAP:
569395d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
569475719184SGleb Smirnoff #ifdef DEVICE_POLLING
569575719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
569675719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
569775719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
569875719184SGleb Smirnoff 				if (error)
569975719184SGleb Smirnoff 					return (error);
570075719184SGleb Smirnoff 				BGE_LOCK(sc);
570175719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
570275719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
570338cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
570475719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
570575719184SGleb Smirnoff 				BGE_UNLOCK(sc);
570675719184SGleb Smirnoff 			} else {
570775719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
570875719184SGleb Smirnoff 				/* Enable interrupt even in error case */
570975719184SGleb Smirnoff 				BGE_LOCK(sc);
571075719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
571175719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
571238cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
571375719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
571475719184SGleb Smirnoff 				BGE_UNLOCK(sc);
571575719184SGleb Smirnoff 			}
571675719184SGleb Smirnoff 		}
571775719184SGleb Smirnoff #endif
5718d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5719d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
5720d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
5721d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
572235f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
572395d67482SBill Paul 			else
572435f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
572595d67482SBill Paul 		}
5726cb2eacc7SYaroslav Tykhiy 
5727d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5728d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
5729d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
5730d8b57f98SPyun YongHyeon 
5731ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5732ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
5733ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
5734ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
5735ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
5736ca3f1187SPyun YongHyeon 			else
5737ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
5738ca3f1187SPyun YongHyeon 		}
5739ca3f1187SPyun YongHyeon 
5740cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5741cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
5742cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5743cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5744cb2eacc7SYaroslav Tykhiy 		}
5745cb2eacc7SYaroslav Tykhiy 
574604bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
574704bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
574804bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
574904bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
575004bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
5751cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
575204bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
575304bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
5754cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5755cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5756cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
575704bde852SPyun YongHyeon 		}
5758cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5759cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
5760cb2eacc7SYaroslav Tykhiy #endif
576195d67482SBill Paul 		break;
576295d67482SBill Paul 	default:
5763673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
576495d67482SBill Paul 		break;
576595d67482SBill Paul 	}
576695d67482SBill Paul 
576795d67482SBill Paul 	return (error);
576895d67482SBill Paul }
576995d67482SBill Paul 
577095d67482SBill Paul static void
5771b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
577295d67482SBill Paul {
5773b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
577495d67482SBill Paul 
5775b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5776b74e67fbSGleb Smirnoff 
5777b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5778b74e67fbSGleb Smirnoff 		return;
5779b74e67fbSGleb Smirnoff 
5780b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
578195d67482SBill Paul 
5782fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
578395d67482SBill Paul 
578413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5785426742bfSGleb Smirnoff 	bge_init_locked(sc);
578695d67482SBill Paul 
578795d67482SBill Paul 	ifp->if_oerrors++;
578895d67482SBill Paul }
578995d67482SBill Paul 
57905a147ba6SPyun YongHyeon static void
57915a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
57925a147ba6SPyun YongHyeon {
57935a147ba6SPyun YongHyeon 	int i;
57945a147ba6SPyun YongHyeon 
57955a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
57965a147ba6SPyun YongHyeon 
57975a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
57985a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
57995a147ba6SPyun YongHyeon 			return;
58005a147ba6SPyun YongHyeon 		DELAY(100);
58015a147ba6SPyun YongHyeon         }
58025a147ba6SPyun YongHyeon }
58035a147ba6SPyun YongHyeon 
580495d67482SBill Paul /*
580595d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
580695d67482SBill Paul  * RX and TX lists.
580795d67482SBill Paul  */
580895d67482SBill Paul static void
58093f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
581095d67482SBill Paul {
581195d67482SBill Paul 	struct ifnet *ifp;
581295d67482SBill Paul 
58130f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
58140f9bd73bSSam Leffler 
5815fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
581695d67482SBill Paul 
58170f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
581895d67482SBill Paul 
581944b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
582044b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
582144b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
582244b63691SBjoern A. Zeeb 
582344b63691SBjoern A. Zeeb 	/*
582444b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
582544b63691SBjoern A. Zeeb 	 */
582644b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
5827548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
582844b63691SBjoern A. Zeeb 
582995d67482SBill Paul 	/*
58303f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
583195d67482SBill Paul 	 */
58325a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
58335a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
58345a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
58355a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
58365a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
58375a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
58385a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
58395a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
584095d67482SBill Paul 
584195d67482SBill Paul 	/*
58423f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
584395d67482SBill Paul 	 */
58445a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
58455a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
58465a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
58475a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
58485a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
58495a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
58505a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
58515a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
585295d67482SBill Paul 
585395d67482SBill Paul 	/*
585495d67482SBill Paul 	 * Shut down all of the memory managers and related
585595d67482SBill Paul 	 * state machines.
585695d67482SBill Paul 	 */
58575a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
58585a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
58595a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
58605a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
58615a147ba6SPyun YongHyeon 
58620c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
586395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
58647ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
586595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
586695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
58670434d1b8SBill Paul 	}
58682280c16bSPyun YongHyeon 	/* Update MAC statistics. */
58692280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
58702280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
587195d67482SBill Paul 
58728cb1383cSDoug Ambrisko 	bge_reset(sc);
5873548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
5874548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
58758cb1383cSDoug Ambrisko 
58768cb1383cSDoug Ambrisko 	/*
58778cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
58788cb1383cSDoug Ambrisko 	 */
58798cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
58808cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
58818cb1383cSDoug Ambrisko 	else
588295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
588395d67482SBill Paul 
588495d67482SBill Paul 	/* Free the RX lists. */
588595d67482SBill Paul 	bge_free_rx_ring_std(sc);
588695d67482SBill Paul 
588795d67482SBill Paul 	/* Free jumbo RX list. */
58884c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
588995d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
589095d67482SBill Paul 
589195d67482SBill Paul 	/* Free TX buffers. */
589295d67482SBill Paul 	bge_free_tx_ring(sc);
589395d67482SBill Paul 
589495d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
589595d67482SBill Paul 
58965dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
58971493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
58981493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
58991493e883SOleg Bulyzhin 	sc->bge_link = 0;
590095d67482SBill Paul 
59011493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
590295d67482SBill Paul }
590395d67482SBill Paul 
590495d67482SBill Paul /*
590595d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
590695d67482SBill Paul  * get confused by errant DMAs when rebooting.
590795d67482SBill Paul  */
5908b6c974e8SWarner Losh static int
59093f74909aSGleb Smirnoff bge_shutdown(device_t dev)
591095d67482SBill Paul {
591195d67482SBill Paul 	struct bge_softc *sc;
591295d67482SBill Paul 
591395d67482SBill Paul 	sc = device_get_softc(dev);
59140f9bd73bSSam Leffler 	BGE_LOCK(sc);
591595d67482SBill Paul 	bge_stop(sc);
59160f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
5917b6c974e8SWarner Losh 
5918b6c974e8SWarner Losh 	return (0);
591995d67482SBill Paul }
592014afefa3SPawel Jakub Dawidek 
592114afefa3SPawel Jakub Dawidek static int
592214afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
592314afefa3SPawel Jakub Dawidek {
592414afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
592514afefa3SPawel Jakub Dawidek 
592614afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
592714afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
592814afefa3SPawel Jakub Dawidek 	bge_stop(sc);
592914afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
593014afefa3SPawel Jakub Dawidek 
593114afefa3SPawel Jakub Dawidek 	return (0);
593214afefa3SPawel Jakub Dawidek }
593314afefa3SPawel Jakub Dawidek 
593414afefa3SPawel Jakub Dawidek static int
593514afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
593614afefa3SPawel Jakub Dawidek {
593714afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
593814afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
593914afefa3SPawel Jakub Dawidek 
594014afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
594114afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
594214afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
594314afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
594414afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
594514afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
594614afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
594714afefa3SPawel Jakub Dawidek 	}
594814afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
594914afefa3SPawel Jakub Dawidek 
595014afefa3SPawel Jakub Dawidek 	return (0);
595114afefa3SPawel Jakub Dawidek }
5952dab5cd05SOleg Bulyzhin 
5953dab5cd05SOleg Bulyzhin static void
59543f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
5955dab5cd05SOleg Bulyzhin {
59561f313773SOleg Bulyzhin 	struct mii_data *mii;
59571f313773SOleg Bulyzhin 	uint32_t link, status;
5958dab5cd05SOleg Bulyzhin 
5959dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
59601f313773SOleg Bulyzhin 
59613f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
59627b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
59637b97099dSOleg Bulyzhin 
5964dab5cd05SOleg Bulyzhin 	/*
5965dab5cd05SOleg Bulyzhin 	 * Process link state changes.
5966dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
5967dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
5968dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
5969dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
5970dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
5971dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
5972dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
5973dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
59741f313773SOleg Bulyzhin 	 *
59751f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
59764c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
5977dab5cd05SOleg Bulyzhin 	 */
5978dab5cd05SOleg Bulyzhin 
59791f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
59804c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
5981dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
5982dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
59831f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
59845dda8085SOleg Bulyzhin 			mii_pollstat(mii);
59851f313773SOleg Bulyzhin 			if (!sc->bge_link &&
59861f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
59871f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
59881f313773SOleg Bulyzhin 				sc->bge_link++;
59891f313773SOleg Bulyzhin 				if (bootverbose)
59901f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
59911f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
59921f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
59931f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
59941f313773SOleg Bulyzhin 				sc->bge_link = 0;
59951f313773SOleg Bulyzhin 				if (bootverbose)
59961f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
59971f313773SOleg Bulyzhin 			}
59981f313773SOleg Bulyzhin 
59993f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6000dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6001dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6002daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6003daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6004daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6005daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6006dab5cd05SOleg Bulyzhin 		}
6007dab5cd05SOleg Bulyzhin 		return;
6008dab5cd05SOleg Bulyzhin 	}
6009dab5cd05SOleg Bulyzhin 
6010652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
60111f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
60127b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
60137b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
60141f313773SOleg Bulyzhin 				sc->bge_link++;
60159b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
60161f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
60171f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
60189b80ffe7SPyun YongHyeon 					DELAY(40);
60199b80ffe7SPyun YongHyeon 				}
60200c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
60211f313773SOleg Bulyzhin 				if (bootverbose)
60221f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
60233f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
60243f74909aSGleb Smirnoff 				    LINK_STATE_UP);
60257b97099dSOleg Bulyzhin 			}
60261f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6027dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
60281f313773SOleg Bulyzhin 			if (bootverbose)
60291f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
60307b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
60311f313773SOleg Bulyzhin 		}
60326ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
60331f313773SOleg Bulyzhin 		/*
60340c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
60350c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
60360c8aa4eaSJung-uk Kim 		 * PHY link status directly.
60371f313773SOleg Bulyzhin 		 */
60381f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
60391f313773SOleg Bulyzhin 
60401f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
60411f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
60421f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
60435dda8085SOleg Bulyzhin 			mii_pollstat(mii);
60441f313773SOleg Bulyzhin 			if (!sc->bge_link &&
60451f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
60461f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
60471f313773SOleg Bulyzhin 				sc->bge_link++;
60481f313773SOleg Bulyzhin 				if (bootverbose)
60491f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
60501f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
60511f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
60521f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
60531f313773SOleg Bulyzhin 				sc->bge_link = 0;
60541f313773SOleg Bulyzhin 				if (bootverbose)
60551f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
60561f313773SOleg Bulyzhin 			}
60571f313773SOleg Bulyzhin 		}
60580c8aa4eaSJung-uk Kim 	} else {
60590c8aa4eaSJung-uk Kim 		/*
60606ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
60616ede2cfaSPyun YongHyeon 		 * link status.
60620c8aa4eaSJung-uk Kim 		 */
60636ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
60646ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
60656ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6066dab5cd05SOleg Bulyzhin 	}
6067dab5cd05SOleg Bulyzhin 
60682246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6069dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6070dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6071dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6072dab5cd05SOleg Bulyzhin }
60736f8718a3SScott Long 
60746f8718a3SScott Long static void
60756f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
60766f8718a3SScott Long {
60776f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
60782280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
60797e32f79aSPyun YongHyeon 	char tn[32];
60807e32f79aSPyun YongHyeon 	int unit;
60816f8718a3SScott Long 
60826f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
60836f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
60846f8718a3SScott Long 
60856f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
60866f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
60876f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
60886f8718a3SScott Long 	    "Debug Information");
60896f8718a3SScott Long 
60906f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
60916f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
6092548c8f1aSPyun YongHyeon 	    "MAC Register Read");
6093548c8f1aSPyun YongHyeon 
6094548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
6095548c8f1aSPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_ape_read, "I",
6096548c8f1aSPyun YongHyeon 	    "APE Register Read");
60976f8718a3SScott Long 
60986f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
60996f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
61006f8718a3SScott Long 	    "Memory Read");
61016f8718a3SScott Long 
61026f8718a3SScott Long #endif
6103763757b2SScott Long 
61047e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
6105beaa2ae1SPyun YongHyeon 	/*
6106beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6107beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6108beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6109beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6110beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6111beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6112beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6113beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6114beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6115beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6116beaa2ae1SPyun YongHyeon 	 */
61177e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
61187e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
61197e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
6120beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6121beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
6122beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6123beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6124beaa2ae1SPyun YongHyeon 
61252ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
61262ae7f64bSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.msi", unit);
61272ae7f64bSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_msi);
61282ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
61292ae7f64bSPyun YongHyeon 	    CTLFLAG_RD, &sc->bge_msi, 0, "Enable MSI");
61305c952e8dSPyun YongHyeon 
613135f945cdSPyun YongHyeon 	/*
613235f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
613335f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
613435f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
613535f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
613635f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
613735f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
613835f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
613935f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
614035f945cdSPyun YongHyeon 	 */
614135f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
614235f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
614335f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
614435f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
614535f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
614635f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
614735f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
614835f945cdSPyun YongHyeon 
6149d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
61502280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
61512280c16bSPyun YongHyeon 	else
61522280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
61532280c16bSPyun YongHyeon }
6154d949071dSJung-uk Kim 
61552280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
61562280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
61572280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
61582280c16bSPyun YongHyeon 	    desc)
61592280c16bSPyun YongHyeon 
61602280c16bSPyun YongHyeon static void
61612280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
61622280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
61632280c16bSPyun YongHyeon {
61642280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
61652280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
61662280c16bSPyun YongHyeon 
61672280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6168763757b2SScott Long 	    NULL, "BGE Statistics");
6169763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6170763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6171763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6172763757b2SScott Long 	    "FramesDroppedDueToFilters");
6173763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6174763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6175763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6176763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6177763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6178763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
617906e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
618006e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
618106e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
618206e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6183763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6184763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6185763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6186763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6187763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6188763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6189763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6190763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6191763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6192763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6193763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6194763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6195763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6196763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6197763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6198763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6199763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6200763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6201763757b2SScott Long 
6202763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
6203763757b2SScott Long 	    NULL, "BGE RX Statistics");
6204763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6205763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
62061cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6207763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6208763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6209763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
62101cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6211763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6212763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6213763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6214763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6215763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6216763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6217763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6218763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6219763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6220763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6221763757b2SScott Long 	    "xoffPauseFramesReceived");
6222763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6223763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6224763757b2SScott Long 	    "ControlFramesReceived");
6225763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6226763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6227763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6228763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6229763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6230763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6231763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6232763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6233763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
623406e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6235763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
623606e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6237763757b2SScott Long 
6238763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
6239763757b2SScott Long 	    NULL, "BGE TX Statistics");
6240763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6241763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
62421cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6243763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6244763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6245763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6246763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6247763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6248763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6249763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6250763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6251763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6252763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6253763757b2SScott Long 	    "InternalMacTransmitErrors");
6254763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6255763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6256763757b2SScott Long 	    "SingleCollisionFrames");
6257763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6258763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6259763757b2SScott Long 	    "MultipleCollisionFrames");
6260763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6261763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6262763757b2SScott Long 	    "DeferredTransmissions");
6263763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6264763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6265763757b2SScott Long 	    "ExcessiveCollisions");
6266763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
626706e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
626806e83c7eSScott Long 	    "LateCollisions");
6269763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
62701cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6271763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6272763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6273763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6274763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6275763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6276763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6277763757b2SScott Long 	    "CarrierSenseErrors");
6278763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6279763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6280763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6281763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6282763757b2SScott Long }
6283763757b2SScott Long 
62842280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
62852280c16bSPyun YongHyeon 
62862280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
62876dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
62882280c16bSPyun YongHyeon 
62892280c16bSPyun YongHyeon static void
62902280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
62912280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
62922280c16bSPyun YongHyeon {
62932280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
62942280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
62952280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
62962280c16bSPyun YongHyeon 
62972280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
62982280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
62992280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
63002280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
63012280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
63022280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
63032280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
63042280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
63052280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
63062280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
63072280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
63082280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
63092280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
63102280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
63112280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
63122280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
63132280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
63142280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
63152280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
63162280c16bSPyun YongHyeon 
63172280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
63182280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
63192280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
63202280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
63212280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
63222280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
63232280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
63241cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
63252280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
63262280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
63272280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
63282280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
63292280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
63302280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
63312280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
63322280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
63332280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
63342280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
63352280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
63362280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
63372280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
63382280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
63392280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
63402280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
63412280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
63422280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
63432280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
63442280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
63452280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
63462280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
63472280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
63482280c16bSPyun YongHyeon 
63492280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
63502280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
63512280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
63521cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
63532280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
63542280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
63552280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
63562280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
63572280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
63582280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
63592280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
63602280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
63612280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
63622280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
63632280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
63642280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
63652280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
63662280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
63672280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
63682280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
63692280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
63702280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
63712280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
63722280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
63732280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
63741cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
63752280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
63761cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
63772280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
63781cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
63792280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
63802280c16bSPyun YongHyeon }
63812280c16bSPyun YongHyeon 
63822280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
63832280c16bSPyun YongHyeon 
6384763757b2SScott Long static int
6385763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6386763757b2SScott Long {
6387763757b2SScott Long 	struct bge_softc *sc;
638806e83c7eSScott Long 	uint32_t result;
6389d949071dSJung-uk Kim 	int offset;
6390763757b2SScott Long 
6391763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6392763757b2SScott Long 	offset = arg2;
6393d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6394d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6395041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
63966f8718a3SScott Long }
63976f8718a3SScott Long 
63986f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
63996f8718a3SScott Long static int
64006f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
64016f8718a3SScott Long {
64026f8718a3SScott Long 	struct bge_softc *sc;
64036f8718a3SScott Long 	uint16_t *sbdata;
640428276ad6SPyun YongHyeon 	int error, result, sbsz;
64056f8718a3SScott Long 	int i, j;
64066f8718a3SScott Long 
64076f8718a3SScott Long 	result = -1;
64086f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
64096f8718a3SScott Long 	if (error || (req->newptr == NULL))
64106f8718a3SScott Long 		return (error);
64116f8718a3SScott Long 
64126f8718a3SScott Long 	if (result == 1) {
64136f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
64146f8718a3SScott Long 
641528276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
641628276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
641728276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
641828276ad6SPyun YongHyeon 		else
641928276ad6SPyun YongHyeon 			sbsz = 32;
64206f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
64216f8718a3SScott Long 		printf("Status Block:\n");
642228276ad6SPyun YongHyeon 		BGE_LOCK(sc);
642328276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
642428276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
642528276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
642628276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
64276f8718a3SScott Long 			printf("%06x:", i);
642828276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
642928276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
64306f8718a3SScott Long 			printf("\n");
64316f8718a3SScott Long 		}
64326f8718a3SScott Long 
64336f8718a3SScott Long 		printf("Registers:\n");
64340c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
64356f8718a3SScott Long 			printf("%06x:", i);
64366f8718a3SScott Long 			for (j = 0; j < 8; j++) {
64376f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
64386f8718a3SScott Long 				i += 4;
64396f8718a3SScott Long 			}
64406f8718a3SScott Long 			printf("\n");
64416f8718a3SScott Long 		}
644228276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
64436f8718a3SScott Long 
64446f8718a3SScott Long 		printf("Hardware Flags:\n");
644528276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
644628276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6447a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6448a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
64495345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
64506f8718a3SScott Long 			printf(" - 575X Plus\n");
64515345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
64526f8718a3SScott Long 			printf(" - 5705 Plus\n");
64535345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
64545345bad0SScott Long 			printf(" - 5714 Family\n");
64555345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
64565345bad0SScott Long 			printf(" - 5700 Family\n");
64576f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
64586f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
64596f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
64606f8718a3SScott Long 			printf(" - PCI-X Bus\n");
64616f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
64626f8718a3SScott Long 			printf(" - PCI Express Bus\n");
64637d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
64646f8718a3SScott Long 			printf(" - No 3 LEDs\n");
64656f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
64666f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
64676f8718a3SScott Long 	}
64686f8718a3SScott Long 
64696f8718a3SScott Long 	return (error);
64706f8718a3SScott Long }
64716f8718a3SScott Long 
64726f8718a3SScott Long static int
64736f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
64746f8718a3SScott Long {
64756f8718a3SScott Long 	struct bge_softc *sc;
64766f8718a3SScott Long 	int error;
64776f8718a3SScott Long 	uint16_t result;
64786f8718a3SScott Long 	uint32_t val;
64796f8718a3SScott Long 
64806f8718a3SScott Long 	result = -1;
64816f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
64826f8718a3SScott Long 	if (error || (req->newptr == NULL))
64836f8718a3SScott Long 		return (error);
64846f8718a3SScott Long 
64856f8718a3SScott Long 	if (result < 0x8000) {
64866f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
64876f8718a3SScott Long 		val = CSR_READ_4(sc, result);
64886f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
64896f8718a3SScott Long 	}
64906f8718a3SScott Long 
64916f8718a3SScott Long 	return (error);
64926f8718a3SScott Long }
64936f8718a3SScott Long 
64946f8718a3SScott Long static int
6495548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6496548c8f1aSPyun YongHyeon {
6497548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6498548c8f1aSPyun YongHyeon 	int error;
6499548c8f1aSPyun YongHyeon 	uint16_t result;
6500548c8f1aSPyun YongHyeon 	uint32_t val;
6501548c8f1aSPyun YongHyeon 
6502548c8f1aSPyun YongHyeon 	result = -1;
6503548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6504548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6505548c8f1aSPyun YongHyeon 		return (error);
6506548c8f1aSPyun YongHyeon 
6507548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6508548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6509548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6510548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6511548c8f1aSPyun YongHyeon 	}
6512548c8f1aSPyun YongHyeon 
6513548c8f1aSPyun YongHyeon 	return (error);
6514548c8f1aSPyun YongHyeon }
6515548c8f1aSPyun YongHyeon 
6516548c8f1aSPyun YongHyeon static int
65176f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
65186f8718a3SScott Long {
65196f8718a3SScott Long 	struct bge_softc *sc;
65206f8718a3SScott Long 	int error;
65216f8718a3SScott Long 	uint16_t result;
65226f8718a3SScott Long 	uint32_t val;
65236f8718a3SScott Long 
65246f8718a3SScott Long 	result = -1;
65256f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65266f8718a3SScott Long 	if (error || (req->newptr == NULL))
65276f8718a3SScott Long 		return (error);
65286f8718a3SScott Long 
65296f8718a3SScott Long 	if (result < 0x8000) {
65306f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65316f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
65326f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
65336f8718a3SScott Long 	}
65346f8718a3SScott Long 
65356f8718a3SScott Long 	return (error);
65366f8718a3SScott Long }
65376f8718a3SScott Long #endif
653838cc658fSJohn Baldwin 
653938cc658fSJohn Baldwin static int
65405fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
65415fea260fSMarius Strobl {
65425fea260fSMarius Strobl 
65435fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
65445fea260fSMarius Strobl 		return (1);
65455fea260fSMarius Strobl 
65465fea260fSMarius Strobl #ifdef __sparc64__
65475fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
65485fea260fSMarius Strobl 	return (0);
65495fea260fSMarius Strobl #endif
65505fea260fSMarius Strobl 	return (1);
65515fea260fSMarius Strobl }
65525fea260fSMarius Strobl 
65535fea260fSMarius Strobl static int
655438cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
655538cc658fSJohn Baldwin {
655638cc658fSJohn Baldwin 	uint32_t mac_addr;
655738cc658fSJohn Baldwin 
655873635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
655938cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
656038cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
656138cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
656273635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
656338cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
656438cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
656538cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
656638cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
65675fea260fSMarius Strobl 		return (0);
656838cc658fSJohn Baldwin 	}
65695fea260fSMarius Strobl 	return (1);
657038cc658fSJohn Baldwin }
657138cc658fSJohn Baldwin 
657238cc658fSJohn Baldwin static int
657338cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
657438cc658fSJohn Baldwin {
657538cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
657638cc658fSJohn Baldwin 
657738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
657838cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
657938cc658fSJohn Baldwin 
65805fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
65815fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
658238cc658fSJohn Baldwin }
658338cc658fSJohn Baldwin 
658438cc658fSJohn Baldwin static int
658538cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
658638cc658fSJohn Baldwin {
658738cc658fSJohn Baldwin 
65885fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
65895fea260fSMarius Strobl 		return (1);
65905fea260fSMarius Strobl 
65915fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
65925fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
659338cc658fSJohn Baldwin }
659438cc658fSJohn Baldwin 
659538cc658fSJohn Baldwin static int
659638cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
659738cc658fSJohn Baldwin {
659838cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
659938cc658fSJohn Baldwin 		/* NOTE: Order is critical */
66005fea260fSMarius Strobl 		bge_get_eaddr_fw,
660138cc658fSJohn Baldwin 		bge_get_eaddr_mem,
660238cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
660338cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
660438cc658fSJohn Baldwin 		NULL
660538cc658fSJohn Baldwin 	};
660638cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
660738cc658fSJohn Baldwin 
660838cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
660938cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
661038cc658fSJohn Baldwin 			break;
661138cc658fSJohn Baldwin 	}
661238cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
661338cc658fSJohn Baldwin }
6614