xref: /freebsd/sys/dev/bge/if_bge.c (revision 40438c47610ea302b68b9715d861285385f4486f)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
38d7acafa1SMarius Strobl  * Broadcom BCM57xx(x)/BCM590x NetXtreme and NetLink family Ethernet driver
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
41d7acafa1SMarius Strobl  * Alteon Networks as part of the Tigon I and Tigon II Gigabit Ethernet
4222a4ecedSMarius Strobl  * MAC chips. The BCM5700, sometimes referred to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
82f1a7e6d5SScott Long #include <sys/sysctl.h>
83dfe0df9aSPyun YongHyeon #include <sys/taskqueue.h>
8495d67482SBill Paul 
8595d67482SBill Paul #include <net/if.h>
8676039bc8SGleb Smirnoff #include <net/if_var.h>
8795d67482SBill Paul #include <net/if_arp.h>
8895d67482SBill Paul #include <net/ethernet.h>
8995d67482SBill Paul #include <net/if_dl.h>
9095d67482SBill Paul #include <net/if_media.h>
9195d67482SBill Paul 
9295d67482SBill Paul #include <net/bpf.h>
9395d67482SBill Paul 
9495d67482SBill Paul #include <net/if_types.h>
9595d67482SBill Paul #include <net/if_vlan_var.h>
9695d67482SBill Paul 
9795d67482SBill Paul #include <netinet/in_systm.h>
9895d67482SBill Paul #include <netinet/in.h>
9995d67482SBill Paul #include <netinet/ip.h>
100ca3f1187SPyun YongHyeon #include <netinet/tcp.h>
10195d67482SBill Paul 
10295d67482SBill Paul #include <machine/bus.h>
10395d67482SBill Paul #include <machine/resource.h>
10495d67482SBill Paul #include <sys/bus.h>
10595d67482SBill Paul #include <sys/rman.h>
10695d67482SBill Paul 
10795d67482SBill Paul #include <dev/mii/mii.h>
10895d67482SBill Paul #include <dev/mii/miivar.h>
1092d3ce713SDavid E. O'Brien #include "miidevs.h"
11095d67482SBill Paul #include <dev/mii/brgphyreg.h>
11195d67482SBill Paul 
11208013fd3SMarius Strobl #ifdef __sparc64__
11308013fd3SMarius Strobl #include <dev/ofw/ofw_bus.h>
11408013fd3SMarius Strobl #include <dev/ofw/openfirm.h>
11508013fd3SMarius Strobl #include <machine/ofw_machdep.h>
11608013fd3SMarius Strobl #include <machine/ver.h>
11708013fd3SMarius Strobl #endif
11808013fd3SMarius Strobl 
1194fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1204fbd232cSWarner Losh #include <dev/pci/pcivar.h>
12195d67482SBill Paul 
12295d67482SBill Paul #include <dev/bge/if_bgereg.h>
12395d67482SBill Paul 
12435f945cdSPyun YongHyeon #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP)
125d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12695d67482SBill Paul 
127f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
128f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12995d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
13095d67482SBill Paul 
1317b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
13295d67482SBill Paul #include "miibus_if.h"
13395d67482SBill Paul 
13495d67482SBill Paul /*
13595d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13695d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13795d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13895d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13995d67482SBill Paul  */
140852c67f9SMarius Strobl static const struct bge_type {
1414c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1424c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
14329658c96SDimitry Andric } bge_devs[] = {
1444c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1454c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
14695d67482SBill Paul 
1474c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1484c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1494c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1504c0da0ffSGleb Smirnoff 
1514c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1524c0da0ffSGleb Smirnoff 
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1724c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1731108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5717 },
1741108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5718 },
175bbe2ca75SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5719 },
1764c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1774c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
178effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
179a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5723 },
1802927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5725 },
1812927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5727 },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1834c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1844c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1854c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1894c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1904c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1914c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1929e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1939e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1949e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1959e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
196f7d1b2ebSXin LI 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5756 },
197a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761 },
198a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761E },
199a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761S },
200a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761SE },
2012927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5762 },
202a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5764 },
2034c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
2044c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
2054c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
2064c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
207a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5784 },
208a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785F },
209a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785G },
2109e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
2119e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
212a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787F },
2139e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
2144c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
2154c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
2164c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
2174c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
2184c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
21938cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
22038cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
221a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57760 },
222b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57761 },
223fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57762 },
22467129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57764 },
225b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57765 },
226fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57766 },
22767129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57767 },
228a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
229b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57781 },
23067129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57782 },
231b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57785 },
23267129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57786 },
23367129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57787 },
234a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
235a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
236b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57791 },
237b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57795 },
2384c0da0ffSGleb Smirnoff 
2394c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2404c0da0ffSGleb Smirnoff 
2414c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2424c0da0ffSGleb Smirnoff 
243a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
244a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
245a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PP250450 },
246a5779553SStanislav Sedov 
2474c0da0ffSGleb Smirnoff 	{ 0, 0 }
24895d67482SBill Paul };
24995d67482SBill Paul 
2504c0da0ffSGleb Smirnoff static const struct bge_vendor {
2514c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2524c0da0ffSGleb Smirnoff 	const char	*v_name;
25329658c96SDimitry Andric } bge_vendors[] = {
2544c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2554c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2564c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2584c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2594c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
260a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2614c0da0ffSGleb Smirnoff 
2624c0da0ffSGleb Smirnoff 	{ 0, NULL }
2634c0da0ffSGleb Smirnoff };
2644c0da0ffSGleb Smirnoff 
2654c0da0ffSGleb Smirnoff static const struct bge_revision {
2664c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2674c0da0ffSGleb Smirnoff 	const char	*br_name;
26829658c96SDimitry Andric } bge_revisions[] = {
2694c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2704c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2714c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2724c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2744c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2764c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2794c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2824c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2844c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2859e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2884c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2894c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2904c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2914c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2924c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2934c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2944c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2954c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2964c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2974c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2984c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2994c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
3004c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
3014c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
30242787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
3034c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
3044c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
3054c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
3064c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
3074c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
3084c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
3094c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
3104c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
3110c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
3121108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_A0,	"BCM5717 A0" },
3131108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_B0,	"BCM5717 B0" },
314bbe2ca75SPyun YongHyeon 	{ BGE_CHIPID_BCM5719_A0,	"BCM5719 A0" },
31550515680SPyun YongHyeon 	{ BGE_CHIPID_BCM5720_A0,	"BCM5720 A0" },
3160c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
3170c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
3180c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
319bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
320a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
321a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
3222927f01fSPyun YongHyeon 	{ BGE_CHIPID_BCM5762_A0,	"BCM5762 A0" },
323a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
324a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
32581179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3266f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
3276f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3286f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
32938cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
33038cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
331b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_A0,	"BCM57765 A0" },
332b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_B0,	"BCM57765 B0" },
333a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
334a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3354c0da0ffSGleb Smirnoff 
3364c0da0ffSGleb Smirnoff 	{ 0, NULL }
3374c0da0ffSGleb Smirnoff };
3384c0da0ffSGleb Smirnoff 
3394c0da0ffSGleb Smirnoff /*
3404c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3414c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3424c0da0ffSGleb Smirnoff  */
34329658c96SDimitry Andric static const struct bge_revision bge_majorrevs[] = {
3449e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3459e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3469e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3479e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3489e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3499e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3509e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3519e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3529e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3539e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3549e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
355a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
356a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
357a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
35881179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3596f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
36038cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
361b4a256acSPyun YongHyeon 	{ BGE_ASICREV_BCM57765,		"unknown BCM57765" },
362fe26ad88SPyun YongHyeon 	{ BGE_ASICREV_BCM57766,		"unknown BCM57766" },
363a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3641108273aSPyun YongHyeon 	{ BGE_ASICREV_BCM5717,		"unknown BCM5717" },
365bbe2ca75SPyun YongHyeon 	{ BGE_ASICREV_BCM5719,		"unknown BCM5719" },
36650515680SPyun YongHyeon 	{ BGE_ASICREV_BCM5720,		"unknown BCM5720" },
3672927f01fSPyun YongHyeon 	{ BGE_ASICREV_BCM5762,		"unknown BCM5762" },
3684c0da0ffSGleb Smirnoff 
3694c0da0ffSGleb Smirnoff 	{ 0, NULL }
3704c0da0ffSGleb Smirnoff };
3714c0da0ffSGleb Smirnoff 
3720c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3730c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3740c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3750c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3760c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
377a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3781108273aSPyun YongHyeon #define	BGE_IS_5717_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5717_PLUS)
379fe26ad88SPyun YongHyeon #define	BGE_IS_57765_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_57765_PLUS)
3804c0da0ffSGleb Smirnoff 
381d7acafa1SMarius Strobl static uint32_t bge_chipid(device_t);
382d7acafa1SMarius Strobl static const struct bge_vendor * bge_lookup_vendor(uint16_t);
383d7acafa1SMarius Strobl static const struct bge_revision * bge_lookup_rev(uint32_t);
38438cc658fSJohn Baldwin 
38538cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
38638cc658fSJohn Baldwin 
387e51a25f8SAlfred Perlstein static int bge_probe(device_t);
388e51a25f8SAlfred Perlstein static int bge_attach(device_t);
389e51a25f8SAlfred Perlstein static int bge_detach(device_t);
39014afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
39114afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3923f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
393f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3945b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
395f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
3965b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
3975b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
398f41ac2beSBill Paul 
399ea9c3a30SPyun YongHyeon static void bge_devinfo(struct bge_softc *);
400062af0b0SPyun YongHyeon static int bge_mbox_reorder(struct bge_softc *);
401062af0b0SPyun YongHyeon 
4025fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
40338cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
40438cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
40538cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
40638cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
40738cc658fSJohn Baldwin 
408b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
4091108273aSPyun YongHyeon static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
410dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
41195d67482SBill Paul 
4128cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
413e51a25f8SAlfred Perlstein static void bge_tick(void *);
4142280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
415e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
4163f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
417d598b626SPyun YongHyeon static struct mbuf *bge_check_short_dma(struct mbuf *);
4182e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
4191108273aSPyun YongHyeon     uint16_t *, uint16_t *);
420676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
42195d67482SBill Paul 
422e51a25f8SAlfred Perlstein static void bge_intr(void *);
423dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
424dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
425fba8b109SMarcel Moolenaar static void bge_start_locked(if_t);
426fba8b109SMarcel Moolenaar static void bge_start(if_t);
427fba8b109SMarcel Moolenaar static int bge_ioctl(if_t, u_long, caddr_t);
4280f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
429e51a25f8SAlfred Perlstein static void bge_init(void *);
4305a147ba6SPyun YongHyeon static void bge_stop_block(struct bge_softc *, bus_size_t, uint32_t);
431e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
432b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
433b6c974e8SWarner Losh static int bge_shutdown(device_t);
434fba8b109SMarcel Moolenaar static int bge_ifmedia_upd_locked(if_t);
435fba8b109SMarcel Moolenaar static int bge_ifmedia_upd(if_t);
436fba8b109SMarcel Moolenaar static void bge_ifmedia_sts(if_t, struct ifmediareq *);
437df360178SGleb Smirnoff static uint64_t bge_get_counter(if_t, ift_counter);
43895d67482SBill Paul 
43938cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
44038cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
44138cc658fSJohn Baldwin 
4423f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
443e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
44495d67482SBill Paul 
4453e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
446e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
447cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
44895d67482SBill Paul 
449e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
450e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
451943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
452943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
453e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
454e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
455e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
456e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
457e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
458e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
45995d67482SBill Paul 
460e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
461e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
46250515680SPyun YongHyeon static uint32_t bge_dma_swap_options(struct bge_softc *);
46395d67482SBill Paul 
4645fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4653f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
466e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
46738cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
46895d67482SBill Paul #ifdef notdef
4693f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
47095d67482SBill Paul #endif
4719ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
472e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
47395d67482SBill Paul 
474e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
475e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
476e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
47775719184SGleb Smirnoff #ifdef DEVICE_POLLING
478fba8b109SMarcel Moolenaar static int bge_poll(if_t ifp, enum poll_cmd cmd, int count);
47975719184SGleb Smirnoff #endif
48095d67482SBill Paul 
481548c8f1aSPyun YongHyeon #define	BGE_RESET_SHUTDOWN	0
4828cb1383cSDoug Ambrisko #define	BGE_RESET_START		1
483548c8f1aSPyun YongHyeon #define	BGE_RESET_SUSPEND	2
4848cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4858cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4868cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
487797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4888cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
489dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
49095d67482SBill Paul 
491548c8f1aSPyun YongHyeon static void bge_ape_lock_init(struct bge_softc *);
492548c8f1aSPyun YongHyeon static void bge_ape_read_fw_ver(struct bge_softc *);
493548c8f1aSPyun YongHyeon static int bge_ape_lock(struct bge_softc *, int);
494548c8f1aSPyun YongHyeon static void bge_ape_unlock(struct bge_softc *, int);
495548c8f1aSPyun YongHyeon static void bge_ape_send_event(struct bge_softc *, uint32_t);
496548c8f1aSPyun YongHyeon static void bge_ape_driver_state_change(struct bge_softc *, int);
497548c8f1aSPyun YongHyeon 
4986f8718a3SScott Long /*
4996f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
5006f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
5016f8718a3SScott Long  * traps on certain architectures.
5026f8718a3SScott Long  */
5036f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
5046f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
5056f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
506548c8f1aSPyun YongHyeon static int bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS);
5076f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
5086f8718a3SScott Long #endif
5096f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
5102280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
5112280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
5122280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
5132280c16bSPyun YongHyeon     struct sysctl_oid_list *);
514763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
5156f8718a3SScott Long 
51695d67482SBill Paul static device_method_t bge_methods[] = {
51795d67482SBill Paul 	/* Device interface */
51895d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
51995d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
52095d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
52195d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
52214afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
52314afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
52495d67482SBill Paul 
52595d67482SBill Paul 	/* MII interface */
52695d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
52795d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
52895d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
52995d67482SBill Paul 
5304b7ec270SMarius Strobl 	DEVMETHOD_END
53195d67482SBill Paul };
53295d67482SBill Paul 
53395d67482SBill Paul static driver_t bge_driver = {
53495d67482SBill Paul 	"bge",
53595d67482SBill Paul 	bge_methods,
53695d67482SBill Paul 	sizeof(struct bge_softc)
53795d67482SBill Paul };
53895d67482SBill Paul 
53995d67482SBill Paul static devclass_t bge_devclass;
54095d67482SBill Paul 
541f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
54295d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
54395d67482SBill Paul 
544f1a7e6d5SScott Long static int bge_allow_asf = 1;
545f1a7e6d5SScott Long 
5466472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
547af3b2549SHans Petter Selasky SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RDTUN, &bge_allow_asf, 0,
548f1a7e6d5SScott Long 	"Allow ASF mode if available");
549c4529f41SMichael Reifenberger 
55008013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
55108013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
55208013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
55308013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
55408013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
55508013fd3SMarius Strobl 
55608013fd3SMarius Strobl static int
5575fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
55808013fd3SMarius Strobl {
55908013fd3SMarius Strobl #ifdef __sparc64__
56008013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
56108013fd3SMarius Strobl 	device_t dev;
56208013fd3SMarius Strobl 	uint32_t subvendor;
56308013fd3SMarius Strobl 
56408013fd3SMarius Strobl 	dev = sc->bge_dev;
56508013fd3SMarius Strobl 
56608013fd3SMarius Strobl 	/*
56708013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
56808013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
56908013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
57008013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
57108013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
57208013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
57308013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
57408013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
57508013fd3SMarius Strobl 	 * there.
57608013fd3SMarius Strobl 	 */
57708013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
57808013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5792d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
58008013fd3SMarius Strobl 		return (0);
58108013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
58208013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
58308013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
58408013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
58508013fd3SMarius Strobl 			return (0);
58608013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
58708013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
58808013fd3SMarius Strobl 			return (0);
58908013fd3SMarius Strobl 	}
59008013fd3SMarius Strobl #endif
59108013fd3SMarius Strobl 	return (1);
59208013fd3SMarius Strobl }
59308013fd3SMarius Strobl 
5943f74909aSGleb Smirnoff static uint32_t
5953f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
59695d67482SBill Paul {
59795d67482SBill Paul 	device_t dev;
5986f8718a3SScott Long 	uint32_t val;
59995d67482SBill Paul 
600a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
601a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
602a4431ebaSPyun YongHyeon 		return (0);
603a4431ebaSPyun YongHyeon 
60495d67482SBill Paul 	dev = sc->bge_dev;
60595d67482SBill Paul 
60695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
6076f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
6086f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
6096f8718a3SScott Long 	return (val);
61095d67482SBill Paul }
61195d67482SBill Paul 
61295d67482SBill Paul static void
6133f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
61495d67482SBill Paul {
61595d67482SBill Paul 	device_t dev;
61695d67482SBill Paul 
617a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
618a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
619a4431ebaSPyun YongHyeon 		return;
620a4431ebaSPyun YongHyeon 
62195d67482SBill Paul 	dev = sc->bge_dev;
62295d67482SBill Paul 
62395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
62495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
6256f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
62695d67482SBill Paul }
62795d67482SBill Paul 
62895d67482SBill Paul #ifdef notdef
6293f74909aSGleb Smirnoff static uint32_t
6303f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
63195d67482SBill Paul {
63295d67482SBill Paul 	device_t dev;
63395d67482SBill Paul 
63495d67482SBill Paul 	dev = sc->bge_dev;
63595d67482SBill Paul 
63695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
63795d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
63895d67482SBill Paul }
63995d67482SBill Paul #endif
64095d67482SBill Paul 
64195d67482SBill Paul static void
6423f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
64395d67482SBill Paul {
64495d67482SBill Paul 	device_t dev;
64595d67482SBill Paul 
64695d67482SBill Paul 	dev = sc->bge_dev;
64795d67482SBill Paul 
64895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
64995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
65095d67482SBill Paul }
65195d67482SBill Paul 
6526f8718a3SScott Long static void
6536f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6546f8718a3SScott Long {
6556f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6566f8718a3SScott Long }
6576f8718a3SScott Long 
65838cc658fSJohn Baldwin static void
65938cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
66038cc658fSJohn Baldwin {
66138cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
66238cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
66338cc658fSJohn Baldwin 
66438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
665062af0b0SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
666062af0b0SPyun YongHyeon 		CSR_READ_4(sc, off);
66738cc658fSJohn Baldwin }
66838cc658fSJohn Baldwin 
669f41ac2beSBill Paul /*
670548c8f1aSPyun YongHyeon  * Clear all stale locks and select the lock for this driver instance.
671548c8f1aSPyun YongHyeon  */
672548c8f1aSPyun YongHyeon static void
673548c8f1aSPyun YongHyeon bge_ape_lock_init(struct bge_softc *sc)
674548c8f1aSPyun YongHyeon {
675548c8f1aSPyun YongHyeon 	uint32_t bit, regbase;
676548c8f1aSPyun YongHyeon 	int i;
677548c8f1aSPyun YongHyeon 
678548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
679548c8f1aSPyun YongHyeon 		regbase = BGE_APE_LOCK_GRANT;
680548c8f1aSPyun YongHyeon 	else
681548c8f1aSPyun YongHyeon 		regbase = BGE_APE_PER_LOCK_GRANT;
682548c8f1aSPyun YongHyeon 
683548c8f1aSPyun YongHyeon 	/* Clear any stale locks. */
684548c8f1aSPyun YongHyeon 	for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
685548c8f1aSPyun YongHyeon 		switch (i) {
686548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY0:
687548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY1:
688548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY2:
689548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY3:
690548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
691548c8f1aSPyun YongHyeon 			break;
692548c8f1aSPyun YongHyeon 		default:
693bd9c196aSPyun YongHyeon 			if (sc->bge_func_addr == 0)
694548c8f1aSPyun YongHyeon 				bit = BGE_APE_LOCK_GRANT_DRIVER0;
695548c8f1aSPyun YongHyeon 			else
696548c8f1aSPyun YongHyeon 				bit = (1 << sc->bge_func_addr);
697548c8f1aSPyun YongHyeon 		}
698548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, regbase + 4 * i, bit);
699548c8f1aSPyun YongHyeon 	}
700548c8f1aSPyun YongHyeon 
701548c8f1aSPyun YongHyeon 	/* Select the PHY lock based on the device's function number. */
702548c8f1aSPyun YongHyeon 	switch (sc->bge_func_addr) {
703548c8f1aSPyun YongHyeon 	case 0:
704548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY0;
705548c8f1aSPyun YongHyeon 		break;
706548c8f1aSPyun YongHyeon 	case 1:
707548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY1;
708548c8f1aSPyun YongHyeon 		break;
709548c8f1aSPyun YongHyeon 	case 2:
710548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY2;
711548c8f1aSPyun YongHyeon 		break;
712548c8f1aSPyun YongHyeon 	case 3:
713548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY3;
714548c8f1aSPyun YongHyeon 		break;
715548c8f1aSPyun YongHyeon 	default:
716548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev,
717548c8f1aSPyun YongHyeon 		    "PHY lock not supported on this function\n");
718548c8f1aSPyun YongHyeon 	}
719548c8f1aSPyun YongHyeon }
720548c8f1aSPyun YongHyeon 
721548c8f1aSPyun YongHyeon /*
722548c8f1aSPyun YongHyeon  * Check for APE firmware, set flags, and print version info.
723548c8f1aSPyun YongHyeon  */
724548c8f1aSPyun YongHyeon static void
725548c8f1aSPyun YongHyeon bge_ape_read_fw_ver(struct bge_softc *sc)
726548c8f1aSPyun YongHyeon {
727548c8f1aSPyun YongHyeon 	const char *fwtype;
728548c8f1aSPyun YongHyeon 	uint32_t apedata, features;
729548c8f1aSPyun YongHyeon 
730548c8f1aSPyun YongHyeon 	/* Check for a valid APE signature in shared memory. */
731548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
732548c8f1aSPyun YongHyeon 	if (apedata != BGE_APE_SEG_SIG_MAGIC) {
733548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags &= ~ BGE_MFW_ON_APE;
734548c8f1aSPyun YongHyeon 		return;
735548c8f1aSPyun YongHyeon 	}
736548c8f1aSPyun YongHyeon 
737548c8f1aSPyun YongHyeon 	/* Check if APE firmware is running. */
738548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
739548c8f1aSPyun YongHyeon 	if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
740548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE signature found "
741548c8f1aSPyun YongHyeon 		    "but FW status not ready! 0x%08x\n", apedata);
742548c8f1aSPyun YongHyeon 		return;
743548c8f1aSPyun YongHyeon 	}
744548c8f1aSPyun YongHyeon 
745548c8f1aSPyun YongHyeon 	sc->bge_mfw_flags |= BGE_MFW_ON_APE;
746548c8f1aSPyun YongHyeon 
747548c8f1aSPyun YongHyeon 	/* Fetch the APE firwmare type and version. */
748548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
749548c8f1aSPyun YongHyeon 	features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
750548c8f1aSPyun YongHyeon 	if ((features & BGE_APE_FW_FEATURE_NCSI) != 0) {
751548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_NCSI;
752548c8f1aSPyun YongHyeon 		fwtype = "NCSI";
753548c8f1aSPyun YongHyeon 	} else if ((features & BGE_APE_FW_FEATURE_DASH) != 0) {
754548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_DASH;
755548c8f1aSPyun YongHyeon 		fwtype = "DASH";
756548c8f1aSPyun YongHyeon 	} else
757548c8f1aSPyun YongHyeon 		fwtype = "UNKN";
758548c8f1aSPyun YongHyeon 
759548c8f1aSPyun YongHyeon 	/* Print the APE firmware version. */
760548c8f1aSPyun YongHyeon 	device_printf(sc->bge_dev, "APE FW version: %s v%d.%d.%d.%d\n",
761548c8f1aSPyun YongHyeon 	    fwtype,
762548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
763548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
764548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
765548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_BLDMSK));
766548c8f1aSPyun YongHyeon }
767548c8f1aSPyun YongHyeon 
768548c8f1aSPyun YongHyeon static int
769548c8f1aSPyun YongHyeon bge_ape_lock(struct bge_softc *sc, int locknum)
770548c8f1aSPyun YongHyeon {
771548c8f1aSPyun YongHyeon 	uint32_t bit, gnt, req, status;
772548c8f1aSPyun YongHyeon 	int i, off;
773548c8f1aSPyun YongHyeon 
774548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
775548c8f1aSPyun YongHyeon 		return (0);
776548c8f1aSPyun YongHyeon 
777548c8f1aSPyun YongHyeon 	/* Lock request/grant registers have different bases. */
778548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761) {
779548c8f1aSPyun YongHyeon 		req = BGE_APE_LOCK_REQ;
780548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
781548c8f1aSPyun YongHyeon 	} else {
782548c8f1aSPyun YongHyeon 		req = BGE_APE_PER_LOCK_REQ;
783548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
784548c8f1aSPyun YongHyeon 	}
785548c8f1aSPyun YongHyeon 
786548c8f1aSPyun YongHyeon 	off = 4 * locknum;
787548c8f1aSPyun YongHyeon 
788548c8f1aSPyun YongHyeon 	switch (locknum) {
789548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
790548c8f1aSPyun YongHyeon 		/* Lock required when using GPIO. */
791548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
792548c8f1aSPyun YongHyeon 			return (0);
793548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
794548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
795548c8f1aSPyun YongHyeon 		else
796548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
797548c8f1aSPyun YongHyeon 		break;
798548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
799548c8f1aSPyun YongHyeon 		/* Lock required to reset the device. */
800548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
801548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
802548c8f1aSPyun YongHyeon 		else
803548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
804548c8f1aSPyun YongHyeon 		break;
805548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
806548c8f1aSPyun YongHyeon 		/* Lock required when accessing certain APE memory. */
807548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
808548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
809548c8f1aSPyun YongHyeon 		else
810548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
811548c8f1aSPyun YongHyeon 		break;
812548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
813548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
814548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
815548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
816548c8f1aSPyun YongHyeon 		/* Lock required when accessing PHYs. */
817548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_REQ_DRIVER0;
818548c8f1aSPyun YongHyeon 		break;
819548c8f1aSPyun YongHyeon 	default:
820548c8f1aSPyun YongHyeon 		return (EINVAL);
821548c8f1aSPyun YongHyeon 	}
822548c8f1aSPyun YongHyeon 
823548c8f1aSPyun YongHyeon 	/* Request a lock. */
824548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, req + off, bit);
825548c8f1aSPyun YongHyeon 
826548c8f1aSPyun YongHyeon 	/* Wait up to 1 second to acquire lock. */
827548c8f1aSPyun YongHyeon 	for (i = 0; i < 20000; i++) {
828548c8f1aSPyun YongHyeon 		status = APE_READ_4(sc, gnt + off);
829548c8f1aSPyun YongHyeon 		if (status == bit)
830548c8f1aSPyun YongHyeon 			break;
831548c8f1aSPyun YongHyeon 		DELAY(50);
832548c8f1aSPyun YongHyeon 	}
833548c8f1aSPyun YongHyeon 
834548c8f1aSPyun YongHyeon 	/* Handle any errors. */
835548c8f1aSPyun YongHyeon 	if (status != bit) {
836548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE lock %d request failed! "
837548c8f1aSPyun YongHyeon 		    "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
838548c8f1aSPyun YongHyeon 		    locknum, req + off, bit & 0xFFFF, gnt + off,
839548c8f1aSPyun YongHyeon 		    status & 0xFFFF);
840548c8f1aSPyun YongHyeon 		/* Revoke the lock request. */
841548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, gnt + off, bit);
842548c8f1aSPyun YongHyeon 		return (EBUSY);
843548c8f1aSPyun YongHyeon 	}
844548c8f1aSPyun YongHyeon 
845548c8f1aSPyun YongHyeon 	return (0);
846548c8f1aSPyun YongHyeon }
847548c8f1aSPyun YongHyeon 
848548c8f1aSPyun YongHyeon static void
849548c8f1aSPyun YongHyeon bge_ape_unlock(struct bge_softc *sc, int locknum)
850548c8f1aSPyun YongHyeon {
851548c8f1aSPyun YongHyeon 	uint32_t bit, gnt;
852548c8f1aSPyun YongHyeon 	int off;
853548c8f1aSPyun YongHyeon 
854548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
855548c8f1aSPyun YongHyeon 		return;
856548c8f1aSPyun YongHyeon 
857548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
858548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
859548c8f1aSPyun YongHyeon 	else
860548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
861548c8f1aSPyun YongHyeon 
862548c8f1aSPyun YongHyeon 	off = 4 * locknum;
863548c8f1aSPyun YongHyeon 
864548c8f1aSPyun YongHyeon 	switch (locknum) {
865548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
866548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
867548c8f1aSPyun YongHyeon 			return;
868548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
869548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
870548c8f1aSPyun YongHyeon 		else
871548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
872548c8f1aSPyun YongHyeon 		break;
873548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
874548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
875548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
876548c8f1aSPyun YongHyeon 		else
877548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
878548c8f1aSPyun YongHyeon 		break;
879548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
880548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
881548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
882548c8f1aSPyun YongHyeon 		else
883548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
884548c8f1aSPyun YongHyeon 		break;
885548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
886548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
887548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
888548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
889548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_GRANT_DRIVER0;
890548c8f1aSPyun YongHyeon 		break;
891548c8f1aSPyun YongHyeon 	default:
892548c8f1aSPyun YongHyeon 		return;
893548c8f1aSPyun YongHyeon 	}
894548c8f1aSPyun YongHyeon 
895548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, gnt + off, bit);
896548c8f1aSPyun YongHyeon }
897548c8f1aSPyun YongHyeon 
898548c8f1aSPyun YongHyeon /*
899548c8f1aSPyun YongHyeon  * Send an event to the APE firmware.
900548c8f1aSPyun YongHyeon  */
901548c8f1aSPyun YongHyeon static void
902548c8f1aSPyun YongHyeon bge_ape_send_event(struct bge_softc *sc, uint32_t event)
903548c8f1aSPyun YongHyeon {
904548c8f1aSPyun YongHyeon 	uint32_t apedata;
905548c8f1aSPyun YongHyeon 	int i;
906548c8f1aSPyun YongHyeon 
907548c8f1aSPyun YongHyeon 	/* NCSI does not support APE events. */
908548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
909548c8f1aSPyun YongHyeon 		return;
910548c8f1aSPyun YongHyeon 
911548c8f1aSPyun YongHyeon 	/* Wait up to 1ms for APE to service previous event. */
912548c8f1aSPyun YongHyeon 	for (i = 10; i > 0; i--) {
913548c8f1aSPyun YongHyeon 		if (bge_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
914548c8f1aSPyun YongHyeon 			break;
915548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
916548c8f1aSPyun YongHyeon 		if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
917548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
918548c8f1aSPyun YongHyeon 			    BGE_APE_EVENT_STATUS_EVENT_PENDING);
919548c8f1aSPyun YongHyeon 			bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
920548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
921548c8f1aSPyun YongHyeon 			break;
922548c8f1aSPyun YongHyeon 		}
923548c8f1aSPyun YongHyeon 		bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
924548c8f1aSPyun YongHyeon 		DELAY(100);
925548c8f1aSPyun YongHyeon 	}
926548c8f1aSPyun YongHyeon 	if (i == 0)
927548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE event 0x%08x send timed out\n",
928548c8f1aSPyun YongHyeon 		    event);
929548c8f1aSPyun YongHyeon }
930548c8f1aSPyun YongHyeon 
931548c8f1aSPyun YongHyeon static void
932548c8f1aSPyun YongHyeon bge_ape_driver_state_change(struct bge_softc *sc, int kind)
933548c8f1aSPyun YongHyeon {
934548c8f1aSPyun YongHyeon 	uint32_t apedata, event;
935548c8f1aSPyun YongHyeon 
936548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
937548c8f1aSPyun YongHyeon 		return;
938548c8f1aSPyun YongHyeon 
939548c8f1aSPyun YongHyeon 	switch (kind) {
940548c8f1aSPyun YongHyeon 	case BGE_RESET_START:
941548c8f1aSPyun YongHyeon 		/* If this is the first load, clear the load counter. */
942548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
943548c8f1aSPyun YongHyeon 		if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC)
944548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
945548c8f1aSPyun YongHyeon 		else {
946548c8f1aSPyun YongHyeon 			apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
947548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
948548c8f1aSPyun YongHyeon 		}
949548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
950548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_SIG_MAGIC);
951548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
952548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_LEN_MAGIC);
953548c8f1aSPyun YongHyeon 
954548c8f1aSPyun YongHyeon 		/* Add some version info if bge(4) supports it. */
955548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
956548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
957548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
958548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_BEHAV_NO_PHYLOCK);
959548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
960548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
961548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
962548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_START);
963548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_START;
964548c8f1aSPyun YongHyeon 		break;
965548c8f1aSPyun YongHyeon 	case BGE_RESET_SHUTDOWN:
966548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
967548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_UNLOAD);
968548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
969548c8f1aSPyun YongHyeon 		break;
970548c8f1aSPyun YongHyeon 	case BGE_RESET_SUSPEND:
971548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
972548c8f1aSPyun YongHyeon 		break;
973548c8f1aSPyun YongHyeon 	default:
974548c8f1aSPyun YongHyeon 		return;
975548c8f1aSPyun YongHyeon 	}
976548c8f1aSPyun YongHyeon 
977548c8f1aSPyun YongHyeon 	bge_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
978548c8f1aSPyun YongHyeon 	    BGE_APE_EVENT_STATUS_STATE_CHNGE);
979548c8f1aSPyun YongHyeon }
980548c8f1aSPyun YongHyeon 
981548c8f1aSPyun YongHyeon /*
982f41ac2beSBill Paul  * Map a single buffer address.
983f41ac2beSBill Paul  */
984f41ac2beSBill Paul 
985f41ac2beSBill Paul static void
9863f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
987f41ac2beSBill Paul {
988f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
989f41ac2beSBill Paul 
990f41ac2beSBill Paul 	if (error)
991f41ac2beSBill Paul 		return;
992f41ac2beSBill Paul 
9935b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
9945b610048SPyun YongHyeon 
995f41ac2beSBill Paul 	ctx = arg;
996f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
997f41ac2beSBill Paul }
998f41ac2beSBill Paul 
99938cc658fSJohn Baldwin static uint8_t
100038cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
100138cc658fSJohn Baldwin {
100238cc658fSJohn Baldwin 	uint32_t access, byte = 0;
100338cc658fSJohn Baldwin 	int i;
100438cc658fSJohn Baldwin 
100538cc658fSJohn Baldwin 	/* Lock. */
100638cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
100738cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
100838cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
100938cc658fSJohn Baldwin 			break;
101038cc658fSJohn Baldwin 		DELAY(20);
101138cc658fSJohn Baldwin 	}
101238cc658fSJohn Baldwin 	if (i == 8000)
101338cc658fSJohn Baldwin 		return (1);
101438cc658fSJohn Baldwin 
101538cc658fSJohn Baldwin 	/* Enable access. */
101638cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
101738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
101838cc658fSJohn Baldwin 
101938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
102038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
102138cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
102238cc658fSJohn Baldwin 		DELAY(10);
102338cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
102438cc658fSJohn Baldwin 			DELAY(10);
102538cc658fSJohn Baldwin 			break;
102638cc658fSJohn Baldwin 		}
102738cc658fSJohn Baldwin 	}
102838cc658fSJohn Baldwin 
102938cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
103038cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
103138cc658fSJohn Baldwin 		return (1);
103238cc658fSJohn Baldwin 	}
103338cc658fSJohn Baldwin 
103438cc658fSJohn Baldwin 	/* Get result. */
103538cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
103638cc658fSJohn Baldwin 
103738cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
103838cc658fSJohn Baldwin 
103938cc658fSJohn Baldwin 	/* Disable access. */
104038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
104138cc658fSJohn Baldwin 
104238cc658fSJohn Baldwin 	/* Unlock. */
104338cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
104438cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
104538cc658fSJohn Baldwin 
104638cc658fSJohn Baldwin 	return (0);
104738cc658fSJohn Baldwin }
104838cc658fSJohn Baldwin 
104938cc658fSJohn Baldwin /*
105038cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
105138cc658fSJohn Baldwin  */
105238cc658fSJohn Baldwin static int
105338cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
105438cc658fSJohn Baldwin {
105538cc658fSJohn Baldwin 	int err = 0, i;
105638cc658fSJohn Baldwin 	uint8_t byte = 0;
105738cc658fSJohn Baldwin 
105838cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
105938cc658fSJohn Baldwin 		return (1);
106038cc658fSJohn Baldwin 
106138cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
106238cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
106338cc658fSJohn Baldwin 		if (err)
106438cc658fSJohn Baldwin 			break;
106538cc658fSJohn Baldwin 		*(dest + i) = byte;
106638cc658fSJohn Baldwin 	}
106738cc658fSJohn Baldwin 
106838cc658fSJohn Baldwin 	return (err ? 1 : 0);
106938cc658fSJohn Baldwin }
107038cc658fSJohn Baldwin 
107195d67482SBill Paul /*
107295d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
107395d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
107495d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
107595d67482SBill Paul  * access method.
107695d67482SBill Paul  */
10773f74909aSGleb Smirnoff static uint8_t
10783f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
107995d67482SBill Paul {
108095d67482SBill Paul 	int i;
10813f74909aSGleb Smirnoff 	uint32_t byte = 0;
108295d67482SBill Paul 
108395d67482SBill Paul 	/*
108495d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
108595d67482SBill Paul 	 * having to use the bitbang method.
108695d67482SBill Paul 	 */
108795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
108895d67482SBill Paul 
108995d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
109095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
109195d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
109295d67482SBill Paul 	DELAY(20);
109395d67482SBill Paul 
109495d67482SBill Paul 	/* Issue the read EEPROM command. */
109595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
109695d67482SBill Paul 
109795d67482SBill Paul 	/* Wait for completion */
109895d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
109995d67482SBill Paul 		DELAY(10);
110095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
110195d67482SBill Paul 			break;
110295d67482SBill Paul 	}
110395d67482SBill Paul 
1104d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
1105fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
1106f6789fbaSPyun YongHyeon 		return (1);
110795d67482SBill Paul 	}
110895d67482SBill Paul 
110995d67482SBill Paul 	/* Get result. */
111095d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
111195d67482SBill Paul 
11120c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
111395d67482SBill Paul 
111495d67482SBill Paul 	return (0);
111595d67482SBill Paul }
111695d67482SBill Paul 
111795d67482SBill Paul /*
111895d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
111995d67482SBill Paul  */
112095d67482SBill Paul static int
11213f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
112295d67482SBill Paul {
11233f74909aSGleb Smirnoff 	int i, error = 0;
11243f74909aSGleb Smirnoff 	uint8_t byte = 0;
112595d67482SBill Paul 
112695d67482SBill Paul 	for (i = 0; i < cnt; i++) {
11273f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
11283f74909aSGleb Smirnoff 		if (error)
112995d67482SBill Paul 			break;
113095d67482SBill Paul 		*(dest + i) = byte;
113195d67482SBill Paul 	}
113295d67482SBill Paul 
11333f74909aSGleb Smirnoff 	return (error ? 1 : 0);
113495d67482SBill Paul }
113595d67482SBill Paul 
113695d67482SBill Paul static int
11373f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
113895d67482SBill Paul {
113995d67482SBill Paul 	struct bge_softc *sc;
1140a813ed78SPyun YongHyeon 	uint32_t val;
114195d67482SBill Paul 	int i;
114295d67482SBill Paul 
114395d67482SBill Paul 	sc = device_get_softc(dev);
114495d67482SBill Paul 
1145548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1146548c8f1aSPyun YongHyeon 		return (0);
1147548c8f1aSPyun YongHyeon 
1148a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1149a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1150a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1151a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1152a813ed78SPyun YongHyeon 		DELAY(80);
115337ceeb4dSPaul Saab 	}
115437ceeb4dSPaul Saab 
115595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
115695d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
115795d67482SBill Paul 
1158a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
115995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1160d5d23857SJung-uk Kim 		DELAY(10);
116195d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
1162a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
1163a813ed78SPyun YongHyeon 			DELAY(5);
1164a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
116595d67482SBill Paul 			break;
116695d67482SBill Paul 		}
1167a813ed78SPyun YongHyeon 	}
116895d67482SBill Paul 
116995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
11705fea260fSMarius Strobl 		device_printf(sc->bge_dev,
11715fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
11725fea260fSMarius Strobl 		    phy, reg, val);
117337ceeb4dSPaul Saab 		val = 0;
117495d67482SBill Paul 	}
117595d67482SBill Paul 
1176a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1177a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1178a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1179a813ed78SPyun YongHyeon 		DELAY(80);
118037ceeb4dSPaul Saab 	}
118137ceeb4dSPaul Saab 
1182548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1183548c8f1aSPyun YongHyeon 
118495d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
118595d67482SBill Paul 		return (0);
118695d67482SBill Paul 
11870c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
118895d67482SBill Paul }
118995d67482SBill Paul 
119095d67482SBill Paul static int
11913f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
119295d67482SBill Paul {
119395d67482SBill Paul 	struct bge_softc *sc;
119495d67482SBill Paul 	int i;
119595d67482SBill Paul 
119695d67482SBill Paul 	sc = device_get_softc(dev);
119795d67482SBill Paul 
119838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
119938cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
120038cc658fSJohn Baldwin 		return (0);
120138cc658fSJohn Baldwin 
1202548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1203548c8f1aSPyun YongHyeon 		return (0);
1204548c8f1aSPyun YongHyeon 
1205a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1206a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1207a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1208a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1209a813ed78SPyun YongHyeon 		DELAY(80);
121037ceeb4dSPaul Saab 	}
121137ceeb4dSPaul Saab 
121295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
121395d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
121495d67482SBill Paul 
121595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1216d5d23857SJung-uk Kim 		DELAY(10);
121738cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
121838cc658fSJohn Baldwin 			DELAY(5);
121938cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
122095d67482SBill Paul 			break;
1221d5d23857SJung-uk Kim 		}
122238cc658fSJohn Baldwin 	}
1223d5d23857SJung-uk Kim 
1224a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1225a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1226a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1227a813ed78SPyun YongHyeon 		DELAY(80);
1228a813ed78SPyun YongHyeon 	}
1229a813ed78SPyun YongHyeon 
1230548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1231548c8f1aSPyun YongHyeon 
1232a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
123338cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
12342246e8c6SPyun YongHyeon 		    "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
123538cc658fSJohn Baldwin 		    phy, reg, val);
123637ceeb4dSPaul Saab 
123795d67482SBill Paul 	return (0);
123895d67482SBill Paul }
123995d67482SBill Paul 
124095d67482SBill Paul static void
12413f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
124295d67482SBill Paul {
124395d67482SBill Paul 	struct bge_softc *sc;
124495d67482SBill Paul 	struct mii_data *mii;
1245a0a03d1eSPyun YongHyeon 	uint32_t mac_mode, rx_mode, tx_mode;
1246e4146b95SPyun YongHyeon 
124795d67482SBill Paul 	sc = device_get_softc(dev);
1248fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(sc->bge_ifp) & IFF_DRV_RUNNING) == 0)
1249e4146b95SPyun YongHyeon 		return;
125095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
125195d67482SBill Paul 
1252d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1253d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
1254d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
1255d4f5240aSPyun YongHyeon 		case IFM_10_T:
1256d4f5240aSPyun YongHyeon 		case IFM_100_TX:
1257d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
1258d4f5240aSPyun YongHyeon 			break;
1259d4f5240aSPyun YongHyeon 		case IFM_1000_T:
1260d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
1261d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
1262d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1263d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
1264d4f5240aSPyun YongHyeon 			else
1265d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
1266d4f5240aSPyun YongHyeon 			break;
1267d4f5240aSPyun YongHyeon 		default:
1268d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
1269d4f5240aSPyun YongHyeon 			break;
1270d4f5240aSPyun YongHyeon 		}
1271d4f5240aSPyun YongHyeon 	} else
1272d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
1273d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
1274d4f5240aSPyun YongHyeon 		return;
1275a0a03d1eSPyun YongHyeon 
1276a0a03d1eSPyun YongHyeon 	/*
1277a0a03d1eSPyun YongHyeon 	 * APE firmware touches these registers to keep the MAC
1278a0a03d1eSPyun YongHyeon 	 * connected to the outside world.  Try to keep the
1279a0a03d1eSPyun YongHyeon 	 * accesses atomic.
1280a0a03d1eSPyun YongHyeon 	 */
1281a0a03d1eSPyun YongHyeon 
1282a0a03d1eSPyun YongHyeon 	/* Set the port mode (MII/GMII) to match the link speed. */
1283a0a03d1eSPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
1284a0a03d1eSPyun YongHyeon 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
1285a0a03d1eSPyun YongHyeon 	tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
1286a0a03d1eSPyun YongHyeon 	rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
1287a0a03d1eSPyun YongHyeon 
1288ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1289ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1290a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_GMII;
12913f74909aSGleb Smirnoff 	else
1292a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_MII;
129395d67482SBill Paul 
1294a0a03d1eSPyun YongHyeon 	/* Set MAC flow control behavior to match link flow control settings. */
1295a0a03d1eSPyun YongHyeon 	tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
1296a0a03d1eSPyun YongHyeon 	rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
12974951ca86SPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1298a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1299a0a03d1eSPyun YongHyeon 			tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
1300a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1301a0a03d1eSPyun YongHyeon 			rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
1302a0a03d1eSPyun YongHyeon 	} else
1303a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
1304a0a03d1eSPyun YongHyeon 
1305a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
13069b80ffe7SPyun YongHyeon 	DELAY(40);
1307a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
1308a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
130995d67482SBill Paul }
131095d67482SBill Paul 
131195d67482SBill Paul /*
131295d67482SBill Paul  * Intialize a standard receive ring descriptor.
131395d67482SBill Paul  */
131495d67482SBill Paul static int
1315943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
131695d67482SBill Paul {
1317943787f3SPyun YongHyeon 	struct mbuf *m;
131895d67482SBill Paul 	struct bge_rx_bd *r;
1319a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
1320943787f3SPyun YongHyeon 	bus_dmamap_t map;
1321a23634a1SPyun YongHyeon 	int error, nsegs;
132295d67482SBill Paul 
1323f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
1324fba8b109SMarcel Moolenaar 	    (if_getmtu(sc->bge_ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
1325f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
1326c6499eccSGleb Smirnoff 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1327f5459d4cSPyun YongHyeon 		if (m == NULL)
1328f5459d4cSPyun YongHyeon 			return (ENOBUFS);
1329f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1330f5459d4cSPyun YongHyeon 	} else {
1331c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1332943787f3SPyun YongHyeon 		if (m == NULL)
133395d67482SBill Paul 			return (ENOBUFS);
1334943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
1335f5459d4cSPyun YongHyeon 	}
1336652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1337943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
1338943787f3SPyun YongHyeon 
13390ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
1340943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
1341a23634a1SPyun YongHyeon 	if (error != 0) {
1342943787f3SPyun YongHyeon 		m_freem(m);
1343a23634a1SPyun YongHyeon 		return (error);
1344f41ac2beSBill Paul 	}
1345943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1346943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1347943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1348943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1349943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
1350943787f3SPyun YongHyeon 	}
1351943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
1352943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1353943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
1354943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
1355e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1356943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1357a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1358a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1359e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1360a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1361e907febfSPyun YongHyeon 	r->bge_idx = i;
1362f41ac2beSBill Paul 
13630ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1364943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
136595d67482SBill Paul 
136695d67482SBill Paul 	return (0);
136795d67482SBill Paul }
136895d67482SBill Paul 
136995d67482SBill Paul /*
137095d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
137195d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
137295d67482SBill Paul  */
137395d67482SBill Paul static int
1374943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
137595d67482SBill Paul {
13761be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1377943787f3SPyun YongHyeon 	bus_dmamap_t map;
13781be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1379943787f3SPyun YongHyeon 	struct mbuf *m;
1380943787f3SPyun YongHyeon 	int error, nsegs;
138195d67482SBill Paul 
1382c6499eccSGleb Smirnoff 	MGETHDR(m, M_NOWAIT, MT_DATA);
1383943787f3SPyun YongHyeon 	if (m == NULL)
138495d67482SBill Paul 		return (ENOBUFS);
138595d67482SBill Paul 
1386c6499eccSGleb Smirnoff 	m_cljget(m, M_NOWAIT, MJUM9BYTES);
1387943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
1388943787f3SPyun YongHyeon 		m_freem(m);
138995d67482SBill Paul 		return (ENOBUFS);
139095d67482SBill Paul 	}
1391943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1392652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1393943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
13941be6acb7SGleb Smirnoff 
13951be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1396943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1397943787f3SPyun YongHyeon 	if (error != 0) {
1398943787f3SPyun YongHyeon 		m_freem(m);
13991be6acb7SGleb Smirnoff 		return (error);
1400f7cea149SGleb Smirnoff 	}
14011be6acb7SGleb Smirnoff 
1402aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1403943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1404943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1405943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1406943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1407943787f3SPyun YongHyeon 	}
1408943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1409943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1410943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1411943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1412943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1413e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1414e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1415e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1416e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1417e0b7b101SPyun YongHyeon 
14181be6acb7SGleb Smirnoff 	/*
14191be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
14201be6acb7SGleb Smirnoff 	 */
1421943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
14224e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
14234e7ba1abSGleb Smirnoff 	r->bge_idx = i;
14244e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
14254e7ba1abSGleb Smirnoff 	switch (nsegs) {
14264e7ba1abSGleb Smirnoff 	case 4:
14274e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
14284e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
14294e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1430e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
14314e7ba1abSGleb Smirnoff 	case 3:
1432e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1433e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1434e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1435e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
14364e7ba1abSGleb Smirnoff 	case 2:
14374e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
14384e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
14394e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1440e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
14414e7ba1abSGleb Smirnoff 	case 1:
14424e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
14434e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
14444e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1445e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
14464e7ba1abSGleb Smirnoff 		break;
14474e7ba1abSGleb Smirnoff 	default:
14484e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
14494e7ba1abSGleb Smirnoff 	}
1450f41ac2beSBill Paul 
1451a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1452943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
145395d67482SBill Paul 
145495d67482SBill Paul 	return (0);
145595d67482SBill Paul }
145695d67482SBill Paul 
145795d67482SBill Paul static int
14583f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
145995d67482SBill Paul {
14603ee5d7daSPyun YongHyeon 	int error, i;
146195d67482SBill Paul 
1462e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
146303e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1464e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1465943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
14663ee5d7daSPyun YongHyeon 			return (error);
146703e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
14681888f324SPyun YongHyeon 	}
146995d67482SBill Paul 
1470f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1471d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1472f41ac2beSBill Paul 
1473e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1474e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
147595d67482SBill Paul 
147695d67482SBill Paul 	return (0);
147795d67482SBill Paul }
147895d67482SBill Paul 
147995d67482SBill Paul static void
14803f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
148195d67482SBill Paul {
148295d67482SBill Paul 	int i;
148395d67482SBill Paul 
148495d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
148595d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
14860ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1487e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1488e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
14890ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1490f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1491e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1492e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
149395d67482SBill Paul 		}
1494f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
149595d67482SBill Paul 		    sizeof(struct bge_rx_bd));
149695d67482SBill Paul 	}
149795d67482SBill Paul }
149895d67482SBill Paul 
149995d67482SBill Paul static int
15003f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
150195d67482SBill Paul {
150295d67482SBill Paul 	struct bge_rcb *rcb;
15033ee5d7daSPyun YongHyeon 	int error, i;
150495d67482SBill Paul 
1505e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
150603e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
150795d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1508943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
15093ee5d7daSPyun YongHyeon 			return (error);
151003e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
15111888f324SPyun YongHyeon 	}
151295d67482SBill Paul 
1513f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1514d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1515f41ac2beSBill Paul 
1516e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
151795d67482SBill Paul 
15188a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1519f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
15208a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
15218a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
152267111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
152395d67482SBill Paul 
1524e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
152595d67482SBill Paul 
152695d67482SBill Paul 	return (0);
152795d67482SBill Paul }
152895d67482SBill Paul 
152995d67482SBill Paul static void
15303f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
153195d67482SBill Paul {
153295d67482SBill Paul 	int i;
153395d67482SBill Paul 
153495d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
153595d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1536e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1537e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1538e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1539f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1540f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1541e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1542e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
154395d67482SBill Paul 		}
1544f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
15451be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
154695d67482SBill Paul 	}
154795d67482SBill Paul }
154895d67482SBill Paul 
154995d67482SBill Paul static void
15503f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
155195d67482SBill Paul {
155295d67482SBill Paul 	int i;
155395d67482SBill Paul 
1554f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
155595d67482SBill Paul 		return;
155695d67482SBill Paul 
155795d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
155895d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
15590ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1560e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1561e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15620ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1563f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1564e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1565e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
156695d67482SBill Paul 		}
1567f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
156895d67482SBill Paul 		    sizeof(struct bge_tx_bd));
156995d67482SBill Paul 	}
157095d67482SBill Paul }
157195d67482SBill Paul 
157295d67482SBill Paul static int
15733f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
157495d67482SBill Paul {
157595d67482SBill Paul 	sc->bge_txcnt = 0;
157695d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
15773927098fSPaul Saab 
1578e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1579e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
15805c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1581e6bf277eSPyun YongHyeon 
158214bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
158314bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
158438cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
158514bbd30fSGleb Smirnoff 
15863927098fSPaul Saab 	/* 5700 b2 errata */
1587e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
158838cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
15893927098fSPaul Saab 
159014bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
159138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
15923927098fSPaul Saab 	/* 5700 b2 errata */
1593e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
159438cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
159595d67482SBill Paul 
159695d67482SBill Paul 	return (0);
159795d67482SBill Paul }
159895d67482SBill Paul 
159995d67482SBill Paul static void
16003e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
16013e9b1bcaSJung-uk Kim {
1602fba8b109SMarcel Moolenaar 	if_t ifp;
16033e9b1bcaSJung-uk Kim 
16043e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
16053e9b1bcaSJung-uk Kim 
16063e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
16073e9b1bcaSJung-uk Kim 
160845ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
1609fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_PROMISC)
161045ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16113e9b1bcaSJung-uk Kim 	else
161245ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16133e9b1bcaSJung-uk Kim }
16143e9b1bcaSJung-uk Kim 
16153e9b1bcaSJung-uk Kim static void
16163f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
161795d67482SBill Paul {
1618fba8b109SMarcel Moolenaar 	if_t ifp;
1619fba8b109SMarcel Moolenaar 	int mc_count = 0;
16203f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
1621fba8b109SMarcel Moolenaar 	int h, i, mcnt;
1622fba8b109SMarcel Moolenaar 	unsigned char *mta;
162395d67482SBill Paul 
16240f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
16250f9bd73bSSam Leffler 
1626fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
162795d67482SBill Paul 
1628fba8b109SMarcel Moolenaar 	mc_count = if_multiaddr_count(ifp, -1);
1629fba8b109SMarcel Moolenaar 	mta = malloc(sizeof(unsigned char) *  ETHER_ADDR_LEN *
1630fba8b109SMarcel Moolenaar 	    mc_count, M_DEVBUF, M_NOWAIT);
1631fba8b109SMarcel Moolenaar 
1632fba8b109SMarcel Moolenaar 	if(mta == NULL) {
1633fba8b109SMarcel Moolenaar 		device_printf(sc->bge_dev,
1634fba8b109SMarcel Moolenaar 		    "Failed to allocated temp mcast list\n");
1635fba8b109SMarcel Moolenaar 		return;
1636fba8b109SMarcel Moolenaar 	}
1637fba8b109SMarcel Moolenaar 
1638fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
163995d67482SBill Paul 		for (i = 0; i < 4; i++)
16400c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
164195d67482SBill Paul 		return;
164295d67482SBill Paul 	}
164395d67482SBill Paul 
164495d67482SBill Paul 	/* First, zot all the existing filters. */
164595d67482SBill Paul 	for (i = 0; i < 4; i++)
164695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
164795d67482SBill Paul 
1648fba8b109SMarcel Moolenaar 	if_multiaddr_array(ifp, mta, &mcnt, mc_count);
1649fba8b109SMarcel Moolenaar 	for(i = 0; i < mcnt; i++) {
1650a127e581SPeter Wemm 		h = ether_crc32_le(mta + (i * ETHER_ADDR_LEN),
1651a127e581SPeter Wemm 		    ETHER_ADDR_LEN) & 0x7F;
16520c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
165395d67482SBill Paul 	}
165495d67482SBill Paul 
165595d67482SBill Paul 	for (i = 0; i < 4; i++)
165695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1657fba8b109SMarcel Moolenaar 
1658fba8b109SMarcel Moolenaar 	free(mta, M_DEVBUF);
165995d67482SBill Paul }
166095d67482SBill Paul 
16618cb1383cSDoug Ambrisko static void
1662cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1663cb2eacc7SYaroslav Tykhiy {
1664fba8b109SMarcel Moolenaar 	if_t ifp;
1665cb2eacc7SYaroslav Tykhiy 
1666cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1667cb2eacc7SYaroslav Tykhiy 
1668cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1669cb2eacc7SYaroslav Tykhiy 
1670cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1671fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING)
1672cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1673cb2eacc7SYaroslav Tykhiy 	else
1674cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1675cb2eacc7SYaroslav Tykhiy }
1676cb2eacc7SYaroslav Tykhiy 
1677cb2eacc7SYaroslav Tykhiy static void
1678797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
16798cb1383cSDoug Ambrisko {
1680797ab05eSPyun YongHyeon 
16818cb1383cSDoug Ambrisko 	/*
16828cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
16838cb1383cSDoug Ambrisko 	 */
16848cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1685888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
16868cb1383cSDoug Ambrisko 
16878cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16888cb1383cSDoug Ambrisko 		switch (type) {
16898cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1690224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1691224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
16928cb1383cSDoug Ambrisko 			break;
1693548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1694224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1695224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
16968cb1383cSDoug Ambrisko 			break;
1697548c8f1aSPyun YongHyeon 		case BGE_RESET_SUSPEND:
1698548c8f1aSPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1699548c8f1aSPyun YongHyeon 			    BGE_FW_DRV_STATE_SUSPEND);
1700548c8f1aSPyun YongHyeon 			break;
17018cb1383cSDoug Ambrisko 		}
17028cb1383cSDoug Ambrisko 	}
1703548c8f1aSPyun YongHyeon 
1704548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1705548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17068cb1383cSDoug Ambrisko }
17078cb1383cSDoug Ambrisko 
17088cb1383cSDoug Ambrisko static void
1709797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
17108cb1383cSDoug Ambrisko {
1711797ab05eSPyun YongHyeon 
17128cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
17138cb1383cSDoug Ambrisko 		switch (type) {
17148cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1715224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1716224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
17178cb1383cSDoug Ambrisko 			/* START DONE */
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_DONE);
17228cb1383cSDoug Ambrisko 			break;
17238cb1383cSDoug Ambrisko 		}
17248cb1383cSDoug Ambrisko 	}
1725548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_SHUTDOWN)
1726548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17278cb1383cSDoug Ambrisko }
17288cb1383cSDoug Ambrisko 
17298cb1383cSDoug Ambrisko static void
1730797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
17318cb1383cSDoug Ambrisko {
1732797ab05eSPyun YongHyeon 
17338cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17348cb1383cSDoug Ambrisko 		switch (type) {
17358cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1736224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1737224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
17388cb1383cSDoug Ambrisko 			break;
1739548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1740224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1741224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
17428cb1383cSDoug Ambrisko 			break;
17438cb1383cSDoug Ambrisko 		}
17448cb1383cSDoug Ambrisko 	}
17458cb1383cSDoug Ambrisko }
17468cb1383cSDoug Ambrisko 
1747797ab05eSPyun YongHyeon static void
1748797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
17498cb1383cSDoug Ambrisko {
17508cb1383cSDoug Ambrisko 	int i;
17518cb1383cSDoug Ambrisko 
17528cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17533c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
17543fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
17559931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
17568cb1383cSDoug Ambrisko 
17578cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
17589931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
17599931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
17608cb1383cSDoug Ambrisko 				break;
17618cb1383cSDoug Ambrisko 			DELAY(10);
17628cb1383cSDoug Ambrisko 		}
17638cb1383cSDoug Ambrisko 	}
17648cb1383cSDoug Ambrisko }
17658cb1383cSDoug Ambrisko 
176650515680SPyun YongHyeon static uint32_t
176750515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
176850515680SPyun YongHyeon {
176950515680SPyun YongHyeon 	uint32_t dma_options;
177050515680SPyun YongHyeon 
177150515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
177250515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
177350515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
177450515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
177550515680SPyun YongHyeon #endif
177650515680SPyun YongHyeon 	return (dma_options);
177750515680SPyun YongHyeon }
177850515680SPyun YongHyeon 
177995d67482SBill Paul /*
1780c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
178195d67482SBill Paul  */
178295d67482SBill Paul static int
17833f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
178495d67482SBill Paul {
178550515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1786fbc374afSPyun YongHyeon 	uint16_t val;
178795d67482SBill Paul 	int i;
178895d67482SBill Paul 
17898cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
17901108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
17911108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
17921108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
17931108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
179495d67482SBill Paul 
179595d67482SBill Paul 	/*
179695d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
179795d67482SBill Paul 	 * internal memory.
179895d67482SBill Paul 	 */
179995d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
18003f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
180195d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
180295d67482SBill Paul 
180395d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
18043f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
180595d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
180695d67482SBill Paul 
1807fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1808fbc374afSPyun YongHyeon 		/*
1809d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1810fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1811fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1812fbc374afSPyun YongHyeon 		 */
1813fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1814fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1815fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1816fbc374afSPyun YongHyeon 	}
1817fbc374afSPyun YongHyeon 
1818f8bb33c3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765 ||
1819f8bb33c3SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57766) {
1820f8bb33c3SPyun YongHyeon 		/*
1821f8bb33c3SPyun YongHyeon 		 * For the 57766 and non Ax versions of 57765, bootcode
1822f8bb33c3SPyun YongHyeon 		 * needs to setup the PCIE Fast Training Sequence (FTS)
1823f8bb33c3SPyun YongHyeon 		 * value to prevent transmit hangs.
1824f8bb33c3SPyun YongHyeon 		 */
1825f8bb33c3SPyun YongHyeon 		if (sc->bge_chiprev != BGE_CHIPREV_57765_AX) {
1826f8bb33c3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL,
1827f8bb33c3SPyun YongHyeon 			    CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL) |
1828f8bb33c3SPyun YongHyeon 			    BGE_CPMU_PADRNG_CTL_RDIV2);
1829f8bb33c3SPyun YongHyeon 		}
1830f8bb33c3SPyun YongHyeon 	}
1831f8bb33c3SPyun YongHyeon 
1832186f842bSJung-uk Kim 	/*
1833186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1834186f842bSJung-uk Kim 	 */
1835186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1836186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1837652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
183848630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
183948630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
184048630d79SPyun YongHyeon 		else
1841186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1842652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
18434c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1844186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1845186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1846186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1847186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1848186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1849186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1850cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1851cbb2b2feSPyun YongHyeon 			/*
1852cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1853cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1854cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1855cbb2b2feSPyun YongHyeon 			 * register.
1856cbb2b2feSPyun YongHyeon 			 */
1857cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1858cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1859186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1860186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1861186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1862186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1863186f842bSJung-uk Kim 		} else {
1864186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1865186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1866186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
18670c8aa4eaSJung-uk Kim 			    0x0F;
1868186f842bSJung-uk Kim 		}
1869e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1870e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
18713f74909aSGleb Smirnoff 			uint32_t tmp;
18725cba12d3SPaul Saab 
1873186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
18740c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1875186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1876186f842bSJung-uk Kim 				dma_rw_ctl |=
1877186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
18785cba12d3SPaul Saab 
1879186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1880186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1881186f842bSJung-uk Kim 		}
1882186f842bSJung-uk Kim 	} else {
1883186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1884186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1885186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1886186f842bSJung-uk Kim 
1887186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1888186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1889186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1890186f842bSJung-uk Kim 	}
1891186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1892186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1893186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1894186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1895e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1896186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
18975cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1898b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18991108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1900b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1901b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1902bbe2ca75SPyun YongHyeon 		/*
1903bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1904bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1905bbe2ca75SPyun YongHyeon 		 * disabled.
1906bbe2ca75SPyun YongHyeon 		 */
19072927f01fSPyun YongHyeon 		if (!BGE_IS_57765_PLUS(sc) &&
19082927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
19092927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5762)
1910bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1911b4a256acSPyun YongHyeon 	}
19125cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
191395d67482SBill Paul 
191495d67482SBill Paul 	/*
191595d67482SBill Paul 	 * Set up general mode register.
191695d67482SBill Paul 	 */
1917548c8f1aSPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc);
19182927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
19192927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
1920548c8f1aSPyun YongHyeon 		/* Retain Host-2-BMC settings written by APE firmware. */
1921548c8f1aSPyun YongHyeon 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1922548c8f1aSPyun YongHyeon 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1923548c8f1aSPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1924548c8f1aSPyun YongHyeon 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1925548c8f1aSPyun YongHyeon 	}
1926548c8f1aSPyun YongHyeon 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1927548c8f1aSPyun YongHyeon 	    BGE_MODECTL_TX_NO_PHDR_CSUM;
192895d67482SBill Paul 
192995d67482SBill Paul 	/*
193090447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
193190447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
193290447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
193390447aadSMarius Strobl 	 * certain bridges.
193490447aadSMarius Strobl 	 */
193590447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
193690447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
193750515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
193890447aadSMarius Strobl 
193990447aadSMarius Strobl 	/*
19408cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
19418cb1383cSDoug Ambrisko 	 */
19428cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
194350515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
194450515680SPyun YongHyeon 
194550515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
19468cb1383cSDoug Ambrisko 
19478cb1383cSDoug Ambrisko 	/*
1948ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1949*40438c47SMarius Strobl 	 * properly by these devices.
195095d67482SBill Paul 	 */
1951*40438c47SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
195295d67482SBill Paul 
1953d7acafa1SMarius Strobl 	/* Set the timer prescaler (always 66 MHz). */
19540c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
195595d67482SBill Paul 
195638cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
195738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
195838cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
195938cc658fSJohn Baldwin 
196038cc658fSJohn Baldwin 		/* Put PHY into ready state */
196138cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
196238cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
196338cc658fSJohn Baldwin 		DELAY(40);
196438cc658fSJohn Baldwin 	}
196538cc658fSJohn Baldwin 
196695d67482SBill Paul 	return (0);
196795d67482SBill Paul }
196895d67482SBill Paul 
196995d67482SBill Paul static int
19703f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
197195d67482SBill Paul {
197295d67482SBill Paul 	struct bge_rcb *rcb;
1973e907febfSPyun YongHyeon 	bus_size_t vrcb;
1974e907febfSPyun YongHyeon 	bge_hostaddr taddr;
19752927f01fSPyun YongHyeon 	uint32_t dmactl, rdmareg, val;
19768a315a6dSPyun YongHyeon 	int i, limit;
197795d67482SBill Paul 
197895d67482SBill Paul 	/*
197995d67482SBill Paul 	 * Initialize the memory window pointer register so that
198095d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
198195d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
198295d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
198395d67482SBill Paul 	 */
198495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
198595d67482SBill Paul 
1986822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1987822f63fcSBill Paul 
19887ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
198995d67482SBill Paul 		/* Configure mbuf memory pool */
19900dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1991822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1992822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1993822f63fcSBill Paul 		else
199495d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
199595d67482SBill Paul 
199695d67482SBill Paul 		/* Configure DMA resource pool */
19970434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
19980434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
199995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
20000434d1b8SBill Paul 	}
200195d67482SBill Paul 
200295d67482SBill Paul 	/* Configure mbuf pool watermarks */
200350515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
20041108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
2005fba8b109SMarcel Moolenaar 		if (if_getmtu(sc->bge_ifp) > ETHERMTU) {
20061108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
20071108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
20081108273aSPyun YongHyeon 		} else {
20091108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
20101108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
20111108273aSPyun YongHyeon 		}
20121108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
2013fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
2014fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
2015fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
201638cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
201738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
201838cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
201938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
202038cc658fSJohn Baldwin 	} else {
202138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
202238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
202338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
202438cc658fSJohn Baldwin 	}
202595d67482SBill Paul 
202695d67482SBill Paul 	/* Configure DMA resource watermarks */
202795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
202895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
202995d67482SBill Paul 
203095d67482SBill Paul 	/* Enable buffer manager */
2031bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
2032bbe2ca75SPyun YongHyeon 	/*
2033bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
2034bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
2035bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
2036bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
2037bbe2ca75SPyun YongHyeon 	 */
2038bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2039bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
2040bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
204195d67482SBill Paul 
204295d67482SBill Paul 	/* Poll for buffer manager start indication */
204395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2044d5d23857SJung-uk Kim 		DELAY(10);
20450c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
204695d67482SBill Paul 			break;
204795d67482SBill Paul 	}
204895d67482SBill Paul 
204995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
20505a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
205195d67482SBill Paul 		return (ENXIO);
205295d67482SBill Paul 	}
205395d67482SBill Paul 
205495d67482SBill Paul 	/* Enable flow-through queues */
20550c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
205695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
205795d67482SBill Paul 
205895d67482SBill Paul 	/* Wait until queue initialization is complete */
205995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2060d5d23857SJung-uk Kim 		DELAY(10);
206195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
206295d67482SBill Paul 			break;
206395d67482SBill Paul 	}
206495d67482SBill Paul 
206595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2066fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
206795d67482SBill Paul 		return (ENXIO);
206895d67482SBill Paul 	}
206995d67482SBill Paul 
20708a315a6dSPyun YongHyeon 	/*
20718a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
20728a315a6dSPyun YongHyeon 	 *
20738a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
20748a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
20758a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
20768a315a6dSPyun YongHyeon 	 *
20778a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
20788a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
20798a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
20808a315a6dSPyun YongHyeon 	 *   to the controller.
20818a315a6dSPyun YongHyeon 	 *
20828a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
20838a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
20848a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
20858a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
20868a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
20878a315a6dSPyun YongHyeon 	 *   be disabled.
20888a315a6dSPyun YongHyeon 	 *
20898a315a6dSPyun YongHyeon 	 * Receive Return Ring
20908a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
20918a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
20928a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
20938a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
20948a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
20958a315a6dSPyun YongHyeon 	 *
20968a315a6dSPyun YongHyeon 	 * Send Ring
20978a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
20988a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
20998a315a6dSPyun YongHyeon 	 */
21008a315a6dSPyun YongHyeon 
21018a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
2102f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2103f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
2104f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2105f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
2106f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2107f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2108f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
21091108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
21101108273aSPyun YongHyeon 		/*
21111108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
21121108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
21131108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
21141108273aSPyun YongHyeon 		 * Bit 0     : Reserved
21151108273aSPyun YongHyeon 		 */
21161108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
21171108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
21181108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
21198a315a6dSPyun YongHyeon 		/*
21208a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
21218a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
21228a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
21238a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21248a315a6dSPyun YongHyeon 		 */
21250434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
21268a315a6dSPyun YongHyeon 	} else {
21278a315a6dSPyun YongHyeon 		/*
21288a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
21298a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
21308a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
21318a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
21328a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21338a315a6dSPyun YongHyeon 		 */
21340434d1b8SBill Paul 		rcb->bge_maxlen_flags =
21350434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
21368a315a6dSPyun YongHyeon 	}
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_STD_RX_RINGS_5717;
21411108273aSPyun YongHyeon 	else
214295d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
21438a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
21440c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
21450c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
214667111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
214767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
214895d67482SBill Paul 
21498a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
21508a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
21518a315a6dSPyun YongHyeon 
215295d67482SBill Paul 	/*
21538a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
21548a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
21558a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
215695d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
215795d67482SBill Paul 	 * high enough to require it).
215895d67482SBill Paul 	 */
21594c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2160f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
21618a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
2162f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
2163f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2164f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
2165f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2166f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2167f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2168f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
21691be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
21701be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2171bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
217250515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
217350515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21741108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
21751108273aSPyun YongHyeon 		else
217695d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
217767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
217867111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
217967111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
218067111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
21818a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
21820434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
21830434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
218467111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
21858a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
21868a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
21878a315a6dSPyun YongHyeon 	}
218895d67482SBill Paul 
21898a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
21905e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
2191f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
219267111612SJohn Polstra 		rcb->bge_maxlen_flags =
219367111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
21940434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
21950434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
21968a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
21978a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
21980434d1b8SBill Paul 	}
219995d67482SBill Paul 
2200ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2201ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2202427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2203427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2204427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
22058d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
22068d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2207ca4f8986SPyun YongHyeon 	}
220895d67482SBill Paul 	/*
22098a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
22108a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
22118a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
22128a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
22138a315a6dSPyun YongHyeon 	 *
221495d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
221595d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
221695d67482SBill Paul 	 * each ring.
22179ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
22189ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
22199ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
22209ba784dbSScott Long 	 * are reports that it might not need to be so strict.
222138cc658fSJohn Baldwin 	 *
222238cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
222338cc658fSJohn Baldwin 	 * well.
222495d67482SBill Paul 	 */
22255345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
22266f8718a3SScott Long 		val = 8;
22276f8718a3SScott Long 	else
22286f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
22296f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
22302a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
22312a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
22322a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
22331108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
22341108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
22351108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
22361108273aSPyun YongHyeon 	}
223795d67482SBill Paul 
223895d67482SBill Paul 	/*
22398a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
22408a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
22418a315a6dSPyun YongHyeon 	 * located in NIC memory.
224295d67482SBill Paul 	 */
22438a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
22448a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
22458a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
22462927f01fSPyun YongHyeon 	else if (BGE_IS_57765_PLUS(sc) ||
22472927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
22482927f01fSPyun YongHyeon 		limit = 2;
22492927f01fSPyun YongHyeon 	else if (BGE_IS_5717_PLUS(sc))
22502927f01fSPyun YongHyeon 		limit = 4;
22518a315a6dSPyun YongHyeon 	else
22528a315a6dSPyun YongHyeon 		limit = 1;
2253e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
22548a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2255e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2256e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2257e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2258e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
225995d67482SBill Paul 	}
226095d67482SBill Paul 
22618a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
2262e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2263e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2264e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2265e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2266bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
226750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
226850515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
22691108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
22701108273aSPyun YongHyeon 	else
2271e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2272e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2273e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2274e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
227595d67482SBill Paul 
22768a315a6dSPyun YongHyeon 	/*
22778a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
22788a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
22798a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
22808a315a6dSPyun YongHyeon 	 */
2281bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
228250515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
228350515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
22841108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
22851108273aSPyun YongHyeon 		limit = 16;
22861108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
22878a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
2288b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
22892927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762 ||
2290fe26ad88SPyun YongHyeon 	    BGE_IS_57765_PLUS(sc))
22918a315a6dSPyun YongHyeon 		limit = 4;
22928a315a6dSPyun YongHyeon 	else
22938a315a6dSPyun YongHyeon 		limit = 1;
22948a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
2295e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
22968a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2297e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2298e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2299e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
23008a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
2301e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
230238cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
23033f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
2304e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
230595d67482SBill Paul 	}
230695d67482SBill Paul 
230795d67482SBill Paul 	/*
23088a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
23098a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
23108a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
231195d67482SBill Paul 	 */
2312e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2313e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2314e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2315e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
23168a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2317e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2318e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
231995d67482SBill Paul 
232095d67482SBill Paul 	/* Set random backoff seed for TX */
232195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
23220a2cc827SPyun YongHyeon 	    (IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
23234a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
23240a2cc827SPyun YongHyeon 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5]) &
232595d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
232695d67482SBill Paul 
232795d67482SBill Paul 	/* Set inter-packet gap */
232850515680SPyun YongHyeon 	val = 0x2620;
23292927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
23302927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
233150515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
233250515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
233350515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
233495d67482SBill Paul 
233595d67482SBill Paul 	/*
233695d67482SBill Paul 	 * Specify which ring to use for packets that don't match
233795d67482SBill Paul 	 * any RX rules.
233895d67482SBill Paul 	 */
233995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
234095d67482SBill Paul 
234195d67482SBill Paul 	/*
234295d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
234395d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
234495d67482SBill Paul 	 */
234595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
234695d67482SBill Paul 
234795d67482SBill Paul 	/* Inialize RX list placement stats mask. */
23480c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
234995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
235095d67482SBill Paul 
235195d67482SBill Paul 	/* Disable host coalescing until we get it set up */
235295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
235395d67482SBill Paul 
235495d67482SBill Paul 	/* Poll to make sure it's shut down. */
235595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2356d5d23857SJung-uk Kim 		DELAY(10);
235795d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
235895d67482SBill Paul 			break;
235995d67482SBill Paul 	}
236095d67482SBill Paul 
236195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2362fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2363fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
236495d67482SBill Paul 		return (ENXIO);
236595d67482SBill Paul 	}
236695d67482SBill Paul 
236795d67482SBill Paul 	/* Set up host coalescing defaults */
236895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
236995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
237095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
237195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
23727ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
237395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
237495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
23750434d1b8SBill Paul 	}
2376b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2377b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
237895d67482SBill Paul 
237995d67482SBill Paul 	/* Set up address of statistics block */
23807ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
2381f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2382f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
238395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2384f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
23850434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
238695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
23870434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
23880434d1b8SBill Paul 	}
23890434d1b8SBill Paul 
23900434d1b8SBill Paul 	/* Set up address of status block */
2391f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2392f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
239395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2394f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
239595d67482SBill Paul 
239630f57f61SPyun YongHyeon 	/* Set up status block size. */
239730f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2398864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
239930f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2400864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2401864104feSPyun YongHyeon 	} else {
240230f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2403864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2404864104feSPyun YongHyeon 	}
2405864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2406864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2407864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
240830f57f61SPyun YongHyeon 
240995d67482SBill Paul 	/* Turn on host coalescing state machine */
241030f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
241195d67482SBill Paul 
241295d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
241395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
241495d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
241595d67482SBill Paul 
241695d67482SBill Paul 	/* Turn on RX list placement state machine */
241795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
241895d67482SBill Paul 
241995d67482SBill Paul 	/* Turn on RX list selector state machine. */
24207ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
242195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
242295d67482SBill Paul 
24232246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2424ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2425ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2426ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2427ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2428ea3b4127SPyun YongHyeon 
2429ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2430ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2431ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2432ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2433ea3b4127SPyun YongHyeon 	else
2434ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2435ea3b4127SPyun YongHyeon 
2436548c8f1aSPyun YongHyeon 	/* Allow APE to send/receive frames. */
2437548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2438548c8f1aSPyun YongHyeon 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2439548c8f1aSPyun YongHyeon 
2440ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
24419b80ffe7SPyun YongHyeon 	DELAY(40);
244295d67482SBill Paul 
244395d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
244491bd90d8SPyun YongHyeon 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
244595d67482SBill Paul 
244695d67482SBill Paul #ifdef notdef
244795d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
244895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
244995d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
245095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
245195d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
245295d67482SBill Paul #endif
245395d67482SBill Paul 
245495d67482SBill Paul 	/* Turn on DMA completion state machine */
24557ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
245695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
245795d67482SBill Paul 
24586f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
24596f8718a3SScott Long 
24606f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2461a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
24623889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
24636f8718a3SScott Long 
24647aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
24657aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
24667aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
24677aa4b937SPyun YongHyeon 
246895d67482SBill Paul 	/* Turn on write DMA state machine */
24696f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
24704f09c4c7SMarius Strobl 	DELAY(40);
247195d67482SBill Paul 
247295d67482SBill Paul 	/* Turn on read DMA state machine */
24734f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
24741108273aSPyun YongHyeon 
24751108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
24761108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
24771108273aSPyun YongHyeon 
2478a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2479a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2480a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2481a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2482a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2483a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
24844f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
24854f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
24861108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2487ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
24881108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
24891108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
249055a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
249155a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
249255a24a05SPyun YongHyeon 	}
249350515680SPyun YongHyeon 
24942927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
24952927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
249650515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
249750515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2498e3215f76SPyun YongHyeon 		/*
2499e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2500e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2501e3215f76SPyun YongHyeon 		 */
2502e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2503e3215f76SPyun YongHyeon 	}
250450515680SPyun YongHyeon 
2505d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2506d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2507d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
25081108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
25092927f01fSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc) || BGE_IS_57765_PLUS(sc)) {
25102927f01fSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
25112927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL_REG2;
25122927f01fSPyun YongHyeon 		else
25132927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL;
25142927f01fSPyun YongHyeon 		dmactl = CSR_READ_4(sc, rdmareg);
2515bbe2ca75SPyun YongHyeon 		/*
2516bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2517bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2518bbe2ca75SPyun YongHyeon 		 */
25192927f01fSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5719_A0 ||
25202927f01fSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
2521bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2522bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2523bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2524bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2525bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2526bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2527bbe2ca75SPyun YongHyeon 		}
2528d255f2a9SPyun YongHyeon 		/*
2529d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2530d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2531d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2532d255f2a9SPyun YongHyeon 		 */
25332927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, rdmareg, dmactl |
2534d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2535d255f2a9SPyun YongHyeon 	}
2536bbe2ca75SPyun YongHyeon 
2537e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2538bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2539bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2540bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2541bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2542e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2543e3215f76SPyun YongHyeon 		/*
2544e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2545e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2546e3215f76SPyun YongHyeon 		 */
2547e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2548e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2549e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2550e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
25512927f01fSPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5762) {
25522927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2,
25532927f01fSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2) |
25542927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
25552927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2556bbe2ca75SPyun YongHyeon 	}
2557bbe2ca75SPyun YongHyeon 
25584f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
25594f09c4c7SMarius Strobl 	DELAY(40);
256095d67482SBill Paul 
256129b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
256229b44b09SPyun YongHyeon 		for (i = 0; i < BGE_NUM_RDMA_CHANNELS / 2; i++) {
256329b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LENGTH + i * 4);
256429b44b09SPyun YongHyeon 			if ((val & 0xFFFF) > BGE_FRAMELEN)
256529b44b09SPyun YongHyeon 				break;
256629b44b09SPyun YongHyeon 			if (((val >> 16) & 0xFFFF) > BGE_FRAMELEN)
256729b44b09SPyun YongHyeon 				break;
256829b44b09SPyun YongHyeon 		}
256929b44b09SPyun YongHyeon 		if (i != BGE_NUM_RDMA_CHANNELS / 2) {
257029b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
257129b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
257229b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5719;
257329b44b09SPyun YongHyeon 			else
257429b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5720;
257529b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
257629b44b09SPyun YongHyeon 		}
257729b44b09SPyun YongHyeon 	}
257829b44b09SPyun YongHyeon 
257995d67482SBill Paul 	/* Turn on RX data completion state machine */
258095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
258195d67482SBill Paul 
258295d67482SBill Paul 	/* Turn on RX BD initiator state machine */
258395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
258495d67482SBill Paul 
258595d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
258695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
258795d67482SBill Paul 
258895d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25897ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
259095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
259195d67482SBill Paul 
259295d67482SBill Paul 	/* Turn on send BD completion state machine */
259395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
259495d67482SBill Paul 
259595d67482SBill Paul 	/* Turn on send data completion state machine */
2596a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2597a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2598a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2599a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
260095d67482SBill Paul 
260195d67482SBill Paul 	/* Turn on send data initiator state machine */
26021108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
26031108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
26041108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2605ca3f1187SPyun YongHyeon 	else
260695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
260795d67482SBill Paul 
260895d67482SBill Paul 	/* Turn on send BD initiator state machine */
260995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
261095d67482SBill Paul 
261195d67482SBill Paul 	/* Turn on send BD selector state machine */
261295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
261395d67482SBill Paul 
26140c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
261595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
261695d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
261795d67482SBill Paul 
261895d67482SBill Paul 	/* ack/clear link change events */
261995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26200434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26210434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2622f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
262395d67482SBill Paul 
26246ede2cfaSPyun YongHyeon 	/*
26256ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
26266ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
26276ede2cfaSPyun YongHyeon 	 */
2628652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
262995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2630a1d52896SBill Paul 	} else {
26317ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
26327ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
26337ed3f0f0SPyun YongHyeon 			DELAY(80);
26347ed3f0f0SPyun YongHyeon 		}
26351f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
26364c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2637a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2638a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2639a1d52896SBill Paul 	}
264095d67482SBill Paul 
26411f313773SOleg Bulyzhin 	/*
26421f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
26431f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
26441f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
26451f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
26461f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
26471f313773SOleg Bulyzhin 	 */
26481f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26491f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26501f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
26511f313773SOleg Bulyzhin 
265295d67482SBill Paul 	/* Enable link state change attentions. */
265395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
265495d67482SBill Paul 
265595d67482SBill Paul 	return (0);
265695d67482SBill Paul }
265795d67482SBill Paul 
2658d7acafa1SMarius Strobl static const struct bge_revision *
26594c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
26604c0da0ffSGleb Smirnoff {
26614c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
26624c0da0ffSGleb Smirnoff 
26634c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
26644c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
26654c0da0ffSGleb Smirnoff 			return (br);
26664c0da0ffSGleb Smirnoff 	}
26674c0da0ffSGleb Smirnoff 
26684c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
26694c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
26704c0da0ffSGleb Smirnoff 			return (br);
26714c0da0ffSGleb Smirnoff 	}
26724c0da0ffSGleb Smirnoff 
26734c0da0ffSGleb Smirnoff 	return (NULL);
26744c0da0ffSGleb Smirnoff }
26754c0da0ffSGleb Smirnoff 
2676d7acafa1SMarius Strobl static const struct bge_vendor *
26774c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26784c0da0ffSGleb Smirnoff {
26794c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26804c0da0ffSGleb Smirnoff 
26814c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26824c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26834c0da0ffSGleb Smirnoff 			return (v);
26844c0da0ffSGleb Smirnoff 
26854c0da0ffSGleb Smirnoff 	return (NULL);
26864c0da0ffSGleb Smirnoff }
26874c0da0ffSGleb Smirnoff 
2688d7acafa1SMarius Strobl static uint32_t
2689d7acafa1SMarius Strobl bge_chipid(device_t dev)
269095d67482SBill Paul {
2691978f2704SMarius Strobl 	uint32_t id;
269295d67482SBill Paul 
2693a5779553SStanislav Sedov 	id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2694a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
26951108273aSPyun YongHyeon 	if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
26961108273aSPyun YongHyeon 		/*
2697d7acafa1SMarius Strobl 		 * Find the ASCI revision.  Different chips use different
2698d7acafa1SMarius Strobl 		 * registers.
26991108273aSPyun YongHyeon 		 */
27001108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
27011108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
27021108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2703bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
270450515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
27052927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5725:
27062927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5727:
27072927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5762:
270867129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57764:
270967129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57767:
271067129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57787:
27111108273aSPyun YongHyeon 			id = pci_read_config(dev,
27121108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
27131108273aSPyun YongHyeon 			break;
2714b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2715fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57762:
2716b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2717fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57766:
2718b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
271967129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57782:
2720b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
272167129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57786:
2722b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2723b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2724b4a256acSPyun YongHyeon 			id = pci_read_config(dev,
2725b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2726b4a256acSPyun YongHyeon 			break;
27271108273aSPyun YongHyeon 		default:
2728d7acafa1SMarius Strobl 			id = pci_read_config(dev, BGE_PCI_PRODID_ASICREV, 4);
27291108273aSPyun YongHyeon 		}
27301108273aSPyun YongHyeon 	}
2731d7acafa1SMarius Strobl 	return (id);
2732d7acafa1SMarius Strobl }
2733d7acafa1SMarius Strobl 
2734d7acafa1SMarius Strobl /*
2735d7acafa1SMarius Strobl  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2736d7acafa1SMarius Strobl  * against our list and return its name if we find a match.
2737d7acafa1SMarius Strobl  *
2738d7acafa1SMarius Strobl  * Note that since the Broadcom controller contains VPD support, we
2739d7acafa1SMarius Strobl  * try to get the device name string from the controller itself instead
2740d7acafa1SMarius Strobl  * of the compiled-in string. It guarantees we'll always announce the
2741d7acafa1SMarius Strobl  * right product name. We fall back to the compiled-in string when
2742d7acafa1SMarius Strobl  * VPD is unavailable or corrupt.
2743d7acafa1SMarius Strobl  */
2744d7acafa1SMarius Strobl static int
2745d7acafa1SMarius Strobl bge_probe(device_t dev)
2746d7acafa1SMarius Strobl {
2747d7acafa1SMarius Strobl 	char buf[96];
2748d7acafa1SMarius Strobl 	char model[64];
2749d7acafa1SMarius Strobl 	const struct bge_revision *br;
2750d7acafa1SMarius Strobl 	const char *pname;
2751d7acafa1SMarius Strobl 	struct bge_softc *sc;
2752d7acafa1SMarius Strobl 	const struct bge_type *t = bge_devs;
2753d7acafa1SMarius Strobl 	const struct bge_vendor *v;
2754d7acafa1SMarius Strobl 	uint32_t id;
2755d7acafa1SMarius Strobl 	uint16_t did, vid;
2756d7acafa1SMarius Strobl 
2757d7acafa1SMarius Strobl 	sc = device_get_softc(dev);
2758d7acafa1SMarius Strobl 	sc->bge_dev = dev;
2759d7acafa1SMarius Strobl 	vid = pci_get_vendor(dev);
2760d7acafa1SMarius Strobl 	did = pci_get_device(dev);
2761d7acafa1SMarius Strobl 	while(t->bge_vid != 0) {
2762d7acafa1SMarius Strobl 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2763d7acafa1SMarius Strobl 			id = bge_chipid(dev);
27644c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
2765852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2766852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
2767d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s", pname);
2768d7acafa1SMarius Strobl 			else {
2769d7acafa1SMarius Strobl 				v = bge_lookup_vendor(vid);
2770d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s %s",
2771d7acafa1SMarius Strobl 				    v != NULL ? v->v_name : "Unknown",
27727c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
27732ad1b396SMarius Strobl 				    "NetXtreme/NetLink Ethernet Controller");
2774d7acafa1SMarius Strobl 			}
2775d7acafa1SMarius Strobl 			snprintf(buf, sizeof(buf), "%s, %sASIC rev. %#08x",
2776d7acafa1SMarius Strobl 			    model, br != NULL ? "" : "unknown ", id);
27774c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
2778d7acafa1SMarius Strobl 			return (BUS_PROBE_DEFAULT);
277995d67482SBill Paul 		}
278095d67482SBill Paul 		t++;
278195d67482SBill Paul 	}
278295d67482SBill Paul 
278395d67482SBill Paul 	return (ENXIO);
278495d67482SBill Paul }
278595d67482SBill Paul 
2786f41ac2beSBill Paul static void
27873f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2788f41ac2beSBill Paul {
2789f41ac2beSBill Paul 	int i;
2790f41ac2beSBill Paul 
27913f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2792f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2793f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
27940ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2795f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2796f41ac2beSBill Paul 	}
2797943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2798943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2799943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2800f41ac2beSBill Paul 
28013f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2802f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2803f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2804f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2805f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2806f41ac2beSBill Paul 	}
2807943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2808943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2809943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2810f41ac2beSBill Paul 
28113f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2812f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2813f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
28140ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2815f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2816f41ac2beSBill Paul 	}
2817f41ac2beSBill Paul 
28180ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
28190ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2820c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2821c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
28220ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
28230ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2824f41ac2beSBill Paul 
28253f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2826068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring_paddr)
2827e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2828e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2829068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring)
2830f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2831f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2832f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2833f41ac2beSBill Paul 
2834f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2835f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2836f41ac2beSBill Paul 
28373f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2838068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring_paddr)
2839e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2840e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2841e65bed95SPyun YongHyeon 
2842068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring)
2843f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2844f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2845f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2846f41ac2beSBill Paul 
2847f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2848f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2849f41ac2beSBill Paul 
28503f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2851068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring_paddr)
2852e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2853e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2854e65bed95SPyun YongHyeon 
2855068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring)
2856f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2857f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2858f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2859f41ac2beSBill Paul 
2860f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2861f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2862f41ac2beSBill Paul 
28633f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2864068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring_paddr)
2865e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2866e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2867e65bed95SPyun YongHyeon 
2868068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring)
2869f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2870f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2871f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2872f41ac2beSBill Paul 
2873f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2874f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2875f41ac2beSBill Paul 
28763f74909aSGleb Smirnoff 	/* Destroy status block. */
2877068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block_paddr)
2878e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2879e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2880e65bed95SPyun YongHyeon 
2881068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block)
2882f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2883f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2884f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2885f41ac2beSBill Paul 
2886f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2887f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2888f41ac2beSBill Paul 
28893f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2890068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats_paddr)
2891e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2892e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2893e65bed95SPyun YongHyeon 
2894068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats)
2895f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2896f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2897f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2898f41ac2beSBill Paul 
2899f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2900f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2901f41ac2beSBill Paul 
29025b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
29035b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
29045b610048SPyun YongHyeon 
29053f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2906f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2907f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2908f41ac2beSBill Paul }
2909f41ac2beSBill Paul 
2910f41ac2beSBill Paul static int
29115b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
29125b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
29135b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2914f41ac2beSBill Paul {
29153f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
29165b610048SPyun YongHyeon 	int error;
2917f41ac2beSBill Paul 
29185b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2919fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
29205b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
29215b610048SPyun YongHyeon 	if (error != 0) {
29225b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29235b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
29245b610048SPyun YongHyeon 		return (ENOMEM);
29255b610048SPyun YongHyeon 	}
29265b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
29275b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
29285b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
29295b610048SPyun YongHyeon 	if (error != 0) {
29305b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29315b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
29325b610048SPyun YongHyeon 		return (ENOMEM);
29335b610048SPyun YongHyeon 	}
29345b610048SPyun YongHyeon 	/* Load the address of the ring. */
29355b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
29365b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
29375b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
29385b610048SPyun YongHyeon 	if (error != 0) {
29395b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29405b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
29415b610048SPyun YongHyeon 		return (ENOMEM);
29425b610048SPyun YongHyeon 	}
29435b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
29445b610048SPyun YongHyeon 	return (0);
29455b610048SPyun YongHyeon }
29465b610048SPyun YongHyeon 
29475b610048SPyun YongHyeon static int
29485b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
29495b610048SPyun YongHyeon {
29505b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2951fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
29525b610048SPyun YongHyeon 	int i, error;
2953f41ac2beSBill Paul 
2954f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2955f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2956f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2957f41ac2beSBill Paul 	/*
2958f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2959f41ac2beSBill Paul 	 */
29604eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2961f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
29624eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
29634eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2964e65bed95SPyun YongHyeon 	if (error != 0) {
2965fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2966fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2967e65bed95SPyun YongHyeon 		return (ENOMEM);
2968e65bed95SPyun YongHyeon 	}
2969e65bed95SPyun YongHyeon 
29705b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
29715b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
29725b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
29735b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
29745b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
29755b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
29765b610048SPyun YongHyeon 	if (error)
29775b610048SPyun YongHyeon 		return (error);
29785b610048SPyun YongHyeon 
29795b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
29805b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
29815b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
29825b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
29835b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
29845b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
29855b610048SPyun YongHyeon 	if (error)
29865b610048SPyun YongHyeon 		return (error);
29875b610048SPyun YongHyeon 
29885b610048SPyun YongHyeon 	/* Create tag for TX ring. */
29895b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
29905b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
29915b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
29925b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
29935b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
29945b610048SPyun YongHyeon 	if (error)
29955b610048SPyun YongHyeon 		return (error);
29965b610048SPyun YongHyeon 
2997f41ac2beSBill Paul 	/*
29985b610048SPyun YongHyeon 	 * Create tag for status block.
29995b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
30005b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
30015b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
30025b610048SPyun YongHyeon 	 * of configured number of ring.
3003f41ac2beSBill Paul 	 */
30045b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
30055b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
30065b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
30075b610048SPyun YongHyeon 	else
30085b610048SPyun YongHyeon 		sbsz = 32;
30095b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
30105b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
30115b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
30125b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
30135b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
30145b610048SPyun YongHyeon 	if (error)
30155b610048SPyun YongHyeon 		return (error);
30165b610048SPyun YongHyeon 
301712c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
301812c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
301912c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
302012c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
302112c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
302212c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
302312c65daeSPyun YongHyeon 	if (error)
302412c65daeSPyun YongHyeon 		return (error);
302512c65daeSPyun YongHyeon 
30265b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
30275b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30285b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
30295b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
30305b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
30315b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
30325b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
30335b610048SPyun YongHyeon 		if (error)
30345b610048SPyun YongHyeon 			return (error);
30355b610048SPyun YongHyeon 	}
30365b610048SPyun YongHyeon 
30375b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
3038d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
3039d2ffe15aSPyun YongHyeon 		/*
3040d2ffe15aSPyun YongHyeon 		 * XXX
3041d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
3042d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
3043062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
3044062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
3045d2ffe15aSPyun YongHyeon 		 */
3046062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3047d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
3048d2ffe15aSPyun YongHyeon 	}
3049fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
3050fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
3051fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
3052fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
30535b610048SPyun YongHyeon 	if (error != 0) {
30545b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
30555b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
30565b610048SPyun YongHyeon 		return (ENOMEM);
30575b610048SPyun YongHyeon 	}
30585b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
30591108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
3060ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
3061ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
3062ca3f1187SPyun YongHyeon 	} else {
3063ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
3064ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
3065ca3f1187SPyun YongHyeon 	}
30665b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
3067ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
3068ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
3069ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
3070f41ac2beSBill Paul 
3071f41ac2beSBill Paul 	if (error) {
30720ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
30730ac56796SPyun YongHyeon 		return (ENOMEM);
30740ac56796SPyun YongHyeon 	}
30750ac56796SPyun YongHyeon 
30765b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
3077f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
3078f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
3079f5459d4cSPyun YongHyeon 	else
3080f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
30815b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
3082f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
3083f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
30840ac56796SPyun YongHyeon 
30850ac56796SPyun YongHyeon 	if (error) {
30860ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3087f41ac2beSBill Paul 		return (ENOMEM);
3088f41ac2beSBill Paul 	}
3089f41ac2beSBill Paul 
30903f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3091943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3092943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3093943787f3SPyun YongHyeon 	if (error) {
3094943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3095943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3096943787f3SPyun YongHyeon 		return (ENOMEM);
3097943787f3SPyun YongHyeon 	}
3098f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
30990ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3100f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3101f41ac2beSBill Paul 		if (error) {
3102fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3103fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3104f41ac2beSBill Paul 			return (ENOMEM);
3105f41ac2beSBill Paul 		}
3106f41ac2beSBill Paul 	}
3107f41ac2beSBill Paul 
31083f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3109f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
31100ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3111f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3112f41ac2beSBill Paul 		if (error) {
3113fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31140ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3115f41ac2beSBill Paul 			return (ENOMEM);
3116f41ac2beSBill Paul 		}
3117f41ac2beSBill Paul 	}
3118f41ac2beSBill Paul 
31195b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
31204c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
31215b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
31228a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
31231be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
31241be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3125f41ac2beSBill Paul 		if (error) {
3126fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31273f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3128f41ac2beSBill Paul 			return (ENOMEM);
3129f41ac2beSBill Paul 		}
31303f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3131943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3132943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3133943787f3SPyun YongHyeon 		if (error) {
3134943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
31351b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3136943787f3SPyun YongHyeon 			return (ENOMEM);
3137943787f3SPyun YongHyeon 		}
3138f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3139f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3140f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3141f41ac2beSBill Paul 			if (error) {
3142fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
31433f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3144f41ac2beSBill Paul 				return (ENOMEM);
3145f41ac2beSBill Paul 			}
3146f41ac2beSBill Paul 		}
3147f41ac2beSBill Paul 	}
3148f41ac2beSBill Paul 
3149f41ac2beSBill Paul 	return (0);
3150f41ac2beSBill Paul }
3151f41ac2beSBill Paul 
3152bf6ef57aSJohn Polstra /*
3153bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3154bf6ef57aSJohn Polstra  */
3155bf6ef57aSJohn Polstra static int
3156bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3157bf6ef57aSJohn Polstra {
3158bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
315955aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3160bf6ef57aSJohn Polstra 
316155aaf894SMarius Strobl 	d = pci_get_domain(dev);
3162bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3163bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3164bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3165bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
316655aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3167bf6ef57aSJohn Polstra 			return (1);
3168bf6ef57aSJohn Polstra 	return (0);
3169bf6ef57aSJohn Polstra }
3170bf6ef57aSJohn Polstra 
3171bf6ef57aSJohn Polstra /*
3172bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3173bf6ef57aSJohn Polstra  */
3174bf6ef57aSJohn Polstra static int
3175bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3176bf6ef57aSJohn Polstra {
3177bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3178bf6ef57aSJohn Polstra 
3179d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
31805c952e8dSPyun YongHyeon 		return (0);
31815c952e8dSPyun YongHyeon 
31821108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
31831108273aSPyun YongHyeon #ifdef DEVICE_POLLING
31841108273aSPyun YongHyeon 	return (0);
31851108273aSPyun YongHyeon #endif
3186bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3187a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3188bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3189bf6ef57aSJohn Polstra 		/*
3190a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3191a8376f70SMarius Strobl 		 * configured in single-port mode.
3192bf6ef57aSJohn Polstra 		 */
3193bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3194bf6ef57aSJohn Polstra 			can_use_msi = 1;
3195bf6ef57aSJohn Polstra 		break;
3196bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3197bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3198bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3199bf6ef57aSJohn Polstra 			can_use_msi = 1;
3200bf6ef57aSJohn Polstra 		break;
3201a8376f70SMarius Strobl 	default:
3202a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3203bf6ef57aSJohn Polstra 			can_use_msi = 1;
3204bf6ef57aSJohn Polstra 	}
3205bf6ef57aSJohn Polstra 	return (can_use_msi);
3206bf6ef57aSJohn Polstra }
3207bf6ef57aSJohn Polstra 
320895d67482SBill Paul static int
3209062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3210062af0b0SPyun YongHyeon {
3211062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3212062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3213062af0b0SPyun YongHyeon 		const uint16_t vendor;
3214062af0b0SPyun YongHyeon 		const uint16_t device;
3215062af0b0SPyun YongHyeon 		const char *desc;
321629658c96SDimitry Andric 	} mbox_reorder_lists[] = {
3217062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3218062af0b0SPyun YongHyeon 	};
3219062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3220062af0b0SPyun YongHyeon 	device_t bus, dev;
322147f4a4dcSMarius Strobl 	int i;
3222062af0b0SPyun YongHyeon 
3223062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3224062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3225062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3226062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3227062af0b0SPyun YongHyeon 	for (;;) {
3228062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3229062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3230062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3231062af0b0SPyun YongHyeon 			break;
323247f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3233062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3234062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3235062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3236062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3237062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3238062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3239062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3240062af0b0SPyun YongHyeon 				return (1);
3241062af0b0SPyun YongHyeon 			}
3242062af0b0SPyun YongHyeon 		}
3243062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
3244062af0b0SPyun YongHyeon 			break;
3245062af0b0SPyun YongHyeon 	}
3246062af0b0SPyun YongHyeon 	return (0);
3247062af0b0SPyun YongHyeon }
3248062af0b0SPyun YongHyeon 
3249ea9c3a30SPyun YongHyeon static void
3250ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3251ea9c3a30SPyun YongHyeon {
3252ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3253ea9c3a30SPyun YongHyeon 
3254ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3255ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3256ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3257ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3258ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3259ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3260ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3261ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3262ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3263ea9c3a30SPyun YongHyeon 			clk = 133;
3264ea9c3a30SPyun YongHyeon 		else {
3265ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3266ea9c3a30SPyun YongHyeon 			switch (clk) {
3267ea9c3a30SPyun YongHyeon 			case 0:
3268ea9c3a30SPyun YongHyeon 				clk = 33;
3269ea9c3a30SPyun YongHyeon 				break;
3270ea9c3a30SPyun YongHyeon 			case 2:
3271ea9c3a30SPyun YongHyeon 				clk = 50;
3272ea9c3a30SPyun YongHyeon 				break;
3273ea9c3a30SPyun YongHyeon 			case 4:
3274ea9c3a30SPyun YongHyeon 				clk = 66;
3275ea9c3a30SPyun YongHyeon 				break;
3276ea9c3a30SPyun YongHyeon 			case 6:
3277ea9c3a30SPyun YongHyeon 				clk = 100;
3278ea9c3a30SPyun YongHyeon 				break;
3279ea9c3a30SPyun YongHyeon 			case 7:
3280ea9c3a30SPyun YongHyeon 				clk = 133;
3281ea9c3a30SPyun YongHyeon 				break;
3282ea9c3a30SPyun YongHyeon 			}
3283ea9c3a30SPyun YongHyeon 		}
3284ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3285ea9c3a30SPyun YongHyeon 	} else {
3286ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3287ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3288ea9c3a30SPyun YongHyeon 		else
3289ea9c3a30SPyun YongHyeon 			printf("PCI ");
3290ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3291ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3292ea9c3a30SPyun YongHyeon 			clk = 66;
3293ea9c3a30SPyun YongHyeon 		else
3294ea9c3a30SPyun YongHyeon 			clk = 33;
3295ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3296ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3297ea9c3a30SPyun YongHyeon 		else
3298ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3299ea9c3a30SPyun YongHyeon 	}
3300ea9c3a30SPyun YongHyeon }
3301ea9c3a30SPyun YongHyeon 
3302062af0b0SPyun YongHyeon static int
33033f74909aSGleb Smirnoff bge_attach(device_t dev)
330495d67482SBill Paul {
3305fba8b109SMarcel Moolenaar 	if_t ifp;
330695d67482SBill Paul 	struct bge_softc *sc;
3307548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
330808013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3309ad4328baSMarius Strobl 	int capmask, error, reg, rid, trys;
331095d67482SBill Paul 
331195d67482SBill Paul 	sc = device_get_softc(dev);
331295d67482SBill Paul 	sc->bge_dev = dev;
331395d67482SBill Paul 
3314e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3315dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3316e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3317dfe0df9aSPyun YongHyeon 
331895d67482SBill Paul 	pci_enable_busmaster(dev);
331995d67482SBill Paul 
3320ad4328baSMarius Strobl 	/*
3321ad4328baSMarius Strobl 	 * Allocate control/status registers.
3322ad4328baSMarius Strobl 	 */
3323736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
33245f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
332544f8f2fcSMarius Strobl 	    RF_ACTIVE);
332695d67482SBill Paul 
332795d67482SBill Paul 	if (sc->bge_res == NULL) {
3328548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
332995d67482SBill Paul 		error = ENXIO;
333095d67482SBill Paul 		goto fail;
333195d67482SBill Paul 	}
333295d67482SBill Paul 
33334f09c4c7SMarius Strobl 	/* Save various chip information. */
3334548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3335d7acafa1SMarius Strobl 	sc->bge_chipid = bge_chipid(dev);
3336e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3337e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3338e53d81eeSPaul Saab 
3339a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3340daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
33411108273aSPyun YongHyeon 	 /*
33421108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
33431108273aSPyun YongHyeon 	  *
33441108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
33451108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
33461108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
33471108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
33481108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3349bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
335050515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
33511108273aSPyun YongHyeon 	  *
3352548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3353548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3354548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3355548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3356548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3357548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3358548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3359548c8f1aSPyun YongHyeon 	  *
33601108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
33611108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
33621108273aSPyun YongHyeon 	  */
3363bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
336450515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
336550515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3366548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
33671108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
33681108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3369daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33701108273aSPyun YongHyeon 			else
3371daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3372bbe2ca75SPyun YongHyeon 		} else {
33731108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33741108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3375daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33761108273aSPyun YongHyeon 			else
3377daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
33781108273aSPyun YongHyeon 		}
33791108273aSPyun YongHyeon 	}
3380a813ed78SPyun YongHyeon 
33815fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
33825fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
338308013fd3SMarius Strobl 
33840dae9719SJung-uk Kim 	/* Save chipset family. */
33850dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
33862927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3387fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57765:
3388fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57766:
3389fe26ad88SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_57765_PLUS;
3390fe26ad88SPyun YongHyeon 		/* FALLTHROUGH */
33911108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3392bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
339350515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
33941108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
33951108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3396b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
339729b44b09SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
339829b44b09SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
339929b44b09SPyun YongHyeon 			/*
340029b44b09SPyun YongHyeon 			 * Enable work around for DMA engine miscalculation
340129b44b09SPyun YongHyeon 			 * of TXMBUF available space.
340229b44b09SPyun YongHyeon 			 */
340329b44b09SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_RDMA_BUG;
3404bbe2ca75SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3405bbe2ca75SPyun YongHyeon 			    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3406bbe2ca75SPyun YongHyeon 				/* Jumbo frame on BCM5719 A0 does not work. */
3407463a7e27SPyun YongHyeon 				sc->bge_flags &= ~BGE_FLAG_JUMBO;
3408bbe2ca75SPyun YongHyeon 			}
340929b44b09SPyun YongHyeon 		}
34101108273aSPyun YongHyeon 		break;
3411a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3412a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3413a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3414a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3415a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3416a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3417a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3418a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3419a5779553SStanislav Sedov 		break;
34200dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
34210dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
34220dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
34230dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
34247ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
34250dae9719SJung-uk Kim 		break;
34260dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
34270dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
34280dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3429f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
34309fe569d8SXin LI 		/* FALLTHROUGH */
34310dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
34320dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
343338cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
34340dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
34359fe569d8SXin LI 		/* FALLTHROUGH */
34360dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
34370dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
34380dae9719SJung-uk Kim 		break;
34390dae9719SJung-uk Kim 	}
34400dae9719SJung-uk Kim 
3441548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3442548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3443548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3444548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3445548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3446548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
34472927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3448548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3449548c8f1aSPyun YongHyeon 		break;
3450548c8f1aSPyun YongHyeon 	}
3451548c8f1aSPyun YongHyeon 
3452548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3453548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3454548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3455548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3456548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3457548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3458548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3459548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3460548c8f1aSPyun YongHyeon 			error = ENXIO;
3461548c8f1aSPyun YongHyeon 			goto fail;
3462548c8f1aSPyun YongHyeon 		}
3463548c8f1aSPyun YongHyeon 
3464548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3465548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3466548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3467548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3468548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3469548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3470548c8f1aSPyun YongHyeon 
3471548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3472548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3473548c8f1aSPyun YongHyeon 	}
3474548c8f1aSPyun YongHyeon 
3475749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3476749a5269SMarius Strobl 	bge_add_sysctls(sc);
3477749a5269SMarius Strobl 
3478a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
34791108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
34801108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3481a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3482a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3483a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3484a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3485a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3486a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3487a813ed78SPyun YongHyeon 	else
3488a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
34897ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
34907ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
34917ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3492a813ed78SPyun YongHyeon 
3493f681b29aSPyun YongHyeon 	/*
3494d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3495f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3496f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3497f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3498f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3499f681b29aSPyun YongHyeon 	 */
3500f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
35014f0794ffSBjoern A. Zeeb 
3502d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3503d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3504d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3505d9820cd8SPyun YongHyeon 
3506a7fcfcf3SPyun YongHyeon 	/*
3507a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3508a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3509a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3510a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3511a7fcfcf3SPyun YongHyeon 	 */
3512a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3513a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3514a7fcfcf3SPyun YongHyeon 
3515ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3516fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
35174f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
35184f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
35194f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
352084ac96f8SPyun YongHyeon 	}
35214f0794ffSBjoern A. Zeeb 
3522fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3523fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3524fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3525fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3526fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3527fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3528fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3529fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3530fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3531fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3532fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3533fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3534fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3535d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57791 ||
3536d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57795 ||
3537fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3538fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3539fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3540d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3541fb772a6cSMarius Strobl 	}
3542fb772a6cSMarius Strobl 
3543e53d81eeSPaul Saab 	/*
3544ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3545ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3546ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3547ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3548d7acafa1SMarius Strobl 	 * known bug which can't handle TSO if Ethernet header + IP/TCP
3549d7acafa1SMarius Strobl 	 * header is greater than 80 bytes. A workaround for the TSO
3550ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3551ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3552ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3553ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3554ca3f1187SPyun YongHyeon 	 */
35551108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
35561108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
35571108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3558bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3559bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3560bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3561bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3562bbe2ca75SPyun YongHyeon 		}
35631108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
35644f4a16e1SPyun YongHyeon 		/*
35654f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
35664f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3567be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
35684f4a16e1SPyun YongHyeon 		 */
35694f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3570be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3571be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3572ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
35734f4a16e1SPyun YongHyeon 	}
3574ca3f1187SPyun YongHyeon 
3575ca3f1187SPyun YongHyeon 	/*
35766f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3577e53d81eeSPaul Saab 	 */
35783b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
35794c0da0ffSGleb Smirnoff 		/*
35806f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
35816f8718a3SScott Long 		 * must be a PCI Express device.
35826f8718a3SScott Long 		 */
35836f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
35840aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
358548630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
358648630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
358748630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
358848630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
358950515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
359050515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
359148630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
359248630d79SPyun YongHyeon 		else
359348630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
359448630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
35956f8718a3SScott Long 	} else {
35966f8718a3SScott Long 		/*
35976f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
35986f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
35994c0da0ffSGleb Smirnoff 		 */
36003b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
36010aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
360290447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
36034c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3604652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
36056f8718a3SScott Long 	}
36064c0da0ffSGleb Smirnoff 
3607bf6ef57aSJohn Polstra 	/*
3608fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3609fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3610fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3611fd4d32feSPyun YongHyeon 	 */
3612fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3613fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3614fd4d32feSPyun YongHyeon 	/*
3615062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3616062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3617062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3618062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3619062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3620062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3621062af0b0SPyun YongHyeon 	 */
3622062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3623062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3624062af0b0SPyun YongHyeon 	/*
3625bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3626bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3627bf6ef57aSJohn Polstra 	 * normal operation.
3628bf6ef57aSJohn Polstra 	 */
36290aaf1057SPyun YongHyeon 	rid = 0;
36303b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
36310aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3632ad4328baSMarius Strobl 		reg = 1;
3633ad4328baSMarius Strobl 		if (bge_can_use_msi(sc) && pci_alloc_msi(dev, &reg) == 0) {
3634bf6ef57aSJohn Polstra 			rid = 1;
3635bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
36360aaf1057SPyun YongHyeon 		}
36370aaf1057SPyun YongHyeon 	}
3638bf6ef57aSJohn Polstra 
36391108273aSPyun YongHyeon 	/*
36401108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
36411108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
36421108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
36431108273aSPyun YongHyeon 	 */
36441108273aSPyun YongHyeon #ifndef DEVICE_POLLING
36451108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
36461108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
36471108273aSPyun YongHyeon #endif
36481108273aSPyun YongHyeon 
3649bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3650ad4328baSMarius Strobl 	    RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
3651bf6ef57aSJohn Polstra 
3652bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3653bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3654bf6ef57aSJohn Polstra 		error = ENXIO;
3655bf6ef57aSJohn Polstra 		goto fail;
3656bf6ef57aSJohn Polstra 	}
3657bf6ef57aSJohn Polstra 
3658ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
36594f09c4c7SMarius Strobl 
36608cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3661548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3662548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3663888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3664888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3665548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3666548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
36678cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
36688cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3669d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
36708cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
36718cb1383cSDoug Ambrisko 			}
36728cb1383cSDoug Ambrisko 		}
3673548c8f1aSPyun YongHyeon 	}
36748cb1383cSDoug Ambrisko 
36758cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
36763dd76c98SPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
36778cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
36788cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
36798cb1383cSDoug Ambrisko 		error = ENXIO;
36808cb1383cSDoug Ambrisko 		goto fail;
36818cb1383cSDoug Ambrisko 	}
36828cb1383cSDoug Ambrisko 
36833dd76c98SPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
36843dd76c98SPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
368595d67482SBill Paul 
368695d67482SBill Paul 	if (bge_chipinit(sc)) {
3687fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
368895d67482SBill Paul 		error = ENXIO;
368995d67482SBill Paul 		goto fail;
369095d67482SBill Paul 	}
369195d67482SBill Paul 
369238cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
369338cc658fSJohn Baldwin 	if (error) {
369408013fd3SMarius Strobl 		device_printf(sc->bge_dev,
369508013fd3SMarius Strobl 		    "failed to read station address\n");
369695d67482SBill Paul 		error = ENXIO;
369795d67482SBill Paul 		goto fail;
369895d67482SBill Paul 	}
369995d67482SBill Paul 
3700f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
37011108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
37021108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
37031108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3704f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3705f41ac2beSBill Paul 	else
3706f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3707f41ac2beSBill Paul 
37085b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3709fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3710fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3711f41ac2beSBill Paul 		error = ENXIO;
3712f41ac2beSBill Paul 		goto fail;
3713f41ac2beSBill Paul 	}
3714f41ac2beSBill Paul 
371595d67482SBill Paul 	/* Set default tuneable values. */
371695d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
371795d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
371895d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
37196f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
37206f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
372195d67482SBill Paul 
372235f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
372335f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
372435f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
372535f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
372635f945cdSPyun YongHyeon 
372795d67482SBill Paul 	/* Set up ifnet structure */
3728fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3729fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3730fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3731fc74a9f9SBrooks Davis 		error = ENXIO;
3732fc74a9f9SBrooks Davis 		goto fail;
3733fc74a9f9SBrooks Davis 	}
3734fba8b109SMarcel Moolenaar 	if_setsoftc(ifp, sc);
37359bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
3736fba8b109SMarcel Moolenaar 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
3737fba8b109SMarcel Moolenaar 	if_setioctlfn(ifp, bge_ioctl);
3738fba8b109SMarcel Moolenaar 	if_setstartfn(ifp, bge_start);
3739fba8b109SMarcel Moolenaar 	if_setinitfn(ifp, bge_init);
3740df360178SGleb Smirnoff 	if_setgetcounterfn(ifp, bge_get_counter);
37414a81240cSMarcel Moolenaar 	if_setsendqlen(ifp, BGE_TX_RING_CNT - 1);
3742fba8b109SMarcel Moolenaar 	if_setsendqready(ifp);
3743fba8b109SMarcel Moolenaar 	if_sethwassist(ifp, sc->bge_csum_features);
3744fba8b109SMarcel Moolenaar 	if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
3745fba8b109SMarcel Moolenaar 	    IFCAP_VLAN_MTU);
37461108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3747fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, CSUM_TSO, 0);
3748fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_TSO4 | IFCAP_VLAN_HWTSO, 0);
3749ca3f1187SPyun YongHyeon 	}
37504e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
3751fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWCSUM, 0);
37524e35d186SJung-uk Kim #endif
3753fba8b109SMarcel Moolenaar 	if_setcapenable(ifp, if_getcapabilities(ifp));
375475719184SGleb Smirnoff #ifdef DEVICE_POLLING
3755fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
375675719184SGleb Smirnoff #endif
375795d67482SBill Paul 
3758a1d52896SBill Paul 	/*
3759d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3760d375e524SGleb Smirnoff 	 * to hardware bugs.
3761d375e524SGleb Smirnoff 	 */
3762d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3763fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, 0, IFCAP_HWCSUM);
3764fba8b109SMarcel Moolenaar 		if_setcapenablebit(ifp, 0, IFCAP_HWCSUM);
3765fba8b109SMarcel Moolenaar 		if_sethwassist(ifp, 0);
3766d375e524SGleb Smirnoff 	}
3767d375e524SGleb Smirnoff 
3768d375e524SGleb Smirnoff 	/*
3769a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
377041abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
377141abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
377241abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
377341abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
377441abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
377541abcc1bSPaul Saab 	 * SK-9D41.
3776a1d52896SBill Paul 	 */
3777888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3778888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
37795fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
37805fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3781f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3782f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3783fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3784f6789fbaSPyun YongHyeon 			error = ENXIO;
3785f6789fbaSPyun YongHyeon 			goto fail;
3786f6789fbaSPyun YongHyeon 		}
378741abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
378841abcc1bSPaul Saab 	}
378941abcc1bSPaul Saab 
379095d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3791ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3792ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
379370c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3794ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
379570c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
379670c2071bSPyun YongHyeon 		} else
3797652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3798ea3b4127SPyun YongHyeon 	}
379995d67482SBill Paul 
380070c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
380170c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
380270c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
380370c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
380470c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
380570c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
380670c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
380770c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
380870c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
380970c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
381070c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
381170c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
381270c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
381370c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3814fe26ad88SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3815fe26ad88SPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc)) {
381670c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
381770c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
381870c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
381970c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
382070c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
382170c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
382270c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
382370c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
382470c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
382570c2071bSPyun YongHyeon 		} else
382670c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
382770c2071bSPyun YongHyeon 	}
382870c2071bSPyun YongHyeon 
382970c2071bSPyun YongHyeon 	/*
3830d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
383170c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
383270c2071bSPyun YongHyeon 	 */
383370c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
383470c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
383570c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3836d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
383770c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
383870c2071bSPyun YongHyeon 
3839652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
384009a8241fSGleb Smirnoff 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
38410c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
38420c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
38436098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
38446098821cSJung-uk Kim 		    0, NULL);
384595d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
384695d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3847da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
384895d67482SBill Paul 	} else {
384995d67482SBill Paul 		/*
38508cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
38518cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
38528cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
38538cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
38548cb1383cSDoug Ambrisko 		 * the PHY.
385595d67482SBill Paul 		 */
38564012d104SMarius Strobl 		trys = 0;
38578cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
38588cb1383cSDoug Ambrisko again:
38598cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
38608cb1383cSDoug Ambrisko 
3861fba8b109SMarcel Moolenaar 		error = mii_attach(dev, &sc->bge_miibus, ifp,
3862fba8b109SMarcel Moolenaar 		    (ifm_change_cb_t)bge_ifmedia_upd,
3863fba8b109SMarcel Moolenaar 		    (ifm_stat_cb_t)bge_ifmedia_sts, capmask, sc->bge_phy_addr,
3864fba8b109SMarcel Moolenaar 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
38658e5d93dbSMarius Strobl 		if (error != 0) {
38668cb1383cSDoug Ambrisko 			if (trys++ < 4) {
38678cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3868daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3869daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
38708cb1383cSDoug Ambrisko 				goto again;
38718cb1383cSDoug Ambrisko 			}
38728e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
387395d67482SBill Paul 			goto fail;
387495d67482SBill Paul 		}
38758cb1383cSDoug Ambrisko 
38768cb1383cSDoug Ambrisko 		/*
38778cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
38788cb1383cSDoug Ambrisko 		 */
38798cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
38808cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
388195d67482SBill Paul 	}
388295d67482SBill Paul 
388395d67482SBill Paul 	/*
3884e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3885e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3886e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3887e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3888e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3889e255b776SJohn Polstra 	 * payloads by copying the received packets.
3890e255b776SJohn Polstra 	 */
3891652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3892652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3893652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3894e255b776SJohn Polstra 
3895e255b776SJohn Polstra 	/*
389695d67482SBill Paul 	 * Call MI attach routine.
389795d67482SBill Paul 	 */
3898fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
38990f9bd73bSSam Leffler 
390061ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
3901fba8b109SMarcel Moolenaar 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
390261ccb9daSPyun YongHyeon 
39030f9bd73bSSam Leffler 	/*
39040f9bd73bSSam Leffler 	 * Hookup IRQ last.
39050f9bd73bSSam Leffler 	 */
3906dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3907dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
39087e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
39097e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3910dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3911dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3912dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3913dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3914dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3915e010b055SPyun YongHyeon 			error = ENOMEM;
3916dfe0df9aSPyun YongHyeon 			goto fail;
3917dfe0df9aSPyun YongHyeon 		}
3918d7acafa1SMarius Strobl 		error = taskqueue_start_threads(&sc->bge_tq, 1, PI_NET,
3919d7acafa1SMarius Strobl 		    "%s taskq", device_get_nameunit(sc->bge_dev));
3920d7acafa1SMarius Strobl 		if (error != 0) {
3921d7acafa1SMarius Strobl 			device_printf(dev, "could not start threads.\n");
3922d7acafa1SMarius Strobl 			ether_ifdetach(ifp);
3923d7acafa1SMarius Strobl 			goto fail;
3924d7acafa1SMarius Strobl 		}
3925dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3926dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3927dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3928dfe0df9aSPyun YongHyeon 	} else
3929dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3930dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3931dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
39320f9bd73bSSam Leffler 
39330f9bd73bSSam Leffler 	if (error) {
3934e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3935fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
39360f9bd73bSSam Leffler 	}
393795d67482SBill Paul 
393895d67482SBill Paul fail:
3939e010b055SPyun YongHyeon 	if (error)
3940e010b055SPyun YongHyeon 		bge_detach(dev);
394195d67482SBill Paul 	return (error);
394295d67482SBill Paul }
394395d67482SBill Paul 
394495d67482SBill Paul static int
39453f74909aSGleb Smirnoff bge_detach(device_t dev)
394695d67482SBill Paul {
394795d67482SBill Paul 	struct bge_softc *sc;
3948fba8b109SMarcel Moolenaar 	if_t ifp;
394995d67482SBill Paul 
395095d67482SBill Paul 	sc = device_get_softc(dev);
3951fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
395295d67482SBill Paul 
395375719184SGleb Smirnoff #ifdef DEVICE_POLLING
3954fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING)
395575719184SGleb Smirnoff 		ether_poll_deregister(ifp);
395675719184SGleb Smirnoff #endif
395775719184SGleb Smirnoff 
3958e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3959e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
39600f9bd73bSSam Leffler 		BGE_LOCK(sc);
396195d67482SBill Paul 		bge_stop(sc);
39620f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
39635dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3964e010b055SPyun YongHyeon 	}
39655dda8085SOleg Bulyzhin 
3966dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3967dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
396895d67482SBill Paul 
39690aba72ddSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
397095d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
39710aba72ddSPyun YongHyeon 	else if (sc->bge_miibus != NULL) {
397295d67482SBill Paul 		bus_generic_detach(dev);
397395d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
397495d67482SBill Paul 	}
397595d67482SBill Paul 
397695d67482SBill Paul 	bge_release_resources(sc);
397795d67482SBill Paul 
397895d67482SBill Paul 	return (0);
397995d67482SBill Paul }
398095d67482SBill Paul 
398195d67482SBill Paul static void
39823f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
398395d67482SBill Paul {
398495d67482SBill Paul 	device_t dev;
398595d67482SBill Paul 
398695d67482SBill Paul 	dev = sc->bge_dev;
398795d67482SBill Paul 
3988dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3989dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3990dfe0df9aSPyun YongHyeon 
399195d67482SBill Paul 	if (sc->bge_intrhand != NULL)
399295d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
399395d67482SBill Paul 
3994ad4328baSMarius Strobl 	if (sc->bge_irq != NULL) {
3995724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3996ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_irq), sc->bge_irq);
3997724bd939SJohn Polstra 		pci_release_msi(dev);
3998ad4328baSMarius Strobl 	}
399995d67482SBill Paul 
400095d67482SBill Paul 	if (sc->bge_res != NULL)
400195d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
4002ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res), sc->bge_res);
400395d67482SBill Paul 
4004548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
4005548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
4006ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res2), sc->bge_res2);
4007548c8f1aSPyun YongHyeon 
4008ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
4009ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
4010ad61f896SRuslan Ermilov 
4011f41ac2beSBill Paul 	bge_dma_free(sc);
401295d67482SBill Paul 
40130f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
40140f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
401595d67482SBill Paul }
401695d67482SBill Paul 
40178cb1383cSDoug Ambrisko static int
40183f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
401995d67482SBill Paul {
402095d67482SBill Paul 	device_t dev;
4021cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
40226f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
40230aaf1057SPyun YongHyeon 	uint16_t devctl;
40245fea260fSMarius Strobl 	int i;
402595d67482SBill Paul 
402695d67482SBill Paul 	dev = sc->bge_dev;
402795d67482SBill Paul 
4028cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
4029548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4030548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
4031cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
4032cc085b36SPyun YongHyeon 
403338cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
403438cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
40356f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
40366f8718a3SScott Long 			write_op = bge_writemem_direct;
40376f8718a3SScott Long 		else
40386f8718a3SScott Long 			write_op = bge_writemem_ind;
40399ba784dbSScott Long 	} else
40406f8718a3SScott Long 		write_op = bge_writereg_ind;
40416f8718a3SScott Long 
40423dd76c98SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
40433dd76c98SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5701) {
40443dd76c98SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
40453dd76c98SPyun YongHyeon 		for (i = 0; i < 8000; i++) {
40463dd76c98SPyun YongHyeon 			if (CSR_READ_4(sc, BGE_NVRAM_SWARB) &
40473dd76c98SPyun YongHyeon 			    BGE_NVRAMSWARB_GNT1)
40483dd76c98SPyun YongHyeon 				break;
40493dd76c98SPyun YongHyeon 			DELAY(20);
40503dd76c98SPyun YongHyeon 		}
40513dd76c98SPyun YongHyeon 		if (i == 8000) {
40523dd76c98SPyun YongHyeon 			if (bootverbose)
40533dd76c98SPyun YongHyeon 				device_printf(dev, "NVRAM lock timedout!\n");
40543dd76c98SPyun YongHyeon 		}
40553dd76c98SPyun YongHyeon 	}
4056548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
4057548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
4058548c8f1aSPyun YongHyeon 
405995d67482SBill Paul 	/* Save some important PCI state. */
406095d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
406195d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
406295d67482SBill Paul 
406395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
406495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4065e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
406695d67482SBill Paul 
40676f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
40686f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
4069a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
40706f8718a3SScott Long 		if (bootverbose)
4071333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
40726f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
40736f8718a3SScott Long 	}
40746f8718a3SScott Long 
40756f8718a3SScott Long 	/*
40766f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
40776f8718a3SScott Long 	 * When firmware finishes its initialization it will
4078888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
40796f8718a3SScott Long 	 */
4080888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
40816f8718a3SScott Long 
40820c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4083e53d81eeSPaul Saab 
4084e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4085652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4086ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4087ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
40880c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
40890c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4090ad49eccfSPyun YongHyeon 		}
4091e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4092e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
40930c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
40940c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4095e53d81eeSPaul Saab 		}
4096e53d81eeSPaul Saab 	}
4097e53d81eeSPaul Saab 
4098df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4099df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4100df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4101df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4102df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4103df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4104df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4105df4db538SPyun YongHyeon 	}
4106df4db538SPyun YongHyeon 
410721c9e407SDavid Christensen 	/*
41086f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
41096f8718a3SScott Long 	 * powered up in D0 uninitialized.
41106f8718a3SScott Long 	 */
41115512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
41125512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4113caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
41146f8718a3SScott Long 
411595d67482SBill Paul 	/* Issue global reset */
41166f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
411795d67482SBill Paul 
4118cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4119cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4120cc085b36SPyun YongHyeon 	else
412195d67482SBill Paul 		DELAY(1000);
412295d67482SBill Paul 
4123e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4124652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4125e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4126e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
41275fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
41285fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4129e53d81eeSPaul Saab 		}
41300aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4131389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
41320aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4133389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4134389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4135389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
41360aaf1057SPyun YongHyeon 		    devctl, 2);
413748630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
41380aaf1057SPyun YongHyeon 		/* Clear error status. */
4139389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4140389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4141389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4142389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4143e53d81eeSPaul Saab 	}
4144e53d81eeSPaul Saab 
41453f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
414695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
414795d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4148e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4149cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4150cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4151cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4152cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4153548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4154548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4155548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4156548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4157cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
415895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
415995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4160cbb2b2feSPyun YongHyeon 	/*
4161cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4162fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4163cbb2b2feSPyun YongHyeon 	 * read stale status block.
4164cbb2b2feSPyun YongHyeon 	 */
4165cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4166cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4167cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4168cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4169cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4170cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4171cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4172cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4173cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4174cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4175cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4176cbb2b2feSPyun YongHyeon 		}
4177cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4178cbb2b2feSPyun YongHyeon 		    devctl, 2);
4179cbb2b2feSPyun YongHyeon 	}
418022a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
41814c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4182bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4183bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
41840aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
41850aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
41860aaf1057SPyun YongHyeon 			pci_write_config(dev,
41870aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4188bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4189bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4190bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4191bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4192bf6ef57aSJohn Polstra 		}
41934c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
41944c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
41954c0da0ffSGleb Smirnoff 	} else
4196a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4197a7b0c314SPaul Saab 
4198cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4199cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4200cc085b36SPyun YongHyeon 
4201cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4202cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4203cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4204cc085b36SPyun YongHyeon 	DELAY(40);
4205cc085b36SPyun YongHyeon 
4206548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4207548c8f1aSPyun YongHyeon 
420838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
420938cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
421038cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
421138cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
421238cc658fSJohn Baldwin 				break;
421338cc658fSJohn Baldwin 			DELAY(100);
421438cc658fSJohn Baldwin 		}
421538cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4216333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
421738cc658fSJohn Baldwin 			return (1);
421838cc658fSJohn Baldwin 		}
421938cc658fSJohn Baldwin 	} else {
422095d67482SBill Paul 		/*
42216f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
422208013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
42235fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
42245fea260fSMarius Strobl 		 * address is fitted though.
422595d67482SBill Paul 		 */
422695d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4227d5d23857SJung-uk Kim 			DELAY(10);
4228888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4229888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
423095d67482SBill Paul 				break;
423195d67482SBill Paul 		}
423295d67482SBill Paul 
42335fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4234333704a3SPyun YongHyeon 			device_printf(dev,
4235333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4236333704a3SPyun YongHyeon 			    val);
4237b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4238b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4239b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
424038cc658fSJohn Baldwin 	}
424195d67482SBill Paul 
424295d67482SBill Paul 	/*
4243da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4244da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4245da3003f0SBill Paul 	 * to 1.2V.
4246da3003f0SBill Paul 	 */
4247652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4248652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
42495fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
42505fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
42515fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4252da3003f0SBill Paul 	}
4253da3003f0SBill Paul 
4254e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4255652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4256b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4257a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4258a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4259a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
42605fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
42615fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4262e53d81eeSPaul Saab 	}
42638cb1383cSDoug Ambrisko 
426450515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
426550515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
426650515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
426750515680SPyun YongHyeon 
42688cb1383cSDoug Ambrisko 	return (0);
426995d67482SBill Paul }
427095d67482SBill Paul 
4271e0b7b101SPyun YongHyeon static __inline void
4272e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4273e0b7b101SPyun YongHyeon {
4274e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4275e0b7b101SPyun YongHyeon 
4276e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4277e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4278e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4279e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4280e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4281e0b7b101SPyun YongHyeon }
4282e0b7b101SPyun YongHyeon 
4283e0b7b101SPyun YongHyeon static __inline void
4284e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4285e0b7b101SPyun YongHyeon {
4286e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4287e0b7b101SPyun YongHyeon 
4288e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4289e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4290e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4291e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4292e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4293e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4294e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4295e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4296e0b7b101SPyun YongHyeon }
4297e0b7b101SPyun YongHyeon 
429895d67482SBill Paul /*
429995d67482SBill Paul  * Frame reception handling. This is called if there's a frame
430095d67482SBill Paul  * on the receive return list.
430195d67482SBill Paul  *
430295d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
43031be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
430495d67482SBill Paul  * 2) the frame is from the standard receive ring
430595d67482SBill Paul  */
430695d67482SBill Paul 
43071abcdbd1SAttilio Rao static int
4308dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
430995d67482SBill Paul {
4310fba8b109SMarcel Moolenaar 	if_t ifp;
43111abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4312b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
431395d67482SBill Paul 
43147f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
43150f9bd73bSSam Leffler 
43163f74909aSGleb Smirnoff 	/* Nothing to do. */
43177f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
43181abcdbd1SAttilio Rao 		return (rx_npkts);
4319cfcb5025SOleg Bulyzhin 
4320fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
432195d67482SBill Paul 
4322f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4323e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4324f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
432515eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4326f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4327fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
4328fba8b109SMarcel Moolenaar 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))
4329f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
433015eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4331f41ac2beSBill Paul 
43327f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
433395d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
43343f74909aSGleb Smirnoff 		uint32_t		rxidx;
433595d67482SBill Paul 		struct mbuf		*m = NULL;
43363f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
433795d67482SBill Paul 		int			have_tag = 0;
433895d67482SBill Paul 
433975719184SGleb Smirnoff #ifdef DEVICE_POLLING
4340fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_POLLING) {
434175719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
434275719184SGleb Smirnoff 				break;
434375719184SGleb Smirnoff 			sc->rxcycles--;
434475719184SGleb Smirnoff 		}
434575719184SGleb Smirnoff #endif
434675719184SGleb Smirnoff 
43477f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
434895d67482SBill Paul 
434995d67482SBill Paul 		rxidx = cur_rx->bge_idx;
43507f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
435195d67482SBill Paul 
4352fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING &&
4353cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
435495d67482SBill Paul 			have_tag = 1;
435595d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
435695d67482SBill Paul 		}
435795d67482SBill Paul 
435895d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
435995d67482SBill Paul 			jumbocnt++;
4360943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
436195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4362e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
436395d67482SBill Paul 				continue;
436495d67482SBill Paul 			}
4365943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4366e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4367df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
436895d67482SBill Paul 				continue;
436995d67482SBill Paul 			}
437003e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
437195d67482SBill Paul 		} else {
437295d67482SBill Paul 			stdcnt++;
4373e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
437495d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4375e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
437695d67482SBill Paul 				continue;
437795d67482SBill Paul 			}
4378943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4379e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4380df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
438195d67482SBill Paul 				continue;
438295d67482SBill Paul 			}
438303e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
438495d67482SBill Paul 		}
438595d67482SBill Paul 
4386df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
4387e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4388e255b776SJohn Polstra 		/*
4389e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4390e65bed95SPyun YongHyeon 		 * the payload is aligned.
4391e255b776SJohn Polstra 		 */
4392652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4393e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4394e255b776SJohn Polstra 			    cur_rx->bge_len);
4395e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4396e255b776SJohn Polstra 		}
4397e255b776SJohn Polstra #endif
4398473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
439995d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
440095d67482SBill Paul 
4401fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_RXCSUM)
44021108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
440395d67482SBill Paul 
440495d67482SBill Paul 		/*
4405673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4406673d9191SSam Leffler 		 * attach that information to the packet.
440795d67482SBill Paul 		 */
4408d147662cSGleb Smirnoff 		if (have_tag) {
440978ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
441078ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4411d147662cSGleb Smirnoff 		}
441295d67482SBill Paul 
4413dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
44140f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4415fba8b109SMarcel Moolenaar 			if_input(ifp, m);
44160f9bd73bSSam Leffler 			BGE_LOCK(sc);
4417dfe0df9aSPyun YongHyeon 		} else
4418fba8b109SMarcel Moolenaar 			if_input(ifp, m);
4419d4da719cSAttilio Rao 		rx_npkts++;
442025e13e68SXin LI 
4421fba8b109SMarcel Moolenaar 		if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
44228cf7d13dSAttilio Rao 			return (rx_npkts);
442395d67482SBill Paul 	}
442495d67482SBill Paul 
442515eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
442615eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4427e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4428f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4429e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
44304c0da0ffSGleb Smirnoff 
4431c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4432f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
44334c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4434f41ac2beSBill Paul 
44357f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
443638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
443795d67482SBill Paul 	if (stdcnt)
4438767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4439767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
444095d67482SBill Paul 	if (jumbocnt)
4441767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4442767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4443f5a034f9SPyun YongHyeon #ifdef notyet
4444f5a034f9SPyun YongHyeon 	/*
4445f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4446f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4447f5a034f9SPyun YongHyeon 	 */
4448f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4449fba8b109SMarcel Moolenaar 		if_incierrors(ifp, CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS));
4450f5a034f9SPyun YongHyeon #endif
44511abcdbd1SAttilio Rao 	return (rx_npkts);
445295d67482SBill Paul }
445395d67482SBill Paul 
445495d67482SBill Paul static void
44551108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
44561108273aSPyun YongHyeon {
44571108273aSPyun YongHyeon 
44581108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
44591108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
44601108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44611108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44621108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
44631108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
44641108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44651108273aSPyun YongHyeon 			}
44661108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
44671108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
44681108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
44691108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44701108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
44711108273aSPyun YongHyeon 			}
44721108273aSPyun YongHyeon 		}
44731108273aSPyun YongHyeon 	} else {
44741108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44751108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44761108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
44771108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44781108273aSPyun YongHyeon 		}
44791108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
44801108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
44811108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
44821108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
44831108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44841108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
44851108273aSPyun YongHyeon 		}
44861108273aSPyun YongHyeon 	}
44871108273aSPyun YongHyeon }
44881108273aSPyun YongHyeon 
44891108273aSPyun YongHyeon static void
4490b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
449195d67482SBill Paul {
449295a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
4493fba8b109SMarcel Moolenaar 	if_t ifp;
449495d67482SBill Paul 
44950f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
44960f9bd73bSSam Leffler 
44973f74909aSGleb Smirnoff 	/* Nothing to do. */
4498b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4499cfcb5025SOleg Bulyzhin 		return;
4500cfcb5025SOleg Bulyzhin 
4501fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
450295d67482SBill Paul 
4503e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
45045c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
450595d67482SBill Paul 	/*
450695d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
450795d67482SBill Paul 	 * frames that have been sent.
450895d67482SBill Paul 	 */
4509b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
451095a0a340SPyun YongHyeon 		uint32_t		idx;
451195d67482SBill Paul 
451295d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4513f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
451495d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
4515df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
451695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
45170ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4518e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4519e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
45200ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4521f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4522e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4523e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
452495d67482SBill Paul 		}
452595d67482SBill Paul 		sc->bge_txcnt--;
452695d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
452795d67482SBill Paul 	}
452895d67482SBill Paul 
4529fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
45305b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
45315b01e77cSBruce Evans 		sc->bge_timer = 0;
453295d67482SBill Paul }
453395d67482SBill Paul 
453475719184SGleb Smirnoff #ifdef DEVICE_POLLING
45351abcdbd1SAttilio Rao static int
4536fba8b109SMarcel Moolenaar bge_poll(if_t ifp, enum poll_cmd cmd, int count)
453775719184SGleb Smirnoff {
4538fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
4539b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4540366454f2SOleg Bulyzhin 	uint32_t statusword;
45411abcdbd1SAttilio Rao 	int rx_npkts = 0;
454275719184SGleb Smirnoff 
45433f74909aSGleb Smirnoff 	BGE_LOCK(sc);
4544fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
45453f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
45461abcdbd1SAttilio Rao 		return (rx_npkts);
45473f74909aSGleb Smirnoff 	}
454875719184SGleb Smirnoff 
4549dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4550b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4551b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
45522246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4553b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4554b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4555dab5cd05SOleg Bulyzhin 
4556175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
45572246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4558175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4559dab5cd05SOleg Bulyzhin 
4560dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4561b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4562b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4563366454f2SOleg Bulyzhin 
45640c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4565366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4566366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4567366454f2SOleg Bulyzhin 
4568366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4569366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
45704c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4571652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4572366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4573366454f2SOleg Bulyzhin 
4574366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4575dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
4576fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
457725e13e68SXin LI 		BGE_UNLOCK(sc);
45788cf7d13dSAttilio Rao 		return (rx_npkts);
457925e13e68SXin LI 	}
4580b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4581fba8b109SMarcel Moolenaar 	if (!if_sendq_empty(ifp))
4582366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
45833f74909aSGleb Smirnoff 
45843f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
45851abcdbd1SAttilio Rao 	return (rx_npkts);
458675719184SGleb Smirnoff }
458775719184SGleb Smirnoff #endif /* DEVICE_POLLING */
458875719184SGleb Smirnoff 
4589dfe0df9aSPyun YongHyeon static int
4590dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4591dfe0df9aSPyun YongHyeon {
4592dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4593dfe0df9aSPyun YongHyeon 
4594dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4595dfe0df9aSPyun YongHyeon 	/*
4596dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4597dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4598dfe0df9aSPyun YongHyeon 	 */
4599dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4600dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4601dfe0df9aSPyun YongHyeon }
4602dfe0df9aSPyun YongHyeon 
4603dfe0df9aSPyun YongHyeon static void
4604dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4605dfe0df9aSPyun YongHyeon {
4606dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4607fba8b109SMarcel Moolenaar 	if_t ifp;
46081108273aSPyun YongHyeon 	uint32_t status, status_tag;
4609dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4610dfe0df9aSPyun YongHyeon 
4611dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4612dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4613dfe0df9aSPyun YongHyeon 
461466151edfSPyun YongHyeon 	BGE_LOCK(sc);
4615fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
461666151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4617dfe0df9aSPyun YongHyeon 		return;
461866151edfSPyun YongHyeon 	}
4619dfe0df9aSPyun YongHyeon 
4620dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4621dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4622dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4623dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4624dfe0df9aSPyun YongHyeon 
46252246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4626dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4627dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4628dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
46291108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
46302246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4631dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4632dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4633dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4634dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
46351108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
46361108273aSPyun YongHyeon 		status_tag = 0;
463766151edfSPyun YongHyeon 
463866151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
463966151edfSPyun YongHyeon 		bge_link_upd(sc);
464066151edfSPyun YongHyeon 
4641dfe0df9aSPyun YongHyeon 	/* Let controller work. */
46421108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4643dfe0df9aSPyun YongHyeon 
4644fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
464566151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4646dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
464766151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4648dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
464966151edfSPyun YongHyeon 		BGE_LOCK(sc);
4650dfe0df9aSPyun YongHyeon 	}
4651fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4652dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4653dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4654fba8b109SMarcel Moolenaar 		if (!if_sendq_empty(ifp))
4655dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4656dfe0df9aSPyun YongHyeon 	}
465766151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4658dfe0df9aSPyun YongHyeon }
4659dfe0df9aSPyun YongHyeon 
466095d67482SBill Paul static void
46613f74909aSGleb Smirnoff bge_intr(void *xsc)
466295d67482SBill Paul {
466395d67482SBill Paul 	struct bge_softc *sc;
4664fba8b109SMarcel Moolenaar 	if_t ifp;
4665dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4666b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
466795d67482SBill Paul 
466895d67482SBill Paul 	sc = xsc;
4669f41ac2beSBill Paul 
46700f9bd73bSSam Leffler 	BGE_LOCK(sc);
46710f9bd73bSSam Leffler 
4672dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4673dab5cd05SOleg Bulyzhin 
467475719184SGleb Smirnoff #ifdef DEVICE_POLLING
4675fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
467675719184SGleb Smirnoff 		BGE_UNLOCK(sc);
467775719184SGleb Smirnoff 		return;
467875719184SGleb Smirnoff 	}
467975719184SGleb Smirnoff #endif
468075719184SGleb Smirnoff 
4681f30cbfc6SScott Long 	/*
4682b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4683b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4684b848e032SBruce Evans 	 * our current organization this just gives complications and
4685b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4686b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4687b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4688b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4689b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4690b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4691b848e032SBruce Evans 	 *
4692b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4693b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4694b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4695b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4696b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4697b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4698b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4699b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4700b848e032SBruce Evans 	 */
470138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4702b848e032SBruce Evans 
4703f584dfd1SPyun YongHyeon 	/*
4704f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4705f584dfd1SPyun YongHyeon 	 */
4706f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4707f584dfd1SPyun YongHyeon 
4708f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4709f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4710f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4711f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4712f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4713f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4714f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4715f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4716f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4717f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4718f584dfd1SPyun YongHyeon 
47191f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
47204c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4721f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4722dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
472395d67482SBill Paul 
4724fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47253f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4726dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
472725e13e68SXin LI 	}
472895d67482SBill Paul 
4729fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47303f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4731b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
473295d67482SBill Paul 	}
473395d67482SBill Paul 
4734fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
4735fba8b109SMarcel Moolenaar 	    !if_sendq_empty(ifp))
47360f9bd73bSSam Leffler 		bge_start_locked(ifp);
47370f9bd73bSSam Leffler 
47380f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
473995d67482SBill Paul }
474095d67482SBill Paul 
474195d67482SBill Paul static void
47428cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
47438cb1383cSDoug Ambrisko {
47448cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
47458cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
47468cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
47478cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
47488cb1383cSDoug Ambrisko 		else {
4749899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4750888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
47513c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4752888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4753941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4754941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
47553fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
47569931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
47579931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
47588cb1383cSDoug Ambrisko 		}
47598cb1383cSDoug Ambrisko 	}
47608cb1383cSDoug Ambrisko }
47618cb1383cSDoug Ambrisko 
47628cb1383cSDoug Ambrisko static void
4763b74e67fbSGleb Smirnoff bge_tick(void *xsc)
47640f9bd73bSSam Leffler {
4765b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
476695d67482SBill Paul 	struct mii_data *mii = NULL;
476795d67482SBill Paul 
47680f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
476995d67482SBill Paul 
47705dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
47715dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
47725dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
47735dda8085SOleg Bulyzhin 		return;
47745dda8085SOleg Bulyzhin 
47757ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
47760434d1b8SBill Paul 		bge_stats_update_regs(sc);
47770434d1b8SBill Paul 	else
477895d67482SBill Paul 		bge_stats_update(sc);
477995d67482SBill Paul 
4780548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4781548c8f1aSPyun YongHyeon 
4782652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
478395d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
478482b67c01SOleg Bulyzhin 		/*
478582b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
478682b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
478782b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
478882b67c01SOleg Bulyzhin 		 */
478982b67c01SOleg Bulyzhin 		if (!sc->bge_link)
479095d67482SBill Paul 			mii_tick(mii);
47917b97099dSOleg Bulyzhin 	} else {
47927b97099dSOleg Bulyzhin 		/*
47937b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
47947b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
47957b97099dSOleg Bulyzhin 		 * and trigger interrupt.
47967b97099dSOleg Bulyzhin 		 */
47977b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
47983f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
479959578ee0SSergey Kandaurov 		if (!(if_getcapenable(sc->bge_ifp) & IFCAP_POLLING))
48007b97099dSOleg Bulyzhin #endif
48017b97099dSOleg Bulyzhin 		{
48027b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
48034f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
48044f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
48057b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
48064f0794ffSBjoern A. Zeeb 		else
48074f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
48087b97099dSOleg Bulyzhin 		}
4809dab5cd05SOleg Bulyzhin 	}
481095d67482SBill Paul 
48118cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4812b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
48138cb1383cSDoug Ambrisko 
4814dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
481595d67482SBill Paul }
481695d67482SBill Paul 
481795d67482SBill Paul static void
48183f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
48190434d1b8SBill Paul {
4820fba8b109SMarcel Moolenaar 	if_t ifp;
48212280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
482229b44b09SPyun YongHyeon 	uint32_t val;
48230434d1b8SBill Paul 
4824fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
48252280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
48260434d1b8SBill Paul 
48272280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
48282280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48292280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
48302280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48312280c16bSPyun YongHyeon 	stats->outXonSent +=
48322280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48332280c16bSPyun YongHyeon 	stats->outXoffSent +=
48342280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48352280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
48362280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48372280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
48382280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
48392280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
48402280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
48412280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
48422280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
48432280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
48442280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
48452280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
48462280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
48472280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
48482280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
48492280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
48502280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
48512280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
48522280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48537e6e2507SJung-uk Kim 
48542280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
48552280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48562280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
48572280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48582280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
48592280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48602280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
48612280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48622280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
48632280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48642280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
48652280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48662280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
48672280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48682280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
48692280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48702280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
48712280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48722280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
48732280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
48742280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
48752280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
48762280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
48772280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
48782280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
48792280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48802280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
48812280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48822280c16bSPyun YongHyeon 
48832280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
48842280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48852280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
48862280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48872280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
48882280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48892280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
48902280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4891f78094a5SPyun YongHyeon 	/*
4892f78094a5SPyun YongHyeon 	 * XXX
4893f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4894f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4895f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4896f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4897f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4898f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4899f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4900f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4901f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4902f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4903f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4904f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4905f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4906f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4907f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4908f78094a5SPyun YongHyeon 	 * silicon bug.
4909f78094a5SPyun YongHyeon 	 */
4910f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4911f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4912f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
49132280c16bSPyun YongHyeon 		stats->InputDiscards +=
49142280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49152280c16bSPyun YongHyeon 	stats->InputErrors +=
49162280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49172280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
49182280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49192280c16bSPyun YongHyeon 
492029b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
492129b44b09SPyun YongHyeon 		/*
492229b44b09SPyun YongHyeon 		 * If controller transmitted more than BGE_NUM_RDMA_CHANNELS
492329b44b09SPyun YongHyeon 		 * frames, it's safe to disable workaround for DMA engine's
492429b44b09SPyun YongHyeon 		 * miscalculation of TXMBUF space.
492529b44b09SPyun YongHyeon 		 */
492629b44b09SPyun YongHyeon 		if (stats->ifHCOutUcastPkts + stats->ifHCOutMulticastPkts +
492729b44b09SPyun YongHyeon 		    stats->ifHCOutBroadcastPkts > BGE_NUM_RDMA_CHANNELS) {
492829b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
492929b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
493029b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5719;
493129b44b09SPyun YongHyeon 			else
493229b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5720;
493329b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
493429b44b09SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_RDMA_BUG;
493529b44b09SPyun YongHyeon 		}
493629b44b09SPyun YongHyeon 	}
49372280c16bSPyun YongHyeon }
49382280c16bSPyun YongHyeon 
49392280c16bSPyun YongHyeon static void
49402280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
49412280c16bSPyun YongHyeon {
49422280c16bSPyun YongHyeon 
49432280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
49442280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
49452280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
49462280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
49472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
49482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
49492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
49502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
49512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
49522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
49532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
49542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
49552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
49562280c16bSPyun YongHyeon 
49572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
49582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
49592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
49602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
49612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
49622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
49632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
49642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
49652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
49662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
49672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
49682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
49692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
49702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
49712280c16bSPyun YongHyeon 
49722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
49732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
49742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
49752280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
49762280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49772280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49782280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49790434d1b8SBill Paul }
49800434d1b8SBill Paul 
49810434d1b8SBill Paul static void
49823f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
498395d67482SBill Paul {
4984fba8b109SMarcel Moolenaar 	if_t ifp;
4985e907febfSPyun YongHyeon 	bus_size_t stats;
49867e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
498795d67482SBill Paul 
4988fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
498995d67482SBill Paul 
4990e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4991e907febfSPyun YongHyeon 
4992e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4993e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
499495d67482SBill Paul 
49958634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
4996df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cnt - sc->bge_tx_collisions);
49976fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
49986fb34dd2SOleg Bulyzhin 
499937ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
5000df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_nobds);
500137ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
500237ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
5003df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_inerrs);
500437ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
50056fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
5006df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_discards);
50076fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
50086fb34dd2SOleg Bulyzhin 
50096fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
5010df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, cnt - sc->bge_tx_discards);
50116fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
501295d67482SBill Paul 
5013e907febfSPyun YongHyeon #undef	READ_STAT
501495d67482SBill Paul }
501595d67482SBill Paul 
501695d67482SBill Paul /*
5017d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
5018d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
5019d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
5020d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
5021d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
5022d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
5023d375e524SGleb Smirnoff  */
5024d375e524SGleb Smirnoff static __inline int
5025d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
5026d375e524SGleb Smirnoff {
5027d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
5028d375e524SGleb Smirnoff 	struct mbuf *last;
5029d375e524SGleb Smirnoff 
5030d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
5031d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
5032d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
5033d375e524SGleb Smirnoff 		last = m;
5034d375e524SGleb Smirnoff 	} else {
5035d375e524SGleb Smirnoff 		/*
5036d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
5037d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
5038d375e524SGleb Smirnoff 		 */
5039d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
5040d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
5041d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
5042d375e524SGleb Smirnoff 			struct mbuf *n;
5043d375e524SGleb Smirnoff 
5044c6499eccSGleb Smirnoff 			MGET(n, M_NOWAIT, MT_DATA);
5045d375e524SGleb Smirnoff 			if (n == NULL)
5046d375e524SGleb Smirnoff 				return (ENOBUFS);
5047d375e524SGleb Smirnoff 			n->m_len = 0;
5048d375e524SGleb Smirnoff 			last->m_next = n;
5049d375e524SGleb Smirnoff 			last = n;
5050d375e524SGleb Smirnoff 		}
5051d375e524SGleb Smirnoff 	}
5052d375e524SGleb Smirnoff 
5053d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
5054d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
5055d375e524SGleb Smirnoff 	last->m_len += padlen;
5056d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
5057d375e524SGleb Smirnoff 
5058d375e524SGleb Smirnoff 	return (0);
5059d375e524SGleb Smirnoff }
5060d375e524SGleb Smirnoff 
5061ca3f1187SPyun YongHyeon static struct mbuf *
5062d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
5063d598b626SPyun YongHyeon {
5064d598b626SPyun YongHyeon 	struct mbuf *n;
5065d598b626SPyun YongHyeon 	int found;
5066d598b626SPyun YongHyeon 
5067d598b626SPyun YongHyeon 	/*
5068d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
5069d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
5070d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
5071d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
5072d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
5073d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
5074d598b626SPyun YongHyeon 	 */
5075d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
5076d598b626SPyun YongHyeon 		if (n->m_len < 8) {
5077d598b626SPyun YongHyeon 			found++;
5078d598b626SPyun YongHyeon 			if (found > 1)
5079d598b626SPyun YongHyeon 				break;
5080d598b626SPyun YongHyeon 			continue;
5081d598b626SPyun YongHyeon 		}
5082d598b626SPyun YongHyeon 		found = 0;
5083d598b626SPyun YongHyeon 	}
5084d598b626SPyun YongHyeon 
5085d598b626SPyun YongHyeon 	if (found > 1) {
5086c6499eccSGleb Smirnoff 		n = m_defrag(m, M_NOWAIT);
5087d598b626SPyun YongHyeon 		if (n == NULL)
5088d598b626SPyun YongHyeon 			m_freem(m);
5089d598b626SPyun YongHyeon 	} else
5090d598b626SPyun YongHyeon 		n = m;
5091d598b626SPyun YongHyeon 	return (n);
5092d598b626SPyun YongHyeon }
5093d598b626SPyun YongHyeon 
5094d598b626SPyun YongHyeon static struct mbuf *
50951108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
50961108273aSPyun YongHyeon     uint16_t *flags)
5097ca3f1187SPyun YongHyeon {
5098ca3f1187SPyun YongHyeon 	struct ip *ip;
5099ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
5100ca3f1187SPyun YongHyeon 	struct mbuf *n;
5101ca3f1187SPyun YongHyeon 	uint16_t hlen;
51025b355c4fSPyun YongHyeon 	uint32_t poff;
5103ca3f1187SPyun YongHyeon 
5104ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5105ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5106c6499eccSGleb Smirnoff 		n = m_dup(m, M_NOWAIT);
5107ca3f1187SPyun YongHyeon 		m_freem(m);
5108ca3f1187SPyun YongHyeon 		if (n == NULL)
5109ca3f1187SPyun YongHyeon 			return (NULL);
5110ca3f1187SPyun YongHyeon 		m = n;
5111ca3f1187SPyun YongHyeon 	}
51125b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5113ca3f1187SPyun YongHyeon 	if (m == NULL)
5114ca3f1187SPyun YongHyeon 		return (NULL);
51155b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
51165b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5117ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5118ca3f1187SPyun YongHyeon 	if (m == NULL)
5119ca3f1187SPyun YongHyeon 		return (NULL);
5120ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
51215b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5122ca3f1187SPyun YongHyeon 	if (m == NULL)
5123ca3f1187SPyun YongHyeon 		return (NULL);
5124ca3f1187SPyun YongHyeon 	/*
5125ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5126ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5127ca3f1187SPyun YongHyeon 	 */
5128ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
512996486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5130ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5131ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5132ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
513396486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5134ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5135ca3f1187SPyun YongHyeon 	/*
5136ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5137ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5138ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
51391108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5140ca3f1187SPyun YongHyeon 	 */
51411108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5142ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
51431108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
51441108273aSPyun YongHyeon 		/*
51451108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
51461108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
51471108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
51481108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
51491108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
51501108273aSPyun YongHyeon 		 * frames are supported.
51511108273aSPyun YongHyeon 		 */
51521108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
51531108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
51541108273aSPyun YongHyeon 	} else {
51551108273aSPyun YongHyeon 		/*
51561108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
51571108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
51581108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
51591108273aSPyun YongHyeon 		 * supported.
51601108273aSPyun YongHyeon 		 */
5161ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
51621108273aSPyun YongHyeon 	}
5163ca3f1187SPyun YongHyeon 	return (m);
5164ca3f1187SPyun YongHyeon }
5165ca3f1187SPyun YongHyeon 
5166d375e524SGleb Smirnoff /*
516795d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
516895d67482SBill Paul  * pointers to descriptors.
516995d67482SBill Paul  */
517095d67482SBill Paul static int
5171676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
517295d67482SBill Paul {
51737e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5174f41ac2beSBill Paul 	bus_dmamap_t		map;
5175676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5176676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
51777e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5178ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
51797e27542aSGleb Smirnoff 	int			nsegs, i, error;
518095d67482SBill Paul 
51816909dc43SGleb Smirnoff 	csum_flags = 0;
5182ca3f1187SPyun YongHyeon 	mss = 0;
5183ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5184d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5185d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5186d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5187d598b626SPyun YongHyeon 		if (*m_head == NULL)
5188d598b626SPyun YongHyeon 			return (ENOBUFS);
5189d598b626SPyun YongHyeon 		m = *m_head;
5190d598b626SPyun YongHyeon 	}
5191ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
51921108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5193ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5194ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5195ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5196ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
519735f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
51986909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
51996909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
52006909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
52016909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
52026909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
52036909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
52046909dc43SGleb Smirnoff 				m_freem(m);
52056909dc43SGleb Smirnoff 				*m_head = NULL;
52066909dc43SGleb Smirnoff 				return (error);
52076909dc43SGleb Smirnoff 			}
52086909dc43SGleb Smirnoff 		}
52096909dc43SGleb Smirnoff 	}
52106909dc43SGleb Smirnoff 
52111108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
52121108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
52131108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
52141108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
52151108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5216beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5217d94f2b85SPyun YongHyeon 			/*
5218d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5219d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5220d94f2b85SPyun YongHyeon 			 * DMA read operation.
5221d94f2b85SPyun YongHyeon 			 */
5222beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5223c6499eccSGleb Smirnoff 				m = m_defrag(m, M_NOWAIT);
5224d94f2b85SPyun YongHyeon 			else
5225c6499eccSGleb Smirnoff 				m = m_collapse(m, M_NOWAIT,
52261108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5227261f04d6SPyun YongHyeon 			if (m == NULL)
5228261f04d6SPyun YongHyeon 				m = *m_head;
5229d94f2b85SPyun YongHyeon 			*m_head = m;
5230d94f2b85SPyun YongHyeon 		}
52311108273aSPyun YongHyeon 	}
5232d94f2b85SPyun YongHyeon 
52337e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
52340ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5235676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
52367e27542aSGleb Smirnoff 	if (error == EFBIG) {
5237c6499eccSGleb Smirnoff 		m = m_collapse(m, M_NOWAIT, BGE_NSEG_NEW);
5238676ad2c9SGleb Smirnoff 		if (m == NULL) {
5239676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5240676ad2c9SGleb Smirnoff 			*m_head = NULL;
52417e27542aSGleb Smirnoff 			return (ENOBUFS);
52427e27542aSGleb Smirnoff 		}
5243676ad2c9SGleb Smirnoff 		*m_head = m;
52440ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
52450ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5246676ad2c9SGleb Smirnoff 		if (error) {
5247676ad2c9SGleb Smirnoff 			m_freem(m);
5248676ad2c9SGleb Smirnoff 			*m_head = NULL;
52497e27542aSGleb Smirnoff 			return (error);
52507e27542aSGleb Smirnoff 		}
5251676ad2c9SGleb Smirnoff 	} else if (error != 0)
5252676ad2c9SGleb Smirnoff 		return (error);
52537e27542aSGleb Smirnoff 
5254167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5255167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
52560ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
525795d67482SBill Paul 		return (ENOBUFS);
52587e27542aSGleb Smirnoff 	}
52597e27542aSGleb Smirnoff 
52600ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5261e65bed95SPyun YongHyeon 
5262ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5263ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5264ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5265ca3f1187SPyun YongHyeon 	}
5266b77d3a3bSPyun YongHyeon 
5267b77d3a3bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762 &&
5268b77d3a3bSPyun YongHyeon 	    (m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
5269b77d3a3bSPyun YongHyeon 		/*
5270b77d3a3bSPyun YongHyeon 		 * 5725 family of devices corrupts TSO packets when TSO DMA
5271b77d3a3bSPyun YongHyeon 		 * buffers cross into regions which are within MSS bytes of
5272b77d3a3bSPyun YongHyeon 		 * a 4GB boundary.  If we encounter the condition, drop the
5273b77d3a3bSPyun YongHyeon 		 * packet.
5274b77d3a3bSPyun YongHyeon 		 */
5275b77d3a3bSPyun YongHyeon 		for (i = 0; ; i++) {
5276b77d3a3bSPyun YongHyeon 			d = &sc->bge_ldata.bge_tx_ring[idx];
5277b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
5278b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
5279b77d3a3bSPyun YongHyeon 			d->bge_len = segs[i].ds_len;
5280b77d3a3bSPyun YongHyeon 			if (d->bge_addr.bge_addr_lo + segs[i].ds_len + mss <
5281b77d3a3bSPyun YongHyeon 			    d->bge_addr.bge_addr_lo)
5282b77d3a3bSPyun YongHyeon 				break;
5283b77d3a3bSPyun YongHyeon 			d->bge_flags = csum_flags;
5284b77d3a3bSPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5285b77d3a3bSPyun YongHyeon 			d->bge_mss = mss;
5286b77d3a3bSPyun YongHyeon 			if (i == nsegs - 1)
5287b77d3a3bSPyun YongHyeon 				break;
5288b77d3a3bSPyun YongHyeon 			BGE_INC(idx, BGE_TX_RING_CNT);
5289b77d3a3bSPyun YongHyeon 		}
5290b77d3a3bSPyun YongHyeon 		if (i != nsegs - 1) {
5291b77d3a3bSPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map,
5292b77d3a3bSPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
5293b77d3a3bSPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
5294b77d3a3bSPyun YongHyeon 			m_freem(*m_head);
5295b77d3a3bSPyun YongHyeon 			*m_head = NULL;
5296b77d3a3bSPyun YongHyeon 			return (EIO);
5297b77d3a3bSPyun YongHyeon 		}
5298b77d3a3bSPyun YongHyeon 	} else {
52997e27542aSGleb Smirnoff 		for (i = 0; ; i++) {
53007e27542aSGleb Smirnoff 			d = &sc->bge_ldata.bge_tx_ring[idx];
53017e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
53027e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
53037e27542aSGleb Smirnoff 			d->bge_len = segs[i].ds_len;
53047e27542aSGleb Smirnoff 			d->bge_flags = csum_flags;
5305ca3f1187SPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5306ca3f1187SPyun YongHyeon 			d->bge_mss = mss;
53077e27542aSGleb Smirnoff 			if (i == nsegs - 1)
53087e27542aSGleb Smirnoff 				break;
53097e27542aSGleb Smirnoff 			BGE_INC(idx, BGE_TX_RING_CNT);
53107e27542aSGleb Smirnoff 		}
5311b77d3a3bSPyun YongHyeon 	}
53127e27542aSGleb Smirnoff 
53137e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
53147e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5315676ad2c9SGleb Smirnoff 
5316f41ac2beSBill Paul 	/*
5317f41ac2beSBill Paul 	 * Insure that the map for this transmission
5318f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5319f41ac2beSBill Paul 	 * in this chain.
5320f41ac2beSBill Paul 	 */
53217e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
53227e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5323676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
53247e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
532595d67482SBill Paul 
53267e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
53277e27542aSGleb Smirnoff 	*txidx = idx;
532895d67482SBill Paul 
532995d67482SBill Paul 	return (0);
533095d67482SBill Paul }
533195d67482SBill Paul 
533295d67482SBill Paul /*
533395d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
533495d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
533595d67482SBill Paul  */
533695d67482SBill Paul static void
5337fba8b109SMarcel Moolenaar bge_start_locked(if_t ifp)
533895d67482SBill Paul {
533995d67482SBill Paul 	struct bge_softc *sc;
5340167fdb62SPyun YongHyeon 	struct mbuf *m_head;
534114bbd30fSGleb Smirnoff 	uint32_t prodidx;
5342167fdb62SPyun YongHyeon 	int count;
534395d67482SBill Paul 
5344fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
5345167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
534695d67482SBill Paul 
5347167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5348fba8b109SMarcel Moolenaar 	    (if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5349167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
535095d67482SBill Paul 		return;
535195d67482SBill Paul 
535214bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
535395d67482SBill Paul 
5354fba8b109SMarcel Moolenaar 	for (count = 0; !if_sendq_empty(ifp);) {
5355167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5356fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5357167fdb62SPyun YongHyeon 			break;
5358167fdb62SPyun YongHyeon 		}
5359fba8b109SMarcel Moolenaar 		m_head = if_dequeue(ifp);
536095d67482SBill Paul 		if (m_head == NULL)
536195d67482SBill Paul 			break;
536295d67482SBill Paul 
536395d67482SBill Paul 		/*
536495d67482SBill Paul 		 * Pack the data into the transmit ring. If we
536595d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
536695d67482SBill Paul 		 * for the NIC to drain the ring.
536795d67482SBill Paul 		 */
5368676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5369676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5370676ad2c9SGleb Smirnoff 				break;
5371fba8b109SMarcel Moolenaar 			if_sendq_prepend(ifp, m_head);
5372fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
537395d67482SBill Paul 			break;
537495d67482SBill Paul 		}
5375303a718cSDag-Erling Smørgrav 		++count;
537695d67482SBill Paul 
537795d67482SBill Paul 		/*
537895d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
537995d67482SBill Paul 		 * to him.
538095d67482SBill Paul 		 */
5381fba8b109SMarcel Moolenaar 		if_bpfmtap(ifp, m_head);
538295d67482SBill Paul 	}
538395d67482SBill Paul 
5384167fdb62SPyun YongHyeon 	if (count > 0) {
5385aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
53865c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
53873f74909aSGleb Smirnoff 		/* Transmit. */
538838cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
53893927098fSPaul Saab 		/* 5700 b2 errata */
5390e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
539138cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
539295d67482SBill Paul 
539314bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
539414bbd30fSGleb Smirnoff 
539595d67482SBill Paul 		/*
539695d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
539795d67482SBill Paul 		 */
5398b584d2b3SPyun YongHyeon 		sc->bge_timer = BGE_TX_TIMEOUT;
539995d67482SBill Paul 	}
5400167fdb62SPyun YongHyeon }
540195d67482SBill Paul 
54020f9bd73bSSam Leffler /*
54030f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
54040f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
54050f9bd73bSSam Leffler  */
540695d67482SBill Paul static void
5407fba8b109SMarcel Moolenaar bge_start(if_t ifp)
540895d67482SBill Paul {
54090f9bd73bSSam Leffler 	struct bge_softc *sc;
54100f9bd73bSSam Leffler 
5411fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
54120f9bd73bSSam Leffler 	BGE_LOCK(sc);
54130f9bd73bSSam Leffler 	bge_start_locked(ifp);
54140f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
54150f9bd73bSSam Leffler }
54160f9bd73bSSam Leffler 
54170f9bd73bSSam Leffler static void
54183f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
54190f9bd73bSSam Leffler {
5420fba8b109SMarcel Moolenaar 	if_t ifp;
54213f74909aSGleb Smirnoff 	uint16_t *m;
5422f6a65488SPyun YongHyeon 	uint32_t mode;
542395d67482SBill Paul 
54240f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
542595d67482SBill Paul 
5426fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
542795d67482SBill Paul 
5428fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
542995d67482SBill Paul 		return;
543095d67482SBill Paul 
543195d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
543295d67482SBill Paul 	bge_stop(sc);
54338cb1383cSDoug Ambrisko 
54348cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
54358cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
543695d67482SBill Paul 	bge_reset(sc);
54378cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
54388cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
54398cb1383cSDoug Ambrisko 
544095d67482SBill Paul 	bge_chipinit(sc);
544195d67482SBill Paul 
544295d67482SBill Paul 	/*
544395d67482SBill Paul 	 * Init the various state machines, ring
544495d67482SBill Paul 	 * control blocks and firmware.
544595d67482SBill Paul 	 */
544695d67482SBill Paul 	if (bge_blockinit(sc)) {
5447fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
544895d67482SBill Paul 		return;
544995d67482SBill Paul 	}
545095d67482SBill Paul 
5451fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
545295d67482SBill Paul 
545395d67482SBill Paul 	/* Specify MTU. */
5454fba8b109SMarcel Moolenaar 	CSR_WRITE_4(sc, BGE_RX_MTU, if_getmtu(ifp) +
5455cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5456fba8b109SMarcel Moolenaar 	    (if_getcapenable(ifp) & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
545795d67482SBill Paul 
545895d67482SBill Paul 	/* Load our MAC address. */
54593f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
546095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
546195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
546295d67482SBill Paul 
54633e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
54643e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
546595d67482SBill Paul 
546695d67482SBill Paul 	/* Program multicast filter. */
546795d67482SBill Paul 	bge_setmulti(sc);
546895d67482SBill Paul 
5469cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5470cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5471cb2eacc7SYaroslav Tykhiy 
547235f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
547335f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
547435f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
547535f945cdSPyun YongHyeon 	else
547635f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
5477fba8b109SMarcel Moolenaar 	if (if_getcapabilities(ifp) & IFCAP_TXCSUM &&
5478fba8b109SMarcel Moolenaar 	    if_getcapenable(ifp) & IFCAP_TXCSUM) {
5479fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, 0, (BGE_CSUM_FEATURES | CSUM_UDP));
5480fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, sc->bge_csum_features, 0);
548135f945cdSPyun YongHyeon 	}
548235f945cdSPyun YongHyeon 
548395d67482SBill Paul 	/* Init RX ring. */
54843ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
54853ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
54863ee5d7daSPyun YongHyeon 		bge_stop(sc);
54873ee5d7daSPyun YongHyeon 		return;
54883ee5d7daSPyun YongHyeon 	}
548995d67482SBill Paul 
54900434d1b8SBill Paul 	/*
54910434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
54920434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
54930434d1b8SBill Paul 	 * entry of the ring.
54940434d1b8SBill Paul 	 */
54950434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
54963f74909aSGleb Smirnoff 		uint32_t		v, i;
54970434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
54980434d1b8SBill Paul 			DELAY(20);
54990434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
55000434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
55010434d1b8SBill Paul 				break;
55020434d1b8SBill Paul 		}
55030434d1b8SBill Paul 		if (i == 10)
5504fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5505fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
55060434d1b8SBill Paul 	}
55070434d1b8SBill Paul 
550895d67482SBill Paul 	/* Init jumbo RX ring. */
5509f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5510fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
5511fba8b109SMarcel Moolenaar      	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN)) {
55123ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5513333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5514b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
55153ee5d7daSPyun YongHyeon 			bge_stop(sc);
55163ee5d7daSPyun YongHyeon 			return;
55173ee5d7daSPyun YongHyeon 		}
55183ee5d7daSPyun YongHyeon 	}
551995d67482SBill Paul 
55203f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
552195d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
552295d67482SBill Paul 
55237e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
55247e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
55257e6e2507SJung-uk Kim 
552695d67482SBill Paul 	/* Init TX ring. */
552795d67482SBill Paul 	bge_init_tx_ring(sc);
552895d67482SBill Paul 
5529f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5530f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5531f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5532f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
55332927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
55342927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
553550515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
553650515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
553750515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
553850515680SPyun YongHyeon 	}
55393f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5540f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5541a6e66cd2SPyun YongHyeon 	DELAY(100);
554295d67482SBill Paul 
55433f74909aSGleb Smirnoff 	/* Turn on receiver. */
5544548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5545548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5546548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
554769b1f509SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
554869b1f509SPyun YongHyeon 		mode |= BGE_RXMODE_IPV4_FRAG_FIX;
5549548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5550a6e66cd2SPyun YongHyeon 	DELAY(10);
555195d67482SBill Paul 
5552dedcdf57SPyun YongHyeon 	/*
5553dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5554dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5555dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5556dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5557dedcdf57SPyun YongHyeon 	 */
55583fc5fbfbSPyun YongHyeon 	if (BGE_IS_57765_PLUS(sc))
5559b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5560b4a256acSPyun YongHyeon 	else
5561dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5562dedcdf57SPyun YongHyeon 
55632280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
55642280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
55652280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
55662280c16bSPyun YongHyeon 
556795d67482SBill Paul 	/* Tell firmware we're alive. */
556895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
556995d67482SBill Paul 
557075719184SGleb Smirnoff #ifdef DEVICE_POLLING
557175719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
5572fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
557375719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
557475719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
557538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
557675719184SGleb Smirnoff 	} else
557775719184SGleb Smirnoff #endif
557875719184SGleb Smirnoff 
557995d67482SBill Paul 	/* Enable host interrupts. */
558075719184SGleb Smirnoff 	{
558195d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
558295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
558338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
558475719184SGleb Smirnoff 	}
558595d67482SBill Paul 
5586fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
5587fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
558895d67482SBill Paul 
5589e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5590e4146b95SPyun YongHyeon 
55910f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
55920f9bd73bSSam Leffler }
55930f9bd73bSSam Leffler 
55940f9bd73bSSam Leffler static void
55953f74909aSGleb Smirnoff bge_init(void *xsc)
55960f9bd73bSSam Leffler {
55970f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
55980f9bd73bSSam Leffler 
55990f9bd73bSSam Leffler 	BGE_LOCK(sc);
56000f9bd73bSSam Leffler 	bge_init_locked(sc);
56010f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
560295d67482SBill Paul }
560395d67482SBill Paul 
560495d67482SBill Paul /*
560595d67482SBill Paul  * Set media options.
560695d67482SBill Paul  */
560795d67482SBill Paul static int
5608fba8b109SMarcel Moolenaar bge_ifmedia_upd(if_t ifp)
560995d67482SBill Paul {
5610fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
561167d5e043SOleg Bulyzhin 	int res;
561267d5e043SOleg Bulyzhin 
561367d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
561467d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
561567d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
561667d5e043SOleg Bulyzhin 
561767d5e043SOleg Bulyzhin 	return (res);
561867d5e043SOleg Bulyzhin }
561967d5e043SOleg Bulyzhin 
562067d5e043SOleg Bulyzhin static int
5621fba8b109SMarcel Moolenaar bge_ifmedia_upd_locked(if_t ifp)
562267d5e043SOleg Bulyzhin {
5623fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
562495d67482SBill Paul 	struct mii_data *mii;
56254f09c4c7SMarius Strobl 	struct mii_softc *miisc;
562695d67482SBill Paul 	struct ifmedia *ifm;
562795d67482SBill Paul 
562867d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
562967d5e043SOleg Bulyzhin 
563095d67482SBill Paul 	ifm = &sc->bge_ifmedia;
563195d67482SBill Paul 
563295d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5633652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
563495d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
563595d67482SBill Paul 			return (EINVAL);
563695d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
563795d67482SBill Paul 		case IFM_AUTO:
5638ff50922bSDoug White 			/*
5639ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5640ff50922bSDoug White 			 * mechanism for programming the autoneg
5641ff50922bSDoug White 			 * advertisement registers in TBI mode.
5642ff50922bSDoug White 			 */
56430f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5644ff50922bSDoug White 				uint32_t sgdig;
56450f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
56460f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5647ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5648ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5649ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5650ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5651ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5652ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5653ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5654ff50922bSDoug White 					DELAY(5);
5655ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5656ff50922bSDoug White 				}
56570f89fde2SJung-uk Kim 			}
565895d67482SBill Paul 			break;
565995d67482SBill Paul 		case IFM_1000_SX:
566095d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
566195d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
566295d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
566395d67482SBill Paul 			} else {
566495d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
566595d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
566695d67482SBill Paul 			}
56679b80ffe7SPyun YongHyeon 			DELAY(40);
566895d67482SBill Paul 			break;
566995d67482SBill Paul 		default:
567095d67482SBill Paul 			return (EINVAL);
567195d67482SBill Paul 		}
567295d67482SBill Paul 		return (0);
567395d67482SBill Paul 	}
567495d67482SBill Paul 
56751493e883SOleg Bulyzhin 	sc->bge_link_evt++;
567695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
56774f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
56783fcb7a53SMarius Strobl 		PHY_RESET(miisc);
567995d67482SBill Paul 	mii_mediachg(mii);
568095d67482SBill Paul 
5681902827f6SBjoern A. Zeeb 	/*
5682902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5683902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5684902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5685902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5686902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5687902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5688902827f6SBjoern A. Zeeb 	 * get an RX intr.
5689902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5690902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5691902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5692902827f6SBjoern A. Zeeb 	 */
56934f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
56944f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5695902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
56964f0794ffSBjoern A. Zeeb 	else
569763ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5698902827f6SBjoern A. Zeeb 
569995d67482SBill Paul 	return (0);
570095d67482SBill Paul }
570195d67482SBill Paul 
570295d67482SBill Paul /*
570395d67482SBill Paul  * Report current media status.
570495d67482SBill Paul  */
570595d67482SBill Paul static void
5706fba8b109SMarcel Moolenaar bge_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
570795d67482SBill Paul {
5708fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
570995d67482SBill Paul 	struct mii_data *mii;
571095d67482SBill Paul 
571167d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
571295d67482SBill Paul 
5713fba8b109SMarcel Moolenaar 	if ((if_getflags(ifp) & IFF_UP) == 0) {
5714b9d2edd7SPyun YongHyeon 		BGE_UNLOCK(sc);
5715b9d2edd7SPyun YongHyeon 		return;
5716b9d2edd7SPyun YongHyeon 	}
5717652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
571895d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
571995d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
572095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
572195d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
572295d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
57234c0da0ffSGleb Smirnoff 		else {
57244c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
572567d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
57264c0da0ffSGleb Smirnoff 			return;
57274c0da0ffSGleb Smirnoff 		}
572895d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
572995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
573095d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
573195d67482SBill Paul 		else
573295d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
573367d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
573495d67482SBill Paul 		return;
573595d67482SBill Paul 	}
573695d67482SBill Paul 
573795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
573895d67482SBill Paul 	mii_pollstat(mii);
573995d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
574095d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
574167d5e043SOleg Bulyzhin 
574267d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
574395d67482SBill Paul }
574495d67482SBill Paul 
574595d67482SBill Paul static int
5746fba8b109SMarcel Moolenaar bge_ioctl(if_t ifp, u_long command, caddr_t data)
574795d67482SBill Paul {
5748fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
574995d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
575095d67482SBill Paul 	struct mii_data *mii;
5751f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
575295d67482SBill Paul 
575395d67482SBill Paul 	switch (command) {
575495d67482SBill Paul 	case SIOCSIFMTU:
5755f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5756f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
57574c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5758f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
575995d67482SBill Paul 				error = EINVAL;
5760f5459d4cSPyun YongHyeon 				break;
5761f5459d4cSPyun YongHyeon 			}
5762f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5763f5459d4cSPyun YongHyeon 			error = EINVAL;
5764f5459d4cSPyun YongHyeon 			break;
5765f5459d4cSPyun YongHyeon 		}
5766f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5767fba8b109SMarcel Moolenaar 		if (if_getmtu(ifp) != ifr->ifr_mtu) {
5768fba8b109SMarcel Moolenaar 			if_setmtu(ifp, ifr->ifr_mtu);
5769fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5770fba8b109SMarcel Moolenaar 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
57713a429c8fSPyun YongHyeon 				bge_init_locked(sc);
577295d67482SBill Paul 			}
57733a429c8fSPyun YongHyeon 		}
57743a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
577595d67482SBill Paul 		break;
577695d67482SBill Paul 	case SIOCSIFFLAGS:
57770f9bd73bSSam Leffler 		BGE_LOCK(sc);
5778fba8b109SMarcel Moolenaar 		if (if_getflags(ifp) & IFF_UP) {
577995d67482SBill Paul 			/*
578095d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
578195d67482SBill Paul 			 * then just use the 'set promisc mode' command
578295d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
578395d67482SBill Paul 			 * a full re-init means reloading the firmware and
578495d67482SBill Paul 			 * waiting for it to start up, which may take a
5785d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
578695d67482SBill Paul 			 */
5787fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5788fba8b109SMarcel Moolenaar 				flags = if_getflags(ifp) ^ sc->bge_if_flags;
57893e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
57903e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5791f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5792d183af7fSRuslan Ermilov 					bge_setmulti(sc);
579395d67482SBill Paul 			} else
57940f9bd73bSSam Leffler 				bge_init_locked(sc);
579595d67482SBill Paul 		} else {
5796fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
579795d67482SBill Paul 				bge_stop(sc);
579895d67482SBill Paul 			}
579995d67482SBill Paul 		}
5800fba8b109SMarcel Moolenaar 		sc->bge_if_flags = if_getflags(ifp);
58010f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
580295d67482SBill Paul 		error = 0;
580395d67482SBill Paul 		break;
580495d67482SBill Paul 	case SIOCADDMULTI:
580595d67482SBill Paul 	case SIOCDELMULTI:
5806fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
58070f9bd73bSSam Leffler 			BGE_LOCK(sc);
580895d67482SBill Paul 			bge_setmulti(sc);
58090f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
581095d67482SBill Paul 			error = 0;
581195d67482SBill Paul 		}
581295d67482SBill Paul 		break;
581395d67482SBill Paul 	case SIOCSIFMEDIA:
581495d67482SBill Paul 	case SIOCGIFMEDIA:
5815652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
581695d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
581795d67482SBill Paul 			    &sc->bge_ifmedia, command);
581895d67482SBill Paul 		} else {
581995d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
582095d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
582195d67482SBill Paul 			    &mii->mii_media, command);
582295d67482SBill Paul 		}
582395d67482SBill Paul 		break;
582495d67482SBill Paul 	case SIOCSIFCAP:
5825fba8b109SMarcel Moolenaar 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
582675719184SGleb Smirnoff #ifdef DEVICE_POLLING
582775719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
582875719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
5829bd071d4dSGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
583075719184SGleb Smirnoff 				if (error)
583175719184SGleb Smirnoff 					return (error);
583275719184SGleb Smirnoff 				BGE_LOCK(sc);
583375719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
583475719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
583538cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
5836fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, IFCAP_POLLING, 0);
583775719184SGleb Smirnoff 				BGE_UNLOCK(sc);
583875719184SGleb Smirnoff 			} else {
583975719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
584075719184SGleb Smirnoff 				/* Enable interrupt even in error case */
584175719184SGleb Smirnoff 				BGE_LOCK(sc);
584275719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
584375719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
584438cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
5845fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_POLLING);
584675719184SGleb Smirnoff 				BGE_UNLOCK(sc);
584775719184SGleb Smirnoff 			}
584875719184SGleb Smirnoff 		}
584975719184SGleb Smirnoff #endif
5850d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5851fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
5852fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TXCSUM);
5853fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
5854fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp,
5855fba8b109SMarcel Moolenaar 				    sc->bge_csum_features, 0);
585695d67482SBill Paul 			else
5857fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0,
5858fba8b109SMarcel Moolenaar 				    sc->bge_csum_features);
585995d67482SBill Paul 		}
5860cb2eacc7SYaroslav Tykhiy 
5861d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5862fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0)
5863fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_RXCSUM);
5864d8b57f98SPyun YongHyeon 
5865ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5866fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
5867fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TSO4);
5868fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0)
5869fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, CSUM_TSO, 0);
5870ca3f1187SPyun YongHyeon 			else
5871fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0, CSUM_TSO);
5872ca3f1187SPyun YongHyeon 		}
5873ca3f1187SPyun YongHyeon 
5874cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5875fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
5876fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5877cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5878cb2eacc7SYaroslav Tykhiy 		}
5879cb2eacc7SYaroslav Tykhiy 
588004bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
5881fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
5882fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
588304bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
5884fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
5885fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
5886fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
5887fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO);
5888cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5889cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5890cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
589104bde852SPyun YongHyeon 		}
5892cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5893fba8b109SMarcel Moolenaar 		if_vlancap(ifp);
5894cb2eacc7SYaroslav Tykhiy #endif
589595d67482SBill Paul 		break;
589695d67482SBill Paul 	default:
5897673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
589895d67482SBill Paul 		break;
589995d67482SBill Paul 	}
590095d67482SBill Paul 
590195d67482SBill Paul 	return (error);
590295d67482SBill Paul }
590395d67482SBill Paul 
590495d67482SBill Paul static void
5905b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
590695d67482SBill Paul {
5907fba8b109SMarcel Moolenaar 	if_t ifp;
5908b584d2b3SPyun YongHyeon 	uint32_t status;
590995d67482SBill Paul 
5910b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5911b74e67fbSGleb Smirnoff 
5912b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5913b74e67fbSGleb Smirnoff 		return;
5914b74e67fbSGleb Smirnoff 
5915b584d2b3SPyun YongHyeon 	/* If pause frames are active then don't reset the hardware. */
5916b584d2b3SPyun YongHyeon 	if ((CSR_READ_4(sc, BGE_RX_MODE) & BGE_RXMODE_FLOWCTL_ENABLE) != 0) {
5917b584d2b3SPyun YongHyeon 		status = CSR_READ_4(sc, BGE_RX_STS);
5918b584d2b3SPyun YongHyeon 		if ((status & BGE_RXSTAT_REMOTE_XOFFED) != 0) {
5919b584d2b3SPyun YongHyeon 			/*
5920b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5921b584d2b3SPyun YongHyeon 			 * the condition to clear.
5922b584d2b3SPyun YongHyeon 			 */
5923b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5924b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5925b584d2b3SPyun YongHyeon 			return;
5926b584d2b3SPyun YongHyeon 		} else if ((status & BGE_RXSTAT_RCVD_XOFF) != 0 &&
5927b584d2b3SPyun YongHyeon 		    (status & BGE_RXSTAT_RCVD_XON) != 0) {
5928b584d2b3SPyun YongHyeon 			/*
5929b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5930b584d2b3SPyun YongHyeon 			 * the condition to clear.
5931b584d2b3SPyun YongHyeon 			 */
5932b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5933b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5934b584d2b3SPyun YongHyeon 			return;
5935b584d2b3SPyun YongHyeon 		}
5936b584d2b3SPyun YongHyeon 		/*
5937b584d2b3SPyun YongHyeon 		 * Any other condition is unexpected and the controller
5938b584d2b3SPyun YongHyeon 		 * should be reset.
5939b584d2b3SPyun YongHyeon 		 */
5940b584d2b3SPyun YongHyeon 	}
5941b584d2b3SPyun YongHyeon 
5942b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
594395d67482SBill Paul 
5944fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
594595d67482SBill Paul 
5946fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5947426742bfSGleb Smirnoff 	bge_init_locked(sc);
594895d67482SBill Paul 
5949df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
595095d67482SBill Paul }
595195d67482SBill Paul 
59525a147ba6SPyun YongHyeon static void
59535a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
59545a147ba6SPyun YongHyeon {
59555a147ba6SPyun YongHyeon 	int i;
59565a147ba6SPyun YongHyeon 
59575a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
59585a147ba6SPyun YongHyeon 
59595a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
59605a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
59615a147ba6SPyun YongHyeon 			return;
59625a147ba6SPyun YongHyeon 		DELAY(100);
59635a147ba6SPyun YongHyeon         }
59645a147ba6SPyun YongHyeon }
59655a147ba6SPyun YongHyeon 
596695d67482SBill Paul /*
596795d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
596895d67482SBill Paul  * RX and TX lists.
596995d67482SBill Paul  */
597095d67482SBill Paul static void
59713f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
597295d67482SBill Paul {
5973fba8b109SMarcel Moolenaar 	if_t ifp;
597495d67482SBill Paul 
59750f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
59760f9bd73bSSam Leffler 
5977fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
597895d67482SBill Paul 
59790f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
598095d67482SBill Paul 
598144b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
598244b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
598344b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
598444b63691SBjoern A. Zeeb 
598544b63691SBjoern A. Zeeb 	/*
598644b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
598744b63691SBjoern A. Zeeb 	 */
598844b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
5989548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
599044b63691SBjoern A. Zeeb 
599195d67482SBill Paul 	/*
59923f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
599395d67482SBill Paul 	 */
59945a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
59955a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
59965a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
59975a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
59985a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
59995a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
60005a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
60015a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
600295d67482SBill Paul 
600395d67482SBill Paul 	/*
60043f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
600595d67482SBill Paul 	 */
60065a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
60075a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
60085a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
60095a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
60105a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
60115a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60125a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
60135a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
601495d67482SBill Paul 
601595d67482SBill Paul 	/*
601695d67482SBill Paul 	 * Shut down all of the memory managers and related
601795d67482SBill Paul 	 * state machines.
601895d67482SBill Paul 	 */
60195a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
60205a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
60215a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60225a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
60235a147ba6SPyun YongHyeon 
60240c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
602595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
60267ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
602795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
602895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
60290434d1b8SBill Paul 	}
60302280c16bSPyun YongHyeon 	/* Update MAC statistics. */
60312280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
60322280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
603395d67482SBill Paul 
60348cb1383cSDoug Ambrisko 	bge_reset(sc);
6035548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
6036548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
60378cb1383cSDoug Ambrisko 
60388cb1383cSDoug Ambrisko 	/*
60398cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
60408cb1383cSDoug Ambrisko 	 */
60418cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
60428cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
60438cb1383cSDoug Ambrisko 	else
604495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
604595d67482SBill Paul 
604695d67482SBill Paul 	/* Free the RX lists. */
604795d67482SBill Paul 	bge_free_rx_ring_std(sc);
604895d67482SBill Paul 
604995d67482SBill Paul 	/* Free jumbo RX list. */
60504c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
605195d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
605295d67482SBill Paul 
605395d67482SBill Paul 	/* Free TX buffers. */
605495d67482SBill Paul 	bge_free_tx_ring(sc);
605595d67482SBill Paul 
605695d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
605795d67482SBill Paul 
60585dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
60591493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
60601493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
60611493e883SOleg Bulyzhin 	sc->bge_link = 0;
606295d67482SBill Paul 
6063fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
606495d67482SBill Paul }
606595d67482SBill Paul 
606695d67482SBill Paul /*
606795d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
606895d67482SBill Paul  * get confused by errant DMAs when rebooting.
606995d67482SBill Paul  */
6070b6c974e8SWarner Losh static int
60713f74909aSGleb Smirnoff bge_shutdown(device_t dev)
607295d67482SBill Paul {
607395d67482SBill Paul 	struct bge_softc *sc;
607495d67482SBill Paul 
607595d67482SBill Paul 	sc = device_get_softc(dev);
60760f9bd73bSSam Leffler 	BGE_LOCK(sc);
607795d67482SBill Paul 	bge_stop(sc);
60780f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
6079b6c974e8SWarner Losh 
6080b6c974e8SWarner Losh 	return (0);
608195d67482SBill Paul }
608214afefa3SPawel Jakub Dawidek 
608314afefa3SPawel Jakub Dawidek static int
608414afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
608514afefa3SPawel Jakub Dawidek {
608614afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
608714afefa3SPawel Jakub Dawidek 
608814afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
608914afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
609014afefa3SPawel Jakub Dawidek 	bge_stop(sc);
609114afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
609214afefa3SPawel Jakub Dawidek 
609314afefa3SPawel Jakub Dawidek 	return (0);
609414afefa3SPawel Jakub Dawidek }
609514afefa3SPawel Jakub Dawidek 
609614afefa3SPawel Jakub Dawidek static int
609714afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
609814afefa3SPawel Jakub Dawidek {
609914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
6100fba8b109SMarcel Moolenaar 	if_t ifp;
610114afefa3SPawel Jakub Dawidek 
610214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
610314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
610414afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
6105fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_UP) {
610614afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
6107fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
610814afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
610914afefa3SPawel Jakub Dawidek 	}
611014afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
611114afefa3SPawel Jakub Dawidek 
611214afefa3SPawel Jakub Dawidek 	return (0);
611314afefa3SPawel Jakub Dawidek }
6114dab5cd05SOleg Bulyzhin 
6115dab5cd05SOleg Bulyzhin static void
61163f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
6117dab5cd05SOleg Bulyzhin {
61181f313773SOleg Bulyzhin 	struct mii_data *mii;
61191f313773SOleg Bulyzhin 	uint32_t link, status;
6120dab5cd05SOleg Bulyzhin 
6121dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
61221f313773SOleg Bulyzhin 
61233f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
61247b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
61257b97099dSOleg Bulyzhin 
6126dab5cd05SOleg Bulyzhin 	/*
6127dab5cd05SOleg Bulyzhin 	 * Process link state changes.
6128dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
6129dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
6130dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
6131dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
6132dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
6133dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
6134dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
6135dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
61361f313773SOleg Bulyzhin 	 *
61371f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
61384c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
6139dab5cd05SOleg Bulyzhin 	 */
6140dab5cd05SOleg Bulyzhin 
61411f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
61424c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
6143dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
6144dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
61451f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
61465dda8085SOleg Bulyzhin 			mii_pollstat(mii);
61471f313773SOleg Bulyzhin 			if (!sc->bge_link &&
61481f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
61491f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
61501f313773SOleg Bulyzhin 				sc->bge_link++;
61511f313773SOleg Bulyzhin 				if (bootverbose)
61521f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61531f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
61541f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
61551f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
61561f313773SOleg Bulyzhin 				sc->bge_link = 0;
61571f313773SOleg Bulyzhin 				if (bootverbose)
61581f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
61591f313773SOleg Bulyzhin 			}
61601f313773SOleg Bulyzhin 
61613f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6162dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6163dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6164daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6165daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6166daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6167daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6168dab5cd05SOleg Bulyzhin 		}
6169dab5cd05SOleg Bulyzhin 		return;
6170dab5cd05SOleg Bulyzhin 	}
6171dab5cd05SOleg Bulyzhin 
6172652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
61731f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
61747b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
61757b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
61761f313773SOleg Bulyzhin 				sc->bge_link++;
61779b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
61781f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
61791f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
61809b80ffe7SPyun YongHyeon 					DELAY(40);
61819b80ffe7SPyun YongHyeon 				}
61820c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
61831f313773SOleg Bulyzhin 				if (bootverbose)
61841f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61853f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
61863f74909aSGleb Smirnoff 				    LINK_STATE_UP);
61877b97099dSOleg Bulyzhin 			}
61881f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6189dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
61901f313773SOleg Bulyzhin 			if (bootverbose)
61911f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
61927b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
61931f313773SOleg Bulyzhin 		}
61946ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
61951f313773SOleg Bulyzhin 		/*
61960c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
61970c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
61980c8aa4eaSJung-uk Kim 		 * PHY link status directly.
61991f313773SOleg Bulyzhin 		 */
62001f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
62011f313773SOleg Bulyzhin 
62021f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
62031f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
62041f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
62055dda8085SOleg Bulyzhin 			mii_pollstat(mii);
62061f313773SOleg Bulyzhin 			if (!sc->bge_link &&
62071f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
62081f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
62091f313773SOleg Bulyzhin 				sc->bge_link++;
62101f313773SOleg Bulyzhin 				if (bootverbose)
62111f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
62121f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
62131f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
62141f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
62151f313773SOleg Bulyzhin 				sc->bge_link = 0;
62161f313773SOleg Bulyzhin 				if (bootverbose)
62171f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
62181f313773SOleg Bulyzhin 			}
62191f313773SOleg Bulyzhin 		}
62200c8aa4eaSJung-uk Kim 	} else {
62210c8aa4eaSJung-uk Kim 		/*
62226ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
62236ede2cfaSPyun YongHyeon 		 * link status.
62240c8aa4eaSJung-uk Kim 		 */
62256ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
62266ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
62276ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6228dab5cd05SOleg Bulyzhin 	}
6229dab5cd05SOleg Bulyzhin 
62302246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6231dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6232dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6233dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6234dab5cd05SOleg Bulyzhin }
62356f8718a3SScott Long 
62366f8718a3SScott Long static void
62376f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
62386f8718a3SScott Long {
62396f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
62402280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
62417e32f79aSPyun YongHyeon 	int unit;
62426f8718a3SScott Long 
62436f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
62446f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
62456f8718a3SScott Long 
62466f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
62476f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
62486f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
62496f8718a3SScott Long 	    "Debug Information");
62506f8718a3SScott Long 
62516f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
62526f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
6253548c8f1aSPyun YongHyeon 	    "MAC Register Read");
6254548c8f1aSPyun YongHyeon 
6255548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
6256548c8f1aSPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_ape_read, "I",
6257548c8f1aSPyun YongHyeon 	    "APE Register Read");
62586f8718a3SScott Long 
62596f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
62606f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
62616f8718a3SScott Long 	    "Memory Read");
62626f8718a3SScott Long 
62636f8718a3SScott Long #endif
6264763757b2SScott Long 
62657e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
6266beaa2ae1SPyun YongHyeon 	/*
6267beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6268beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6269beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6270beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6271beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6272beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6273beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6274beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6275beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6276beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6277beaa2ae1SPyun YongHyeon 	 */
62787e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
6279beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6280af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_collapse, 0,
6281beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6282beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6283beaa2ae1SPyun YongHyeon 
62842ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
62852ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
6286af3b2549SHans Petter Selasky 	    CTLFLAG_RDTUN, &sc->bge_msi, 0, "Enable MSI");
62875c952e8dSPyun YongHyeon 
628835f945cdSPyun YongHyeon 	/*
628935f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
629035f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
629135f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
629235f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
629335f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
629435f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
629535f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
629635f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
629735f945cdSPyun YongHyeon 	 */
629835f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
629935f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
6300af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_udpcsum, 0,
630135f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
630235f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
630335f945cdSPyun YongHyeon 
6304d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
63052280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
63062280c16bSPyun YongHyeon 	else
63072280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
63082280c16bSPyun YongHyeon }
6309d949071dSJung-uk Kim 
63102280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
63112280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
63122280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
63132280c16bSPyun YongHyeon 	    desc)
63142280c16bSPyun YongHyeon 
63152280c16bSPyun YongHyeon static void
63162280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
63172280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
63182280c16bSPyun YongHyeon {
63192280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
63202280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
63212280c16bSPyun YongHyeon 
63222280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6323763757b2SScott Long 	    NULL, "BGE Statistics");
6324763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6325763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6326763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6327763757b2SScott Long 	    "FramesDroppedDueToFilters");
6328763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6329763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6330763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6331763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6332763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6333763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
633406e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
633506e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
633606e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
633706e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6338763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6339763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6340763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6341763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6342763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6343763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6344763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6345763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6346763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6347763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6348763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6349763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6350763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6351763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6352763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6353763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6354763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6355763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6356763757b2SScott Long 
6357763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
6358763757b2SScott Long 	    NULL, "BGE RX Statistics");
6359763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6360763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
63611cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6362763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6363763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6364763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
63651cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6366763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6367763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6368763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6369763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6370763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6371763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6372763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6373763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6374763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6375763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6376763757b2SScott Long 	    "xoffPauseFramesReceived");
6377763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6378763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6379763757b2SScott Long 	    "ControlFramesReceived");
6380763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6381763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6382763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6383763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6384763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6385763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6386763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6387763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6388763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
638906e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6390763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
639106e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6392763757b2SScott Long 
6393763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
6394763757b2SScott Long 	    NULL, "BGE TX Statistics");
6395763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6396763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
63971cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6398763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6399763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6400763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6401763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6402763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6403763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6404763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6405763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6406763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6407763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6408763757b2SScott Long 	    "InternalMacTransmitErrors");
6409763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6410763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6411763757b2SScott Long 	    "SingleCollisionFrames");
6412763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6413763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6414763757b2SScott Long 	    "MultipleCollisionFrames");
6415763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6416763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6417763757b2SScott Long 	    "DeferredTransmissions");
6418763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6419763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6420763757b2SScott Long 	    "ExcessiveCollisions");
6421763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
642206e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
642306e83c7eSScott Long 	    "LateCollisions");
6424763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
64251cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6426763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6427763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6428763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6429763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6430763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6431763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6432763757b2SScott Long 	    "CarrierSenseErrors");
6433763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6434763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6435763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6436763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6437763757b2SScott Long }
6438763757b2SScott Long 
64392280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
64402280c16bSPyun YongHyeon 
64412280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
64426dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
64432280c16bSPyun YongHyeon 
64442280c16bSPyun YongHyeon static void
64452280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
64462280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
64472280c16bSPyun YongHyeon {
64482280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
64492280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
64502280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
64512280c16bSPyun YongHyeon 
64522280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
64532280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
64542280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
64552280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
64562280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
64572280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
64582280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
64592280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
64602280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
64612280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
64622280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
64632280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
64642280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
64652280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
64662280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
64672280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
64682280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
64692280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
64702280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
64712280c16bSPyun YongHyeon 
64722280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
64732280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
64742280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
64752280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
64762280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
64772280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
64782280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
64791cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
64802280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
64812280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
64822280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
64832280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
64842280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
64852280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
64862280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
64872280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
64882280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
64892280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
64902280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
64912280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
64922280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
64932280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
64942280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
64952280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
64962280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
64972280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
64982280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
64992280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
65002280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
65012280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
65022280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
65032280c16bSPyun YongHyeon 
65042280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
65052280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
65062280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
65071cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
65082280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
65092280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
65102280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
65112280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
65122280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
65132280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
65142280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
65152280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
65162280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
65172280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
65182280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
65192280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
65202280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
65212280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
65222280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
65232280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
65242280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
65252280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
65262280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
65272280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
65282280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
65291cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
65302280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
65311cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
65322280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
65331cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
65342280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
65352280c16bSPyun YongHyeon }
65362280c16bSPyun YongHyeon 
65372280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
65382280c16bSPyun YongHyeon 
6539763757b2SScott Long static int
6540763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6541763757b2SScott Long {
6542763757b2SScott Long 	struct bge_softc *sc;
654306e83c7eSScott Long 	uint32_t result;
6544d949071dSJung-uk Kim 	int offset;
6545763757b2SScott Long 
6546763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6547763757b2SScott Long 	offset = arg2;
6548d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6549d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6550041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
65516f8718a3SScott Long }
65526f8718a3SScott Long 
65536f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
65546f8718a3SScott Long static int
65556f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
65566f8718a3SScott Long {
65576f8718a3SScott Long 	struct bge_softc *sc;
65586f8718a3SScott Long 	uint16_t *sbdata;
655928276ad6SPyun YongHyeon 	int error, result, sbsz;
65606f8718a3SScott Long 	int i, j;
65616f8718a3SScott Long 
65626f8718a3SScott Long 	result = -1;
65636f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65646f8718a3SScott Long 	if (error || (req->newptr == NULL))
65656f8718a3SScott Long 		return (error);
65666f8718a3SScott Long 
65676f8718a3SScott Long 	if (result == 1) {
65686f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65696f8718a3SScott Long 
657028276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
657128276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
657228276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
657328276ad6SPyun YongHyeon 		else
657428276ad6SPyun YongHyeon 			sbsz = 32;
65756f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
65766f8718a3SScott Long 		printf("Status Block:\n");
657728276ad6SPyun YongHyeon 		BGE_LOCK(sc);
657828276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
657928276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
658028276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
658128276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
65826f8718a3SScott Long 			printf("%06x:", i);
658328276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
658428276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
65856f8718a3SScott Long 			printf("\n");
65866f8718a3SScott Long 		}
65876f8718a3SScott Long 
65886f8718a3SScott Long 		printf("Registers:\n");
65890c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
65906f8718a3SScott Long 			printf("%06x:", i);
65916f8718a3SScott Long 			for (j = 0; j < 8; j++) {
65926f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
65936f8718a3SScott Long 				i += 4;
65946f8718a3SScott Long 			}
65956f8718a3SScott Long 			printf("\n");
65966f8718a3SScott Long 		}
659728276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
65986f8718a3SScott Long 
65996f8718a3SScott Long 		printf("Hardware Flags:\n");
660028276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
660128276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6602a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6603a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
66045345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
66056f8718a3SScott Long 			printf(" - 575X Plus\n");
66065345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
66076f8718a3SScott Long 			printf(" - 5705 Plus\n");
66085345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
66095345bad0SScott Long 			printf(" - 5714 Family\n");
66105345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
66115345bad0SScott Long 			printf(" - 5700 Family\n");
66126f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
66136f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
66146f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
66156f8718a3SScott Long 			printf(" - PCI-X Bus\n");
66166f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
66176f8718a3SScott Long 			printf(" - PCI Express Bus\n");
66187d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
66196f8718a3SScott Long 			printf(" - No 3 LEDs\n");
66206f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
66216f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
66226f8718a3SScott Long 	}
66236f8718a3SScott Long 
66246f8718a3SScott Long 	return (error);
66256f8718a3SScott Long }
66266f8718a3SScott Long 
66276f8718a3SScott Long static int
66286f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
66296f8718a3SScott Long {
66306f8718a3SScott Long 	struct bge_softc *sc;
66316f8718a3SScott Long 	int error;
66326f8718a3SScott Long 	uint16_t result;
66336f8718a3SScott Long 	uint32_t val;
66346f8718a3SScott Long 
66356f8718a3SScott Long 	result = -1;
66366f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66376f8718a3SScott Long 	if (error || (req->newptr == NULL))
66386f8718a3SScott Long 		return (error);
66396f8718a3SScott Long 
66406f8718a3SScott Long 	if (result < 0x8000) {
66416f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66426f8718a3SScott Long 		val = CSR_READ_4(sc, result);
66436f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
66446f8718a3SScott Long 	}
66456f8718a3SScott Long 
66466f8718a3SScott Long 	return (error);
66476f8718a3SScott Long }
66486f8718a3SScott Long 
66496f8718a3SScott Long static int
6650548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6651548c8f1aSPyun YongHyeon {
6652548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6653548c8f1aSPyun YongHyeon 	int error;
6654548c8f1aSPyun YongHyeon 	uint16_t result;
6655548c8f1aSPyun YongHyeon 	uint32_t val;
6656548c8f1aSPyun YongHyeon 
6657548c8f1aSPyun YongHyeon 	result = -1;
6658548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6659548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6660548c8f1aSPyun YongHyeon 		return (error);
6661548c8f1aSPyun YongHyeon 
6662548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6663548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6664548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6665548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6666548c8f1aSPyun YongHyeon 	}
6667548c8f1aSPyun YongHyeon 
6668548c8f1aSPyun YongHyeon 	return (error);
6669548c8f1aSPyun YongHyeon }
6670548c8f1aSPyun YongHyeon 
6671548c8f1aSPyun YongHyeon static int
66726f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
66736f8718a3SScott Long {
66746f8718a3SScott Long 	struct bge_softc *sc;
66756f8718a3SScott Long 	int error;
66766f8718a3SScott Long 	uint16_t result;
66776f8718a3SScott Long 	uint32_t val;
66786f8718a3SScott Long 
66796f8718a3SScott Long 	result = -1;
66806f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66816f8718a3SScott Long 	if (error || (req->newptr == NULL))
66826f8718a3SScott Long 		return (error);
66836f8718a3SScott Long 
66846f8718a3SScott Long 	if (result < 0x8000) {
66856f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66866f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
66876f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
66886f8718a3SScott Long 	}
66896f8718a3SScott Long 
66906f8718a3SScott Long 	return (error);
66916f8718a3SScott Long }
66926f8718a3SScott Long #endif
669338cc658fSJohn Baldwin 
669438cc658fSJohn Baldwin static int
66955fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
66965fea260fSMarius Strobl {
66975fea260fSMarius Strobl 
66985fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
66995fea260fSMarius Strobl 		return (1);
67005fea260fSMarius Strobl 
67015fea260fSMarius Strobl #ifdef __sparc64__
67025fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
67035fea260fSMarius Strobl 	return (0);
67045fea260fSMarius Strobl #endif
67055fea260fSMarius Strobl 	return (1);
67065fea260fSMarius Strobl }
67075fea260fSMarius Strobl 
67085fea260fSMarius Strobl static int
670938cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
671038cc658fSJohn Baldwin {
671138cc658fSJohn Baldwin 	uint32_t mac_addr;
671238cc658fSJohn Baldwin 
671373635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
671438cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
671538cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
671638cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
671773635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
671838cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
671938cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
672038cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
672138cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
67225fea260fSMarius Strobl 		return (0);
672338cc658fSJohn Baldwin 	}
67245fea260fSMarius Strobl 	return (1);
672538cc658fSJohn Baldwin }
672638cc658fSJohn Baldwin 
672738cc658fSJohn Baldwin static int
672838cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
672938cc658fSJohn Baldwin {
673038cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
673138cc658fSJohn Baldwin 
673238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
673338cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
673438cc658fSJohn Baldwin 
67355fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
67365fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
673738cc658fSJohn Baldwin }
673838cc658fSJohn Baldwin 
673938cc658fSJohn Baldwin static int
674038cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
674138cc658fSJohn Baldwin {
674238cc658fSJohn Baldwin 
67435fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
67445fea260fSMarius Strobl 		return (1);
67455fea260fSMarius Strobl 
67465fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
67475fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
674838cc658fSJohn Baldwin }
674938cc658fSJohn Baldwin 
675038cc658fSJohn Baldwin static int
675138cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
675238cc658fSJohn Baldwin {
675338cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
675438cc658fSJohn Baldwin 		/* NOTE: Order is critical */
67555fea260fSMarius Strobl 		bge_get_eaddr_fw,
675638cc658fSJohn Baldwin 		bge_get_eaddr_mem,
675738cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
675838cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
675938cc658fSJohn Baldwin 		NULL
676038cc658fSJohn Baldwin 	};
676138cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
676238cc658fSJohn Baldwin 
676338cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
676438cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
676538cc658fSJohn Baldwin 			break;
676638cc658fSJohn Baldwin 	}
676738cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
676838cc658fSJohn Baldwin }
6769df360178SGleb Smirnoff 
6770df360178SGleb Smirnoff static uint64_t
6771df360178SGleb Smirnoff bge_get_counter(if_t ifp, ift_counter cnt)
6772df360178SGleb Smirnoff {
6773df360178SGleb Smirnoff 	struct bge_softc *sc;
6774df360178SGleb Smirnoff 	struct bge_mac_stats *stats;
6775df360178SGleb Smirnoff 
6776df360178SGleb Smirnoff 	sc = if_getsoftc(ifp);
6777df360178SGleb Smirnoff 	if (!BGE_IS_5705_PLUS(sc))
6778df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6779df360178SGleb Smirnoff 	stats = &sc->bge_mac_stats;
6780df360178SGleb Smirnoff 
6781df360178SGleb Smirnoff 	switch (cnt) {
6782df360178SGleb Smirnoff 	case IFCOUNTER_IERRORS:
6783df360178SGleb Smirnoff 		return (stats->NoMoreRxBDs + stats->InputDiscards +
6784df360178SGleb Smirnoff 		    stats->InputErrors);
6785df360178SGleb Smirnoff 	case IFCOUNTER_COLLISIONS:
6786df360178SGleb Smirnoff 		return (stats->etherStatsCollisions);
6787df360178SGleb Smirnoff 	default:
6788df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6789df360178SGleb Smirnoff 	}
6790df360178SGleb Smirnoff }
6791