xref: /freebsd/sys/dev/bge/if_bge.c (revision 2a8c860fe3f3bcfc6ba9206f34d067d998d89c7e)
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 
1386*2a8c860fSRobert Watson 	if (m_cljget(m, M_NOWAIT, MJUM9BYTES) == NULL) {
1387943787f3SPyun YongHyeon 		m_freem(m);
138895d67482SBill Paul 		return (ENOBUFS);
138995d67482SBill Paul 	}
1390943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1391652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1392943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
13931be6acb7SGleb Smirnoff 
13941be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1395943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1396943787f3SPyun YongHyeon 	if (error != 0) {
1397943787f3SPyun YongHyeon 		m_freem(m);
13981be6acb7SGleb Smirnoff 		return (error);
1399f7cea149SGleb Smirnoff 	}
14001be6acb7SGleb Smirnoff 
1401aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1402943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1403943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1404943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1405943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1406943787f3SPyun YongHyeon 	}
1407943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1408943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1409943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1410943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1411943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1412e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1413e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1414e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1415e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1416e0b7b101SPyun YongHyeon 
14171be6acb7SGleb Smirnoff 	/*
14181be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
14191be6acb7SGleb Smirnoff 	 */
1420943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
14214e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
14224e7ba1abSGleb Smirnoff 	r->bge_idx = i;
14234e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
14244e7ba1abSGleb Smirnoff 	switch (nsegs) {
14254e7ba1abSGleb Smirnoff 	case 4:
14264e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
14274e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
14284e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1429e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
14304e7ba1abSGleb Smirnoff 	case 3:
1431e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1432e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1433e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1434e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
14354e7ba1abSGleb Smirnoff 	case 2:
14364e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
14374e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
14384e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1439e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
14404e7ba1abSGleb Smirnoff 	case 1:
14414e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
14424e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
14434e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1444e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
14454e7ba1abSGleb Smirnoff 		break;
14464e7ba1abSGleb Smirnoff 	default:
14474e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
14484e7ba1abSGleb Smirnoff 	}
1449f41ac2beSBill Paul 
1450a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1451943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
145295d67482SBill Paul 
145395d67482SBill Paul 	return (0);
145495d67482SBill Paul }
145595d67482SBill Paul 
145695d67482SBill Paul static int
14573f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
145895d67482SBill Paul {
14593ee5d7daSPyun YongHyeon 	int error, i;
146095d67482SBill Paul 
1461e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
146203e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1463e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1464943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
14653ee5d7daSPyun YongHyeon 			return (error);
146603e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
14671888f324SPyun YongHyeon 	}
146895d67482SBill Paul 
1469f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1470d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1471f41ac2beSBill Paul 
1472e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1473e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
147495d67482SBill Paul 
147595d67482SBill Paul 	return (0);
147695d67482SBill Paul }
147795d67482SBill Paul 
147895d67482SBill Paul static void
14793f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
148095d67482SBill Paul {
148195d67482SBill Paul 	int i;
148295d67482SBill Paul 
148395d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
148495d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
14850ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1486e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1487e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
14880ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1489f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1490e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1491e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
149295d67482SBill Paul 		}
1493f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
149495d67482SBill Paul 		    sizeof(struct bge_rx_bd));
149595d67482SBill Paul 	}
149695d67482SBill Paul }
149795d67482SBill Paul 
149895d67482SBill Paul static int
14993f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
150095d67482SBill Paul {
150195d67482SBill Paul 	struct bge_rcb *rcb;
15023ee5d7daSPyun YongHyeon 	int error, i;
150395d67482SBill Paul 
1504e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
150503e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
150695d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1507943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
15083ee5d7daSPyun YongHyeon 			return (error);
150903e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
15101888f324SPyun YongHyeon 	}
151195d67482SBill Paul 
1512f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1513d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1514f41ac2beSBill Paul 
1515e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
151695d67482SBill Paul 
15178a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1518f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
15198a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
15208a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
152167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
152295d67482SBill Paul 
1523e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
152495d67482SBill Paul 
152595d67482SBill Paul 	return (0);
152695d67482SBill Paul }
152795d67482SBill Paul 
152895d67482SBill Paul static void
15293f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
153095d67482SBill Paul {
153195d67482SBill Paul 	int i;
153295d67482SBill Paul 
153395d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
153495d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1535e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1536e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1537e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1538f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1539f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1540e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1541e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
154295d67482SBill Paul 		}
1543f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
15441be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
154595d67482SBill Paul 	}
154695d67482SBill Paul }
154795d67482SBill Paul 
154895d67482SBill Paul static void
15493f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
155095d67482SBill Paul {
155195d67482SBill Paul 	int i;
155295d67482SBill Paul 
1553f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
155495d67482SBill Paul 		return;
155595d67482SBill Paul 
155695d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
155795d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
15580ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1559e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1560e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15610ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1562f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1563e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1564e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
156595d67482SBill Paul 		}
1566f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
156795d67482SBill Paul 		    sizeof(struct bge_tx_bd));
156895d67482SBill Paul 	}
156995d67482SBill Paul }
157095d67482SBill Paul 
157195d67482SBill Paul static int
15723f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
157395d67482SBill Paul {
157495d67482SBill Paul 	sc->bge_txcnt = 0;
157595d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
15763927098fSPaul Saab 
1577e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1578e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
15795c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1580e6bf277eSPyun YongHyeon 
158114bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
158214bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
158338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
158414bbd30fSGleb Smirnoff 
15853927098fSPaul Saab 	/* 5700 b2 errata */
1586e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
158738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
15883927098fSPaul Saab 
158914bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
159038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
15913927098fSPaul Saab 	/* 5700 b2 errata */
1592e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
159338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
159495d67482SBill Paul 
159595d67482SBill Paul 	return (0);
159695d67482SBill Paul }
159795d67482SBill Paul 
159895d67482SBill Paul static void
15993e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
16003e9b1bcaSJung-uk Kim {
1601fba8b109SMarcel Moolenaar 	if_t ifp;
16023e9b1bcaSJung-uk Kim 
16033e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
16043e9b1bcaSJung-uk Kim 
16053e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
16063e9b1bcaSJung-uk Kim 
160745ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
1608fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_PROMISC)
160945ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16103e9b1bcaSJung-uk Kim 	else
161145ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16123e9b1bcaSJung-uk Kim }
16133e9b1bcaSJung-uk Kim 
16143e9b1bcaSJung-uk Kim static void
16153f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
161695d67482SBill Paul {
1617fba8b109SMarcel Moolenaar 	if_t ifp;
1618fba8b109SMarcel Moolenaar 	int mc_count = 0;
16193f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
1620fba8b109SMarcel Moolenaar 	int h, i, mcnt;
1621fba8b109SMarcel Moolenaar 	unsigned char *mta;
162295d67482SBill Paul 
16230f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
16240f9bd73bSSam Leffler 
1625fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
162695d67482SBill Paul 
1627fba8b109SMarcel Moolenaar 	mc_count = if_multiaddr_count(ifp, -1);
1628fba8b109SMarcel Moolenaar 	mta = malloc(sizeof(unsigned char) *  ETHER_ADDR_LEN *
1629fba8b109SMarcel Moolenaar 	    mc_count, M_DEVBUF, M_NOWAIT);
1630fba8b109SMarcel Moolenaar 
1631fba8b109SMarcel Moolenaar 	if(mta == NULL) {
1632fba8b109SMarcel Moolenaar 		device_printf(sc->bge_dev,
1633fba8b109SMarcel Moolenaar 		    "Failed to allocated temp mcast list\n");
1634fba8b109SMarcel Moolenaar 		return;
1635fba8b109SMarcel Moolenaar 	}
1636fba8b109SMarcel Moolenaar 
1637fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
163895d67482SBill Paul 		for (i = 0; i < 4; i++)
16390c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
164095d67482SBill Paul 		return;
164195d67482SBill Paul 	}
164295d67482SBill Paul 
164395d67482SBill Paul 	/* First, zot all the existing filters. */
164495d67482SBill Paul 	for (i = 0; i < 4; i++)
164595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
164695d67482SBill Paul 
1647fba8b109SMarcel Moolenaar 	if_multiaddr_array(ifp, mta, &mcnt, mc_count);
1648fba8b109SMarcel Moolenaar 	for(i = 0; i < mcnt; i++) {
1649a127e581SPeter Wemm 		h = ether_crc32_le(mta + (i * ETHER_ADDR_LEN),
1650a127e581SPeter Wemm 		    ETHER_ADDR_LEN) & 0x7F;
16510c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
165295d67482SBill Paul 	}
165395d67482SBill Paul 
165495d67482SBill Paul 	for (i = 0; i < 4; i++)
165595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1656fba8b109SMarcel Moolenaar 
1657fba8b109SMarcel Moolenaar 	free(mta, M_DEVBUF);
165895d67482SBill Paul }
165995d67482SBill Paul 
16608cb1383cSDoug Ambrisko static void
1661cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1662cb2eacc7SYaroslav Tykhiy {
1663fba8b109SMarcel Moolenaar 	if_t ifp;
1664cb2eacc7SYaroslav Tykhiy 
1665cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1666cb2eacc7SYaroslav Tykhiy 
1667cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1668cb2eacc7SYaroslav Tykhiy 
1669cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1670fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING)
1671cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1672cb2eacc7SYaroslav Tykhiy 	else
1673cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1674cb2eacc7SYaroslav Tykhiy }
1675cb2eacc7SYaroslav Tykhiy 
1676cb2eacc7SYaroslav Tykhiy static void
1677797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
16788cb1383cSDoug Ambrisko {
1679797ab05eSPyun YongHyeon 
16808cb1383cSDoug Ambrisko 	/*
16818cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
16828cb1383cSDoug Ambrisko 	 */
16838cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1684888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
16858cb1383cSDoug Ambrisko 
16868cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16878cb1383cSDoug Ambrisko 		switch (type) {
16888cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1689224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1690224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
16918cb1383cSDoug Ambrisko 			break;
1692548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1693224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1694224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
16958cb1383cSDoug Ambrisko 			break;
1696548c8f1aSPyun YongHyeon 		case BGE_RESET_SUSPEND:
1697548c8f1aSPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1698548c8f1aSPyun YongHyeon 			    BGE_FW_DRV_STATE_SUSPEND);
1699548c8f1aSPyun YongHyeon 			break;
17008cb1383cSDoug Ambrisko 		}
17018cb1383cSDoug Ambrisko 	}
1702548c8f1aSPyun YongHyeon 
1703548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1704548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17058cb1383cSDoug Ambrisko }
17068cb1383cSDoug Ambrisko 
17078cb1383cSDoug Ambrisko static void
1708797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
17098cb1383cSDoug Ambrisko {
1710797ab05eSPyun YongHyeon 
17118cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
17128cb1383cSDoug Ambrisko 		switch (type) {
17138cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1714224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1715224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
17168cb1383cSDoug Ambrisko 			/* START DONE */
17178cb1383cSDoug Ambrisko 			break;
1718548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1719224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1720224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
17218cb1383cSDoug Ambrisko 			break;
17228cb1383cSDoug Ambrisko 		}
17238cb1383cSDoug Ambrisko 	}
1724548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_SHUTDOWN)
1725548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17268cb1383cSDoug Ambrisko }
17278cb1383cSDoug Ambrisko 
17288cb1383cSDoug Ambrisko static void
1729797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
17308cb1383cSDoug Ambrisko {
1731797ab05eSPyun YongHyeon 
17328cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17338cb1383cSDoug Ambrisko 		switch (type) {
17348cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1735224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1736224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
17378cb1383cSDoug Ambrisko 			break;
1738548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1739224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1740224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
17418cb1383cSDoug Ambrisko 			break;
17428cb1383cSDoug Ambrisko 		}
17438cb1383cSDoug Ambrisko 	}
17448cb1383cSDoug Ambrisko }
17458cb1383cSDoug Ambrisko 
1746797ab05eSPyun YongHyeon static void
1747797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
17488cb1383cSDoug Ambrisko {
17498cb1383cSDoug Ambrisko 	int i;
17508cb1383cSDoug Ambrisko 
17518cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17523c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
17533fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
17549931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
17558cb1383cSDoug Ambrisko 
17568cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
17579931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
17589931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
17598cb1383cSDoug Ambrisko 				break;
17608cb1383cSDoug Ambrisko 			DELAY(10);
17618cb1383cSDoug Ambrisko 		}
17628cb1383cSDoug Ambrisko 	}
17638cb1383cSDoug Ambrisko }
17648cb1383cSDoug Ambrisko 
176550515680SPyun YongHyeon static uint32_t
176650515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
176750515680SPyun YongHyeon {
176850515680SPyun YongHyeon 	uint32_t dma_options;
176950515680SPyun YongHyeon 
177050515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
177150515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
177250515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
177350515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
177450515680SPyun YongHyeon #endif
177550515680SPyun YongHyeon 	return (dma_options);
177650515680SPyun YongHyeon }
177750515680SPyun YongHyeon 
177895d67482SBill Paul /*
1779c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
178095d67482SBill Paul  */
178195d67482SBill Paul static int
17823f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
178395d67482SBill Paul {
178450515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1785fbc374afSPyun YongHyeon 	uint16_t val;
178695d67482SBill Paul 	int i;
178795d67482SBill Paul 
17888cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
17891108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
17901108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
17911108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
17921108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
179395d67482SBill Paul 
179495d67482SBill Paul 	/*
179595d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
179695d67482SBill Paul 	 * internal memory.
179795d67482SBill Paul 	 */
179895d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
17993f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
180095d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
180195d67482SBill Paul 
180295d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
18033f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
180495d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
180595d67482SBill Paul 
1806fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1807fbc374afSPyun YongHyeon 		/*
1808d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1809fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1810fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1811fbc374afSPyun YongHyeon 		 */
1812fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1813fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1814fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1815fbc374afSPyun YongHyeon 	}
1816fbc374afSPyun YongHyeon 
1817f8bb33c3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765 ||
1818f8bb33c3SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57766) {
1819f8bb33c3SPyun YongHyeon 		/*
1820f8bb33c3SPyun YongHyeon 		 * For the 57766 and non Ax versions of 57765, bootcode
1821f8bb33c3SPyun YongHyeon 		 * needs to setup the PCIE Fast Training Sequence (FTS)
1822f8bb33c3SPyun YongHyeon 		 * value to prevent transmit hangs.
1823f8bb33c3SPyun YongHyeon 		 */
1824f8bb33c3SPyun YongHyeon 		if (sc->bge_chiprev != BGE_CHIPREV_57765_AX) {
1825f8bb33c3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL,
1826f8bb33c3SPyun YongHyeon 			    CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL) |
1827f8bb33c3SPyun YongHyeon 			    BGE_CPMU_PADRNG_CTL_RDIV2);
1828f8bb33c3SPyun YongHyeon 		}
1829f8bb33c3SPyun YongHyeon 	}
1830f8bb33c3SPyun YongHyeon 
1831186f842bSJung-uk Kim 	/*
1832186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1833186f842bSJung-uk Kim 	 */
1834186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1835186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1836652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
183748630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
183848630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
183948630d79SPyun YongHyeon 		else
1840186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1841652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
18424c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1843186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1844186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1845186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1846186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1847186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1848186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1849cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1850cbb2b2feSPyun YongHyeon 			/*
1851cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1852cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1853cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1854cbb2b2feSPyun YongHyeon 			 * register.
1855cbb2b2feSPyun YongHyeon 			 */
1856cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1857cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1858186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1859186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1860186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1861186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1862186f842bSJung-uk Kim 		} else {
1863186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1864186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1865186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
18660c8aa4eaSJung-uk Kim 			    0x0F;
1867186f842bSJung-uk Kim 		}
1868e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1869e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
18703f74909aSGleb Smirnoff 			uint32_t tmp;
18715cba12d3SPaul Saab 
1872186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
18730c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1874186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1875186f842bSJung-uk Kim 				dma_rw_ctl |=
1876186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
18775cba12d3SPaul Saab 
1878186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1879186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1880186f842bSJung-uk Kim 		}
1881186f842bSJung-uk Kim 	} else {
1882186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1883186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1884186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1885186f842bSJung-uk Kim 
1886186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1887186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1888186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1889186f842bSJung-uk Kim 	}
1890186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1891186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1892186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1893186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1894e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1895186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
18965cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1897b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18981108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1899b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1900b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1901bbe2ca75SPyun YongHyeon 		/*
1902bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1903bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1904bbe2ca75SPyun YongHyeon 		 * disabled.
1905bbe2ca75SPyun YongHyeon 		 */
19062927f01fSPyun YongHyeon 		if (!BGE_IS_57765_PLUS(sc) &&
19072927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
19082927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5762)
1909bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1910b4a256acSPyun YongHyeon 	}
19115cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
191295d67482SBill Paul 
191395d67482SBill Paul 	/*
191495d67482SBill Paul 	 * Set up general mode register.
191595d67482SBill Paul 	 */
1916548c8f1aSPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc);
19172927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
19182927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
1919548c8f1aSPyun YongHyeon 		/* Retain Host-2-BMC settings written by APE firmware. */
1920548c8f1aSPyun YongHyeon 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1921548c8f1aSPyun YongHyeon 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1922548c8f1aSPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1923548c8f1aSPyun YongHyeon 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1924548c8f1aSPyun YongHyeon 	}
1925548c8f1aSPyun YongHyeon 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1926548c8f1aSPyun YongHyeon 	    BGE_MODECTL_TX_NO_PHDR_CSUM;
192795d67482SBill Paul 
192895d67482SBill Paul 	/*
192990447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
193090447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
193190447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
193290447aadSMarius Strobl 	 * certain bridges.
193390447aadSMarius Strobl 	 */
193490447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
193590447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
193650515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
193790447aadSMarius Strobl 
193890447aadSMarius Strobl 	/*
19398cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
19408cb1383cSDoug Ambrisko 	 */
19418cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
194250515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
194350515680SPyun YongHyeon 
194450515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
19458cb1383cSDoug Ambrisko 
19468cb1383cSDoug Ambrisko 	/*
1947ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
194840438c47SMarius Strobl 	 * properly by these devices.
194995d67482SBill Paul 	 */
195040438c47SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
195195d67482SBill Paul 
1952d7acafa1SMarius Strobl 	/* Set the timer prescaler (always 66 MHz). */
19530c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
195495d67482SBill Paul 
195538cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
195638cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
195738cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
195838cc658fSJohn Baldwin 
195938cc658fSJohn Baldwin 		/* Put PHY into ready state */
196038cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
196138cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
196238cc658fSJohn Baldwin 		DELAY(40);
196338cc658fSJohn Baldwin 	}
196438cc658fSJohn Baldwin 
196595d67482SBill Paul 	return (0);
196695d67482SBill Paul }
196795d67482SBill Paul 
196895d67482SBill Paul static int
19693f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
197095d67482SBill Paul {
197195d67482SBill Paul 	struct bge_rcb *rcb;
1972e907febfSPyun YongHyeon 	bus_size_t vrcb;
1973e907febfSPyun YongHyeon 	bge_hostaddr taddr;
19742927f01fSPyun YongHyeon 	uint32_t dmactl, rdmareg, val;
19758a315a6dSPyun YongHyeon 	int i, limit;
197695d67482SBill Paul 
197795d67482SBill Paul 	/*
197895d67482SBill Paul 	 * Initialize the memory window pointer register so that
197995d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
198095d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
198195d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
198295d67482SBill Paul 	 */
198395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
198495d67482SBill Paul 
1985822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1986822f63fcSBill Paul 
19877ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
198895d67482SBill Paul 		/* Configure mbuf memory pool */
19890dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1990822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1991822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1992822f63fcSBill Paul 		else
199395d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
199495d67482SBill Paul 
199595d67482SBill Paul 		/* Configure DMA resource pool */
19960434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
19970434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
199895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
19990434d1b8SBill Paul 	}
200095d67482SBill Paul 
200195d67482SBill Paul 	/* Configure mbuf pool watermarks */
200250515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
20031108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
2004fba8b109SMarcel Moolenaar 		if (if_getmtu(sc->bge_ifp) > ETHERMTU) {
20051108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
20061108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
20071108273aSPyun YongHyeon 		} else {
20081108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
20091108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
20101108273aSPyun YongHyeon 		}
20111108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
2012fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
2013fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
2014fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
201538cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
201638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
201738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
201838cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
201938cc658fSJohn Baldwin 	} else {
202038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
202138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
202238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
202338cc658fSJohn Baldwin 	}
202495d67482SBill Paul 
202595d67482SBill Paul 	/* Configure DMA resource watermarks */
202695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
202795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
202895d67482SBill Paul 
202995d67482SBill Paul 	/* Enable buffer manager */
2030bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
2031bbe2ca75SPyun YongHyeon 	/*
2032bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
2033bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
2034bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
2035bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
2036bbe2ca75SPyun YongHyeon 	 */
2037bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2038bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
2039bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
204095d67482SBill Paul 
204195d67482SBill Paul 	/* Poll for buffer manager start indication */
204295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2043d5d23857SJung-uk Kim 		DELAY(10);
20440c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
204595d67482SBill Paul 			break;
204695d67482SBill Paul 	}
204795d67482SBill Paul 
204895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
20495a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
205095d67482SBill Paul 		return (ENXIO);
205195d67482SBill Paul 	}
205295d67482SBill Paul 
205395d67482SBill Paul 	/* Enable flow-through queues */
20540c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
205595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
205695d67482SBill Paul 
205795d67482SBill Paul 	/* Wait until queue initialization is complete */
205895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2059d5d23857SJung-uk Kim 		DELAY(10);
206095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
206195d67482SBill Paul 			break;
206295d67482SBill Paul 	}
206395d67482SBill Paul 
206495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2065fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
206695d67482SBill Paul 		return (ENXIO);
206795d67482SBill Paul 	}
206895d67482SBill Paul 
20698a315a6dSPyun YongHyeon 	/*
20708a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
20718a315a6dSPyun YongHyeon 	 *
20728a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
20738a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
20748a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
20758a315a6dSPyun YongHyeon 	 *
20768a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
20778a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
20788a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
20798a315a6dSPyun YongHyeon 	 *   to the controller.
20808a315a6dSPyun YongHyeon 	 *
20818a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
20828a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
20838a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
20848a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
20858a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
20868a315a6dSPyun YongHyeon 	 *   be disabled.
20878a315a6dSPyun YongHyeon 	 *
20888a315a6dSPyun YongHyeon 	 * Receive Return Ring
20898a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
20908a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
20918a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
20928a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
20938a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
20948a315a6dSPyun YongHyeon 	 *
20958a315a6dSPyun YongHyeon 	 * Send Ring
20968a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
20978a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
20988a315a6dSPyun YongHyeon 	 */
20998a315a6dSPyun YongHyeon 
21008a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
2101f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2102f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
2103f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2104f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
2105f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2106f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2107f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
21081108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
21091108273aSPyun YongHyeon 		/*
21101108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
21111108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
21121108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
21131108273aSPyun YongHyeon 		 * Bit 0     : Reserved
21141108273aSPyun YongHyeon 		 */
21151108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
21161108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
21171108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
21188a315a6dSPyun YongHyeon 		/*
21198a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
21208a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
21218a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
21228a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21238a315a6dSPyun YongHyeon 		 */
21240434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
21258a315a6dSPyun YongHyeon 	} else {
21268a315a6dSPyun YongHyeon 		/*
21278a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
21288a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
21298a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
21308a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
21318a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21328a315a6dSPyun YongHyeon 		 */
21330434d1b8SBill Paul 		rcb->bge_maxlen_flags =
21340434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
21358a315a6dSPyun YongHyeon 	}
2136bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
213750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
213850515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21391108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
21401108273aSPyun YongHyeon 	else
214195d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
21428a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
21430c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
21440c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
214567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
214667111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
214795d67482SBill Paul 
21488a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
21498a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
21508a315a6dSPyun YongHyeon 
215195d67482SBill Paul 	/*
21528a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
21538a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
21548a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
215595d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
215695d67482SBill Paul 	 * high enough to require it).
215795d67482SBill Paul 	 */
21584c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2159f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
21608a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
2161f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
2162f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2163f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
2164f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2165f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2166f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2167f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
21681be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
21691be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2170bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
217150515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
217250515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21731108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
21741108273aSPyun YongHyeon 		else
217595d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
217667111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
217767111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
217867111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
217967111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
21808a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
21810434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
21820434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
218367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
21848a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
21858a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
21868a315a6dSPyun YongHyeon 	}
218795d67482SBill Paul 
21888a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
21895e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
2190f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
219167111612SJohn Polstra 		rcb->bge_maxlen_flags =
219267111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
21930434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
21940434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
21958a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
21968a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
21970434d1b8SBill Paul 	}
219895d67482SBill Paul 
2199ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2200ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2201427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2202427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2203427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
22048d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
22058d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2206ca4f8986SPyun YongHyeon 	}
220795d67482SBill Paul 	/*
22088a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
22098a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
22108a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
22118a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
22128a315a6dSPyun YongHyeon 	 *
221395d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
221495d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
221595d67482SBill Paul 	 * each ring.
22169ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
22179ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
22189ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
22199ba784dbSScott Long 	 * are reports that it might not need to be so strict.
222038cc658fSJohn Baldwin 	 *
222138cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
222238cc658fSJohn Baldwin 	 * well.
222395d67482SBill Paul 	 */
22245345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
22256f8718a3SScott Long 		val = 8;
22266f8718a3SScott Long 	else
22276f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
22286f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
22292a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
22302a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
22312a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
22321108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
22331108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
22341108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
22351108273aSPyun YongHyeon 	}
223695d67482SBill Paul 
223795d67482SBill Paul 	/*
22388a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
22398a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
22408a315a6dSPyun YongHyeon 	 * located in NIC memory.
224195d67482SBill Paul 	 */
22428a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
22438a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
22448a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
22452927f01fSPyun YongHyeon 	else if (BGE_IS_57765_PLUS(sc) ||
22462927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
22472927f01fSPyun YongHyeon 		limit = 2;
22482927f01fSPyun YongHyeon 	else if (BGE_IS_5717_PLUS(sc))
22492927f01fSPyun YongHyeon 		limit = 4;
22508a315a6dSPyun YongHyeon 	else
22518a315a6dSPyun YongHyeon 		limit = 1;
2252e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
22538a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2254e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2255e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2256e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2257e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
225895d67482SBill Paul 	}
225995d67482SBill Paul 
22608a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
2261e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2262e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2263e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2264e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2265bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
226650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
226750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
22681108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
22691108273aSPyun YongHyeon 	else
2270e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2271e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2272e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2273e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
227495d67482SBill Paul 
22758a315a6dSPyun YongHyeon 	/*
22768a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
22778a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
22788a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
22798a315a6dSPyun YongHyeon 	 */
2280bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
228150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
228250515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
22831108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
22841108273aSPyun YongHyeon 		limit = 16;
22851108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
22868a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
2287b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
22882927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762 ||
2289fe26ad88SPyun YongHyeon 	    BGE_IS_57765_PLUS(sc))
22908a315a6dSPyun YongHyeon 		limit = 4;
22918a315a6dSPyun YongHyeon 	else
22928a315a6dSPyun YongHyeon 		limit = 1;
22938a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
2294e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
22958a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2296e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2297e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2298e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
22998a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
2300e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
230138cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
23023f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
2303e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
230495d67482SBill Paul 	}
230595d67482SBill Paul 
230695d67482SBill Paul 	/*
23078a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
23088a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
23098a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
231095d67482SBill Paul 	 */
2311e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2312e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2313e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2314e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
23158a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2316e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2317e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
231895d67482SBill Paul 
231995d67482SBill Paul 	/* Set random backoff seed for TX */
232095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
23210a2cc827SPyun YongHyeon 	    (IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
23224a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
23230a2cc827SPyun YongHyeon 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5]) &
232495d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
232595d67482SBill Paul 
232695d67482SBill Paul 	/* Set inter-packet gap */
232750515680SPyun YongHyeon 	val = 0x2620;
23282927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
23292927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
233050515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
233150515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
233250515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
233395d67482SBill Paul 
233495d67482SBill Paul 	/*
233595d67482SBill Paul 	 * Specify which ring to use for packets that don't match
233695d67482SBill Paul 	 * any RX rules.
233795d67482SBill Paul 	 */
233895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
233995d67482SBill Paul 
234095d67482SBill Paul 	/*
234195d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
234295d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
234395d67482SBill Paul 	 */
234495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
234595d67482SBill Paul 
234695d67482SBill Paul 	/* Inialize RX list placement stats mask. */
23470c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
234895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
234995d67482SBill Paul 
235095d67482SBill Paul 	/* Disable host coalescing until we get it set up */
235195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
235295d67482SBill Paul 
235395d67482SBill Paul 	/* Poll to make sure it's shut down. */
235495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2355d5d23857SJung-uk Kim 		DELAY(10);
235695d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
235795d67482SBill Paul 			break;
235895d67482SBill Paul 	}
235995d67482SBill Paul 
236095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2361fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2362fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
236395d67482SBill Paul 		return (ENXIO);
236495d67482SBill Paul 	}
236595d67482SBill Paul 
236695d67482SBill Paul 	/* Set up host coalescing defaults */
236795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
236895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
236995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
237095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
23717ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
237295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
237395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
23740434d1b8SBill Paul 	}
2375b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2376b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
237795d67482SBill Paul 
237895d67482SBill Paul 	/* Set up address of statistics block */
23797ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
2380f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2381f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
238295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2383f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
23840434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
238595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
23860434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
23870434d1b8SBill Paul 	}
23880434d1b8SBill Paul 
23890434d1b8SBill Paul 	/* Set up address of status block */
2390f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2391f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
239295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2393f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
239495d67482SBill Paul 
239530f57f61SPyun YongHyeon 	/* Set up status block size. */
239630f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2397864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
239830f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2399864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2400864104feSPyun YongHyeon 	} else {
240130f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2402864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2403864104feSPyun YongHyeon 	}
2404864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2405864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2406864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
240730f57f61SPyun YongHyeon 
240895d67482SBill Paul 	/* Turn on host coalescing state machine */
240930f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
241095d67482SBill Paul 
241195d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
241295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
241395d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
241495d67482SBill Paul 
241595d67482SBill Paul 	/* Turn on RX list placement state machine */
241695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
241795d67482SBill Paul 
241895d67482SBill Paul 	/* Turn on RX list selector state machine. */
24197ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
242095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
242195d67482SBill Paul 
24222246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2423ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2424ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2425ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2426ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2427ea3b4127SPyun YongHyeon 
2428ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2429ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2430ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2431ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2432ea3b4127SPyun YongHyeon 	else
2433ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2434ea3b4127SPyun YongHyeon 
2435548c8f1aSPyun YongHyeon 	/* Allow APE to send/receive frames. */
2436548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2437548c8f1aSPyun YongHyeon 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2438548c8f1aSPyun YongHyeon 
2439ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
24409b80ffe7SPyun YongHyeon 	DELAY(40);
244195d67482SBill Paul 
244295d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
244391bd90d8SPyun YongHyeon 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
244495d67482SBill Paul 
244595d67482SBill Paul #ifdef notdef
244695d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
244795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
244895d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
244995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
245095d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
245195d67482SBill Paul #endif
245295d67482SBill Paul 
245395d67482SBill Paul 	/* Turn on DMA completion state machine */
24547ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
245595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
245695d67482SBill Paul 
24576f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
24586f8718a3SScott Long 
24596f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2460a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
24613889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
24626f8718a3SScott Long 
24637aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
24647aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
24657aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
24667aa4b937SPyun YongHyeon 
246795d67482SBill Paul 	/* Turn on write DMA state machine */
24686f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
24694f09c4c7SMarius Strobl 	DELAY(40);
247095d67482SBill Paul 
247195d67482SBill Paul 	/* Turn on read DMA state machine */
24724f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
24731108273aSPyun YongHyeon 
24741108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
24751108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
24761108273aSPyun YongHyeon 
2477a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2478a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2479a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2480a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2481a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2482a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
24834f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
24844f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
24851108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2486ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
24871108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
24881108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
248955a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
249055a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
249155a24a05SPyun YongHyeon 	}
249250515680SPyun YongHyeon 
24932927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
24942927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
249550515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
249650515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2497e3215f76SPyun YongHyeon 		/*
2498e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2499e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2500e3215f76SPyun YongHyeon 		 */
2501e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2502e3215f76SPyun YongHyeon 	}
250350515680SPyun YongHyeon 
2504d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2505d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2506d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
25071108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
25082927f01fSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc) || BGE_IS_57765_PLUS(sc)) {
25092927f01fSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
25102927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL_REG2;
25112927f01fSPyun YongHyeon 		else
25122927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL;
25132927f01fSPyun YongHyeon 		dmactl = CSR_READ_4(sc, rdmareg);
2514bbe2ca75SPyun YongHyeon 		/*
2515bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2516bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2517bbe2ca75SPyun YongHyeon 		 */
25182927f01fSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5719_A0 ||
25192927f01fSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
2520bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2521bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2522bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2523bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2524bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2525bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2526bbe2ca75SPyun YongHyeon 		}
2527d255f2a9SPyun YongHyeon 		/*
2528d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2529d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2530d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2531d255f2a9SPyun YongHyeon 		 */
25322927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, rdmareg, dmactl |
2533d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2534d255f2a9SPyun YongHyeon 	}
2535bbe2ca75SPyun YongHyeon 
2536e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2537bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2538bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2539bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2540bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2541e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2542e3215f76SPyun YongHyeon 		/*
2543e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2544e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2545e3215f76SPyun YongHyeon 		 */
2546e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2547e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2548e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2549e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
25502927f01fSPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5762) {
25512927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2,
25522927f01fSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2) |
25532927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
25542927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2555bbe2ca75SPyun YongHyeon 	}
2556bbe2ca75SPyun YongHyeon 
25574f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
25584f09c4c7SMarius Strobl 	DELAY(40);
255995d67482SBill Paul 
256029b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
256129b44b09SPyun YongHyeon 		for (i = 0; i < BGE_NUM_RDMA_CHANNELS / 2; i++) {
256229b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LENGTH + i * 4);
256329b44b09SPyun YongHyeon 			if ((val & 0xFFFF) > BGE_FRAMELEN)
256429b44b09SPyun YongHyeon 				break;
256529b44b09SPyun YongHyeon 			if (((val >> 16) & 0xFFFF) > BGE_FRAMELEN)
256629b44b09SPyun YongHyeon 				break;
256729b44b09SPyun YongHyeon 		}
256829b44b09SPyun YongHyeon 		if (i != BGE_NUM_RDMA_CHANNELS / 2) {
256929b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
257029b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
257129b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5719;
257229b44b09SPyun YongHyeon 			else
257329b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5720;
257429b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
257529b44b09SPyun YongHyeon 		}
257629b44b09SPyun YongHyeon 	}
257729b44b09SPyun YongHyeon 
257895d67482SBill Paul 	/* Turn on RX data completion state machine */
257995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
258095d67482SBill Paul 
258195d67482SBill Paul 	/* Turn on RX BD initiator state machine */
258295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
258395d67482SBill Paul 
258495d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
258595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
258695d67482SBill Paul 
258795d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25887ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
258995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
259095d67482SBill Paul 
259195d67482SBill Paul 	/* Turn on send BD completion state machine */
259295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
259395d67482SBill Paul 
259495d67482SBill Paul 	/* Turn on send data completion state machine */
2595a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2596a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2597a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2598a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
259995d67482SBill Paul 
260095d67482SBill Paul 	/* Turn on send data initiator state machine */
26011108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
26021108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
26031108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2604ca3f1187SPyun YongHyeon 	else
260595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
260695d67482SBill Paul 
260795d67482SBill Paul 	/* Turn on send BD initiator state machine */
260895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
260995d67482SBill Paul 
261095d67482SBill Paul 	/* Turn on send BD selector state machine */
261195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
261295d67482SBill Paul 
26130c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
261495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
261595d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
261695d67482SBill Paul 
261795d67482SBill Paul 	/* ack/clear link change events */
261895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26190434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26200434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2621f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
262295d67482SBill Paul 
26236ede2cfaSPyun YongHyeon 	/*
26246ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
26256ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
26266ede2cfaSPyun YongHyeon 	 */
2627652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
262895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2629a1d52896SBill Paul 	} else {
26307ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
26317ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
26327ed3f0f0SPyun YongHyeon 			DELAY(80);
26337ed3f0f0SPyun YongHyeon 		}
26341f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
26354c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2636a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2637a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2638a1d52896SBill Paul 	}
263995d67482SBill Paul 
26401f313773SOleg Bulyzhin 	/*
26411f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
26421f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
26431f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
26441f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
26451f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
26461f313773SOleg Bulyzhin 	 */
26471f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26481f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26491f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
26501f313773SOleg Bulyzhin 
265195d67482SBill Paul 	/* Enable link state change attentions. */
265295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
265395d67482SBill Paul 
265495d67482SBill Paul 	return (0);
265595d67482SBill Paul }
265695d67482SBill Paul 
2657d7acafa1SMarius Strobl static const struct bge_revision *
26584c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
26594c0da0ffSGleb Smirnoff {
26604c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
26614c0da0ffSGleb Smirnoff 
26624c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
26634c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
26644c0da0ffSGleb Smirnoff 			return (br);
26654c0da0ffSGleb Smirnoff 	}
26664c0da0ffSGleb Smirnoff 
26674c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
26684c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
26694c0da0ffSGleb Smirnoff 			return (br);
26704c0da0ffSGleb Smirnoff 	}
26714c0da0ffSGleb Smirnoff 
26724c0da0ffSGleb Smirnoff 	return (NULL);
26734c0da0ffSGleb Smirnoff }
26744c0da0ffSGleb Smirnoff 
2675d7acafa1SMarius Strobl static const struct bge_vendor *
26764c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26774c0da0ffSGleb Smirnoff {
26784c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26794c0da0ffSGleb Smirnoff 
26804c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26814c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26824c0da0ffSGleb Smirnoff 			return (v);
26834c0da0ffSGleb Smirnoff 
26844c0da0ffSGleb Smirnoff 	return (NULL);
26854c0da0ffSGleb Smirnoff }
26864c0da0ffSGleb Smirnoff 
2687d7acafa1SMarius Strobl static uint32_t
2688d7acafa1SMarius Strobl bge_chipid(device_t dev)
268995d67482SBill Paul {
2690978f2704SMarius Strobl 	uint32_t id;
269195d67482SBill Paul 
2692a5779553SStanislav Sedov 	id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2693a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
26941108273aSPyun YongHyeon 	if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
26951108273aSPyun YongHyeon 		/*
2696d7acafa1SMarius Strobl 		 * Find the ASCI revision.  Different chips use different
2697d7acafa1SMarius Strobl 		 * registers.
26981108273aSPyun YongHyeon 		 */
26991108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
27001108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
27011108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2702bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
270350515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
27042927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5725:
27052927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5727:
27062927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5762:
270767129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57764:
270867129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57767:
270967129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57787:
27101108273aSPyun YongHyeon 			id = pci_read_config(dev,
27111108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
27121108273aSPyun YongHyeon 			break;
2713b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2714fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57762:
2715b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2716fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57766:
2717b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
271867129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57782:
2719b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
272067129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57786:
2721b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2722b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2723b4a256acSPyun YongHyeon 			id = pci_read_config(dev,
2724b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2725b4a256acSPyun YongHyeon 			break;
27261108273aSPyun YongHyeon 		default:
2727d7acafa1SMarius Strobl 			id = pci_read_config(dev, BGE_PCI_PRODID_ASICREV, 4);
27281108273aSPyun YongHyeon 		}
27291108273aSPyun YongHyeon 	}
2730d7acafa1SMarius Strobl 	return (id);
2731d7acafa1SMarius Strobl }
2732d7acafa1SMarius Strobl 
2733d7acafa1SMarius Strobl /*
2734d7acafa1SMarius Strobl  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2735d7acafa1SMarius Strobl  * against our list and return its name if we find a match.
2736d7acafa1SMarius Strobl  *
2737d7acafa1SMarius Strobl  * Note that since the Broadcom controller contains VPD support, we
2738d7acafa1SMarius Strobl  * try to get the device name string from the controller itself instead
2739d7acafa1SMarius Strobl  * of the compiled-in string. It guarantees we'll always announce the
2740d7acafa1SMarius Strobl  * right product name. We fall back to the compiled-in string when
2741d7acafa1SMarius Strobl  * VPD is unavailable or corrupt.
2742d7acafa1SMarius Strobl  */
2743d7acafa1SMarius Strobl static int
2744d7acafa1SMarius Strobl bge_probe(device_t dev)
2745d7acafa1SMarius Strobl {
2746d7acafa1SMarius Strobl 	char buf[96];
2747d7acafa1SMarius Strobl 	char model[64];
2748d7acafa1SMarius Strobl 	const struct bge_revision *br;
2749d7acafa1SMarius Strobl 	const char *pname;
2750d7acafa1SMarius Strobl 	struct bge_softc *sc;
2751d7acafa1SMarius Strobl 	const struct bge_type *t = bge_devs;
2752d7acafa1SMarius Strobl 	const struct bge_vendor *v;
2753d7acafa1SMarius Strobl 	uint32_t id;
2754d7acafa1SMarius Strobl 	uint16_t did, vid;
2755d7acafa1SMarius Strobl 
2756d7acafa1SMarius Strobl 	sc = device_get_softc(dev);
2757d7acafa1SMarius Strobl 	sc->bge_dev = dev;
2758d7acafa1SMarius Strobl 	vid = pci_get_vendor(dev);
2759d7acafa1SMarius Strobl 	did = pci_get_device(dev);
2760d7acafa1SMarius Strobl 	while(t->bge_vid != 0) {
2761d7acafa1SMarius Strobl 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2762d7acafa1SMarius Strobl 			id = bge_chipid(dev);
27634c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
2764852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2765852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
2766d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s", pname);
2767d7acafa1SMarius Strobl 			else {
2768d7acafa1SMarius Strobl 				v = bge_lookup_vendor(vid);
2769d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s %s",
2770d7acafa1SMarius Strobl 				    v != NULL ? v->v_name : "Unknown",
27717c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
27722ad1b396SMarius Strobl 				    "NetXtreme/NetLink Ethernet Controller");
2773d7acafa1SMarius Strobl 			}
2774d7acafa1SMarius Strobl 			snprintf(buf, sizeof(buf), "%s, %sASIC rev. %#08x",
2775d7acafa1SMarius Strobl 			    model, br != NULL ? "" : "unknown ", id);
27764c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
2777d7acafa1SMarius Strobl 			return (BUS_PROBE_DEFAULT);
277895d67482SBill Paul 		}
277995d67482SBill Paul 		t++;
278095d67482SBill Paul 	}
278195d67482SBill Paul 
278295d67482SBill Paul 	return (ENXIO);
278395d67482SBill Paul }
278495d67482SBill Paul 
2785f41ac2beSBill Paul static void
27863f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2787f41ac2beSBill Paul {
2788f41ac2beSBill Paul 	int i;
2789f41ac2beSBill Paul 
27903f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2791f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2792f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
27930ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2794f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2795f41ac2beSBill Paul 	}
2796943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2797943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2798943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2799f41ac2beSBill Paul 
28003f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2801f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2802f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2803f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2804f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2805f41ac2beSBill Paul 	}
2806943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2807943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2808943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2809f41ac2beSBill Paul 
28103f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2811f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2812f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
28130ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2814f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2815f41ac2beSBill Paul 	}
2816f41ac2beSBill Paul 
28170ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
28180ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2819c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2820c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
28210ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
28220ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2823f41ac2beSBill Paul 
28243f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2825068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring_paddr)
2826e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2827e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2828068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring)
2829f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2830f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2831f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2832f41ac2beSBill Paul 
2833f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2834f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2835f41ac2beSBill Paul 
28363f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2837068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring_paddr)
2838e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2839e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2840e65bed95SPyun YongHyeon 
2841068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring)
2842f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2843f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2844f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2845f41ac2beSBill Paul 
2846f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2847f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2848f41ac2beSBill Paul 
28493f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2850068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring_paddr)
2851e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2852e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2853e65bed95SPyun YongHyeon 
2854068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring)
2855f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2856f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2857f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2858f41ac2beSBill Paul 
2859f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2860f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2861f41ac2beSBill Paul 
28623f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2863068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring_paddr)
2864e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2865e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2866e65bed95SPyun YongHyeon 
2867068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring)
2868f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2869f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2870f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2871f41ac2beSBill Paul 
2872f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2873f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2874f41ac2beSBill Paul 
28753f74909aSGleb Smirnoff 	/* Destroy status block. */
2876068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block_paddr)
2877e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2878e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2879e65bed95SPyun YongHyeon 
2880068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block)
2881f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2882f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2883f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2884f41ac2beSBill Paul 
2885f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2886f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2887f41ac2beSBill Paul 
28883f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2889068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats_paddr)
2890e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2891e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2892e65bed95SPyun YongHyeon 
2893068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats)
2894f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2895f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2896f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2897f41ac2beSBill Paul 
2898f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2899f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2900f41ac2beSBill Paul 
29015b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
29025b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
29035b610048SPyun YongHyeon 
29043f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2905f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2906f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2907f41ac2beSBill Paul }
2908f41ac2beSBill Paul 
2909f41ac2beSBill Paul static int
29105b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
29115b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
29125b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2913f41ac2beSBill Paul {
29143f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
29155b610048SPyun YongHyeon 	int error;
2916f41ac2beSBill Paul 
29175b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2918fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
29195b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
29205b610048SPyun YongHyeon 	if (error != 0) {
29215b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29225b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
29235b610048SPyun YongHyeon 		return (ENOMEM);
29245b610048SPyun YongHyeon 	}
29255b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
29265b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
29275b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
29285b610048SPyun YongHyeon 	if (error != 0) {
29295b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29305b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
29315b610048SPyun YongHyeon 		return (ENOMEM);
29325b610048SPyun YongHyeon 	}
29335b610048SPyun YongHyeon 	/* Load the address of the ring. */
29345b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
29355b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
29365b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
29375b610048SPyun YongHyeon 	if (error != 0) {
29385b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29395b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
29405b610048SPyun YongHyeon 		return (ENOMEM);
29415b610048SPyun YongHyeon 	}
29425b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
29435b610048SPyun YongHyeon 	return (0);
29445b610048SPyun YongHyeon }
29455b610048SPyun YongHyeon 
29465b610048SPyun YongHyeon static int
29475b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
29485b610048SPyun YongHyeon {
29495b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2950fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
29515b610048SPyun YongHyeon 	int i, error;
2952f41ac2beSBill Paul 
2953f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2954f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2955f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2956f41ac2beSBill Paul 	/*
2957f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2958f41ac2beSBill Paul 	 */
29594eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2960f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
29614eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
29624eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2963e65bed95SPyun YongHyeon 	if (error != 0) {
2964fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2965fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2966e65bed95SPyun YongHyeon 		return (ENOMEM);
2967e65bed95SPyun YongHyeon 	}
2968e65bed95SPyun YongHyeon 
29695b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
29705b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
29715b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
29725b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
29735b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
29745b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
29755b610048SPyun YongHyeon 	if (error)
29765b610048SPyun YongHyeon 		return (error);
29775b610048SPyun YongHyeon 
29785b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
29795b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
29805b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
29815b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
29825b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
29835b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
29845b610048SPyun YongHyeon 	if (error)
29855b610048SPyun YongHyeon 		return (error);
29865b610048SPyun YongHyeon 
29875b610048SPyun YongHyeon 	/* Create tag for TX ring. */
29885b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
29895b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
29905b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
29915b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
29925b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
29935b610048SPyun YongHyeon 	if (error)
29945b610048SPyun YongHyeon 		return (error);
29955b610048SPyun YongHyeon 
2996f41ac2beSBill Paul 	/*
29975b610048SPyun YongHyeon 	 * Create tag for status block.
29985b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
29995b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
30005b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
30015b610048SPyun YongHyeon 	 * of configured number of ring.
3002f41ac2beSBill Paul 	 */
30035b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
30045b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
30055b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
30065b610048SPyun YongHyeon 	else
30075b610048SPyun YongHyeon 		sbsz = 32;
30085b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
30095b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
30105b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
30115b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
30125b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
30135b610048SPyun YongHyeon 	if (error)
30145b610048SPyun YongHyeon 		return (error);
30155b610048SPyun YongHyeon 
301612c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
301712c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
301812c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
301912c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
302012c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
302112c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
302212c65daeSPyun YongHyeon 	if (error)
302312c65daeSPyun YongHyeon 		return (error);
302412c65daeSPyun YongHyeon 
30255b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
30265b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30275b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
30285b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
30295b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
30305b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
30315b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
30325b610048SPyun YongHyeon 		if (error)
30335b610048SPyun YongHyeon 			return (error);
30345b610048SPyun YongHyeon 	}
30355b610048SPyun YongHyeon 
30365b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
3037d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
3038d2ffe15aSPyun YongHyeon 		/*
3039d2ffe15aSPyun YongHyeon 		 * XXX
3040d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
3041d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
3042062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
3043062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
3044d2ffe15aSPyun YongHyeon 		 */
3045062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3046d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
3047d2ffe15aSPyun YongHyeon 	}
3048fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
3049fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
3050fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
3051fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
30525b610048SPyun YongHyeon 	if (error != 0) {
30535b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
30545b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
30555b610048SPyun YongHyeon 		return (ENOMEM);
30565b610048SPyun YongHyeon 	}
30575b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
30581108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
3059ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
3060ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
3061ca3f1187SPyun YongHyeon 	} else {
3062ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
3063ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
3064ca3f1187SPyun YongHyeon 	}
30655b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
3066ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
3067ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
3068ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
3069f41ac2beSBill Paul 
3070f41ac2beSBill Paul 	if (error) {
30710ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
30720ac56796SPyun YongHyeon 		return (ENOMEM);
30730ac56796SPyun YongHyeon 	}
30740ac56796SPyun YongHyeon 
30755b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
3076f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
3077f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
3078f5459d4cSPyun YongHyeon 	else
3079f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
30805b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
3081f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
3082f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
30830ac56796SPyun YongHyeon 
30840ac56796SPyun YongHyeon 	if (error) {
30850ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3086f41ac2beSBill Paul 		return (ENOMEM);
3087f41ac2beSBill Paul 	}
3088f41ac2beSBill Paul 
30893f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3090943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3091943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3092943787f3SPyun YongHyeon 	if (error) {
3093943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3094943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3095943787f3SPyun YongHyeon 		return (ENOMEM);
3096943787f3SPyun YongHyeon 	}
3097f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
30980ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3099f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3100f41ac2beSBill Paul 		if (error) {
3101fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3102fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3103f41ac2beSBill Paul 			return (ENOMEM);
3104f41ac2beSBill Paul 		}
3105f41ac2beSBill Paul 	}
3106f41ac2beSBill Paul 
31073f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3108f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
31090ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3110f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3111f41ac2beSBill Paul 		if (error) {
3112fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31130ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3114f41ac2beSBill Paul 			return (ENOMEM);
3115f41ac2beSBill Paul 		}
3116f41ac2beSBill Paul 	}
3117f41ac2beSBill Paul 
31185b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
31194c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
31205b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
31218a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
31221be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
31231be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3124f41ac2beSBill Paul 		if (error) {
3125fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31263f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3127f41ac2beSBill Paul 			return (ENOMEM);
3128f41ac2beSBill Paul 		}
31293f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3130943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3131943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3132943787f3SPyun YongHyeon 		if (error) {
3133943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
31341b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3135943787f3SPyun YongHyeon 			return (ENOMEM);
3136943787f3SPyun YongHyeon 		}
3137f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3138f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3139f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3140f41ac2beSBill Paul 			if (error) {
3141fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
31423f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3143f41ac2beSBill Paul 				return (ENOMEM);
3144f41ac2beSBill Paul 			}
3145f41ac2beSBill Paul 		}
3146f41ac2beSBill Paul 	}
3147f41ac2beSBill Paul 
3148f41ac2beSBill Paul 	return (0);
3149f41ac2beSBill Paul }
3150f41ac2beSBill Paul 
3151bf6ef57aSJohn Polstra /*
3152bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3153bf6ef57aSJohn Polstra  */
3154bf6ef57aSJohn Polstra static int
3155bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3156bf6ef57aSJohn Polstra {
3157bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
315855aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3159bf6ef57aSJohn Polstra 
316055aaf894SMarius Strobl 	d = pci_get_domain(dev);
3161bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3162bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3163bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3164bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
316555aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3166bf6ef57aSJohn Polstra 			return (1);
3167bf6ef57aSJohn Polstra 	return (0);
3168bf6ef57aSJohn Polstra }
3169bf6ef57aSJohn Polstra 
3170bf6ef57aSJohn Polstra /*
3171bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3172bf6ef57aSJohn Polstra  */
3173bf6ef57aSJohn Polstra static int
3174bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3175bf6ef57aSJohn Polstra {
3176bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3177bf6ef57aSJohn Polstra 
3178d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
31795c952e8dSPyun YongHyeon 		return (0);
31805c952e8dSPyun YongHyeon 
31811108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
31821108273aSPyun YongHyeon #ifdef DEVICE_POLLING
31831108273aSPyun YongHyeon 	return (0);
31841108273aSPyun YongHyeon #endif
3185bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3186a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3187bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3188bf6ef57aSJohn Polstra 		/*
3189a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3190a8376f70SMarius Strobl 		 * configured in single-port mode.
3191bf6ef57aSJohn Polstra 		 */
3192bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3193bf6ef57aSJohn Polstra 			can_use_msi = 1;
3194bf6ef57aSJohn Polstra 		break;
3195bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3196bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3197bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3198bf6ef57aSJohn Polstra 			can_use_msi = 1;
3199bf6ef57aSJohn Polstra 		break;
3200a8376f70SMarius Strobl 	default:
3201a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3202bf6ef57aSJohn Polstra 			can_use_msi = 1;
3203bf6ef57aSJohn Polstra 	}
3204bf6ef57aSJohn Polstra 	return (can_use_msi);
3205bf6ef57aSJohn Polstra }
3206bf6ef57aSJohn Polstra 
320795d67482SBill Paul static int
3208062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3209062af0b0SPyun YongHyeon {
3210062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3211062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3212062af0b0SPyun YongHyeon 		const uint16_t vendor;
3213062af0b0SPyun YongHyeon 		const uint16_t device;
3214062af0b0SPyun YongHyeon 		const char *desc;
321529658c96SDimitry Andric 	} mbox_reorder_lists[] = {
3216062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3217062af0b0SPyun YongHyeon 	};
3218062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3219062af0b0SPyun YongHyeon 	device_t bus, dev;
322047f4a4dcSMarius Strobl 	int i;
3221062af0b0SPyun YongHyeon 
3222062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3223062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3224062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3225062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3226062af0b0SPyun YongHyeon 	for (;;) {
3227062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3228062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3229062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3230062af0b0SPyun YongHyeon 			break;
323147f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3232062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3233062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3234062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3235062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3236062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3237062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3238062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3239062af0b0SPyun YongHyeon 				return (1);
3240062af0b0SPyun YongHyeon 			}
3241062af0b0SPyun YongHyeon 		}
3242062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
3243062af0b0SPyun YongHyeon 			break;
3244062af0b0SPyun YongHyeon 	}
3245062af0b0SPyun YongHyeon 	return (0);
3246062af0b0SPyun YongHyeon }
3247062af0b0SPyun YongHyeon 
3248ea9c3a30SPyun YongHyeon static void
3249ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3250ea9c3a30SPyun YongHyeon {
3251ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3252ea9c3a30SPyun YongHyeon 
3253ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3254ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3255ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3256ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3257ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3258ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3259ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3260ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3261ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3262ea9c3a30SPyun YongHyeon 			clk = 133;
3263ea9c3a30SPyun YongHyeon 		else {
3264ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3265ea9c3a30SPyun YongHyeon 			switch (clk) {
3266ea9c3a30SPyun YongHyeon 			case 0:
3267ea9c3a30SPyun YongHyeon 				clk = 33;
3268ea9c3a30SPyun YongHyeon 				break;
3269ea9c3a30SPyun YongHyeon 			case 2:
3270ea9c3a30SPyun YongHyeon 				clk = 50;
3271ea9c3a30SPyun YongHyeon 				break;
3272ea9c3a30SPyun YongHyeon 			case 4:
3273ea9c3a30SPyun YongHyeon 				clk = 66;
3274ea9c3a30SPyun YongHyeon 				break;
3275ea9c3a30SPyun YongHyeon 			case 6:
3276ea9c3a30SPyun YongHyeon 				clk = 100;
3277ea9c3a30SPyun YongHyeon 				break;
3278ea9c3a30SPyun YongHyeon 			case 7:
3279ea9c3a30SPyun YongHyeon 				clk = 133;
3280ea9c3a30SPyun YongHyeon 				break;
3281ea9c3a30SPyun YongHyeon 			}
3282ea9c3a30SPyun YongHyeon 		}
3283ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3284ea9c3a30SPyun YongHyeon 	} else {
3285ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3286ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3287ea9c3a30SPyun YongHyeon 		else
3288ea9c3a30SPyun YongHyeon 			printf("PCI ");
3289ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3290ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3291ea9c3a30SPyun YongHyeon 			clk = 66;
3292ea9c3a30SPyun YongHyeon 		else
3293ea9c3a30SPyun YongHyeon 			clk = 33;
3294ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3295ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3296ea9c3a30SPyun YongHyeon 		else
3297ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3298ea9c3a30SPyun YongHyeon 	}
3299ea9c3a30SPyun YongHyeon }
3300ea9c3a30SPyun YongHyeon 
3301062af0b0SPyun YongHyeon static int
33023f74909aSGleb Smirnoff bge_attach(device_t dev)
330395d67482SBill Paul {
3304fba8b109SMarcel Moolenaar 	if_t ifp;
330595d67482SBill Paul 	struct bge_softc *sc;
3306548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
330708013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3308ad4328baSMarius Strobl 	int capmask, error, reg, rid, trys;
330995d67482SBill Paul 
331095d67482SBill Paul 	sc = device_get_softc(dev);
331195d67482SBill Paul 	sc->bge_dev = dev;
331295d67482SBill Paul 
3313e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3314dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3315e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3316dfe0df9aSPyun YongHyeon 
331795d67482SBill Paul 	pci_enable_busmaster(dev);
331895d67482SBill Paul 
3319ad4328baSMarius Strobl 	/*
3320ad4328baSMarius Strobl 	 * Allocate control/status registers.
3321ad4328baSMarius Strobl 	 */
3322736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
33235f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
332444f8f2fcSMarius Strobl 	    RF_ACTIVE);
332595d67482SBill Paul 
332695d67482SBill Paul 	if (sc->bge_res == NULL) {
3327548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
332895d67482SBill Paul 		error = ENXIO;
332995d67482SBill Paul 		goto fail;
333095d67482SBill Paul 	}
333195d67482SBill Paul 
33324f09c4c7SMarius Strobl 	/* Save various chip information. */
3333548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3334d7acafa1SMarius Strobl 	sc->bge_chipid = bge_chipid(dev);
3335e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3336e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3337e53d81eeSPaul Saab 
3338a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3339daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
33401108273aSPyun YongHyeon 	 /*
33411108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
33421108273aSPyun YongHyeon 	  *
33431108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
33441108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
33451108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
33461108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
33471108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3348bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
334950515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
33501108273aSPyun YongHyeon 	  *
3351548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3352548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3353548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3354548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3355548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3356548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3357548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3358548c8f1aSPyun YongHyeon 	  *
33591108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
33601108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
33611108273aSPyun YongHyeon 	  */
3362bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
336350515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
336450515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3365548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
33661108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
33671108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3368daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33691108273aSPyun YongHyeon 			else
3370daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3371bbe2ca75SPyun YongHyeon 		} else {
33721108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33731108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3374daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33751108273aSPyun YongHyeon 			else
3376daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
33771108273aSPyun YongHyeon 		}
33781108273aSPyun YongHyeon 	}
3379a813ed78SPyun YongHyeon 
33805fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
33815fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
338208013fd3SMarius Strobl 
33830dae9719SJung-uk Kim 	/* Save chipset family. */
33840dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
33852927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3386fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57765:
3387fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57766:
3388fe26ad88SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_57765_PLUS;
3389fe26ad88SPyun YongHyeon 		/* FALLTHROUGH */
33901108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3391bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
339250515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
33931108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
33941108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3395b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
339629b44b09SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
339729b44b09SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
339829b44b09SPyun YongHyeon 			/*
339929b44b09SPyun YongHyeon 			 * Enable work around for DMA engine miscalculation
340029b44b09SPyun YongHyeon 			 * of TXMBUF available space.
340129b44b09SPyun YongHyeon 			 */
340229b44b09SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_RDMA_BUG;
3403bbe2ca75SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3404bbe2ca75SPyun YongHyeon 			    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3405bbe2ca75SPyun YongHyeon 				/* Jumbo frame on BCM5719 A0 does not work. */
3406463a7e27SPyun YongHyeon 				sc->bge_flags &= ~BGE_FLAG_JUMBO;
3407bbe2ca75SPyun YongHyeon 			}
340829b44b09SPyun YongHyeon 		}
34091108273aSPyun YongHyeon 		break;
3410a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3411a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3412a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3413a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3414a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3415a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3416a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3417a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3418a5779553SStanislav Sedov 		break;
34190dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
34200dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
34210dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
34220dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
34237ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
34240dae9719SJung-uk Kim 		break;
34250dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
34260dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
34270dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3428f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
34299fe569d8SXin LI 		/* FALLTHROUGH */
34300dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
34310dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
343238cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
34330dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
34349fe569d8SXin LI 		/* FALLTHROUGH */
34350dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
34360dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
34370dae9719SJung-uk Kim 		break;
34380dae9719SJung-uk Kim 	}
34390dae9719SJung-uk Kim 
3440548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3441548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3442548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3443548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3444548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3445548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
34462927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3447548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3448548c8f1aSPyun YongHyeon 		break;
3449548c8f1aSPyun YongHyeon 	}
3450548c8f1aSPyun YongHyeon 
3451548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3452548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3453548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3454548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3455548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3456548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3457548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3458548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3459548c8f1aSPyun YongHyeon 			error = ENXIO;
3460548c8f1aSPyun YongHyeon 			goto fail;
3461548c8f1aSPyun YongHyeon 		}
3462548c8f1aSPyun YongHyeon 
3463548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3464548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3465548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3466548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3467548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3468548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3469548c8f1aSPyun YongHyeon 
3470548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3471548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3472548c8f1aSPyun YongHyeon 	}
3473548c8f1aSPyun YongHyeon 
3474749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3475749a5269SMarius Strobl 	bge_add_sysctls(sc);
3476749a5269SMarius Strobl 
3477a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
34781108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
34791108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3480a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3481a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3482a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3483a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3484a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3485a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3486a813ed78SPyun YongHyeon 	else
3487a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
34887ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
34897ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
34907ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3491a813ed78SPyun YongHyeon 
3492f681b29aSPyun YongHyeon 	/*
3493d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3494f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3495f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3496f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3497f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3498f681b29aSPyun YongHyeon 	 */
3499f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
35004f0794ffSBjoern A. Zeeb 
3501d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3502d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3503d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3504d9820cd8SPyun YongHyeon 
3505a7fcfcf3SPyun YongHyeon 	/*
3506a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3507a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3508a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3509a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3510a7fcfcf3SPyun YongHyeon 	 */
3511a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3512a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3513a7fcfcf3SPyun YongHyeon 
3514ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3515fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
35164f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
35174f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
35184f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
351984ac96f8SPyun YongHyeon 	}
35204f0794ffSBjoern A. Zeeb 
3521fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3522fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3523fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3524fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3525fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3526fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3527fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3528fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3529fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3530fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3531fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3532fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3533fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3534d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57791 ||
3535d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57795 ||
3536fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3537fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3538fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3539d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3540fb772a6cSMarius Strobl 	}
3541fb772a6cSMarius Strobl 
3542e53d81eeSPaul Saab 	/*
3543ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3544ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3545ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3546ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3547d7acafa1SMarius Strobl 	 * known bug which can't handle TSO if Ethernet header + IP/TCP
3548d7acafa1SMarius Strobl 	 * header is greater than 80 bytes. A workaround for the TSO
3549ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3550ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3551ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3552ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3553ca3f1187SPyun YongHyeon 	 */
35541108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
35551108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
35561108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3557bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3558bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3559bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3560bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3561bbe2ca75SPyun YongHyeon 		}
35621108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
35634f4a16e1SPyun YongHyeon 		/*
35644f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
35654f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3566be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
35674f4a16e1SPyun YongHyeon 		 */
35684f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3569be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3570be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3571ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
35724f4a16e1SPyun YongHyeon 	}
3573ca3f1187SPyun YongHyeon 
3574ca3f1187SPyun YongHyeon 	/*
35756f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3576e53d81eeSPaul Saab 	 */
35773b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
35784c0da0ffSGleb Smirnoff 		/*
35796f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
35806f8718a3SScott Long 		 * must be a PCI Express device.
35816f8718a3SScott Long 		 */
35826f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
35830aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
358448630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
358548630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
358648630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
358748630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
358850515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
358950515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
359048630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
359148630d79SPyun YongHyeon 		else
359248630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
359348630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
35946f8718a3SScott Long 	} else {
35956f8718a3SScott Long 		/*
35966f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
35976f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
35984c0da0ffSGleb Smirnoff 		 */
35993b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
36000aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
360190447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
36024c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3603652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
36046f8718a3SScott Long 	}
36054c0da0ffSGleb Smirnoff 
3606bf6ef57aSJohn Polstra 	/*
3607fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3608fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3609fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3610fd4d32feSPyun YongHyeon 	 */
3611fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3612fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3613fd4d32feSPyun YongHyeon 	/*
3614062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3615062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3616062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3617062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3618062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3619062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3620062af0b0SPyun YongHyeon 	 */
3621062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3622062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3623062af0b0SPyun YongHyeon 	/*
3624bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3625bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3626bf6ef57aSJohn Polstra 	 * normal operation.
3627bf6ef57aSJohn Polstra 	 */
36280aaf1057SPyun YongHyeon 	rid = 0;
36293b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
36300aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3631ad4328baSMarius Strobl 		reg = 1;
3632ad4328baSMarius Strobl 		if (bge_can_use_msi(sc) && pci_alloc_msi(dev, &reg) == 0) {
3633bf6ef57aSJohn Polstra 			rid = 1;
3634bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
36350aaf1057SPyun YongHyeon 		}
36360aaf1057SPyun YongHyeon 	}
3637bf6ef57aSJohn Polstra 
36381108273aSPyun YongHyeon 	/*
36391108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
36401108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
36411108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
36421108273aSPyun YongHyeon 	 */
36431108273aSPyun YongHyeon #ifndef DEVICE_POLLING
36441108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
36451108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
36461108273aSPyun YongHyeon #endif
36471108273aSPyun YongHyeon 
3648bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3649ad4328baSMarius Strobl 	    RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
3650bf6ef57aSJohn Polstra 
3651bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3652bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3653bf6ef57aSJohn Polstra 		error = ENXIO;
3654bf6ef57aSJohn Polstra 		goto fail;
3655bf6ef57aSJohn Polstra 	}
3656bf6ef57aSJohn Polstra 
3657ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
36584f09c4c7SMarius Strobl 
36598cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3660548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3661548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3662888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3663888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3664548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3665548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
36668cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
36678cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3668d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
36698cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
36708cb1383cSDoug Ambrisko 			}
36718cb1383cSDoug Ambrisko 		}
3672548c8f1aSPyun YongHyeon 	}
36738cb1383cSDoug Ambrisko 
36748cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
36753dd76c98SPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
36768cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
36778cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
36788cb1383cSDoug Ambrisko 		error = ENXIO;
36798cb1383cSDoug Ambrisko 		goto fail;
36808cb1383cSDoug Ambrisko 	}
36818cb1383cSDoug Ambrisko 
36823dd76c98SPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
36833dd76c98SPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
368495d67482SBill Paul 
368595d67482SBill Paul 	if (bge_chipinit(sc)) {
3686fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
368795d67482SBill Paul 		error = ENXIO;
368895d67482SBill Paul 		goto fail;
368995d67482SBill Paul 	}
369095d67482SBill Paul 
369138cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
369238cc658fSJohn Baldwin 	if (error) {
369308013fd3SMarius Strobl 		device_printf(sc->bge_dev,
369408013fd3SMarius Strobl 		    "failed to read station address\n");
369595d67482SBill Paul 		error = ENXIO;
369695d67482SBill Paul 		goto fail;
369795d67482SBill Paul 	}
369895d67482SBill Paul 
3699f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
37001108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
37011108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
37021108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3703f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3704f41ac2beSBill Paul 	else
3705f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3706f41ac2beSBill Paul 
37075b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3708fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3709fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3710f41ac2beSBill Paul 		error = ENXIO;
3711f41ac2beSBill Paul 		goto fail;
3712f41ac2beSBill Paul 	}
3713f41ac2beSBill Paul 
371495d67482SBill Paul 	/* Set default tuneable values. */
371595d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
371695d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
371795d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
37186f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
37196f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
372095d67482SBill Paul 
372135f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
372235f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
372335f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
372435f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
372535f945cdSPyun YongHyeon 
372695d67482SBill Paul 	/* Set up ifnet structure */
3727fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3728fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3729fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3730fc74a9f9SBrooks Davis 		error = ENXIO;
3731fc74a9f9SBrooks Davis 		goto fail;
3732fc74a9f9SBrooks Davis 	}
3733fba8b109SMarcel Moolenaar 	if_setsoftc(ifp, sc);
37349bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
3735fba8b109SMarcel Moolenaar 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
3736fba8b109SMarcel Moolenaar 	if_setioctlfn(ifp, bge_ioctl);
3737fba8b109SMarcel Moolenaar 	if_setstartfn(ifp, bge_start);
3738fba8b109SMarcel Moolenaar 	if_setinitfn(ifp, bge_init);
3739df360178SGleb Smirnoff 	if_setgetcounterfn(ifp, bge_get_counter);
37404a81240cSMarcel Moolenaar 	if_setsendqlen(ifp, BGE_TX_RING_CNT - 1);
3741fba8b109SMarcel Moolenaar 	if_setsendqready(ifp);
3742fba8b109SMarcel Moolenaar 	if_sethwassist(ifp, sc->bge_csum_features);
3743fba8b109SMarcel Moolenaar 	if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
3744fba8b109SMarcel Moolenaar 	    IFCAP_VLAN_MTU);
37451108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3746fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, CSUM_TSO, 0);
3747fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_TSO4 | IFCAP_VLAN_HWTSO, 0);
3748ca3f1187SPyun YongHyeon 	}
37494e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
3750fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWCSUM, 0);
37514e35d186SJung-uk Kim #endif
3752fba8b109SMarcel Moolenaar 	if_setcapenable(ifp, if_getcapabilities(ifp));
375375719184SGleb Smirnoff #ifdef DEVICE_POLLING
3754fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
375575719184SGleb Smirnoff #endif
375695d67482SBill Paul 
3757a1d52896SBill Paul 	/*
3758d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3759d375e524SGleb Smirnoff 	 * to hardware bugs.
3760d375e524SGleb Smirnoff 	 */
3761d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3762fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, 0, IFCAP_HWCSUM);
3763fba8b109SMarcel Moolenaar 		if_setcapenablebit(ifp, 0, IFCAP_HWCSUM);
3764fba8b109SMarcel Moolenaar 		if_sethwassist(ifp, 0);
3765d375e524SGleb Smirnoff 	}
3766d375e524SGleb Smirnoff 
3767d375e524SGleb Smirnoff 	/*
3768a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
376941abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
377041abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
377141abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
377241abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
377341abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
377441abcc1bSPaul Saab 	 * SK-9D41.
3775a1d52896SBill Paul 	 */
3776888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3777888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
37785fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
37795fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3780f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3781f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3782fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3783f6789fbaSPyun YongHyeon 			error = ENXIO;
3784f6789fbaSPyun YongHyeon 			goto fail;
3785f6789fbaSPyun YongHyeon 		}
378641abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
378741abcc1bSPaul Saab 	}
378841abcc1bSPaul Saab 
378995d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3790ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3791ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
379270c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3793ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
379470c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
379570c2071bSPyun YongHyeon 		} else
3796652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3797ea3b4127SPyun YongHyeon 	}
379895d67482SBill Paul 
379970c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
380070c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
380170c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
380270c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
380370c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
380470c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
380570c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
380670c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
380770c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
380870c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
380970c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
381070c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
381170c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
381270c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3813fe26ad88SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3814fe26ad88SPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc)) {
381570c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
381670c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
381770c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
381870c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
381970c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
382070c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
382170c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
382270c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
382370c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
382470c2071bSPyun YongHyeon 		} else
382570c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
382670c2071bSPyun YongHyeon 	}
382770c2071bSPyun YongHyeon 
382870c2071bSPyun YongHyeon 	/*
3829d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
383070c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
383170c2071bSPyun YongHyeon 	 */
383270c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
383370c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
383470c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3835d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
383670c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
383770c2071bSPyun YongHyeon 
3838652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
383909a8241fSGleb Smirnoff 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
38400c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
38410c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
38426098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
38436098821cSJung-uk Kim 		    0, NULL);
384495d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
384595d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3846da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
384795d67482SBill Paul 	} else {
384895d67482SBill Paul 		/*
38498cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
38508cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
38518cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
38528cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
38538cb1383cSDoug Ambrisko 		 * the PHY.
385495d67482SBill Paul 		 */
38554012d104SMarius Strobl 		trys = 0;
38568cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
38578cb1383cSDoug Ambrisko again:
38588cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
38598cb1383cSDoug Ambrisko 
3860fba8b109SMarcel Moolenaar 		error = mii_attach(dev, &sc->bge_miibus, ifp,
3861fba8b109SMarcel Moolenaar 		    (ifm_change_cb_t)bge_ifmedia_upd,
3862fba8b109SMarcel Moolenaar 		    (ifm_stat_cb_t)bge_ifmedia_sts, capmask, sc->bge_phy_addr,
3863fba8b109SMarcel Moolenaar 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
38648e5d93dbSMarius Strobl 		if (error != 0) {
38658cb1383cSDoug Ambrisko 			if (trys++ < 4) {
38668cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3867daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3868daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
38698cb1383cSDoug Ambrisko 				goto again;
38708cb1383cSDoug Ambrisko 			}
38718e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
387295d67482SBill Paul 			goto fail;
387395d67482SBill Paul 		}
38748cb1383cSDoug Ambrisko 
38758cb1383cSDoug Ambrisko 		/*
38768cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
38778cb1383cSDoug Ambrisko 		 */
38788cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
38798cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
388095d67482SBill Paul 	}
388195d67482SBill Paul 
388295d67482SBill Paul 	/*
3883e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3884e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3885e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3886e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3887e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3888e255b776SJohn Polstra 	 * payloads by copying the received packets.
3889e255b776SJohn Polstra 	 */
3890652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3891652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3892652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3893e255b776SJohn Polstra 
3894e255b776SJohn Polstra 	/*
389595d67482SBill Paul 	 * Call MI attach routine.
389695d67482SBill Paul 	 */
3897fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
38980f9bd73bSSam Leffler 
389961ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
3900fba8b109SMarcel Moolenaar 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
390161ccb9daSPyun YongHyeon 
39020f9bd73bSSam Leffler 	/*
39030f9bd73bSSam Leffler 	 * Hookup IRQ last.
39040f9bd73bSSam Leffler 	 */
3905dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3906dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
39077e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
39087e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3909dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3910dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3911dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3912dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3913dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3914e010b055SPyun YongHyeon 			error = ENOMEM;
3915dfe0df9aSPyun YongHyeon 			goto fail;
3916dfe0df9aSPyun YongHyeon 		}
3917d7acafa1SMarius Strobl 		error = taskqueue_start_threads(&sc->bge_tq, 1, PI_NET,
3918d7acafa1SMarius Strobl 		    "%s taskq", device_get_nameunit(sc->bge_dev));
3919d7acafa1SMarius Strobl 		if (error != 0) {
3920d7acafa1SMarius Strobl 			device_printf(dev, "could not start threads.\n");
3921d7acafa1SMarius Strobl 			ether_ifdetach(ifp);
3922d7acafa1SMarius Strobl 			goto fail;
3923d7acafa1SMarius Strobl 		}
3924dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3925dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3926dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3927dfe0df9aSPyun YongHyeon 	} else
3928dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3929dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3930dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
39310f9bd73bSSam Leffler 
39320f9bd73bSSam Leffler 	if (error) {
3933e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3934fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
39350f9bd73bSSam Leffler 	}
393695d67482SBill Paul 
393795d67482SBill Paul fail:
3938e010b055SPyun YongHyeon 	if (error)
3939e010b055SPyun YongHyeon 		bge_detach(dev);
394095d67482SBill Paul 	return (error);
394195d67482SBill Paul }
394295d67482SBill Paul 
394395d67482SBill Paul static int
39443f74909aSGleb Smirnoff bge_detach(device_t dev)
394595d67482SBill Paul {
394695d67482SBill Paul 	struct bge_softc *sc;
3947fba8b109SMarcel Moolenaar 	if_t ifp;
394895d67482SBill Paul 
394995d67482SBill Paul 	sc = device_get_softc(dev);
3950fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
395195d67482SBill Paul 
395275719184SGleb Smirnoff #ifdef DEVICE_POLLING
3953fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING)
395475719184SGleb Smirnoff 		ether_poll_deregister(ifp);
395575719184SGleb Smirnoff #endif
395675719184SGleb Smirnoff 
3957e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3958e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
39590f9bd73bSSam Leffler 		BGE_LOCK(sc);
396095d67482SBill Paul 		bge_stop(sc);
39610f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
39625dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3963e010b055SPyun YongHyeon 	}
39645dda8085SOleg Bulyzhin 
3965dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3966dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
396795d67482SBill Paul 
39680aba72ddSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
396995d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
39700aba72ddSPyun YongHyeon 	else if (sc->bge_miibus != NULL) {
397195d67482SBill Paul 		bus_generic_detach(dev);
397295d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
397395d67482SBill Paul 	}
397495d67482SBill Paul 
397595d67482SBill Paul 	bge_release_resources(sc);
397695d67482SBill Paul 
397795d67482SBill Paul 	return (0);
397895d67482SBill Paul }
397995d67482SBill Paul 
398095d67482SBill Paul static void
39813f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
398295d67482SBill Paul {
398395d67482SBill Paul 	device_t dev;
398495d67482SBill Paul 
398595d67482SBill Paul 	dev = sc->bge_dev;
398695d67482SBill Paul 
3987dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3988dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3989dfe0df9aSPyun YongHyeon 
399095d67482SBill Paul 	if (sc->bge_intrhand != NULL)
399195d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
399295d67482SBill Paul 
3993ad4328baSMarius Strobl 	if (sc->bge_irq != NULL) {
3994724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3995ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_irq), sc->bge_irq);
3996724bd939SJohn Polstra 		pci_release_msi(dev);
3997ad4328baSMarius Strobl 	}
399895d67482SBill Paul 
399995d67482SBill Paul 	if (sc->bge_res != NULL)
400095d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
4001ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res), sc->bge_res);
400295d67482SBill Paul 
4003548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
4004548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
4005ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res2), sc->bge_res2);
4006548c8f1aSPyun YongHyeon 
4007ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
4008ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
4009ad61f896SRuslan Ermilov 
4010f41ac2beSBill Paul 	bge_dma_free(sc);
401195d67482SBill Paul 
40120f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
40130f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
401495d67482SBill Paul }
401595d67482SBill Paul 
40168cb1383cSDoug Ambrisko static int
40173f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
401895d67482SBill Paul {
401995d67482SBill Paul 	device_t dev;
4020cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
40216f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
40220aaf1057SPyun YongHyeon 	uint16_t devctl;
40235fea260fSMarius Strobl 	int i;
402495d67482SBill Paul 
402595d67482SBill Paul 	dev = sc->bge_dev;
402695d67482SBill Paul 
4027cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
4028548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4029548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
4030cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
4031cc085b36SPyun YongHyeon 
403238cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
403338cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
40346f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
40356f8718a3SScott Long 			write_op = bge_writemem_direct;
40366f8718a3SScott Long 		else
40376f8718a3SScott Long 			write_op = bge_writemem_ind;
40389ba784dbSScott Long 	} else
40396f8718a3SScott Long 		write_op = bge_writereg_ind;
40406f8718a3SScott Long 
40413dd76c98SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
40423dd76c98SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5701) {
40433dd76c98SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
40443dd76c98SPyun YongHyeon 		for (i = 0; i < 8000; i++) {
40453dd76c98SPyun YongHyeon 			if (CSR_READ_4(sc, BGE_NVRAM_SWARB) &
40463dd76c98SPyun YongHyeon 			    BGE_NVRAMSWARB_GNT1)
40473dd76c98SPyun YongHyeon 				break;
40483dd76c98SPyun YongHyeon 			DELAY(20);
40493dd76c98SPyun YongHyeon 		}
40503dd76c98SPyun YongHyeon 		if (i == 8000) {
40513dd76c98SPyun YongHyeon 			if (bootverbose)
40523dd76c98SPyun YongHyeon 				device_printf(dev, "NVRAM lock timedout!\n");
40533dd76c98SPyun YongHyeon 		}
40543dd76c98SPyun YongHyeon 	}
4055548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
4056548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
4057548c8f1aSPyun YongHyeon 
405895d67482SBill Paul 	/* Save some important PCI state. */
405995d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
406095d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
406195d67482SBill Paul 
406295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
406395d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4064e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
406595d67482SBill Paul 
40666f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
40676f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
4068a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
40696f8718a3SScott Long 		if (bootverbose)
4070333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
40716f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
40726f8718a3SScott Long 	}
40736f8718a3SScott Long 
40746f8718a3SScott Long 	/*
40756f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
40766f8718a3SScott Long 	 * When firmware finishes its initialization it will
4077888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
40786f8718a3SScott Long 	 */
4079888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
40806f8718a3SScott Long 
40810c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4082e53d81eeSPaul Saab 
4083e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4084652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4085ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4086ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
40870c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
40880c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4089ad49eccfSPyun YongHyeon 		}
4090e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4091e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
40920c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
40930c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4094e53d81eeSPaul Saab 		}
4095e53d81eeSPaul Saab 	}
4096e53d81eeSPaul Saab 
4097df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4098df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4099df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4100df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4101df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4102df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4103df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4104df4db538SPyun YongHyeon 	}
4105df4db538SPyun YongHyeon 
410621c9e407SDavid Christensen 	/*
41076f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
41086f8718a3SScott Long 	 * powered up in D0 uninitialized.
41096f8718a3SScott Long 	 */
41105512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
41115512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4112caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
41136f8718a3SScott Long 
411495d67482SBill Paul 	/* Issue global reset */
41156f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
411695d67482SBill Paul 
4117cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4118cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4119cc085b36SPyun YongHyeon 	else
412095d67482SBill Paul 		DELAY(1000);
412195d67482SBill Paul 
4122e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4123652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4124e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4125e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
41265fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
41275fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4128e53d81eeSPaul Saab 		}
41290aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4130389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
41310aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4132389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4133389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4134389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
41350aaf1057SPyun YongHyeon 		    devctl, 2);
413648630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
41370aaf1057SPyun YongHyeon 		/* Clear error status. */
4138389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4139389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4140389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4141389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4142e53d81eeSPaul Saab 	}
4143e53d81eeSPaul Saab 
41443f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
414595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
414695d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4147e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4148cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4149cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4150cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4151cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4152548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4153548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4154548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4155548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4156cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
415795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
415895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4159cbb2b2feSPyun YongHyeon 	/*
4160cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4161fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4162cbb2b2feSPyun YongHyeon 	 * read stale status block.
4163cbb2b2feSPyun YongHyeon 	 */
4164cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4165cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4166cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4167cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4168cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4169cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4170cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4171cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4172cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4173cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4174cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4175cbb2b2feSPyun YongHyeon 		}
4176cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4177cbb2b2feSPyun YongHyeon 		    devctl, 2);
4178cbb2b2feSPyun YongHyeon 	}
417922a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
41804c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4181bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4182bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
41830aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
41840aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
41850aaf1057SPyun YongHyeon 			pci_write_config(dev,
41860aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4187bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4188bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4189bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4190bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4191bf6ef57aSJohn Polstra 		}
41924c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
41934c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
41944c0da0ffSGleb Smirnoff 	} else
4195a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4196a7b0c314SPaul Saab 
4197cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4198cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4199cc085b36SPyun YongHyeon 
4200cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4201cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4202cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4203cc085b36SPyun YongHyeon 	DELAY(40);
4204cc085b36SPyun YongHyeon 
4205548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4206548c8f1aSPyun YongHyeon 
420738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
420838cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
420938cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
421038cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
421138cc658fSJohn Baldwin 				break;
421238cc658fSJohn Baldwin 			DELAY(100);
421338cc658fSJohn Baldwin 		}
421438cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4215333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
421638cc658fSJohn Baldwin 			return (1);
421738cc658fSJohn Baldwin 		}
421838cc658fSJohn Baldwin 	} else {
421995d67482SBill Paul 		/*
42206f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
422108013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
42225fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
42235fea260fSMarius Strobl 		 * address is fitted though.
422495d67482SBill Paul 		 */
422595d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4226d5d23857SJung-uk Kim 			DELAY(10);
4227888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4228888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
422995d67482SBill Paul 				break;
423095d67482SBill Paul 		}
423195d67482SBill Paul 
42325fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4233333704a3SPyun YongHyeon 			device_printf(dev,
4234333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4235333704a3SPyun YongHyeon 			    val);
4236b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4237b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4238b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
423938cc658fSJohn Baldwin 	}
424095d67482SBill Paul 
424195d67482SBill Paul 	/*
4242da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4243da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4244da3003f0SBill Paul 	 * to 1.2V.
4245da3003f0SBill Paul 	 */
4246652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4247652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
42485fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
42495fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
42505fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4251da3003f0SBill Paul 	}
4252da3003f0SBill Paul 
4253e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4254652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4255b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4256a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4257a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4258a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
42595fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
42605fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4261e53d81eeSPaul Saab 	}
42628cb1383cSDoug Ambrisko 
426350515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
426450515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
426550515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
426650515680SPyun YongHyeon 
42678cb1383cSDoug Ambrisko 	return (0);
426895d67482SBill Paul }
426995d67482SBill Paul 
4270e0b7b101SPyun YongHyeon static __inline void
4271e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4272e0b7b101SPyun YongHyeon {
4273e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4274e0b7b101SPyun YongHyeon 
4275e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4276e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4277e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4278e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4279e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4280e0b7b101SPyun YongHyeon }
4281e0b7b101SPyun YongHyeon 
4282e0b7b101SPyun YongHyeon static __inline void
4283e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4284e0b7b101SPyun YongHyeon {
4285e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4286e0b7b101SPyun YongHyeon 
4287e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4288e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4289e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4290e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4291e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4292e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4293e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4294e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4295e0b7b101SPyun YongHyeon }
4296e0b7b101SPyun YongHyeon 
429795d67482SBill Paul /*
429895d67482SBill Paul  * Frame reception handling. This is called if there's a frame
429995d67482SBill Paul  * on the receive return list.
430095d67482SBill Paul  *
430195d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
43021be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
430395d67482SBill Paul  * 2) the frame is from the standard receive ring
430495d67482SBill Paul  */
430595d67482SBill Paul 
43061abcdbd1SAttilio Rao static int
4307dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
430895d67482SBill Paul {
4309fba8b109SMarcel Moolenaar 	if_t ifp;
43101abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4311b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
431295d67482SBill Paul 
43137f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
43140f9bd73bSSam Leffler 
43153f74909aSGleb Smirnoff 	/* Nothing to do. */
43167f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
43171abcdbd1SAttilio Rao 		return (rx_npkts);
4318cfcb5025SOleg Bulyzhin 
4319fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
432095d67482SBill Paul 
4321f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4322e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4323f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
432415eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4325f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4326fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
4327fba8b109SMarcel Moolenaar 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))
4328f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
432915eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4330f41ac2beSBill Paul 
43317f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
433295d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
43333f74909aSGleb Smirnoff 		uint32_t		rxidx;
433495d67482SBill Paul 		struct mbuf		*m = NULL;
43353f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
433695d67482SBill Paul 		int			have_tag = 0;
433795d67482SBill Paul 
433875719184SGleb Smirnoff #ifdef DEVICE_POLLING
4339fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_POLLING) {
434075719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
434175719184SGleb Smirnoff 				break;
434275719184SGleb Smirnoff 			sc->rxcycles--;
434375719184SGleb Smirnoff 		}
434475719184SGleb Smirnoff #endif
434575719184SGleb Smirnoff 
43467f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
434795d67482SBill Paul 
434895d67482SBill Paul 		rxidx = cur_rx->bge_idx;
43497f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
435095d67482SBill Paul 
4351fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING &&
4352cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
435395d67482SBill Paul 			have_tag = 1;
435495d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
435595d67482SBill Paul 		}
435695d67482SBill Paul 
435795d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
435895d67482SBill Paul 			jumbocnt++;
4359943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
436095d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4361e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
436295d67482SBill Paul 				continue;
436395d67482SBill Paul 			}
4364943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4365e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4366df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
436795d67482SBill Paul 				continue;
436895d67482SBill Paul 			}
436903e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
437095d67482SBill Paul 		} else {
437195d67482SBill Paul 			stdcnt++;
4372e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
437395d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4374e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
437595d67482SBill Paul 				continue;
437695d67482SBill Paul 			}
4377943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4378e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4379df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
438095d67482SBill Paul 				continue;
438195d67482SBill Paul 			}
438203e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
438395d67482SBill Paul 		}
438495d67482SBill Paul 
4385df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
4386e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4387e255b776SJohn Polstra 		/*
4388e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4389e65bed95SPyun YongHyeon 		 * the payload is aligned.
4390e255b776SJohn Polstra 		 */
4391652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4392e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4393e255b776SJohn Polstra 			    cur_rx->bge_len);
4394e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4395e255b776SJohn Polstra 		}
4396e255b776SJohn Polstra #endif
4397473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
439895d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
439995d67482SBill Paul 
4400fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_RXCSUM)
44011108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
440295d67482SBill Paul 
440395d67482SBill Paul 		/*
4404673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4405673d9191SSam Leffler 		 * attach that information to the packet.
440695d67482SBill Paul 		 */
4407d147662cSGleb Smirnoff 		if (have_tag) {
440878ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
440978ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4410d147662cSGleb Smirnoff 		}
441195d67482SBill Paul 
4412dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
44130f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4414fba8b109SMarcel Moolenaar 			if_input(ifp, m);
44150f9bd73bSSam Leffler 			BGE_LOCK(sc);
4416dfe0df9aSPyun YongHyeon 		} else
4417fba8b109SMarcel Moolenaar 			if_input(ifp, m);
4418d4da719cSAttilio Rao 		rx_npkts++;
441925e13e68SXin LI 
4420fba8b109SMarcel Moolenaar 		if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
44218cf7d13dSAttilio Rao 			return (rx_npkts);
442295d67482SBill Paul 	}
442395d67482SBill Paul 
442415eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
442515eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4426e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4427f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4428e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
44294c0da0ffSGleb Smirnoff 
4430c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4431f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
44324c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4433f41ac2beSBill Paul 
44347f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
443538cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
443695d67482SBill Paul 	if (stdcnt)
4437767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4438767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
443995d67482SBill Paul 	if (jumbocnt)
4440767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4441767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4442f5a034f9SPyun YongHyeon #ifdef notyet
4443f5a034f9SPyun YongHyeon 	/*
4444f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4445f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4446f5a034f9SPyun YongHyeon 	 */
4447f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4448fba8b109SMarcel Moolenaar 		if_incierrors(ifp, CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS));
4449f5a034f9SPyun YongHyeon #endif
44501abcdbd1SAttilio Rao 	return (rx_npkts);
445195d67482SBill Paul }
445295d67482SBill Paul 
445395d67482SBill Paul static void
44541108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
44551108273aSPyun YongHyeon {
44561108273aSPyun YongHyeon 
44571108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
44581108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
44591108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44601108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44611108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
44621108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
44631108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44641108273aSPyun YongHyeon 			}
44651108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
44661108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
44671108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
44681108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44691108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
44701108273aSPyun YongHyeon 			}
44711108273aSPyun YongHyeon 		}
44721108273aSPyun YongHyeon 	} else {
44731108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44741108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44751108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
44761108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44771108273aSPyun YongHyeon 		}
44781108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
44791108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
44801108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
44811108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
44821108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44831108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
44841108273aSPyun YongHyeon 		}
44851108273aSPyun YongHyeon 	}
44861108273aSPyun YongHyeon }
44871108273aSPyun YongHyeon 
44881108273aSPyun YongHyeon static void
4489b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
449095d67482SBill Paul {
449195a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
4492fba8b109SMarcel Moolenaar 	if_t ifp;
449395d67482SBill Paul 
44940f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
44950f9bd73bSSam Leffler 
44963f74909aSGleb Smirnoff 	/* Nothing to do. */
4497b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4498cfcb5025SOleg Bulyzhin 		return;
4499cfcb5025SOleg Bulyzhin 
4500fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
450195d67482SBill Paul 
4502e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
45035c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
450495d67482SBill Paul 	/*
450595d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
450695d67482SBill Paul 	 * frames that have been sent.
450795d67482SBill Paul 	 */
4508b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
450995a0a340SPyun YongHyeon 		uint32_t		idx;
451095d67482SBill Paul 
451195d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4512f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
451395d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
4514df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
451595d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
45160ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4517e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4518e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
45190ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4520f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4521e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4522e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
452395d67482SBill Paul 		}
452495d67482SBill Paul 		sc->bge_txcnt--;
452595d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
452695d67482SBill Paul 	}
452795d67482SBill Paul 
4528fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
45295b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
45305b01e77cSBruce Evans 		sc->bge_timer = 0;
453195d67482SBill Paul }
453295d67482SBill Paul 
453375719184SGleb Smirnoff #ifdef DEVICE_POLLING
45341abcdbd1SAttilio Rao static int
4535fba8b109SMarcel Moolenaar bge_poll(if_t ifp, enum poll_cmd cmd, int count)
453675719184SGleb Smirnoff {
4537fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
4538b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4539366454f2SOleg Bulyzhin 	uint32_t statusword;
45401abcdbd1SAttilio Rao 	int rx_npkts = 0;
454175719184SGleb Smirnoff 
45423f74909aSGleb Smirnoff 	BGE_LOCK(sc);
4543fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
45443f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
45451abcdbd1SAttilio Rao 		return (rx_npkts);
45463f74909aSGleb Smirnoff 	}
454775719184SGleb Smirnoff 
4548dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4549b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4550b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
45512246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4552b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4553b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4554dab5cd05SOleg Bulyzhin 
4555175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
45562246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4557175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4558dab5cd05SOleg Bulyzhin 
4559dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4560b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4561b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4562366454f2SOleg Bulyzhin 
45630c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4564366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4565366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4566366454f2SOleg Bulyzhin 
4567366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4568366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
45694c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4570652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4571366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4572366454f2SOleg Bulyzhin 
4573366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4574dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
4575fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
457625e13e68SXin LI 		BGE_UNLOCK(sc);
45778cf7d13dSAttilio Rao 		return (rx_npkts);
457825e13e68SXin LI 	}
4579b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4580fba8b109SMarcel Moolenaar 	if (!if_sendq_empty(ifp))
4581366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
45823f74909aSGleb Smirnoff 
45833f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
45841abcdbd1SAttilio Rao 	return (rx_npkts);
458575719184SGleb Smirnoff }
458675719184SGleb Smirnoff #endif /* DEVICE_POLLING */
458775719184SGleb Smirnoff 
4588dfe0df9aSPyun YongHyeon static int
4589dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4590dfe0df9aSPyun YongHyeon {
4591dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4592dfe0df9aSPyun YongHyeon 
4593dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4594dfe0df9aSPyun YongHyeon 	/*
4595dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4596dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4597dfe0df9aSPyun YongHyeon 	 */
4598dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4599dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4600dfe0df9aSPyun YongHyeon }
4601dfe0df9aSPyun YongHyeon 
4602dfe0df9aSPyun YongHyeon static void
4603dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4604dfe0df9aSPyun YongHyeon {
4605dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4606fba8b109SMarcel Moolenaar 	if_t ifp;
46071108273aSPyun YongHyeon 	uint32_t status, status_tag;
4608dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4609dfe0df9aSPyun YongHyeon 
4610dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4611dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4612dfe0df9aSPyun YongHyeon 
461366151edfSPyun YongHyeon 	BGE_LOCK(sc);
4614fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
461566151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4616dfe0df9aSPyun YongHyeon 		return;
461766151edfSPyun YongHyeon 	}
4618dfe0df9aSPyun YongHyeon 
4619dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4620dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4621dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4622dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4623dfe0df9aSPyun YongHyeon 
46242246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4625dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4626dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4627dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
46281108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
46292246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4630dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4631dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4632dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4633dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
46341108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
46351108273aSPyun YongHyeon 		status_tag = 0;
463666151edfSPyun YongHyeon 
463766151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
463866151edfSPyun YongHyeon 		bge_link_upd(sc);
463966151edfSPyun YongHyeon 
4640dfe0df9aSPyun YongHyeon 	/* Let controller work. */
46411108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4642dfe0df9aSPyun YongHyeon 
4643fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
464466151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4645dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
464666151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4647dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
464866151edfSPyun YongHyeon 		BGE_LOCK(sc);
4649dfe0df9aSPyun YongHyeon 	}
4650fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4651dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4652dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4653fba8b109SMarcel Moolenaar 		if (!if_sendq_empty(ifp))
4654dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4655dfe0df9aSPyun YongHyeon 	}
465666151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4657dfe0df9aSPyun YongHyeon }
4658dfe0df9aSPyun YongHyeon 
465995d67482SBill Paul static void
46603f74909aSGleb Smirnoff bge_intr(void *xsc)
466195d67482SBill Paul {
466295d67482SBill Paul 	struct bge_softc *sc;
4663fba8b109SMarcel Moolenaar 	if_t ifp;
4664dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4665b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
466695d67482SBill Paul 
466795d67482SBill Paul 	sc = xsc;
4668f41ac2beSBill Paul 
46690f9bd73bSSam Leffler 	BGE_LOCK(sc);
46700f9bd73bSSam Leffler 
4671dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4672dab5cd05SOleg Bulyzhin 
467375719184SGleb Smirnoff #ifdef DEVICE_POLLING
4674fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
467575719184SGleb Smirnoff 		BGE_UNLOCK(sc);
467675719184SGleb Smirnoff 		return;
467775719184SGleb Smirnoff 	}
467875719184SGleb Smirnoff #endif
467975719184SGleb Smirnoff 
4680f30cbfc6SScott Long 	/*
4681b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4682b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4683b848e032SBruce Evans 	 * our current organization this just gives complications and
4684b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4685b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4686b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4687b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4688b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4689b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4690b848e032SBruce Evans 	 *
4691b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4692b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4693b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4694b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4695b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4696b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4697b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4698b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4699b848e032SBruce Evans 	 */
470038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4701b848e032SBruce Evans 
4702f584dfd1SPyun YongHyeon 	/*
4703f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4704f584dfd1SPyun YongHyeon 	 */
4705f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4706f584dfd1SPyun YongHyeon 
4707f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4708f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4709f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4710f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4711f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4712f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4713f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4714f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4715f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4716f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4717f584dfd1SPyun YongHyeon 
47181f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
47194c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4720f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4721dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
472295d67482SBill Paul 
4723fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47243f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4725dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
472625e13e68SXin LI 	}
472795d67482SBill Paul 
4728fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47293f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4730b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
473195d67482SBill Paul 	}
473295d67482SBill Paul 
4733fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
4734fba8b109SMarcel Moolenaar 	    !if_sendq_empty(ifp))
47350f9bd73bSSam Leffler 		bge_start_locked(ifp);
47360f9bd73bSSam Leffler 
47370f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
473895d67482SBill Paul }
473995d67482SBill Paul 
474095d67482SBill Paul static void
47418cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
47428cb1383cSDoug Ambrisko {
47438cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
47448cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
47458cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
47468cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
47478cb1383cSDoug Ambrisko 		else {
4748899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4749888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
47503c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4751888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4752941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4753941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
47543fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
47559931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
47569931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
47578cb1383cSDoug Ambrisko 		}
47588cb1383cSDoug Ambrisko 	}
47598cb1383cSDoug Ambrisko }
47608cb1383cSDoug Ambrisko 
47618cb1383cSDoug Ambrisko static void
4762b74e67fbSGleb Smirnoff bge_tick(void *xsc)
47630f9bd73bSSam Leffler {
4764b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
476595d67482SBill Paul 	struct mii_data *mii = NULL;
476695d67482SBill Paul 
47670f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
476895d67482SBill Paul 
47695dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
47705dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
47715dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
47725dda8085SOleg Bulyzhin 		return;
47735dda8085SOleg Bulyzhin 
47747ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
47750434d1b8SBill Paul 		bge_stats_update_regs(sc);
47760434d1b8SBill Paul 	else
477795d67482SBill Paul 		bge_stats_update(sc);
477895d67482SBill Paul 
4779548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4780548c8f1aSPyun YongHyeon 
4781652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
478295d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
478382b67c01SOleg Bulyzhin 		/*
478482b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
478582b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
478682b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
478782b67c01SOleg Bulyzhin 		 */
478882b67c01SOleg Bulyzhin 		if (!sc->bge_link)
478995d67482SBill Paul 			mii_tick(mii);
47907b97099dSOleg Bulyzhin 	} else {
47917b97099dSOleg Bulyzhin 		/*
47927b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
47937b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
47947b97099dSOleg Bulyzhin 		 * and trigger interrupt.
47957b97099dSOleg Bulyzhin 		 */
47967b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
47973f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
479859578ee0SSergey Kandaurov 		if (!(if_getcapenable(sc->bge_ifp) & IFCAP_POLLING))
47997b97099dSOleg Bulyzhin #endif
48007b97099dSOleg Bulyzhin 		{
48017b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
48024f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
48034f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
48047b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
48054f0794ffSBjoern A. Zeeb 		else
48064f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
48077b97099dSOleg Bulyzhin 		}
4808dab5cd05SOleg Bulyzhin 	}
480995d67482SBill Paul 
48108cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4811b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
48128cb1383cSDoug Ambrisko 
4813dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
481495d67482SBill Paul }
481595d67482SBill Paul 
481695d67482SBill Paul static void
48173f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
48180434d1b8SBill Paul {
4819fba8b109SMarcel Moolenaar 	if_t ifp;
48202280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
482129b44b09SPyun YongHyeon 	uint32_t val;
48220434d1b8SBill Paul 
4823fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
48242280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
48250434d1b8SBill Paul 
48262280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
48272280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48282280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
48292280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48302280c16bSPyun YongHyeon 	stats->outXonSent +=
48312280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48322280c16bSPyun YongHyeon 	stats->outXoffSent +=
48332280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48342280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
48352280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48362280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
48372280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
48382280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
48392280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
48402280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
48412280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
48422280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
48432280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
48442280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
48452280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
48462280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
48472280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
48482280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
48492280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
48502280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
48512280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48527e6e2507SJung-uk Kim 
48532280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
48542280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48552280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
48562280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48572280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
48582280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48592280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
48602280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48612280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
48622280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48632280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
48642280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48652280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
48662280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48672280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
48682280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48692280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
48702280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48712280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
48722280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
48732280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
48742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
48752280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
48762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
48772280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
48782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48792280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
48802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48812280c16bSPyun YongHyeon 
48822280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
48832280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48842280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
48852280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48862280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
48872280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48882280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
48892280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4890f78094a5SPyun YongHyeon 	/*
4891f78094a5SPyun YongHyeon 	 * XXX
4892f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4893f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4894f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4895f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4896f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4897f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4898f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4899f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4900f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4901f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4902f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4903f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4904f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4905f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4906f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4907f78094a5SPyun YongHyeon 	 * silicon bug.
4908f78094a5SPyun YongHyeon 	 */
4909f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4910f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4911f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
49122280c16bSPyun YongHyeon 		stats->InputDiscards +=
49132280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49142280c16bSPyun YongHyeon 	stats->InputErrors +=
49152280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49162280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
49172280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49182280c16bSPyun YongHyeon 
491929b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
492029b44b09SPyun YongHyeon 		/*
492129b44b09SPyun YongHyeon 		 * If controller transmitted more than BGE_NUM_RDMA_CHANNELS
492229b44b09SPyun YongHyeon 		 * frames, it's safe to disable workaround for DMA engine's
492329b44b09SPyun YongHyeon 		 * miscalculation of TXMBUF space.
492429b44b09SPyun YongHyeon 		 */
492529b44b09SPyun YongHyeon 		if (stats->ifHCOutUcastPkts + stats->ifHCOutMulticastPkts +
492629b44b09SPyun YongHyeon 		    stats->ifHCOutBroadcastPkts > BGE_NUM_RDMA_CHANNELS) {
492729b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
492829b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
492929b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5719;
493029b44b09SPyun YongHyeon 			else
493129b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5720;
493229b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
493329b44b09SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_RDMA_BUG;
493429b44b09SPyun YongHyeon 		}
493529b44b09SPyun YongHyeon 	}
49362280c16bSPyun YongHyeon }
49372280c16bSPyun YongHyeon 
49382280c16bSPyun YongHyeon static void
49392280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
49402280c16bSPyun YongHyeon {
49412280c16bSPyun YongHyeon 
49422280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
49432280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
49442280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
49452280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
49462280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
49472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
49482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
49492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
49502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
49512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
49522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
49532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
49542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
49552280c16bSPyun YongHyeon 
49562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
49572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
49582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
49592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
49602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
49612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
49622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
49632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
49642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
49652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
49662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
49672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
49682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
49692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
49702280c16bSPyun YongHyeon 
49712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
49722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
49732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
49742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
49752280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49762280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49772280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49780434d1b8SBill Paul }
49790434d1b8SBill Paul 
49800434d1b8SBill Paul static void
49813f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
498295d67482SBill Paul {
4983fba8b109SMarcel Moolenaar 	if_t ifp;
4984e907febfSPyun YongHyeon 	bus_size_t stats;
49857e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
498695d67482SBill Paul 
4987fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
498895d67482SBill Paul 
4989e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4990e907febfSPyun YongHyeon 
4991e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4992e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
499395d67482SBill Paul 
49948634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
4995df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cnt - sc->bge_tx_collisions);
49966fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
49976fb34dd2SOleg Bulyzhin 
499837ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
4999df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_nobds);
500037ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
500137ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
5002df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_inerrs);
500337ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
50046fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
5005df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_discards);
50066fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
50076fb34dd2SOleg Bulyzhin 
50086fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
5009df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, cnt - sc->bge_tx_discards);
50106fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
501195d67482SBill Paul 
5012e907febfSPyun YongHyeon #undef	READ_STAT
501395d67482SBill Paul }
501495d67482SBill Paul 
501595d67482SBill Paul /*
5016d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
5017d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
5018d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
5019d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
5020d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
5021d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
5022d375e524SGleb Smirnoff  */
5023d375e524SGleb Smirnoff static __inline int
5024d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
5025d375e524SGleb Smirnoff {
5026d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
5027d375e524SGleb Smirnoff 	struct mbuf *last;
5028d375e524SGleb Smirnoff 
5029d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
5030d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
5031d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
5032d375e524SGleb Smirnoff 		last = m;
5033d375e524SGleb Smirnoff 	} else {
5034d375e524SGleb Smirnoff 		/*
5035d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
5036d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
5037d375e524SGleb Smirnoff 		 */
5038d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
5039d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
5040d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
5041d375e524SGleb Smirnoff 			struct mbuf *n;
5042d375e524SGleb Smirnoff 
5043c6499eccSGleb Smirnoff 			MGET(n, M_NOWAIT, MT_DATA);
5044d375e524SGleb Smirnoff 			if (n == NULL)
5045d375e524SGleb Smirnoff 				return (ENOBUFS);
5046d375e524SGleb Smirnoff 			n->m_len = 0;
5047d375e524SGleb Smirnoff 			last->m_next = n;
5048d375e524SGleb Smirnoff 			last = n;
5049d375e524SGleb Smirnoff 		}
5050d375e524SGleb Smirnoff 	}
5051d375e524SGleb Smirnoff 
5052d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
5053d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
5054d375e524SGleb Smirnoff 	last->m_len += padlen;
5055d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
5056d375e524SGleb Smirnoff 
5057d375e524SGleb Smirnoff 	return (0);
5058d375e524SGleb Smirnoff }
5059d375e524SGleb Smirnoff 
5060ca3f1187SPyun YongHyeon static struct mbuf *
5061d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
5062d598b626SPyun YongHyeon {
5063d598b626SPyun YongHyeon 	struct mbuf *n;
5064d598b626SPyun YongHyeon 	int found;
5065d598b626SPyun YongHyeon 
5066d598b626SPyun YongHyeon 	/*
5067d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
5068d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
5069d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
5070d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
5071d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
5072d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
5073d598b626SPyun YongHyeon 	 */
5074d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
5075d598b626SPyun YongHyeon 		if (n->m_len < 8) {
5076d598b626SPyun YongHyeon 			found++;
5077d598b626SPyun YongHyeon 			if (found > 1)
5078d598b626SPyun YongHyeon 				break;
5079d598b626SPyun YongHyeon 			continue;
5080d598b626SPyun YongHyeon 		}
5081d598b626SPyun YongHyeon 		found = 0;
5082d598b626SPyun YongHyeon 	}
5083d598b626SPyun YongHyeon 
5084d598b626SPyun YongHyeon 	if (found > 1) {
5085c6499eccSGleb Smirnoff 		n = m_defrag(m, M_NOWAIT);
5086d598b626SPyun YongHyeon 		if (n == NULL)
5087d598b626SPyun YongHyeon 			m_freem(m);
5088d598b626SPyun YongHyeon 	} else
5089d598b626SPyun YongHyeon 		n = m;
5090d598b626SPyun YongHyeon 	return (n);
5091d598b626SPyun YongHyeon }
5092d598b626SPyun YongHyeon 
5093d598b626SPyun YongHyeon static struct mbuf *
50941108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
50951108273aSPyun YongHyeon     uint16_t *flags)
5096ca3f1187SPyun YongHyeon {
5097ca3f1187SPyun YongHyeon 	struct ip *ip;
5098ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
5099ca3f1187SPyun YongHyeon 	struct mbuf *n;
5100ca3f1187SPyun YongHyeon 	uint16_t hlen;
51015b355c4fSPyun YongHyeon 	uint32_t poff;
5102ca3f1187SPyun YongHyeon 
5103ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5104ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5105c6499eccSGleb Smirnoff 		n = m_dup(m, M_NOWAIT);
5106ca3f1187SPyun YongHyeon 		m_freem(m);
5107ca3f1187SPyun YongHyeon 		if (n == NULL)
5108ca3f1187SPyun YongHyeon 			return (NULL);
5109ca3f1187SPyun YongHyeon 		m = n;
5110ca3f1187SPyun YongHyeon 	}
51115b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5112ca3f1187SPyun YongHyeon 	if (m == NULL)
5113ca3f1187SPyun YongHyeon 		return (NULL);
51145b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
51155b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5116ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5117ca3f1187SPyun YongHyeon 	if (m == NULL)
5118ca3f1187SPyun YongHyeon 		return (NULL);
5119ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
51205b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5121ca3f1187SPyun YongHyeon 	if (m == NULL)
5122ca3f1187SPyun YongHyeon 		return (NULL);
5123ca3f1187SPyun YongHyeon 	/*
5124ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5125ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5126ca3f1187SPyun YongHyeon 	 */
5127ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
512896486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5129ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5130ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5131ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
513296486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5133ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5134ca3f1187SPyun YongHyeon 	/*
5135ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5136ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5137ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
51381108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5139ca3f1187SPyun YongHyeon 	 */
51401108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5141ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
51421108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
51431108273aSPyun YongHyeon 		/*
51441108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
51451108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
51461108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
51471108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
51481108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
51491108273aSPyun YongHyeon 		 * frames are supported.
51501108273aSPyun YongHyeon 		 */
51511108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
51521108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
51531108273aSPyun YongHyeon 	} else {
51541108273aSPyun YongHyeon 		/*
51551108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
51561108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
51571108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
51581108273aSPyun YongHyeon 		 * supported.
51591108273aSPyun YongHyeon 		 */
5160ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
51611108273aSPyun YongHyeon 	}
5162ca3f1187SPyun YongHyeon 	return (m);
5163ca3f1187SPyun YongHyeon }
5164ca3f1187SPyun YongHyeon 
5165d375e524SGleb Smirnoff /*
516695d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
516795d67482SBill Paul  * pointers to descriptors.
516895d67482SBill Paul  */
516995d67482SBill Paul static int
5170676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
517195d67482SBill Paul {
51727e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5173f41ac2beSBill Paul 	bus_dmamap_t		map;
5174676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5175676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
51767e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5177ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
51787e27542aSGleb Smirnoff 	int			nsegs, i, error;
517995d67482SBill Paul 
51806909dc43SGleb Smirnoff 	csum_flags = 0;
5181ca3f1187SPyun YongHyeon 	mss = 0;
5182ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5183d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5184d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5185d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5186d598b626SPyun YongHyeon 		if (*m_head == NULL)
5187d598b626SPyun YongHyeon 			return (ENOBUFS);
5188d598b626SPyun YongHyeon 		m = *m_head;
5189d598b626SPyun YongHyeon 	}
5190ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
51911108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5192ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5193ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5194ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5195ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
519635f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
51976909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
51986909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
51996909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
52006909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
52016909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
52026909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
52036909dc43SGleb Smirnoff 				m_freem(m);
52046909dc43SGleb Smirnoff 				*m_head = NULL;
52056909dc43SGleb Smirnoff 				return (error);
52066909dc43SGleb Smirnoff 			}
52076909dc43SGleb Smirnoff 		}
52086909dc43SGleb Smirnoff 	}
52096909dc43SGleb Smirnoff 
52101108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
52111108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
52121108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
52131108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
52141108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5215beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5216d94f2b85SPyun YongHyeon 			/*
5217d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5218d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5219d94f2b85SPyun YongHyeon 			 * DMA read operation.
5220d94f2b85SPyun YongHyeon 			 */
5221beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5222c6499eccSGleb Smirnoff 				m = m_defrag(m, M_NOWAIT);
5223d94f2b85SPyun YongHyeon 			else
5224c6499eccSGleb Smirnoff 				m = m_collapse(m, M_NOWAIT,
52251108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5226261f04d6SPyun YongHyeon 			if (m == NULL)
5227261f04d6SPyun YongHyeon 				m = *m_head;
5228d94f2b85SPyun YongHyeon 			*m_head = m;
5229d94f2b85SPyun YongHyeon 		}
52301108273aSPyun YongHyeon 	}
5231d94f2b85SPyun YongHyeon 
52327e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
52330ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5234676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
52357e27542aSGleb Smirnoff 	if (error == EFBIG) {
5236c6499eccSGleb Smirnoff 		m = m_collapse(m, M_NOWAIT, BGE_NSEG_NEW);
5237676ad2c9SGleb Smirnoff 		if (m == NULL) {
5238676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5239676ad2c9SGleb Smirnoff 			*m_head = NULL;
52407e27542aSGleb Smirnoff 			return (ENOBUFS);
52417e27542aSGleb Smirnoff 		}
5242676ad2c9SGleb Smirnoff 		*m_head = m;
52430ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
52440ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5245676ad2c9SGleb Smirnoff 		if (error) {
5246676ad2c9SGleb Smirnoff 			m_freem(m);
5247676ad2c9SGleb Smirnoff 			*m_head = NULL;
52487e27542aSGleb Smirnoff 			return (error);
52497e27542aSGleb Smirnoff 		}
5250676ad2c9SGleb Smirnoff 	} else if (error != 0)
5251676ad2c9SGleb Smirnoff 		return (error);
52527e27542aSGleb Smirnoff 
5253167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5254167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
52550ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
525695d67482SBill Paul 		return (ENOBUFS);
52577e27542aSGleb Smirnoff 	}
52587e27542aSGleb Smirnoff 
52590ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5260e65bed95SPyun YongHyeon 
5261ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5262ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5263ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5264ca3f1187SPyun YongHyeon 	}
5265b77d3a3bSPyun YongHyeon 
5266b77d3a3bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762 &&
5267b77d3a3bSPyun YongHyeon 	    (m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
5268b77d3a3bSPyun YongHyeon 		/*
5269b77d3a3bSPyun YongHyeon 		 * 5725 family of devices corrupts TSO packets when TSO DMA
5270b77d3a3bSPyun YongHyeon 		 * buffers cross into regions which are within MSS bytes of
5271b77d3a3bSPyun YongHyeon 		 * a 4GB boundary.  If we encounter the condition, drop the
5272b77d3a3bSPyun YongHyeon 		 * packet.
5273b77d3a3bSPyun YongHyeon 		 */
5274b77d3a3bSPyun YongHyeon 		for (i = 0; ; i++) {
5275b77d3a3bSPyun YongHyeon 			d = &sc->bge_ldata.bge_tx_ring[idx];
5276b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
5277b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
5278b77d3a3bSPyun YongHyeon 			d->bge_len = segs[i].ds_len;
5279b77d3a3bSPyun YongHyeon 			if (d->bge_addr.bge_addr_lo + segs[i].ds_len + mss <
5280b77d3a3bSPyun YongHyeon 			    d->bge_addr.bge_addr_lo)
5281b77d3a3bSPyun YongHyeon 				break;
5282b77d3a3bSPyun YongHyeon 			d->bge_flags = csum_flags;
5283b77d3a3bSPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5284b77d3a3bSPyun YongHyeon 			d->bge_mss = mss;
5285b77d3a3bSPyun YongHyeon 			if (i == nsegs - 1)
5286b77d3a3bSPyun YongHyeon 				break;
5287b77d3a3bSPyun YongHyeon 			BGE_INC(idx, BGE_TX_RING_CNT);
5288b77d3a3bSPyun YongHyeon 		}
5289b77d3a3bSPyun YongHyeon 		if (i != nsegs - 1) {
5290b77d3a3bSPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map,
5291b77d3a3bSPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
5292b77d3a3bSPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
5293b77d3a3bSPyun YongHyeon 			m_freem(*m_head);
5294b77d3a3bSPyun YongHyeon 			*m_head = NULL;
5295b77d3a3bSPyun YongHyeon 			return (EIO);
5296b77d3a3bSPyun YongHyeon 		}
5297b77d3a3bSPyun YongHyeon 	} else {
52987e27542aSGleb Smirnoff 		for (i = 0; ; i++) {
52997e27542aSGleb Smirnoff 			d = &sc->bge_ldata.bge_tx_ring[idx];
53007e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
53017e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
53027e27542aSGleb Smirnoff 			d->bge_len = segs[i].ds_len;
53037e27542aSGleb Smirnoff 			d->bge_flags = csum_flags;
5304ca3f1187SPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5305ca3f1187SPyun YongHyeon 			d->bge_mss = mss;
53067e27542aSGleb Smirnoff 			if (i == nsegs - 1)
53077e27542aSGleb Smirnoff 				break;
53087e27542aSGleb Smirnoff 			BGE_INC(idx, BGE_TX_RING_CNT);
53097e27542aSGleb Smirnoff 		}
5310b77d3a3bSPyun YongHyeon 	}
53117e27542aSGleb Smirnoff 
53127e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
53137e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5314676ad2c9SGleb Smirnoff 
5315f41ac2beSBill Paul 	/*
5316f41ac2beSBill Paul 	 * Insure that the map for this transmission
5317f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5318f41ac2beSBill Paul 	 * in this chain.
5319f41ac2beSBill Paul 	 */
53207e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
53217e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5322676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
53237e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
532495d67482SBill Paul 
53257e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
53267e27542aSGleb Smirnoff 	*txidx = idx;
532795d67482SBill Paul 
532895d67482SBill Paul 	return (0);
532995d67482SBill Paul }
533095d67482SBill Paul 
533195d67482SBill Paul /*
533295d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
533395d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
533495d67482SBill Paul  */
533595d67482SBill Paul static void
5336fba8b109SMarcel Moolenaar bge_start_locked(if_t ifp)
533795d67482SBill Paul {
533895d67482SBill Paul 	struct bge_softc *sc;
5339167fdb62SPyun YongHyeon 	struct mbuf *m_head;
534014bbd30fSGleb Smirnoff 	uint32_t prodidx;
5341167fdb62SPyun YongHyeon 	int count;
534295d67482SBill Paul 
5343fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
5344167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
534595d67482SBill Paul 
5346167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5347fba8b109SMarcel Moolenaar 	    (if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5348167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
534995d67482SBill Paul 		return;
535095d67482SBill Paul 
535114bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
535295d67482SBill Paul 
5353fba8b109SMarcel Moolenaar 	for (count = 0; !if_sendq_empty(ifp);) {
5354167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5355fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5356167fdb62SPyun YongHyeon 			break;
5357167fdb62SPyun YongHyeon 		}
5358fba8b109SMarcel Moolenaar 		m_head = if_dequeue(ifp);
535995d67482SBill Paul 		if (m_head == NULL)
536095d67482SBill Paul 			break;
536195d67482SBill Paul 
536295d67482SBill Paul 		/*
536395d67482SBill Paul 		 * Pack the data into the transmit ring. If we
536495d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
536595d67482SBill Paul 		 * for the NIC to drain the ring.
536695d67482SBill Paul 		 */
5367676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5368676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5369676ad2c9SGleb Smirnoff 				break;
5370fba8b109SMarcel Moolenaar 			if_sendq_prepend(ifp, m_head);
5371fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
537295d67482SBill Paul 			break;
537395d67482SBill Paul 		}
5374303a718cSDag-Erling Smørgrav 		++count;
537595d67482SBill Paul 
537695d67482SBill Paul 		/*
537795d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
537895d67482SBill Paul 		 * to him.
537995d67482SBill Paul 		 */
5380fba8b109SMarcel Moolenaar 		if_bpfmtap(ifp, m_head);
538195d67482SBill Paul 	}
538295d67482SBill Paul 
5383167fdb62SPyun YongHyeon 	if (count > 0) {
5384aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
53855c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
53863f74909aSGleb Smirnoff 		/* Transmit. */
538738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
53883927098fSPaul Saab 		/* 5700 b2 errata */
5389e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
539038cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
539195d67482SBill Paul 
539214bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
539314bbd30fSGleb Smirnoff 
539495d67482SBill Paul 		/*
539595d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
539695d67482SBill Paul 		 */
5397b584d2b3SPyun YongHyeon 		sc->bge_timer = BGE_TX_TIMEOUT;
539895d67482SBill Paul 	}
5399167fdb62SPyun YongHyeon }
540095d67482SBill Paul 
54010f9bd73bSSam Leffler /*
54020f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
54030f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
54040f9bd73bSSam Leffler  */
540595d67482SBill Paul static void
5406fba8b109SMarcel Moolenaar bge_start(if_t ifp)
540795d67482SBill Paul {
54080f9bd73bSSam Leffler 	struct bge_softc *sc;
54090f9bd73bSSam Leffler 
5410fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
54110f9bd73bSSam Leffler 	BGE_LOCK(sc);
54120f9bd73bSSam Leffler 	bge_start_locked(ifp);
54130f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
54140f9bd73bSSam Leffler }
54150f9bd73bSSam Leffler 
54160f9bd73bSSam Leffler static void
54173f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
54180f9bd73bSSam Leffler {
5419fba8b109SMarcel Moolenaar 	if_t ifp;
54203f74909aSGleb Smirnoff 	uint16_t *m;
5421f6a65488SPyun YongHyeon 	uint32_t mode;
542295d67482SBill Paul 
54230f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
542495d67482SBill Paul 
5425fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
542695d67482SBill Paul 
5427fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
542895d67482SBill Paul 		return;
542995d67482SBill Paul 
543095d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
543195d67482SBill Paul 	bge_stop(sc);
54328cb1383cSDoug Ambrisko 
54338cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
54348cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
543595d67482SBill Paul 	bge_reset(sc);
54368cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
54378cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
54388cb1383cSDoug Ambrisko 
543995d67482SBill Paul 	bge_chipinit(sc);
544095d67482SBill Paul 
544195d67482SBill Paul 	/*
544295d67482SBill Paul 	 * Init the various state machines, ring
544395d67482SBill Paul 	 * control blocks and firmware.
544495d67482SBill Paul 	 */
544595d67482SBill Paul 	if (bge_blockinit(sc)) {
5446fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
544795d67482SBill Paul 		return;
544895d67482SBill Paul 	}
544995d67482SBill Paul 
5450fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
545195d67482SBill Paul 
545295d67482SBill Paul 	/* Specify MTU. */
5453fba8b109SMarcel Moolenaar 	CSR_WRITE_4(sc, BGE_RX_MTU, if_getmtu(ifp) +
5454cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5455fba8b109SMarcel Moolenaar 	    (if_getcapenable(ifp) & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
545695d67482SBill Paul 
545795d67482SBill Paul 	/* Load our MAC address. */
54583f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
545995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
546095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
546195d67482SBill Paul 
54623e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
54633e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
546495d67482SBill Paul 
546595d67482SBill Paul 	/* Program multicast filter. */
546695d67482SBill Paul 	bge_setmulti(sc);
546795d67482SBill Paul 
5468cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5469cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5470cb2eacc7SYaroslav Tykhiy 
547135f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
547235f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
547335f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
547435f945cdSPyun YongHyeon 	else
547535f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
5476fba8b109SMarcel Moolenaar 	if (if_getcapabilities(ifp) & IFCAP_TXCSUM &&
5477fba8b109SMarcel Moolenaar 	    if_getcapenable(ifp) & IFCAP_TXCSUM) {
5478fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, 0, (BGE_CSUM_FEATURES | CSUM_UDP));
5479fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, sc->bge_csum_features, 0);
548035f945cdSPyun YongHyeon 	}
548135f945cdSPyun YongHyeon 
548295d67482SBill Paul 	/* Init RX ring. */
54833ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
54843ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
54853ee5d7daSPyun YongHyeon 		bge_stop(sc);
54863ee5d7daSPyun YongHyeon 		return;
54873ee5d7daSPyun YongHyeon 	}
548895d67482SBill Paul 
54890434d1b8SBill Paul 	/*
54900434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
54910434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
54920434d1b8SBill Paul 	 * entry of the ring.
54930434d1b8SBill Paul 	 */
54940434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
54953f74909aSGleb Smirnoff 		uint32_t		v, i;
54960434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
54970434d1b8SBill Paul 			DELAY(20);
54980434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
54990434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
55000434d1b8SBill Paul 				break;
55010434d1b8SBill Paul 		}
55020434d1b8SBill Paul 		if (i == 10)
5503fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5504fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
55050434d1b8SBill Paul 	}
55060434d1b8SBill Paul 
550795d67482SBill Paul 	/* Init jumbo RX ring. */
5508f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5509fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
5510fba8b109SMarcel Moolenaar      	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN)) {
55113ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5512333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5513b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
55143ee5d7daSPyun YongHyeon 			bge_stop(sc);
55153ee5d7daSPyun YongHyeon 			return;
55163ee5d7daSPyun YongHyeon 		}
55173ee5d7daSPyun YongHyeon 	}
551895d67482SBill Paul 
55193f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
552095d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
552195d67482SBill Paul 
55227e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
55237e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
55247e6e2507SJung-uk Kim 
552595d67482SBill Paul 	/* Init TX ring. */
552695d67482SBill Paul 	bge_init_tx_ring(sc);
552795d67482SBill Paul 
5528f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5529f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5530f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5531f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
55322927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
55332927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
553450515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
553550515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
553650515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
553750515680SPyun YongHyeon 	}
55383f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5539f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5540a6e66cd2SPyun YongHyeon 	DELAY(100);
554195d67482SBill Paul 
55423f74909aSGleb Smirnoff 	/* Turn on receiver. */
5543548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5544548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5545548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
554669b1f509SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
554769b1f509SPyun YongHyeon 		mode |= BGE_RXMODE_IPV4_FRAG_FIX;
5548548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5549a6e66cd2SPyun YongHyeon 	DELAY(10);
555095d67482SBill Paul 
5551dedcdf57SPyun YongHyeon 	/*
5552dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5553dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5554dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5555dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5556dedcdf57SPyun YongHyeon 	 */
55573fc5fbfbSPyun YongHyeon 	if (BGE_IS_57765_PLUS(sc))
5558b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5559b4a256acSPyun YongHyeon 	else
5560dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5561dedcdf57SPyun YongHyeon 
55622280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
55632280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
55642280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
55652280c16bSPyun YongHyeon 
556695d67482SBill Paul 	/* Tell firmware we're alive. */
556795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
556895d67482SBill Paul 
556975719184SGleb Smirnoff #ifdef DEVICE_POLLING
557075719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
5571fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
557275719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
557375719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
557438cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
557575719184SGleb Smirnoff 	} else
557675719184SGleb Smirnoff #endif
557775719184SGleb Smirnoff 
557895d67482SBill Paul 	/* Enable host interrupts. */
557975719184SGleb Smirnoff 	{
558095d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
558195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
558238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
558375719184SGleb Smirnoff 	}
558495d67482SBill Paul 
5585fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
5586fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
558795d67482SBill Paul 
5588e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5589e4146b95SPyun YongHyeon 
55900f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
55910f9bd73bSSam Leffler }
55920f9bd73bSSam Leffler 
55930f9bd73bSSam Leffler static void
55943f74909aSGleb Smirnoff bge_init(void *xsc)
55950f9bd73bSSam Leffler {
55960f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
55970f9bd73bSSam Leffler 
55980f9bd73bSSam Leffler 	BGE_LOCK(sc);
55990f9bd73bSSam Leffler 	bge_init_locked(sc);
56000f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
560195d67482SBill Paul }
560295d67482SBill Paul 
560395d67482SBill Paul /*
560495d67482SBill Paul  * Set media options.
560595d67482SBill Paul  */
560695d67482SBill Paul static int
5607fba8b109SMarcel Moolenaar bge_ifmedia_upd(if_t ifp)
560895d67482SBill Paul {
5609fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
561067d5e043SOleg Bulyzhin 	int res;
561167d5e043SOleg Bulyzhin 
561267d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
561367d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
561467d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
561567d5e043SOleg Bulyzhin 
561667d5e043SOleg Bulyzhin 	return (res);
561767d5e043SOleg Bulyzhin }
561867d5e043SOleg Bulyzhin 
561967d5e043SOleg Bulyzhin static int
5620fba8b109SMarcel Moolenaar bge_ifmedia_upd_locked(if_t ifp)
562167d5e043SOleg Bulyzhin {
5622fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
562395d67482SBill Paul 	struct mii_data *mii;
56244f09c4c7SMarius Strobl 	struct mii_softc *miisc;
562595d67482SBill Paul 	struct ifmedia *ifm;
562695d67482SBill Paul 
562767d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
562867d5e043SOleg Bulyzhin 
562995d67482SBill Paul 	ifm = &sc->bge_ifmedia;
563095d67482SBill Paul 
563195d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5632652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
563395d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
563495d67482SBill Paul 			return (EINVAL);
563595d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
563695d67482SBill Paul 		case IFM_AUTO:
5637ff50922bSDoug White 			/*
5638ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5639ff50922bSDoug White 			 * mechanism for programming the autoneg
5640ff50922bSDoug White 			 * advertisement registers in TBI mode.
5641ff50922bSDoug White 			 */
56420f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5643ff50922bSDoug White 				uint32_t sgdig;
56440f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
56450f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5646ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5647ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5648ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5649ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5650ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5651ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5652ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5653ff50922bSDoug White 					DELAY(5);
5654ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5655ff50922bSDoug White 				}
56560f89fde2SJung-uk Kim 			}
565795d67482SBill Paul 			break;
565895d67482SBill Paul 		case IFM_1000_SX:
565995d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
566095d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
566195d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
566295d67482SBill Paul 			} else {
566395d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
566495d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
566595d67482SBill Paul 			}
56669b80ffe7SPyun YongHyeon 			DELAY(40);
566795d67482SBill Paul 			break;
566895d67482SBill Paul 		default:
566995d67482SBill Paul 			return (EINVAL);
567095d67482SBill Paul 		}
567195d67482SBill Paul 		return (0);
567295d67482SBill Paul 	}
567395d67482SBill Paul 
56741493e883SOleg Bulyzhin 	sc->bge_link_evt++;
567595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
56764f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
56773fcb7a53SMarius Strobl 		PHY_RESET(miisc);
567895d67482SBill Paul 	mii_mediachg(mii);
567995d67482SBill Paul 
5680902827f6SBjoern A. Zeeb 	/*
5681902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5682902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5683902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5684902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5685902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5686902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5687902827f6SBjoern A. Zeeb 	 * get an RX intr.
5688902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5689902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5690902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5691902827f6SBjoern A. Zeeb 	 */
56924f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
56934f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5694902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
56954f0794ffSBjoern A. Zeeb 	else
569663ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5697902827f6SBjoern A. Zeeb 
569895d67482SBill Paul 	return (0);
569995d67482SBill Paul }
570095d67482SBill Paul 
570195d67482SBill Paul /*
570295d67482SBill Paul  * Report current media status.
570395d67482SBill Paul  */
570495d67482SBill Paul static void
5705fba8b109SMarcel Moolenaar bge_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
570695d67482SBill Paul {
5707fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
570895d67482SBill Paul 	struct mii_data *mii;
570995d67482SBill Paul 
571067d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
571195d67482SBill Paul 
5712fba8b109SMarcel Moolenaar 	if ((if_getflags(ifp) & IFF_UP) == 0) {
5713b9d2edd7SPyun YongHyeon 		BGE_UNLOCK(sc);
5714b9d2edd7SPyun YongHyeon 		return;
5715b9d2edd7SPyun YongHyeon 	}
5716652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
571795d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
571895d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
571995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
572095d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
572195d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
57224c0da0ffSGleb Smirnoff 		else {
57234c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
572467d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
57254c0da0ffSGleb Smirnoff 			return;
57264c0da0ffSGleb Smirnoff 		}
572795d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
572895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
572995d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
573095d67482SBill Paul 		else
573195d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
573267d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
573395d67482SBill Paul 		return;
573495d67482SBill Paul 	}
573595d67482SBill Paul 
573695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
573795d67482SBill Paul 	mii_pollstat(mii);
573895d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
573995d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
574067d5e043SOleg Bulyzhin 
574167d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
574295d67482SBill Paul }
574395d67482SBill Paul 
574495d67482SBill Paul static int
5745fba8b109SMarcel Moolenaar bge_ioctl(if_t ifp, u_long command, caddr_t data)
574695d67482SBill Paul {
5747fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
574895d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
574995d67482SBill Paul 	struct mii_data *mii;
5750f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
575195d67482SBill Paul 
575295d67482SBill Paul 	switch (command) {
575395d67482SBill Paul 	case SIOCSIFMTU:
5754f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5755f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
57564c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5757f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
575895d67482SBill Paul 				error = EINVAL;
5759f5459d4cSPyun YongHyeon 				break;
5760f5459d4cSPyun YongHyeon 			}
5761f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5762f5459d4cSPyun YongHyeon 			error = EINVAL;
5763f5459d4cSPyun YongHyeon 			break;
5764f5459d4cSPyun YongHyeon 		}
5765f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5766fba8b109SMarcel Moolenaar 		if (if_getmtu(ifp) != ifr->ifr_mtu) {
5767fba8b109SMarcel Moolenaar 			if_setmtu(ifp, ifr->ifr_mtu);
5768fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5769fba8b109SMarcel Moolenaar 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
57703a429c8fSPyun YongHyeon 				bge_init_locked(sc);
577195d67482SBill Paul 			}
57723a429c8fSPyun YongHyeon 		}
57733a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
577495d67482SBill Paul 		break;
577595d67482SBill Paul 	case SIOCSIFFLAGS:
57760f9bd73bSSam Leffler 		BGE_LOCK(sc);
5777fba8b109SMarcel Moolenaar 		if (if_getflags(ifp) & IFF_UP) {
577895d67482SBill Paul 			/*
577995d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
578095d67482SBill Paul 			 * then just use the 'set promisc mode' command
578195d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
578295d67482SBill Paul 			 * a full re-init means reloading the firmware and
578395d67482SBill Paul 			 * waiting for it to start up, which may take a
5784d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
578595d67482SBill Paul 			 */
5786fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5787fba8b109SMarcel Moolenaar 				flags = if_getflags(ifp) ^ sc->bge_if_flags;
57883e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
57893e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5790f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5791d183af7fSRuslan Ermilov 					bge_setmulti(sc);
579295d67482SBill Paul 			} else
57930f9bd73bSSam Leffler 				bge_init_locked(sc);
579495d67482SBill Paul 		} else {
5795fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
579695d67482SBill Paul 				bge_stop(sc);
579795d67482SBill Paul 			}
579895d67482SBill Paul 		}
5799fba8b109SMarcel Moolenaar 		sc->bge_if_flags = if_getflags(ifp);
58000f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
580195d67482SBill Paul 		error = 0;
580295d67482SBill Paul 		break;
580395d67482SBill Paul 	case SIOCADDMULTI:
580495d67482SBill Paul 	case SIOCDELMULTI:
5805fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
58060f9bd73bSSam Leffler 			BGE_LOCK(sc);
580795d67482SBill Paul 			bge_setmulti(sc);
58080f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
580995d67482SBill Paul 			error = 0;
581095d67482SBill Paul 		}
581195d67482SBill Paul 		break;
581295d67482SBill Paul 	case SIOCSIFMEDIA:
581395d67482SBill Paul 	case SIOCGIFMEDIA:
5814652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
581595d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
581695d67482SBill Paul 			    &sc->bge_ifmedia, command);
581795d67482SBill Paul 		} else {
581895d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
581995d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
582095d67482SBill Paul 			    &mii->mii_media, command);
582195d67482SBill Paul 		}
582295d67482SBill Paul 		break;
582395d67482SBill Paul 	case SIOCSIFCAP:
5824fba8b109SMarcel Moolenaar 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
582575719184SGleb Smirnoff #ifdef DEVICE_POLLING
582675719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
582775719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
5828bd071d4dSGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
582975719184SGleb Smirnoff 				if (error)
583075719184SGleb Smirnoff 					return (error);
583175719184SGleb Smirnoff 				BGE_LOCK(sc);
583275719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
583375719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
583438cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
5835fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, IFCAP_POLLING, 0);
583675719184SGleb Smirnoff 				BGE_UNLOCK(sc);
583775719184SGleb Smirnoff 			} else {
583875719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
583975719184SGleb Smirnoff 				/* Enable interrupt even in error case */
584075719184SGleb Smirnoff 				BGE_LOCK(sc);
584175719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
584275719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
584338cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
5844fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_POLLING);
584575719184SGleb Smirnoff 				BGE_UNLOCK(sc);
584675719184SGleb Smirnoff 			}
584775719184SGleb Smirnoff 		}
584875719184SGleb Smirnoff #endif
5849d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5850fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
5851fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TXCSUM);
5852fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
5853fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp,
5854fba8b109SMarcel Moolenaar 				    sc->bge_csum_features, 0);
585595d67482SBill Paul 			else
5856fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0,
5857fba8b109SMarcel Moolenaar 				    sc->bge_csum_features);
585895d67482SBill Paul 		}
5859cb2eacc7SYaroslav Tykhiy 
5860d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5861fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0)
5862fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_RXCSUM);
5863d8b57f98SPyun YongHyeon 
5864ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5865fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
5866fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TSO4);
5867fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0)
5868fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, CSUM_TSO, 0);
5869ca3f1187SPyun YongHyeon 			else
5870fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0, CSUM_TSO);
5871ca3f1187SPyun YongHyeon 		}
5872ca3f1187SPyun YongHyeon 
5873cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5874fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
5875fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5876cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5877cb2eacc7SYaroslav Tykhiy 		}
5878cb2eacc7SYaroslav Tykhiy 
587904bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
5880fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
5881fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
588204bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
5883fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
5884fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
5885fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
5886fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO);
5887cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5888cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5889cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
589004bde852SPyun YongHyeon 		}
5891cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5892fba8b109SMarcel Moolenaar 		if_vlancap(ifp);
5893cb2eacc7SYaroslav Tykhiy #endif
589495d67482SBill Paul 		break;
589595d67482SBill Paul 	default:
5896673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
589795d67482SBill Paul 		break;
589895d67482SBill Paul 	}
589995d67482SBill Paul 
590095d67482SBill Paul 	return (error);
590195d67482SBill Paul }
590295d67482SBill Paul 
590395d67482SBill Paul static void
5904b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
590595d67482SBill Paul {
5906fba8b109SMarcel Moolenaar 	if_t ifp;
5907b584d2b3SPyun YongHyeon 	uint32_t status;
590895d67482SBill Paul 
5909b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5910b74e67fbSGleb Smirnoff 
5911b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5912b74e67fbSGleb Smirnoff 		return;
5913b74e67fbSGleb Smirnoff 
5914b584d2b3SPyun YongHyeon 	/* If pause frames are active then don't reset the hardware. */
5915b584d2b3SPyun YongHyeon 	if ((CSR_READ_4(sc, BGE_RX_MODE) & BGE_RXMODE_FLOWCTL_ENABLE) != 0) {
5916b584d2b3SPyun YongHyeon 		status = CSR_READ_4(sc, BGE_RX_STS);
5917b584d2b3SPyun YongHyeon 		if ((status & BGE_RXSTAT_REMOTE_XOFFED) != 0) {
5918b584d2b3SPyun YongHyeon 			/*
5919b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5920b584d2b3SPyun YongHyeon 			 * the condition to clear.
5921b584d2b3SPyun YongHyeon 			 */
5922b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5923b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5924b584d2b3SPyun YongHyeon 			return;
5925b584d2b3SPyun YongHyeon 		} else if ((status & BGE_RXSTAT_RCVD_XOFF) != 0 &&
5926b584d2b3SPyun YongHyeon 		    (status & BGE_RXSTAT_RCVD_XON) != 0) {
5927b584d2b3SPyun YongHyeon 			/*
5928b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5929b584d2b3SPyun YongHyeon 			 * the condition to clear.
5930b584d2b3SPyun YongHyeon 			 */
5931b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5932b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5933b584d2b3SPyun YongHyeon 			return;
5934b584d2b3SPyun YongHyeon 		}
5935b584d2b3SPyun YongHyeon 		/*
5936b584d2b3SPyun YongHyeon 		 * Any other condition is unexpected and the controller
5937b584d2b3SPyun YongHyeon 		 * should be reset.
5938b584d2b3SPyun YongHyeon 		 */
5939b584d2b3SPyun YongHyeon 	}
5940b584d2b3SPyun YongHyeon 
5941b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
594295d67482SBill Paul 
5943fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
594495d67482SBill Paul 
5945fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5946426742bfSGleb Smirnoff 	bge_init_locked(sc);
594795d67482SBill Paul 
5948df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
594995d67482SBill Paul }
595095d67482SBill Paul 
59515a147ba6SPyun YongHyeon static void
59525a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
59535a147ba6SPyun YongHyeon {
59545a147ba6SPyun YongHyeon 	int i;
59555a147ba6SPyun YongHyeon 
59565a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
59575a147ba6SPyun YongHyeon 
59585a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
59595a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
59605a147ba6SPyun YongHyeon 			return;
59615a147ba6SPyun YongHyeon 		DELAY(100);
59625a147ba6SPyun YongHyeon         }
59635a147ba6SPyun YongHyeon }
59645a147ba6SPyun YongHyeon 
596595d67482SBill Paul /*
596695d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
596795d67482SBill Paul  * RX and TX lists.
596895d67482SBill Paul  */
596995d67482SBill Paul static void
59703f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
597195d67482SBill Paul {
5972fba8b109SMarcel Moolenaar 	if_t ifp;
597395d67482SBill Paul 
59740f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
59750f9bd73bSSam Leffler 
5976fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
597795d67482SBill Paul 
59780f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
597995d67482SBill Paul 
598044b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
598144b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
598244b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
598344b63691SBjoern A. Zeeb 
598444b63691SBjoern A. Zeeb 	/*
598544b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
598644b63691SBjoern A. Zeeb 	 */
598744b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
5988548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
598944b63691SBjoern A. Zeeb 
599095d67482SBill Paul 	/*
59913f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
599295d67482SBill Paul 	 */
59935a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
59945a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
59955a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
59965a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
59975a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
59985a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
59995a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
60005a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
600195d67482SBill Paul 
600295d67482SBill Paul 	/*
60033f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
600495d67482SBill Paul 	 */
60055a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
60065a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
60075a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
60085a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
60095a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
60105a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60115a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
60125a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
601395d67482SBill Paul 
601495d67482SBill Paul 	/*
601595d67482SBill Paul 	 * Shut down all of the memory managers and related
601695d67482SBill Paul 	 * state machines.
601795d67482SBill Paul 	 */
60185a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
60195a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
60205a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60215a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
60225a147ba6SPyun YongHyeon 
60230c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
602495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
60257ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
602695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
602795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
60280434d1b8SBill Paul 	}
60292280c16bSPyun YongHyeon 	/* Update MAC statistics. */
60302280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
60312280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
603295d67482SBill Paul 
60338cb1383cSDoug Ambrisko 	bge_reset(sc);
6034548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
6035548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
60368cb1383cSDoug Ambrisko 
60378cb1383cSDoug Ambrisko 	/*
60388cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
60398cb1383cSDoug Ambrisko 	 */
60408cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
60418cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
60428cb1383cSDoug Ambrisko 	else
604395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
604495d67482SBill Paul 
604595d67482SBill Paul 	/* Free the RX lists. */
604695d67482SBill Paul 	bge_free_rx_ring_std(sc);
604795d67482SBill Paul 
604895d67482SBill Paul 	/* Free jumbo RX list. */
60494c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
605095d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
605195d67482SBill Paul 
605295d67482SBill Paul 	/* Free TX buffers. */
605395d67482SBill Paul 	bge_free_tx_ring(sc);
605495d67482SBill Paul 
605595d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
605695d67482SBill Paul 
60575dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
60581493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
60591493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
60601493e883SOleg Bulyzhin 	sc->bge_link = 0;
606195d67482SBill Paul 
6062fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
606395d67482SBill Paul }
606495d67482SBill Paul 
606595d67482SBill Paul /*
606695d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
606795d67482SBill Paul  * get confused by errant DMAs when rebooting.
606895d67482SBill Paul  */
6069b6c974e8SWarner Losh static int
60703f74909aSGleb Smirnoff bge_shutdown(device_t dev)
607195d67482SBill Paul {
607295d67482SBill Paul 	struct bge_softc *sc;
607395d67482SBill Paul 
607495d67482SBill Paul 	sc = device_get_softc(dev);
60750f9bd73bSSam Leffler 	BGE_LOCK(sc);
607695d67482SBill Paul 	bge_stop(sc);
60770f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
6078b6c974e8SWarner Losh 
6079b6c974e8SWarner Losh 	return (0);
608095d67482SBill Paul }
608114afefa3SPawel Jakub Dawidek 
608214afefa3SPawel Jakub Dawidek static int
608314afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
608414afefa3SPawel Jakub Dawidek {
608514afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
608614afefa3SPawel Jakub Dawidek 
608714afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
608814afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
608914afefa3SPawel Jakub Dawidek 	bge_stop(sc);
609014afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
609114afefa3SPawel Jakub Dawidek 
609214afefa3SPawel Jakub Dawidek 	return (0);
609314afefa3SPawel Jakub Dawidek }
609414afefa3SPawel Jakub Dawidek 
609514afefa3SPawel Jakub Dawidek static int
609614afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
609714afefa3SPawel Jakub Dawidek {
609814afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
6099fba8b109SMarcel Moolenaar 	if_t ifp;
610014afefa3SPawel Jakub Dawidek 
610114afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
610214afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
610314afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
6104fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_UP) {
610514afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
6106fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
610714afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
610814afefa3SPawel Jakub Dawidek 	}
610914afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
611014afefa3SPawel Jakub Dawidek 
611114afefa3SPawel Jakub Dawidek 	return (0);
611214afefa3SPawel Jakub Dawidek }
6113dab5cd05SOleg Bulyzhin 
6114dab5cd05SOleg Bulyzhin static void
61153f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
6116dab5cd05SOleg Bulyzhin {
61171f313773SOleg Bulyzhin 	struct mii_data *mii;
61181f313773SOleg Bulyzhin 	uint32_t link, status;
6119dab5cd05SOleg Bulyzhin 
6120dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
61211f313773SOleg Bulyzhin 
61223f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
61237b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
61247b97099dSOleg Bulyzhin 
6125dab5cd05SOleg Bulyzhin 	/*
6126dab5cd05SOleg Bulyzhin 	 * Process link state changes.
6127dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
6128dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
6129dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
6130dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
6131dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
6132dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
6133dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
6134dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
61351f313773SOleg Bulyzhin 	 *
61361f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
61374c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
6138dab5cd05SOleg Bulyzhin 	 */
6139dab5cd05SOleg Bulyzhin 
61401f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
61414c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
6142dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
6143dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
61441f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
61455dda8085SOleg Bulyzhin 			mii_pollstat(mii);
61461f313773SOleg Bulyzhin 			if (!sc->bge_link &&
61471f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
61481f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
61491f313773SOleg Bulyzhin 				sc->bge_link++;
61501f313773SOleg Bulyzhin 				if (bootverbose)
61511f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61521f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
61531f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
61541f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
61551f313773SOleg Bulyzhin 				sc->bge_link = 0;
61561f313773SOleg Bulyzhin 				if (bootverbose)
61571f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
61581f313773SOleg Bulyzhin 			}
61591f313773SOleg Bulyzhin 
61603f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6161dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6162dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6163daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6164daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6165daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6166daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6167dab5cd05SOleg Bulyzhin 		}
6168dab5cd05SOleg Bulyzhin 		return;
6169dab5cd05SOleg Bulyzhin 	}
6170dab5cd05SOleg Bulyzhin 
6171652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
61721f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
61737b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
61747b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
61751f313773SOleg Bulyzhin 				sc->bge_link++;
61769b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
61771f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
61781f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
61799b80ffe7SPyun YongHyeon 					DELAY(40);
61809b80ffe7SPyun YongHyeon 				}
61810c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
61821f313773SOleg Bulyzhin 				if (bootverbose)
61831f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61843f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
61853f74909aSGleb Smirnoff 				    LINK_STATE_UP);
61867b97099dSOleg Bulyzhin 			}
61871f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6188dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
61891f313773SOleg Bulyzhin 			if (bootverbose)
61901f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
61917b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
61921f313773SOleg Bulyzhin 		}
61936ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
61941f313773SOleg Bulyzhin 		/*
61950c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
61960c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
61970c8aa4eaSJung-uk Kim 		 * PHY link status directly.
61981f313773SOleg Bulyzhin 		 */
61991f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
62001f313773SOleg Bulyzhin 
62011f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
62021f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
62031f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
62045dda8085SOleg Bulyzhin 			mii_pollstat(mii);
62051f313773SOleg Bulyzhin 			if (!sc->bge_link &&
62061f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
62071f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
62081f313773SOleg Bulyzhin 				sc->bge_link++;
62091f313773SOleg Bulyzhin 				if (bootverbose)
62101f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
62111f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
62121f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
62131f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
62141f313773SOleg Bulyzhin 				sc->bge_link = 0;
62151f313773SOleg Bulyzhin 				if (bootverbose)
62161f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
62171f313773SOleg Bulyzhin 			}
62181f313773SOleg Bulyzhin 		}
62190c8aa4eaSJung-uk Kim 	} else {
62200c8aa4eaSJung-uk Kim 		/*
62216ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
62226ede2cfaSPyun YongHyeon 		 * link status.
62230c8aa4eaSJung-uk Kim 		 */
62246ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
62256ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
62266ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6227dab5cd05SOleg Bulyzhin 	}
6228dab5cd05SOleg Bulyzhin 
62292246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6230dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6231dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6232dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6233dab5cd05SOleg Bulyzhin }
62346f8718a3SScott Long 
62356f8718a3SScott Long static void
62366f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
62376f8718a3SScott Long {
62386f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
62392280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
62407e32f79aSPyun YongHyeon 	int unit;
62416f8718a3SScott Long 
62426f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
62436f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
62446f8718a3SScott Long 
62456f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
62466f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
62476f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
62486f8718a3SScott Long 	    "Debug Information");
62496f8718a3SScott Long 
62506f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
62516f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
6252548c8f1aSPyun YongHyeon 	    "MAC Register Read");
6253548c8f1aSPyun YongHyeon 
6254548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
6255548c8f1aSPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_ape_read, "I",
6256548c8f1aSPyun YongHyeon 	    "APE Register Read");
62576f8718a3SScott Long 
62586f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
62596f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
62606f8718a3SScott Long 	    "Memory Read");
62616f8718a3SScott Long 
62626f8718a3SScott Long #endif
6263763757b2SScott Long 
62647e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
6265beaa2ae1SPyun YongHyeon 	/*
6266beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6267beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6268beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6269beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6270beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6271beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6272beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6273beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6274beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6275beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6276beaa2ae1SPyun YongHyeon 	 */
62777e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
6278beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6279af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_collapse, 0,
6280beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6281beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6282beaa2ae1SPyun YongHyeon 
62832ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
62842ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
6285af3b2549SHans Petter Selasky 	    CTLFLAG_RDTUN, &sc->bge_msi, 0, "Enable MSI");
62865c952e8dSPyun YongHyeon 
628735f945cdSPyun YongHyeon 	/*
628835f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
628935f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
629035f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
629135f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
629235f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
629335f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
629435f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
629535f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
629635f945cdSPyun YongHyeon 	 */
629735f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
629835f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
6299af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_udpcsum, 0,
630035f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
630135f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
630235f945cdSPyun YongHyeon 
6303d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
63042280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
63052280c16bSPyun YongHyeon 	else
63062280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
63072280c16bSPyun YongHyeon }
6308d949071dSJung-uk Kim 
63092280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
63102280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
63112280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
63122280c16bSPyun YongHyeon 	    desc)
63132280c16bSPyun YongHyeon 
63142280c16bSPyun YongHyeon static void
63152280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
63162280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
63172280c16bSPyun YongHyeon {
63182280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
63192280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
63202280c16bSPyun YongHyeon 
63212280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6322763757b2SScott Long 	    NULL, "BGE Statistics");
6323763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6324763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6325763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6326763757b2SScott Long 	    "FramesDroppedDueToFilters");
6327763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6328763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6329763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6330763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6331763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6332763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
633306e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
633406e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
633506e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
633606e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6337763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6338763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6339763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6340763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6341763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6342763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6343763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6344763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6345763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6346763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6347763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6348763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6349763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6350763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6351763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6352763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6353763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6354763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6355763757b2SScott Long 
6356763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
6357763757b2SScott Long 	    NULL, "BGE RX Statistics");
6358763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6359763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
63601cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6361763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6362763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6363763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
63641cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6365763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6366763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6367763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6368763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6369763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6370763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6371763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6372763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6373763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6374763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6375763757b2SScott Long 	    "xoffPauseFramesReceived");
6376763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6377763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6378763757b2SScott Long 	    "ControlFramesReceived");
6379763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6380763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6381763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6382763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6383763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6384763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6385763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6386763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6387763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
638806e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6389763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
639006e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6391763757b2SScott Long 
6392763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
6393763757b2SScott Long 	    NULL, "BGE TX Statistics");
6394763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6395763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
63961cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6397763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6398763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6399763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6400763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6401763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6402763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6403763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6404763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6405763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6406763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6407763757b2SScott Long 	    "InternalMacTransmitErrors");
6408763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6409763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6410763757b2SScott Long 	    "SingleCollisionFrames");
6411763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6412763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6413763757b2SScott Long 	    "MultipleCollisionFrames");
6414763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6415763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6416763757b2SScott Long 	    "DeferredTransmissions");
6417763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6418763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6419763757b2SScott Long 	    "ExcessiveCollisions");
6420763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
642106e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
642206e83c7eSScott Long 	    "LateCollisions");
6423763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
64241cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6425763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6426763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6427763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6428763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6429763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6430763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6431763757b2SScott Long 	    "CarrierSenseErrors");
6432763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6433763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6434763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6435763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6436763757b2SScott Long }
6437763757b2SScott Long 
64382280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
64392280c16bSPyun YongHyeon 
64402280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
64416dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
64422280c16bSPyun YongHyeon 
64432280c16bSPyun YongHyeon static void
64442280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
64452280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
64462280c16bSPyun YongHyeon {
64472280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
64482280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
64492280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
64502280c16bSPyun YongHyeon 
64512280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
64522280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
64532280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
64542280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
64552280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
64562280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
64572280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
64582280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
64592280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
64602280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
64612280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
64622280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
64632280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
64642280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
64652280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
64662280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
64672280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
64682280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
64692280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
64702280c16bSPyun YongHyeon 
64712280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
64722280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
64732280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
64742280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
64752280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
64762280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
64772280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
64781cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
64792280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
64802280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
64812280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
64822280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
64832280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
64842280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
64852280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
64862280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
64872280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
64882280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
64892280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
64902280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
64912280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
64922280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
64932280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
64942280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
64952280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
64962280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
64972280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
64982280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
64992280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
65002280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
65012280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
65022280c16bSPyun YongHyeon 
65032280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
65042280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
65052280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
65061cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
65072280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
65082280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
65092280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
65102280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
65112280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
65122280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
65132280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
65142280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
65152280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
65162280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
65172280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
65182280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
65192280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
65202280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
65212280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
65222280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
65232280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
65242280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
65252280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
65262280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
65272280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
65281cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
65292280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
65301cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
65312280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
65321cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
65332280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
65342280c16bSPyun YongHyeon }
65352280c16bSPyun YongHyeon 
65362280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
65372280c16bSPyun YongHyeon 
6538763757b2SScott Long static int
6539763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6540763757b2SScott Long {
6541763757b2SScott Long 	struct bge_softc *sc;
654206e83c7eSScott Long 	uint32_t result;
6543d949071dSJung-uk Kim 	int offset;
6544763757b2SScott Long 
6545763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6546763757b2SScott Long 	offset = arg2;
6547d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6548d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6549041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
65506f8718a3SScott Long }
65516f8718a3SScott Long 
65526f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
65536f8718a3SScott Long static int
65546f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
65556f8718a3SScott Long {
65566f8718a3SScott Long 	struct bge_softc *sc;
65576f8718a3SScott Long 	uint16_t *sbdata;
655828276ad6SPyun YongHyeon 	int error, result, sbsz;
65596f8718a3SScott Long 	int i, j;
65606f8718a3SScott Long 
65616f8718a3SScott Long 	result = -1;
65626f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65636f8718a3SScott Long 	if (error || (req->newptr == NULL))
65646f8718a3SScott Long 		return (error);
65656f8718a3SScott Long 
65666f8718a3SScott Long 	if (result == 1) {
65676f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65686f8718a3SScott Long 
656928276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
657028276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
657128276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
657228276ad6SPyun YongHyeon 		else
657328276ad6SPyun YongHyeon 			sbsz = 32;
65746f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
65756f8718a3SScott Long 		printf("Status Block:\n");
657628276ad6SPyun YongHyeon 		BGE_LOCK(sc);
657728276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
657828276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
657928276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
658028276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
65816f8718a3SScott Long 			printf("%06x:", i);
658228276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
658328276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
65846f8718a3SScott Long 			printf("\n");
65856f8718a3SScott Long 		}
65866f8718a3SScott Long 
65876f8718a3SScott Long 		printf("Registers:\n");
65880c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
65896f8718a3SScott Long 			printf("%06x:", i);
65906f8718a3SScott Long 			for (j = 0; j < 8; j++) {
65916f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
65926f8718a3SScott Long 				i += 4;
65936f8718a3SScott Long 			}
65946f8718a3SScott Long 			printf("\n");
65956f8718a3SScott Long 		}
659628276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
65976f8718a3SScott Long 
65986f8718a3SScott Long 		printf("Hardware Flags:\n");
659928276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
660028276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6601a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6602a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
66035345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
66046f8718a3SScott Long 			printf(" - 575X Plus\n");
66055345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
66066f8718a3SScott Long 			printf(" - 5705 Plus\n");
66075345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
66085345bad0SScott Long 			printf(" - 5714 Family\n");
66095345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
66105345bad0SScott Long 			printf(" - 5700 Family\n");
66116f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
66126f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
66136f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
66146f8718a3SScott Long 			printf(" - PCI-X Bus\n");
66156f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
66166f8718a3SScott Long 			printf(" - PCI Express Bus\n");
66177d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
66186f8718a3SScott Long 			printf(" - No 3 LEDs\n");
66196f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
66206f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
66216f8718a3SScott Long 	}
66226f8718a3SScott Long 
66236f8718a3SScott Long 	return (error);
66246f8718a3SScott Long }
66256f8718a3SScott Long 
66266f8718a3SScott Long static int
66276f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
66286f8718a3SScott Long {
66296f8718a3SScott Long 	struct bge_softc *sc;
66306f8718a3SScott Long 	int error;
66316f8718a3SScott Long 	uint16_t result;
66326f8718a3SScott Long 	uint32_t val;
66336f8718a3SScott Long 
66346f8718a3SScott Long 	result = -1;
66356f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66366f8718a3SScott Long 	if (error || (req->newptr == NULL))
66376f8718a3SScott Long 		return (error);
66386f8718a3SScott Long 
66396f8718a3SScott Long 	if (result < 0x8000) {
66406f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66416f8718a3SScott Long 		val = CSR_READ_4(sc, result);
66426f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
66436f8718a3SScott Long 	}
66446f8718a3SScott Long 
66456f8718a3SScott Long 	return (error);
66466f8718a3SScott Long }
66476f8718a3SScott Long 
66486f8718a3SScott Long static int
6649548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6650548c8f1aSPyun YongHyeon {
6651548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6652548c8f1aSPyun YongHyeon 	int error;
6653548c8f1aSPyun YongHyeon 	uint16_t result;
6654548c8f1aSPyun YongHyeon 	uint32_t val;
6655548c8f1aSPyun YongHyeon 
6656548c8f1aSPyun YongHyeon 	result = -1;
6657548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6658548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6659548c8f1aSPyun YongHyeon 		return (error);
6660548c8f1aSPyun YongHyeon 
6661548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6662548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6663548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6664548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6665548c8f1aSPyun YongHyeon 	}
6666548c8f1aSPyun YongHyeon 
6667548c8f1aSPyun YongHyeon 	return (error);
6668548c8f1aSPyun YongHyeon }
6669548c8f1aSPyun YongHyeon 
6670548c8f1aSPyun YongHyeon static int
66716f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
66726f8718a3SScott Long {
66736f8718a3SScott Long 	struct bge_softc *sc;
66746f8718a3SScott Long 	int error;
66756f8718a3SScott Long 	uint16_t result;
66766f8718a3SScott Long 	uint32_t val;
66776f8718a3SScott Long 
66786f8718a3SScott Long 	result = -1;
66796f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66806f8718a3SScott Long 	if (error || (req->newptr == NULL))
66816f8718a3SScott Long 		return (error);
66826f8718a3SScott Long 
66836f8718a3SScott Long 	if (result < 0x8000) {
66846f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66856f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
66866f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
66876f8718a3SScott Long 	}
66886f8718a3SScott Long 
66896f8718a3SScott Long 	return (error);
66906f8718a3SScott Long }
66916f8718a3SScott Long #endif
669238cc658fSJohn Baldwin 
669338cc658fSJohn Baldwin static int
66945fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
66955fea260fSMarius Strobl {
66965fea260fSMarius Strobl 
66975fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
66985fea260fSMarius Strobl 		return (1);
66995fea260fSMarius Strobl 
67005fea260fSMarius Strobl #ifdef __sparc64__
67015fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
67025fea260fSMarius Strobl 	return (0);
67035fea260fSMarius Strobl #endif
67045fea260fSMarius Strobl 	return (1);
67055fea260fSMarius Strobl }
67065fea260fSMarius Strobl 
67075fea260fSMarius Strobl static int
670838cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
670938cc658fSJohn Baldwin {
671038cc658fSJohn Baldwin 	uint32_t mac_addr;
671138cc658fSJohn Baldwin 
671273635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
671338cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
671438cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
671538cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
671673635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
671738cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
671838cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
671938cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
672038cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
67215fea260fSMarius Strobl 		return (0);
672238cc658fSJohn Baldwin 	}
67235fea260fSMarius Strobl 	return (1);
672438cc658fSJohn Baldwin }
672538cc658fSJohn Baldwin 
672638cc658fSJohn Baldwin static int
672738cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
672838cc658fSJohn Baldwin {
672938cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
673038cc658fSJohn Baldwin 
673138cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
673238cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
673338cc658fSJohn Baldwin 
67345fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
67355fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
673638cc658fSJohn Baldwin }
673738cc658fSJohn Baldwin 
673838cc658fSJohn Baldwin static int
673938cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
674038cc658fSJohn Baldwin {
674138cc658fSJohn Baldwin 
67425fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
67435fea260fSMarius Strobl 		return (1);
67445fea260fSMarius Strobl 
67455fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
67465fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
674738cc658fSJohn Baldwin }
674838cc658fSJohn Baldwin 
674938cc658fSJohn Baldwin static int
675038cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
675138cc658fSJohn Baldwin {
675238cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
675338cc658fSJohn Baldwin 		/* NOTE: Order is critical */
67545fea260fSMarius Strobl 		bge_get_eaddr_fw,
675538cc658fSJohn Baldwin 		bge_get_eaddr_mem,
675638cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
675738cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
675838cc658fSJohn Baldwin 		NULL
675938cc658fSJohn Baldwin 	};
676038cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
676138cc658fSJohn Baldwin 
676238cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
676338cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
676438cc658fSJohn Baldwin 			break;
676538cc658fSJohn Baldwin 	}
676638cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
676738cc658fSJohn Baldwin }
6768df360178SGleb Smirnoff 
6769df360178SGleb Smirnoff static uint64_t
6770df360178SGleb Smirnoff bge_get_counter(if_t ifp, ift_counter cnt)
6771df360178SGleb Smirnoff {
6772df360178SGleb Smirnoff 	struct bge_softc *sc;
6773df360178SGleb Smirnoff 	struct bge_mac_stats *stats;
6774df360178SGleb Smirnoff 
6775df360178SGleb Smirnoff 	sc = if_getsoftc(ifp);
6776df360178SGleb Smirnoff 	if (!BGE_IS_5705_PLUS(sc))
6777df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6778df360178SGleb Smirnoff 	stats = &sc->bge_mac_stats;
6779df360178SGleb Smirnoff 
6780df360178SGleb Smirnoff 	switch (cnt) {
6781df360178SGleb Smirnoff 	case IFCOUNTER_IERRORS:
6782df360178SGleb Smirnoff 		return (stats->NoMoreRxBDs + stats->InputDiscards +
6783df360178SGleb Smirnoff 		    stats->InputErrors);
6784df360178SGleb Smirnoff 	case IFCOUNTER_COLLISIONS:
6785df360178SGleb Smirnoff 		return (stats->etherStatsCollisions);
6786df360178SGleb Smirnoff 	default:
6787df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6788df360178SGleb Smirnoff 	}
6789df360178SGleb Smirnoff }
6790