xref: /freebsd/sys/dev/bge/if_bge.c (revision b77d3a3b8e4fdfed8868201e4ac6290065bfa185)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
38d7acafa1SMarius Strobl  * Broadcom BCM57xx(x)/BCM590x NetXtreme and NetLink family Ethernet driver
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
41d7acafa1SMarius Strobl  * Alteon Networks as part of the Tigon I and Tigon II Gigabit Ethernet
4222a4ecedSMarius Strobl  * MAC chips. The BCM5700, sometimes referred to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
82f1a7e6d5SScott Long #include <sys/sysctl.h>
83dfe0df9aSPyun YongHyeon #include <sys/taskqueue.h>
8495d67482SBill Paul 
8595d67482SBill Paul #include <net/if.h>
8695d67482SBill Paul #include <net/if_arp.h>
8795d67482SBill Paul #include <net/ethernet.h>
8895d67482SBill Paul #include <net/if_dl.h>
8995d67482SBill Paul #include <net/if_media.h>
9095d67482SBill Paul 
9195d67482SBill Paul #include <net/bpf.h>
9295d67482SBill Paul 
9395d67482SBill Paul #include <net/if_types.h>
9495d67482SBill Paul #include <net/if_vlan_var.h>
9595d67482SBill Paul 
9695d67482SBill Paul #include <netinet/in_systm.h>
9795d67482SBill Paul #include <netinet/in.h>
9895d67482SBill Paul #include <netinet/ip.h>
99ca3f1187SPyun YongHyeon #include <netinet/tcp.h>
10095d67482SBill Paul 
10195d67482SBill Paul #include <machine/bus.h>
10295d67482SBill Paul #include <machine/resource.h>
10395d67482SBill Paul #include <sys/bus.h>
10495d67482SBill Paul #include <sys/rman.h>
10595d67482SBill Paul 
10695d67482SBill Paul #include <dev/mii/mii.h>
10795d67482SBill Paul #include <dev/mii/miivar.h>
1082d3ce713SDavid E. O'Brien #include "miidevs.h"
10995d67482SBill Paul #include <dev/mii/brgphyreg.h>
11095d67482SBill Paul 
11108013fd3SMarius Strobl #ifdef __sparc64__
11208013fd3SMarius Strobl #include <dev/ofw/ofw_bus.h>
11308013fd3SMarius Strobl #include <dev/ofw/openfirm.h>
11408013fd3SMarius Strobl #include <machine/ofw_machdep.h>
11508013fd3SMarius Strobl #include <machine/ver.h>
11608013fd3SMarius Strobl #endif
11708013fd3SMarius Strobl 
1184fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1194fbd232cSWarner Losh #include <dev/pci/pcivar.h>
12095d67482SBill Paul 
12195d67482SBill Paul #include <dev/bge/if_bgereg.h>
12295d67482SBill Paul 
12335f945cdSPyun YongHyeon #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP)
124d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12595d67482SBill Paul 
126f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
127f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12895d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12995d67482SBill Paul 
1307b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
13195d67482SBill Paul #include "miibus_if.h"
13295d67482SBill Paul 
13395d67482SBill Paul /*
13495d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13595d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13695d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13795d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13895d67482SBill Paul  */
139852c67f9SMarius Strobl static const struct bge_type {
1404c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1414c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
14229658c96SDimitry Andric } bge_devs[] = {
1434c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1444c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
14595d67482SBill Paul 
1464c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1474c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1484c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1494c0da0ffSGleb Smirnoff 
1504c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1514c0da0ffSGleb Smirnoff 
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1721108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5717 },
1731108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5718 },
174bbe2ca75SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5719 },
1754c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1764c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
177effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
178a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5723 },
1792927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5725 },
1802927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5727 },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1834c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1844c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1854c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1894c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1904c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1919e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1929e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1939e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1949e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
195f7d1b2ebSXin LI 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5756 },
196a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761 },
197a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761E },
198a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761S },
199a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761SE },
2002927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5762 },
201a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5764 },
2024c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
2034c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
2044c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
2054c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
206a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5784 },
207a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785F },
208a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785G },
2099e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
2109e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
211a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787F },
2129e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
2134c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
2144c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
2154c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
2164c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
2174c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
21838cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
21938cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
220a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57760 },
221b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57761 },
222fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57762 },
223b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57765 },
224fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57766 },
225a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
226b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57781 },
227b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57785 },
228a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
229a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
230b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57791 },
231b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57795 },
2324c0da0ffSGleb Smirnoff 
2334c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2344c0da0ffSGleb Smirnoff 
2354c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2364c0da0ffSGleb Smirnoff 
237a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
238a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
239a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PP250450 },
240a5779553SStanislav Sedov 
2414c0da0ffSGleb Smirnoff 	{ 0, 0 }
24295d67482SBill Paul };
24395d67482SBill Paul 
2444c0da0ffSGleb Smirnoff static const struct bge_vendor {
2454c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2464c0da0ffSGleb Smirnoff 	const char	*v_name;
24729658c96SDimitry Andric } bge_vendors[] = {
2484c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2494c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2504c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2514c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2524c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2534c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
254a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2554c0da0ffSGleb Smirnoff 
2564c0da0ffSGleb Smirnoff 	{ 0, NULL }
2574c0da0ffSGleb Smirnoff };
2584c0da0ffSGleb Smirnoff 
2594c0da0ffSGleb Smirnoff static const struct bge_revision {
2604c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2614c0da0ffSGleb Smirnoff 	const char	*br_name;
26229658c96SDimitry Andric } bge_revisions[] = {
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2654c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2674c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2684c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2694c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2704c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2714c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2724c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2744c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2764c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2799e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2824c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2844c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2854c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2884c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2894c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2904c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2914c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2924c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2934c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2944c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2954c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
29642787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2974c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2984c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2994c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
3004c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
3014c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
3024c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
3034c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
3044c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
3050c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
3061108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_A0,	"BCM5717 A0" },
3071108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_B0,	"BCM5717 B0" },
308bbe2ca75SPyun YongHyeon 	{ BGE_CHIPID_BCM5719_A0,	"BCM5719 A0" },
30950515680SPyun YongHyeon 	{ BGE_CHIPID_BCM5720_A0,	"BCM5720 A0" },
3100c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
3110c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
3120c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
313bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
314a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
315a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
3162927f01fSPyun YongHyeon 	{ BGE_CHIPID_BCM5762_A0,	"BCM5762 A0" },
317a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
318a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
31981179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3206f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
3216f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3226f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
32338cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
32438cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
325b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_A0,	"BCM57765 A0" },
326b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_B0,	"BCM57765 B0" },
327a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
328a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3294c0da0ffSGleb Smirnoff 
3304c0da0ffSGleb Smirnoff 	{ 0, NULL }
3314c0da0ffSGleb Smirnoff };
3324c0da0ffSGleb Smirnoff 
3334c0da0ffSGleb Smirnoff /*
3344c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3354c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3364c0da0ffSGleb Smirnoff  */
33729658c96SDimitry Andric static const struct bge_revision bge_majorrevs[] = {
3389e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3399e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3409e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3419e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3429e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3439e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3449e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3459e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3469e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3479e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3489e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
349a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
350a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
351a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
35281179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3536f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
35438cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
355b4a256acSPyun YongHyeon 	{ BGE_ASICREV_BCM57765,		"unknown BCM57765" },
356fe26ad88SPyun YongHyeon 	{ BGE_ASICREV_BCM57766,		"unknown BCM57766" },
357a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3581108273aSPyun YongHyeon 	{ BGE_ASICREV_BCM5717,		"unknown BCM5717" },
359bbe2ca75SPyun YongHyeon 	{ BGE_ASICREV_BCM5719,		"unknown BCM5719" },
36050515680SPyun YongHyeon 	{ BGE_ASICREV_BCM5720,		"unknown BCM5720" },
3612927f01fSPyun YongHyeon 	{ BGE_ASICREV_BCM5762,		"unknown BCM5762" },
3624c0da0ffSGleb Smirnoff 
3634c0da0ffSGleb Smirnoff 	{ 0, NULL }
3644c0da0ffSGleb Smirnoff };
3654c0da0ffSGleb Smirnoff 
3660c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3670c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3680c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3690c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3700c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
371a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3721108273aSPyun YongHyeon #define	BGE_IS_5717_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5717_PLUS)
373fe26ad88SPyun YongHyeon #define	BGE_IS_57765_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_57765_PLUS)
3744c0da0ffSGleb Smirnoff 
375d7acafa1SMarius Strobl static uint32_t bge_chipid(device_t);
376d7acafa1SMarius Strobl static const struct bge_vendor * bge_lookup_vendor(uint16_t);
377d7acafa1SMarius Strobl static const struct bge_revision * bge_lookup_rev(uint32_t);
37838cc658fSJohn Baldwin 
37938cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
38038cc658fSJohn Baldwin 
381e51a25f8SAlfred Perlstein static int bge_probe(device_t);
382e51a25f8SAlfred Perlstein static int bge_attach(device_t);
383e51a25f8SAlfred Perlstein static int bge_detach(device_t);
38414afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
38514afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3863f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
387f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3885b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
389f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
3905b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
3915b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
392f41ac2beSBill Paul 
393ea9c3a30SPyun YongHyeon static void bge_devinfo(struct bge_softc *);
394062af0b0SPyun YongHyeon static int bge_mbox_reorder(struct bge_softc *);
395062af0b0SPyun YongHyeon 
3965fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
39738cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
39838cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
39938cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
40038cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
40138cc658fSJohn Baldwin 
402b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
4031108273aSPyun YongHyeon static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
404dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
40595d67482SBill Paul 
4068cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
407e51a25f8SAlfred Perlstein static void bge_tick(void *);
4082280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
409e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
4103f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
411d598b626SPyun YongHyeon static struct mbuf *bge_check_short_dma(struct mbuf *);
4122e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
4131108273aSPyun YongHyeon     uint16_t *, uint16_t *);
414676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
41595d67482SBill Paul 
416e51a25f8SAlfred Perlstein static void bge_intr(void *);
417dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
418dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
4190f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
420e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
421e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
4220f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
423e51a25f8SAlfred Perlstein static void bge_init(void *);
4245a147ba6SPyun YongHyeon static void bge_stop_block(struct bge_softc *, bus_size_t, uint32_t);
425e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
426b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
427b6c974e8SWarner Losh static int bge_shutdown(device_t);
42867d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
429e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
430e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
43195d67482SBill Paul 
43238cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
43338cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
43438cc658fSJohn Baldwin 
4353f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
436e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
43795d67482SBill Paul 
4383e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
439e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
440cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
44195d67482SBill Paul 
442e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
443e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
444943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
445943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
446e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
447e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
448e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
449e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
450e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
451e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
45295d67482SBill Paul 
453e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
454e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
45550515680SPyun YongHyeon static uint32_t bge_dma_swap_options(struct bge_softc *);
45695d67482SBill Paul 
4575fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4583f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
459e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
46038cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
46195d67482SBill Paul #ifdef notdef
4623f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
46395d67482SBill Paul #endif
4649ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
465e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
46695d67482SBill Paul 
467e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
468e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
469e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
47075719184SGleb Smirnoff #ifdef DEVICE_POLLING
4711abcdbd1SAttilio Rao static int bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
47275719184SGleb Smirnoff #endif
47395d67482SBill Paul 
474548c8f1aSPyun YongHyeon #define	BGE_RESET_SHUTDOWN	0
4758cb1383cSDoug Ambrisko #define	BGE_RESET_START		1
476548c8f1aSPyun YongHyeon #define	BGE_RESET_SUSPEND	2
4778cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4788cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4798cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
480797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4818cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
482dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
48395d67482SBill Paul 
484548c8f1aSPyun YongHyeon static void bge_ape_lock_init(struct bge_softc *);
485548c8f1aSPyun YongHyeon static void bge_ape_read_fw_ver(struct bge_softc *);
486548c8f1aSPyun YongHyeon static int bge_ape_lock(struct bge_softc *, int);
487548c8f1aSPyun YongHyeon static void bge_ape_unlock(struct bge_softc *, int);
488548c8f1aSPyun YongHyeon static void bge_ape_send_event(struct bge_softc *, uint32_t);
489548c8f1aSPyun YongHyeon static void bge_ape_driver_state_change(struct bge_softc *, int);
490548c8f1aSPyun YongHyeon 
4916f8718a3SScott Long /*
4926f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4936f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4946f8718a3SScott Long  * traps on certain architectures.
4956f8718a3SScott Long  */
4966f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4976f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4986f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
499548c8f1aSPyun YongHyeon static int bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS);
5006f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
5016f8718a3SScott Long #endif
5026f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
5032280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
5042280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
5052280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
5062280c16bSPyun YongHyeon     struct sysctl_oid_list *);
507763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
5086f8718a3SScott Long 
50995d67482SBill Paul static device_method_t bge_methods[] = {
51095d67482SBill Paul 	/* Device interface */
51195d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
51295d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
51395d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
51495d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
51514afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
51614afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
51795d67482SBill Paul 
51895d67482SBill Paul 	/* MII interface */
51995d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
52095d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
52195d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
52295d67482SBill Paul 
5234b7ec270SMarius Strobl 	DEVMETHOD_END
52495d67482SBill Paul };
52595d67482SBill Paul 
52695d67482SBill Paul static driver_t bge_driver = {
52795d67482SBill Paul 	"bge",
52895d67482SBill Paul 	bge_methods,
52995d67482SBill Paul 	sizeof(struct bge_softc)
53095d67482SBill Paul };
53195d67482SBill Paul 
53295d67482SBill Paul static devclass_t bge_devclass;
53395d67482SBill Paul 
534f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
53595d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
53695d67482SBill Paul 
537f1a7e6d5SScott Long static int bge_allow_asf = 1;
538f1a7e6d5SScott Long 
539f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
540f1a7e6d5SScott Long 
5416472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
542f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
543f1a7e6d5SScott Long 	"Allow ASF mode if available");
544c4529f41SMichael Reifenberger 
54508013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
54608013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
54708013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
54808013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
54908013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
55008013fd3SMarius Strobl 
55108013fd3SMarius Strobl static int
5525fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
55308013fd3SMarius Strobl {
55408013fd3SMarius Strobl #ifdef __sparc64__
55508013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
55608013fd3SMarius Strobl 	device_t dev;
55708013fd3SMarius Strobl 	uint32_t subvendor;
55808013fd3SMarius Strobl 
55908013fd3SMarius Strobl 	dev = sc->bge_dev;
56008013fd3SMarius Strobl 
56108013fd3SMarius Strobl 	/*
56208013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
56308013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
56408013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
56508013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
56608013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
56708013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
56808013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
56908013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
57008013fd3SMarius Strobl 	 * there.
57108013fd3SMarius Strobl 	 */
57208013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
57308013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5742d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
57508013fd3SMarius Strobl 		return (0);
57608013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
57708013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
57808013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
57908013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
58008013fd3SMarius Strobl 			return (0);
58108013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
58208013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
58308013fd3SMarius Strobl 			return (0);
58408013fd3SMarius Strobl 	}
58508013fd3SMarius Strobl #endif
58608013fd3SMarius Strobl 	return (1);
58708013fd3SMarius Strobl }
58808013fd3SMarius Strobl 
5893f74909aSGleb Smirnoff static uint32_t
5903f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
59195d67482SBill Paul {
59295d67482SBill Paul 	device_t dev;
5936f8718a3SScott Long 	uint32_t val;
59495d67482SBill Paul 
595a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
596a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
597a4431ebaSPyun YongHyeon 		return (0);
598a4431ebaSPyun YongHyeon 
59995d67482SBill Paul 	dev = sc->bge_dev;
60095d67482SBill Paul 
60195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
6026f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
6036f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
6046f8718a3SScott Long 	return (val);
60595d67482SBill Paul }
60695d67482SBill Paul 
60795d67482SBill Paul static void
6083f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
60995d67482SBill Paul {
61095d67482SBill Paul 	device_t dev;
61195d67482SBill Paul 
612a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
613a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
614a4431ebaSPyun YongHyeon 		return;
615a4431ebaSPyun YongHyeon 
61695d67482SBill Paul 	dev = sc->bge_dev;
61795d67482SBill Paul 
61895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
61995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
6206f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
62195d67482SBill Paul }
62295d67482SBill Paul 
62395d67482SBill Paul #ifdef notdef
6243f74909aSGleb Smirnoff static uint32_t
6253f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
62695d67482SBill Paul {
62795d67482SBill Paul 	device_t dev;
62895d67482SBill Paul 
62995d67482SBill Paul 	dev = sc->bge_dev;
63095d67482SBill Paul 
63195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
63295d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
63395d67482SBill Paul }
63495d67482SBill Paul #endif
63595d67482SBill Paul 
63695d67482SBill Paul static void
6373f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
63895d67482SBill Paul {
63995d67482SBill Paul 	device_t dev;
64095d67482SBill Paul 
64195d67482SBill Paul 	dev = sc->bge_dev;
64295d67482SBill Paul 
64395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
64495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
64595d67482SBill Paul }
64695d67482SBill Paul 
6476f8718a3SScott Long static void
6486f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6496f8718a3SScott Long {
6506f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6516f8718a3SScott Long }
6526f8718a3SScott Long 
65338cc658fSJohn Baldwin static void
65438cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
65538cc658fSJohn Baldwin {
65638cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
65738cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
65838cc658fSJohn Baldwin 
65938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
660062af0b0SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
661062af0b0SPyun YongHyeon 		CSR_READ_4(sc, off);
66238cc658fSJohn Baldwin }
66338cc658fSJohn Baldwin 
664f41ac2beSBill Paul /*
665548c8f1aSPyun YongHyeon  * Clear all stale locks and select the lock for this driver instance.
666548c8f1aSPyun YongHyeon  */
667548c8f1aSPyun YongHyeon static void
668548c8f1aSPyun YongHyeon bge_ape_lock_init(struct bge_softc *sc)
669548c8f1aSPyun YongHyeon {
670548c8f1aSPyun YongHyeon 	uint32_t bit, regbase;
671548c8f1aSPyun YongHyeon 	int i;
672548c8f1aSPyun YongHyeon 
673548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
674548c8f1aSPyun YongHyeon 		regbase = BGE_APE_LOCK_GRANT;
675548c8f1aSPyun YongHyeon 	else
676548c8f1aSPyun YongHyeon 		regbase = BGE_APE_PER_LOCK_GRANT;
677548c8f1aSPyun YongHyeon 
678548c8f1aSPyun YongHyeon 	/* Clear any stale locks. */
679548c8f1aSPyun YongHyeon 	for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
680548c8f1aSPyun YongHyeon 		switch (i) {
681548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY0:
682548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY1:
683548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY2:
684548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY3:
685548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
686548c8f1aSPyun YongHyeon 			break;
687548c8f1aSPyun YongHyeon 		default:
688bd9c196aSPyun YongHyeon 			if (sc->bge_func_addr == 0)
689548c8f1aSPyun YongHyeon 				bit = BGE_APE_LOCK_GRANT_DRIVER0;
690548c8f1aSPyun YongHyeon 			else
691548c8f1aSPyun YongHyeon 				bit = (1 << sc->bge_func_addr);
692548c8f1aSPyun YongHyeon 		}
693548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, regbase + 4 * i, bit);
694548c8f1aSPyun YongHyeon 	}
695548c8f1aSPyun YongHyeon 
696548c8f1aSPyun YongHyeon 	/* Select the PHY lock based on the device's function number. */
697548c8f1aSPyun YongHyeon 	switch (sc->bge_func_addr) {
698548c8f1aSPyun YongHyeon 	case 0:
699548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY0;
700548c8f1aSPyun YongHyeon 		break;
701548c8f1aSPyun YongHyeon 	case 1:
702548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY1;
703548c8f1aSPyun YongHyeon 		break;
704548c8f1aSPyun YongHyeon 	case 2:
705548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY2;
706548c8f1aSPyun YongHyeon 		break;
707548c8f1aSPyun YongHyeon 	case 3:
708548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY3;
709548c8f1aSPyun YongHyeon 		break;
710548c8f1aSPyun YongHyeon 	default:
711548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev,
712548c8f1aSPyun YongHyeon 		    "PHY lock not supported on this function\n");
713548c8f1aSPyun YongHyeon 	}
714548c8f1aSPyun YongHyeon }
715548c8f1aSPyun YongHyeon 
716548c8f1aSPyun YongHyeon /*
717548c8f1aSPyun YongHyeon  * Check for APE firmware, set flags, and print version info.
718548c8f1aSPyun YongHyeon  */
719548c8f1aSPyun YongHyeon static void
720548c8f1aSPyun YongHyeon bge_ape_read_fw_ver(struct bge_softc *sc)
721548c8f1aSPyun YongHyeon {
722548c8f1aSPyun YongHyeon 	const char *fwtype;
723548c8f1aSPyun YongHyeon 	uint32_t apedata, features;
724548c8f1aSPyun YongHyeon 
725548c8f1aSPyun YongHyeon 	/* Check for a valid APE signature in shared memory. */
726548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
727548c8f1aSPyun YongHyeon 	if (apedata != BGE_APE_SEG_SIG_MAGIC) {
728548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags &= ~ BGE_MFW_ON_APE;
729548c8f1aSPyun YongHyeon 		return;
730548c8f1aSPyun YongHyeon 	}
731548c8f1aSPyun YongHyeon 
732548c8f1aSPyun YongHyeon 	/* Check if APE firmware is running. */
733548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
734548c8f1aSPyun YongHyeon 	if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
735548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE signature found "
736548c8f1aSPyun YongHyeon 		    "but FW status not ready! 0x%08x\n", apedata);
737548c8f1aSPyun YongHyeon 		return;
738548c8f1aSPyun YongHyeon 	}
739548c8f1aSPyun YongHyeon 
740548c8f1aSPyun YongHyeon 	sc->bge_mfw_flags |= BGE_MFW_ON_APE;
741548c8f1aSPyun YongHyeon 
742548c8f1aSPyun YongHyeon 	/* Fetch the APE firwmare type and version. */
743548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
744548c8f1aSPyun YongHyeon 	features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
745548c8f1aSPyun YongHyeon 	if ((features & BGE_APE_FW_FEATURE_NCSI) != 0) {
746548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_NCSI;
747548c8f1aSPyun YongHyeon 		fwtype = "NCSI";
748548c8f1aSPyun YongHyeon 	} else if ((features & BGE_APE_FW_FEATURE_DASH) != 0) {
749548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_DASH;
750548c8f1aSPyun YongHyeon 		fwtype = "DASH";
751548c8f1aSPyun YongHyeon 	} else
752548c8f1aSPyun YongHyeon 		fwtype = "UNKN";
753548c8f1aSPyun YongHyeon 
754548c8f1aSPyun YongHyeon 	/* Print the APE firmware version. */
755548c8f1aSPyun YongHyeon 	device_printf(sc->bge_dev, "APE FW version: %s v%d.%d.%d.%d\n",
756548c8f1aSPyun YongHyeon 	    fwtype,
757548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
758548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
759548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
760548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_BLDMSK));
761548c8f1aSPyun YongHyeon }
762548c8f1aSPyun YongHyeon 
763548c8f1aSPyun YongHyeon static int
764548c8f1aSPyun YongHyeon bge_ape_lock(struct bge_softc *sc, int locknum)
765548c8f1aSPyun YongHyeon {
766548c8f1aSPyun YongHyeon 	uint32_t bit, gnt, req, status;
767548c8f1aSPyun YongHyeon 	int i, off;
768548c8f1aSPyun YongHyeon 
769548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
770548c8f1aSPyun YongHyeon 		return (0);
771548c8f1aSPyun YongHyeon 
772548c8f1aSPyun YongHyeon 	/* Lock request/grant registers have different bases. */
773548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761) {
774548c8f1aSPyun YongHyeon 		req = BGE_APE_LOCK_REQ;
775548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
776548c8f1aSPyun YongHyeon 	} else {
777548c8f1aSPyun YongHyeon 		req = BGE_APE_PER_LOCK_REQ;
778548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
779548c8f1aSPyun YongHyeon 	}
780548c8f1aSPyun YongHyeon 
781548c8f1aSPyun YongHyeon 	off = 4 * locknum;
782548c8f1aSPyun YongHyeon 
783548c8f1aSPyun YongHyeon 	switch (locknum) {
784548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
785548c8f1aSPyun YongHyeon 		/* Lock required when using GPIO. */
786548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
787548c8f1aSPyun YongHyeon 			return (0);
788548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
789548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
790548c8f1aSPyun YongHyeon 		else
791548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
792548c8f1aSPyun YongHyeon 		break;
793548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
794548c8f1aSPyun YongHyeon 		/* Lock required to reset the device. */
795548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
796548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
797548c8f1aSPyun YongHyeon 		else
798548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
799548c8f1aSPyun YongHyeon 		break;
800548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
801548c8f1aSPyun YongHyeon 		/* Lock required when accessing certain APE memory. */
802548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
803548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
804548c8f1aSPyun YongHyeon 		else
805548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
806548c8f1aSPyun YongHyeon 		break;
807548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
808548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
809548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
810548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
811548c8f1aSPyun YongHyeon 		/* Lock required when accessing PHYs. */
812548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_REQ_DRIVER0;
813548c8f1aSPyun YongHyeon 		break;
814548c8f1aSPyun YongHyeon 	default:
815548c8f1aSPyun YongHyeon 		return (EINVAL);
816548c8f1aSPyun YongHyeon 	}
817548c8f1aSPyun YongHyeon 
818548c8f1aSPyun YongHyeon 	/* Request a lock. */
819548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, req + off, bit);
820548c8f1aSPyun YongHyeon 
821548c8f1aSPyun YongHyeon 	/* Wait up to 1 second to acquire lock. */
822548c8f1aSPyun YongHyeon 	for (i = 0; i < 20000; i++) {
823548c8f1aSPyun YongHyeon 		status = APE_READ_4(sc, gnt + off);
824548c8f1aSPyun YongHyeon 		if (status == bit)
825548c8f1aSPyun YongHyeon 			break;
826548c8f1aSPyun YongHyeon 		DELAY(50);
827548c8f1aSPyun YongHyeon 	}
828548c8f1aSPyun YongHyeon 
829548c8f1aSPyun YongHyeon 	/* Handle any errors. */
830548c8f1aSPyun YongHyeon 	if (status != bit) {
831548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE lock %d request failed! "
832548c8f1aSPyun YongHyeon 		    "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
833548c8f1aSPyun YongHyeon 		    locknum, req + off, bit & 0xFFFF, gnt + off,
834548c8f1aSPyun YongHyeon 		    status & 0xFFFF);
835548c8f1aSPyun YongHyeon 		/* Revoke the lock request. */
836548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, gnt + off, bit);
837548c8f1aSPyun YongHyeon 		return (EBUSY);
838548c8f1aSPyun YongHyeon 	}
839548c8f1aSPyun YongHyeon 
840548c8f1aSPyun YongHyeon 	return (0);
841548c8f1aSPyun YongHyeon }
842548c8f1aSPyun YongHyeon 
843548c8f1aSPyun YongHyeon static void
844548c8f1aSPyun YongHyeon bge_ape_unlock(struct bge_softc *sc, int locknum)
845548c8f1aSPyun YongHyeon {
846548c8f1aSPyun YongHyeon 	uint32_t bit, gnt;
847548c8f1aSPyun YongHyeon 	int off;
848548c8f1aSPyun YongHyeon 
849548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
850548c8f1aSPyun YongHyeon 		return;
851548c8f1aSPyun YongHyeon 
852548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
853548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
854548c8f1aSPyun YongHyeon 	else
855548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
856548c8f1aSPyun YongHyeon 
857548c8f1aSPyun YongHyeon 	off = 4 * locknum;
858548c8f1aSPyun YongHyeon 
859548c8f1aSPyun YongHyeon 	switch (locknum) {
860548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
861548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
862548c8f1aSPyun YongHyeon 			return;
863548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
864548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
865548c8f1aSPyun YongHyeon 		else
866548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
867548c8f1aSPyun YongHyeon 		break;
868548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
869548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
870548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
871548c8f1aSPyun YongHyeon 		else
872548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
873548c8f1aSPyun YongHyeon 		break;
874548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
875548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
876548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
877548c8f1aSPyun YongHyeon 		else
878548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
879548c8f1aSPyun YongHyeon 		break;
880548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
881548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
882548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
883548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
884548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_GRANT_DRIVER0;
885548c8f1aSPyun YongHyeon 		break;
886548c8f1aSPyun YongHyeon 	default:
887548c8f1aSPyun YongHyeon 		return;
888548c8f1aSPyun YongHyeon 	}
889548c8f1aSPyun YongHyeon 
890548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, gnt + off, bit);
891548c8f1aSPyun YongHyeon }
892548c8f1aSPyun YongHyeon 
893548c8f1aSPyun YongHyeon /*
894548c8f1aSPyun YongHyeon  * Send an event to the APE firmware.
895548c8f1aSPyun YongHyeon  */
896548c8f1aSPyun YongHyeon static void
897548c8f1aSPyun YongHyeon bge_ape_send_event(struct bge_softc *sc, uint32_t event)
898548c8f1aSPyun YongHyeon {
899548c8f1aSPyun YongHyeon 	uint32_t apedata;
900548c8f1aSPyun YongHyeon 	int i;
901548c8f1aSPyun YongHyeon 
902548c8f1aSPyun YongHyeon 	/* NCSI does not support APE events. */
903548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
904548c8f1aSPyun YongHyeon 		return;
905548c8f1aSPyun YongHyeon 
906548c8f1aSPyun YongHyeon 	/* Wait up to 1ms for APE to service previous event. */
907548c8f1aSPyun YongHyeon 	for (i = 10; i > 0; i--) {
908548c8f1aSPyun YongHyeon 		if (bge_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
909548c8f1aSPyun YongHyeon 			break;
910548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
911548c8f1aSPyun YongHyeon 		if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
912548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
913548c8f1aSPyun YongHyeon 			    BGE_APE_EVENT_STATUS_EVENT_PENDING);
914548c8f1aSPyun YongHyeon 			bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
915548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
916548c8f1aSPyun YongHyeon 			break;
917548c8f1aSPyun YongHyeon 		}
918548c8f1aSPyun YongHyeon 		bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
919548c8f1aSPyun YongHyeon 		DELAY(100);
920548c8f1aSPyun YongHyeon 	}
921548c8f1aSPyun YongHyeon 	if (i == 0)
922548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE event 0x%08x send timed out\n",
923548c8f1aSPyun YongHyeon 		    event);
924548c8f1aSPyun YongHyeon }
925548c8f1aSPyun YongHyeon 
926548c8f1aSPyun YongHyeon static void
927548c8f1aSPyun YongHyeon bge_ape_driver_state_change(struct bge_softc *sc, int kind)
928548c8f1aSPyun YongHyeon {
929548c8f1aSPyun YongHyeon 	uint32_t apedata, event;
930548c8f1aSPyun YongHyeon 
931548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
932548c8f1aSPyun YongHyeon 		return;
933548c8f1aSPyun YongHyeon 
934548c8f1aSPyun YongHyeon 	switch (kind) {
935548c8f1aSPyun YongHyeon 	case BGE_RESET_START:
936548c8f1aSPyun YongHyeon 		/* If this is the first load, clear the load counter. */
937548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
938548c8f1aSPyun YongHyeon 		if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC)
939548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
940548c8f1aSPyun YongHyeon 		else {
941548c8f1aSPyun YongHyeon 			apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
942548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
943548c8f1aSPyun YongHyeon 		}
944548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
945548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_SIG_MAGIC);
946548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
947548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_LEN_MAGIC);
948548c8f1aSPyun YongHyeon 
949548c8f1aSPyun YongHyeon 		/* Add some version info if bge(4) supports it. */
950548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
951548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
952548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
953548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_BEHAV_NO_PHYLOCK);
954548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
955548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
956548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
957548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_START);
958548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_START;
959548c8f1aSPyun YongHyeon 		break;
960548c8f1aSPyun YongHyeon 	case BGE_RESET_SHUTDOWN:
961548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
962548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_UNLOAD);
963548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
964548c8f1aSPyun YongHyeon 		break;
965548c8f1aSPyun YongHyeon 	case BGE_RESET_SUSPEND:
966548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
967548c8f1aSPyun YongHyeon 		break;
968548c8f1aSPyun YongHyeon 	default:
969548c8f1aSPyun YongHyeon 		return;
970548c8f1aSPyun YongHyeon 	}
971548c8f1aSPyun YongHyeon 
972548c8f1aSPyun YongHyeon 	bge_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
973548c8f1aSPyun YongHyeon 	    BGE_APE_EVENT_STATUS_STATE_CHNGE);
974548c8f1aSPyun YongHyeon }
975548c8f1aSPyun YongHyeon 
976548c8f1aSPyun YongHyeon /*
977f41ac2beSBill Paul  * Map a single buffer address.
978f41ac2beSBill Paul  */
979f41ac2beSBill Paul 
980f41ac2beSBill Paul static void
9813f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
982f41ac2beSBill Paul {
983f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
984f41ac2beSBill Paul 
985f41ac2beSBill Paul 	if (error)
986f41ac2beSBill Paul 		return;
987f41ac2beSBill Paul 
9885b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
9895b610048SPyun YongHyeon 
990f41ac2beSBill Paul 	ctx = arg;
991f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
992f41ac2beSBill Paul }
993f41ac2beSBill Paul 
99438cc658fSJohn Baldwin static uint8_t
99538cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
99638cc658fSJohn Baldwin {
99738cc658fSJohn Baldwin 	uint32_t access, byte = 0;
99838cc658fSJohn Baldwin 	int i;
99938cc658fSJohn Baldwin 
100038cc658fSJohn Baldwin 	/* Lock. */
100138cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
100238cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
100338cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
100438cc658fSJohn Baldwin 			break;
100538cc658fSJohn Baldwin 		DELAY(20);
100638cc658fSJohn Baldwin 	}
100738cc658fSJohn Baldwin 	if (i == 8000)
100838cc658fSJohn Baldwin 		return (1);
100938cc658fSJohn Baldwin 
101038cc658fSJohn Baldwin 	/* Enable access. */
101138cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
101238cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
101338cc658fSJohn Baldwin 
101438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
101538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
101638cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
101738cc658fSJohn Baldwin 		DELAY(10);
101838cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
101938cc658fSJohn Baldwin 			DELAY(10);
102038cc658fSJohn Baldwin 			break;
102138cc658fSJohn Baldwin 		}
102238cc658fSJohn Baldwin 	}
102338cc658fSJohn Baldwin 
102438cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
102538cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
102638cc658fSJohn Baldwin 		return (1);
102738cc658fSJohn Baldwin 	}
102838cc658fSJohn Baldwin 
102938cc658fSJohn Baldwin 	/* Get result. */
103038cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
103138cc658fSJohn Baldwin 
103238cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
103338cc658fSJohn Baldwin 
103438cc658fSJohn Baldwin 	/* Disable access. */
103538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
103638cc658fSJohn Baldwin 
103738cc658fSJohn Baldwin 	/* Unlock. */
103838cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
103938cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
104038cc658fSJohn Baldwin 
104138cc658fSJohn Baldwin 	return (0);
104238cc658fSJohn Baldwin }
104338cc658fSJohn Baldwin 
104438cc658fSJohn Baldwin /*
104538cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
104638cc658fSJohn Baldwin  */
104738cc658fSJohn Baldwin static int
104838cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
104938cc658fSJohn Baldwin {
105038cc658fSJohn Baldwin 	int err = 0, i;
105138cc658fSJohn Baldwin 	uint8_t byte = 0;
105238cc658fSJohn Baldwin 
105338cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
105438cc658fSJohn Baldwin 		return (1);
105538cc658fSJohn Baldwin 
105638cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
105738cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
105838cc658fSJohn Baldwin 		if (err)
105938cc658fSJohn Baldwin 			break;
106038cc658fSJohn Baldwin 		*(dest + i) = byte;
106138cc658fSJohn Baldwin 	}
106238cc658fSJohn Baldwin 
106338cc658fSJohn Baldwin 	return (err ? 1 : 0);
106438cc658fSJohn Baldwin }
106538cc658fSJohn Baldwin 
106695d67482SBill Paul /*
106795d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
106895d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
106995d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
107095d67482SBill Paul  * access method.
107195d67482SBill Paul  */
10723f74909aSGleb Smirnoff static uint8_t
10733f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
107495d67482SBill Paul {
107595d67482SBill Paul 	int i;
10763f74909aSGleb Smirnoff 	uint32_t byte = 0;
107795d67482SBill Paul 
107895d67482SBill Paul 	/*
107995d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
108095d67482SBill Paul 	 * having to use the bitbang method.
108195d67482SBill Paul 	 */
108295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
108395d67482SBill Paul 
108495d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
108595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
108695d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
108795d67482SBill Paul 	DELAY(20);
108895d67482SBill Paul 
108995d67482SBill Paul 	/* Issue the read EEPROM command. */
109095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
109195d67482SBill Paul 
109295d67482SBill Paul 	/* Wait for completion */
109395d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
109495d67482SBill Paul 		DELAY(10);
109595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
109695d67482SBill Paul 			break;
109795d67482SBill Paul 	}
109895d67482SBill Paul 
1099d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
1100fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
1101f6789fbaSPyun YongHyeon 		return (1);
110295d67482SBill Paul 	}
110395d67482SBill Paul 
110495d67482SBill Paul 	/* Get result. */
110595d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
110695d67482SBill Paul 
11070c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
110895d67482SBill Paul 
110995d67482SBill Paul 	return (0);
111095d67482SBill Paul }
111195d67482SBill Paul 
111295d67482SBill Paul /*
111395d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
111495d67482SBill Paul  */
111595d67482SBill Paul static int
11163f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
111795d67482SBill Paul {
11183f74909aSGleb Smirnoff 	int i, error = 0;
11193f74909aSGleb Smirnoff 	uint8_t byte = 0;
112095d67482SBill Paul 
112195d67482SBill Paul 	for (i = 0; i < cnt; i++) {
11223f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
11233f74909aSGleb Smirnoff 		if (error)
112495d67482SBill Paul 			break;
112595d67482SBill Paul 		*(dest + i) = byte;
112695d67482SBill Paul 	}
112795d67482SBill Paul 
11283f74909aSGleb Smirnoff 	return (error ? 1 : 0);
112995d67482SBill Paul }
113095d67482SBill Paul 
113195d67482SBill Paul static int
11323f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
113395d67482SBill Paul {
113495d67482SBill Paul 	struct bge_softc *sc;
1135a813ed78SPyun YongHyeon 	uint32_t val;
113695d67482SBill Paul 	int i;
113795d67482SBill Paul 
113895d67482SBill Paul 	sc = device_get_softc(dev);
113995d67482SBill Paul 
1140548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1141548c8f1aSPyun YongHyeon 		return (0);
1142548c8f1aSPyun YongHyeon 
1143a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1144a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1145a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1146a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1147a813ed78SPyun YongHyeon 		DELAY(80);
114837ceeb4dSPaul Saab 	}
114937ceeb4dSPaul Saab 
115095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
115195d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
115295d67482SBill Paul 
1153a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
115495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1155d5d23857SJung-uk Kim 		DELAY(10);
115695d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
1157a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
1158a813ed78SPyun YongHyeon 			DELAY(5);
1159a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
116095d67482SBill Paul 			break;
116195d67482SBill Paul 		}
1162a813ed78SPyun YongHyeon 	}
116395d67482SBill Paul 
116495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
11655fea260fSMarius Strobl 		device_printf(sc->bge_dev,
11665fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
11675fea260fSMarius Strobl 		    phy, reg, val);
116837ceeb4dSPaul Saab 		val = 0;
116995d67482SBill Paul 	}
117095d67482SBill Paul 
1171a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1172a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1173a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1174a813ed78SPyun YongHyeon 		DELAY(80);
117537ceeb4dSPaul Saab 	}
117637ceeb4dSPaul Saab 
1177548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1178548c8f1aSPyun YongHyeon 
117995d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
118095d67482SBill Paul 		return (0);
118195d67482SBill Paul 
11820c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
118395d67482SBill Paul }
118495d67482SBill Paul 
118595d67482SBill Paul static int
11863f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
118795d67482SBill Paul {
118895d67482SBill Paul 	struct bge_softc *sc;
118995d67482SBill Paul 	int i;
119095d67482SBill Paul 
119195d67482SBill Paul 	sc = device_get_softc(dev);
119295d67482SBill Paul 
119338cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
119438cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
119538cc658fSJohn Baldwin 		return (0);
119638cc658fSJohn Baldwin 
1197548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1198548c8f1aSPyun YongHyeon 		return (0);
1199548c8f1aSPyun YongHyeon 
1200a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1201a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1202a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1203a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1204a813ed78SPyun YongHyeon 		DELAY(80);
120537ceeb4dSPaul Saab 	}
120637ceeb4dSPaul Saab 
120795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
120895d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
120995d67482SBill Paul 
121095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1211d5d23857SJung-uk Kim 		DELAY(10);
121238cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
121338cc658fSJohn Baldwin 			DELAY(5);
121438cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
121595d67482SBill Paul 			break;
1216d5d23857SJung-uk Kim 		}
121738cc658fSJohn Baldwin 	}
1218d5d23857SJung-uk Kim 
1219a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1220a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1221a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1222a813ed78SPyun YongHyeon 		DELAY(80);
1223a813ed78SPyun YongHyeon 	}
1224a813ed78SPyun YongHyeon 
1225548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1226548c8f1aSPyun YongHyeon 
1227a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
122838cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
12292246e8c6SPyun YongHyeon 		    "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
123038cc658fSJohn Baldwin 		    phy, reg, val);
123137ceeb4dSPaul Saab 
123295d67482SBill Paul 	return (0);
123395d67482SBill Paul }
123495d67482SBill Paul 
123595d67482SBill Paul static void
12363f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
123795d67482SBill Paul {
123895d67482SBill Paul 	struct bge_softc *sc;
123995d67482SBill Paul 	struct mii_data *mii;
1240a0a03d1eSPyun YongHyeon 	uint32_t mac_mode, rx_mode, tx_mode;
1241e4146b95SPyun YongHyeon 
124295d67482SBill Paul 	sc = device_get_softc(dev);
1243e4146b95SPyun YongHyeon 	if ((sc->bge_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1244e4146b95SPyun YongHyeon 		return;
124595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
124695d67482SBill Paul 
1247d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1248d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
1249d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
1250d4f5240aSPyun YongHyeon 		case IFM_10_T:
1251d4f5240aSPyun YongHyeon 		case IFM_100_TX:
1252d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
1253d4f5240aSPyun YongHyeon 			break;
1254d4f5240aSPyun YongHyeon 		case IFM_1000_T:
1255d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
1256d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
1257d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1258d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
1259d4f5240aSPyun YongHyeon 			else
1260d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
1261d4f5240aSPyun YongHyeon 			break;
1262d4f5240aSPyun YongHyeon 		default:
1263d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
1264d4f5240aSPyun YongHyeon 			break;
1265d4f5240aSPyun YongHyeon 		}
1266d4f5240aSPyun YongHyeon 	} else
1267d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
1268d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
1269d4f5240aSPyun YongHyeon 		return;
1270a0a03d1eSPyun YongHyeon 
1271a0a03d1eSPyun YongHyeon 	/*
1272a0a03d1eSPyun YongHyeon 	 * APE firmware touches these registers to keep the MAC
1273a0a03d1eSPyun YongHyeon 	 * connected to the outside world.  Try to keep the
1274a0a03d1eSPyun YongHyeon 	 * accesses atomic.
1275a0a03d1eSPyun YongHyeon 	 */
1276a0a03d1eSPyun YongHyeon 
1277a0a03d1eSPyun YongHyeon 	/* Set the port mode (MII/GMII) to match the link speed. */
1278a0a03d1eSPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
1279a0a03d1eSPyun YongHyeon 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
1280a0a03d1eSPyun YongHyeon 	tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
1281a0a03d1eSPyun YongHyeon 	rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
1282a0a03d1eSPyun YongHyeon 
1283ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1284ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1285a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_GMII;
12863f74909aSGleb Smirnoff 	else
1287a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_MII;
128895d67482SBill Paul 
1289a0a03d1eSPyun YongHyeon 	/* Set MAC flow control behavior to match link flow control settings. */
1290a0a03d1eSPyun YongHyeon 	tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
1291a0a03d1eSPyun YongHyeon 	rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
12924951ca86SPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1293a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1294a0a03d1eSPyun YongHyeon 			tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
1295a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1296a0a03d1eSPyun YongHyeon 			rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
1297a0a03d1eSPyun YongHyeon 	} else
1298a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
1299a0a03d1eSPyun YongHyeon 
1300a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
13019b80ffe7SPyun YongHyeon 	DELAY(40);
1302a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
1303a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
130495d67482SBill Paul }
130595d67482SBill Paul 
130695d67482SBill Paul /*
130795d67482SBill Paul  * Intialize a standard receive ring descriptor.
130895d67482SBill Paul  */
130995d67482SBill Paul static int
1310943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
131195d67482SBill Paul {
1312943787f3SPyun YongHyeon 	struct mbuf *m;
131395d67482SBill Paul 	struct bge_rx_bd *r;
1314a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
1315943787f3SPyun YongHyeon 	bus_dmamap_t map;
1316a23634a1SPyun YongHyeon 	int error, nsegs;
131795d67482SBill Paul 
1318f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
1319f5459d4cSPyun YongHyeon 	    (sc->bge_ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
1320f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
1321c6499eccSGleb Smirnoff 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1322f5459d4cSPyun YongHyeon 		if (m == NULL)
1323f5459d4cSPyun YongHyeon 			return (ENOBUFS);
1324f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1325f5459d4cSPyun YongHyeon 	} else {
1326c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1327943787f3SPyun YongHyeon 		if (m == NULL)
132895d67482SBill Paul 			return (ENOBUFS);
1329943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
1330f5459d4cSPyun YongHyeon 	}
1331652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1332943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
1333943787f3SPyun YongHyeon 
13340ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
1335943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
1336a23634a1SPyun YongHyeon 	if (error != 0) {
1337943787f3SPyun YongHyeon 		m_freem(m);
1338a23634a1SPyun YongHyeon 		return (error);
1339f41ac2beSBill Paul 	}
1340943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1341943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1342943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1343943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1344943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
1345943787f3SPyun YongHyeon 	}
1346943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
1347943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1348943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
1349943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
1350e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1351943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1352a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1353a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1354e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1355a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1356e907febfSPyun YongHyeon 	r->bge_idx = i;
1357f41ac2beSBill Paul 
13580ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1359943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
136095d67482SBill Paul 
136195d67482SBill Paul 	return (0);
136295d67482SBill Paul }
136395d67482SBill Paul 
136495d67482SBill Paul /*
136595d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
136695d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
136795d67482SBill Paul  */
136895d67482SBill Paul static int
1369943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
137095d67482SBill Paul {
13711be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1372943787f3SPyun YongHyeon 	bus_dmamap_t map;
13731be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1374943787f3SPyun YongHyeon 	struct mbuf *m;
1375943787f3SPyun YongHyeon 	int error, nsegs;
137695d67482SBill Paul 
1377c6499eccSGleb Smirnoff 	MGETHDR(m, M_NOWAIT, MT_DATA);
1378943787f3SPyun YongHyeon 	if (m == NULL)
137995d67482SBill Paul 		return (ENOBUFS);
138095d67482SBill Paul 
1381c6499eccSGleb Smirnoff 	m_cljget(m, M_NOWAIT, MJUM9BYTES);
1382943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
1383943787f3SPyun YongHyeon 		m_freem(m);
138495d67482SBill Paul 		return (ENOBUFS);
138595d67482SBill Paul 	}
1386943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1387652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1388943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
13891be6acb7SGleb Smirnoff 
13901be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1391943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1392943787f3SPyun YongHyeon 	if (error != 0) {
1393943787f3SPyun YongHyeon 		m_freem(m);
13941be6acb7SGleb Smirnoff 		return (error);
1395f7cea149SGleb Smirnoff 	}
13961be6acb7SGleb Smirnoff 
1397aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1398943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1399943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1400943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1401943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1402943787f3SPyun YongHyeon 	}
1403943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1404943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1405943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1406943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1407943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1408e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1409e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1410e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1411e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1412e0b7b101SPyun YongHyeon 
14131be6acb7SGleb Smirnoff 	/*
14141be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
14151be6acb7SGleb Smirnoff 	 */
1416943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
14174e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
14184e7ba1abSGleb Smirnoff 	r->bge_idx = i;
14194e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
14204e7ba1abSGleb Smirnoff 	switch (nsegs) {
14214e7ba1abSGleb Smirnoff 	case 4:
14224e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
14234e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
14244e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1425e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
14264e7ba1abSGleb Smirnoff 	case 3:
1427e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1428e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1429e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1430e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
14314e7ba1abSGleb Smirnoff 	case 2:
14324e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
14334e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
14344e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1435e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
14364e7ba1abSGleb Smirnoff 	case 1:
14374e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
14384e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
14394e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1440e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
14414e7ba1abSGleb Smirnoff 		break;
14424e7ba1abSGleb Smirnoff 	default:
14434e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
14444e7ba1abSGleb Smirnoff 	}
1445f41ac2beSBill Paul 
1446a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1447943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
144895d67482SBill Paul 
144995d67482SBill Paul 	return (0);
145095d67482SBill Paul }
145195d67482SBill Paul 
145295d67482SBill Paul static int
14533f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
145495d67482SBill Paul {
14553ee5d7daSPyun YongHyeon 	int error, i;
145695d67482SBill Paul 
1457e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
145803e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1459e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1460943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
14613ee5d7daSPyun YongHyeon 			return (error);
146203e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
14631888f324SPyun YongHyeon 	}
146495d67482SBill Paul 
1465f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1466d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1467f41ac2beSBill Paul 
1468e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1469e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
147095d67482SBill Paul 
147195d67482SBill Paul 	return (0);
147295d67482SBill Paul }
147395d67482SBill Paul 
147495d67482SBill Paul static void
14753f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
147695d67482SBill Paul {
147795d67482SBill Paul 	int i;
147895d67482SBill Paul 
147995d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
148095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
14810ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1482e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1483e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
14840ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1485f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1486e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1487e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
148895d67482SBill Paul 		}
1489f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
149095d67482SBill Paul 		    sizeof(struct bge_rx_bd));
149195d67482SBill Paul 	}
149295d67482SBill Paul }
149395d67482SBill Paul 
149495d67482SBill Paul static int
14953f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
149695d67482SBill Paul {
149795d67482SBill Paul 	struct bge_rcb *rcb;
14983ee5d7daSPyun YongHyeon 	int error, i;
149995d67482SBill Paul 
1500e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
150103e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
150295d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1503943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
15043ee5d7daSPyun YongHyeon 			return (error);
150503e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
15061888f324SPyun YongHyeon 	}
150795d67482SBill Paul 
1508f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1509d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1510f41ac2beSBill Paul 
1511e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
151295d67482SBill Paul 
15138a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1514f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
15158a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
15168a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
151767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
151895d67482SBill Paul 
1519e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
152095d67482SBill Paul 
152195d67482SBill Paul 	return (0);
152295d67482SBill Paul }
152395d67482SBill Paul 
152495d67482SBill Paul static void
15253f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
152695d67482SBill Paul {
152795d67482SBill Paul 	int i;
152895d67482SBill Paul 
152995d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
153095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1531e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1532e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1533e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1534f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1535f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1536e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1537e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
153895d67482SBill Paul 		}
1539f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
15401be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
154195d67482SBill Paul 	}
154295d67482SBill Paul }
154395d67482SBill Paul 
154495d67482SBill Paul static void
15453f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
154695d67482SBill Paul {
154795d67482SBill Paul 	int i;
154895d67482SBill Paul 
1549f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
155095d67482SBill Paul 		return;
155195d67482SBill Paul 
155295d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
155395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
15540ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1555e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1556e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15570ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1558f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1559e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1560e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
156195d67482SBill Paul 		}
1562f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
156395d67482SBill Paul 		    sizeof(struct bge_tx_bd));
156495d67482SBill Paul 	}
156595d67482SBill Paul }
156695d67482SBill Paul 
156795d67482SBill Paul static int
15683f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
156995d67482SBill Paul {
157095d67482SBill Paul 	sc->bge_txcnt = 0;
157195d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
15723927098fSPaul Saab 
1573e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1574e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
15755c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1576e6bf277eSPyun YongHyeon 
157714bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
157814bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
157938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
158014bbd30fSGleb Smirnoff 
15813927098fSPaul Saab 	/* 5700 b2 errata */
1582e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
158338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
15843927098fSPaul Saab 
158514bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
158638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
15873927098fSPaul Saab 	/* 5700 b2 errata */
1588e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
158938cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
159095d67482SBill Paul 
159195d67482SBill Paul 	return (0);
159295d67482SBill Paul }
159395d67482SBill Paul 
159495d67482SBill Paul static void
15953e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
15963e9b1bcaSJung-uk Kim {
15973e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
15983e9b1bcaSJung-uk Kim 
15993e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
16003e9b1bcaSJung-uk Kim 
16013e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
16023e9b1bcaSJung-uk Kim 
160345ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
16043e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
160545ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16063e9b1bcaSJung-uk Kim 	else
160745ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16083e9b1bcaSJung-uk Kim }
16093e9b1bcaSJung-uk Kim 
16103e9b1bcaSJung-uk Kim static void
16113f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
161295d67482SBill Paul {
161395d67482SBill Paul 	struct ifnet *ifp;
161495d67482SBill Paul 	struct ifmultiaddr *ifma;
16153f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
161695d67482SBill Paul 	int h, i;
161795d67482SBill Paul 
16180f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
16190f9bd73bSSam Leffler 
1620fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
162195d67482SBill Paul 
162295d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
162395d67482SBill Paul 		for (i = 0; i < 4; i++)
16240c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
162595d67482SBill Paul 		return;
162695d67482SBill Paul 	}
162795d67482SBill Paul 
162895d67482SBill Paul 	/* First, zot all the existing filters. */
162995d67482SBill Paul 	for (i = 0; i < 4; i++)
163095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
163195d67482SBill Paul 
163295d67482SBill Paul 	/* Now program new ones. */
1633eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
163495d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
163595d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
163695d67482SBill Paul 			continue;
16370e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
16380c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
16390c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
164095d67482SBill Paul 	}
1641eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
164295d67482SBill Paul 
164395d67482SBill Paul 	for (i = 0; i < 4; i++)
164495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
164595d67482SBill Paul }
164695d67482SBill Paul 
16478cb1383cSDoug Ambrisko static void
1648cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1649cb2eacc7SYaroslav Tykhiy {
1650cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1651cb2eacc7SYaroslav Tykhiy 
1652cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1653cb2eacc7SYaroslav Tykhiy 
1654cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1655cb2eacc7SYaroslav Tykhiy 
1656cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1657cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1658cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1659cb2eacc7SYaroslav Tykhiy 	else
1660cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1661cb2eacc7SYaroslav Tykhiy }
1662cb2eacc7SYaroslav Tykhiy 
1663cb2eacc7SYaroslav Tykhiy static void
1664797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
16658cb1383cSDoug Ambrisko {
1666797ab05eSPyun YongHyeon 
16678cb1383cSDoug Ambrisko 	/*
16688cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
16698cb1383cSDoug Ambrisko 	 */
16708cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1671888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
16728cb1383cSDoug Ambrisko 
16738cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16748cb1383cSDoug Ambrisko 		switch (type) {
16758cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1676224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1677224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
16788cb1383cSDoug Ambrisko 			break;
1679548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1680224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1681224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
16828cb1383cSDoug Ambrisko 			break;
1683548c8f1aSPyun YongHyeon 		case BGE_RESET_SUSPEND:
1684548c8f1aSPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1685548c8f1aSPyun YongHyeon 			    BGE_FW_DRV_STATE_SUSPEND);
1686548c8f1aSPyun YongHyeon 			break;
16878cb1383cSDoug Ambrisko 		}
16888cb1383cSDoug Ambrisko 	}
1689548c8f1aSPyun YongHyeon 
1690548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1691548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
16928cb1383cSDoug Ambrisko }
16938cb1383cSDoug Ambrisko 
16948cb1383cSDoug Ambrisko static void
1695797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
16968cb1383cSDoug Ambrisko {
1697797ab05eSPyun YongHyeon 
16988cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16998cb1383cSDoug Ambrisko 		switch (type) {
17008cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1701224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1702224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
17038cb1383cSDoug Ambrisko 			/* START DONE */
17048cb1383cSDoug Ambrisko 			break;
1705548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1706224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1707224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
17088cb1383cSDoug Ambrisko 			break;
17098cb1383cSDoug Ambrisko 		}
17108cb1383cSDoug Ambrisko 	}
1711548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_SHUTDOWN)
1712548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17138cb1383cSDoug Ambrisko }
17148cb1383cSDoug Ambrisko 
17158cb1383cSDoug Ambrisko static void
1716797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
17178cb1383cSDoug Ambrisko {
1718797ab05eSPyun YongHyeon 
17198cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17208cb1383cSDoug Ambrisko 		switch (type) {
17218cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1722224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1723224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
17248cb1383cSDoug Ambrisko 			break;
1725548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1726224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1727224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
17288cb1383cSDoug Ambrisko 			break;
17298cb1383cSDoug Ambrisko 		}
17308cb1383cSDoug Ambrisko 	}
17318cb1383cSDoug Ambrisko }
17328cb1383cSDoug Ambrisko 
1733797ab05eSPyun YongHyeon static void
1734797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
17358cb1383cSDoug Ambrisko {
17368cb1383cSDoug Ambrisko 	int i;
17378cb1383cSDoug Ambrisko 
17388cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17393c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
17403fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
17419931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
17428cb1383cSDoug Ambrisko 
17438cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
17449931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
17459931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
17468cb1383cSDoug Ambrisko 				break;
17478cb1383cSDoug Ambrisko 			DELAY(10);
17488cb1383cSDoug Ambrisko 		}
17498cb1383cSDoug Ambrisko 	}
17508cb1383cSDoug Ambrisko }
17518cb1383cSDoug Ambrisko 
175250515680SPyun YongHyeon static uint32_t
175350515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
175450515680SPyun YongHyeon {
175550515680SPyun YongHyeon 	uint32_t dma_options;
175650515680SPyun YongHyeon 
175750515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
175850515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
175950515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
176050515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
176150515680SPyun YongHyeon #endif
176250515680SPyun YongHyeon 	return (dma_options);
176350515680SPyun YongHyeon }
176450515680SPyun YongHyeon 
176595d67482SBill Paul /*
1766c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
176795d67482SBill Paul  */
176895d67482SBill Paul static int
17693f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
177095d67482SBill Paul {
177150515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1772fbc374afSPyun YongHyeon 	uint16_t val;
177395d67482SBill Paul 	int i;
177495d67482SBill Paul 
17758cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
17761108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
17771108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
17781108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
17791108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
178095d67482SBill Paul 
178195d67482SBill Paul 	/*
178295d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
178395d67482SBill Paul 	 * internal memory.
178495d67482SBill Paul 	 */
178595d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
17863f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
178795d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
178895d67482SBill Paul 
178995d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
17903f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
179195d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
179295d67482SBill Paul 
1793fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1794fbc374afSPyun YongHyeon 		/*
1795d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1796fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1797fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1798fbc374afSPyun YongHyeon 		 */
1799fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1800fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1801fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1802fbc374afSPyun YongHyeon 	}
1803fbc374afSPyun YongHyeon 
1804f8bb33c3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765 ||
1805f8bb33c3SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57766) {
1806f8bb33c3SPyun YongHyeon 		/*
1807f8bb33c3SPyun YongHyeon 		 * For the 57766 and non Ax versions of 57765, bootcode
1808f8bb33c3SPyun YongHyeon 		 * needs to setup the PCIE Fast Training Sequence (FTS)
1809f8bb33c3SPyun YongHyeon 		 * value to prevent transmit hangs.
1810f8bb33c3SPyun YongHyeon 		 */
1811f8bb33c3SPyun YongHyeon 		if (sc->bge_chiprev != BGE_CHIPREV_57765_AX) {
1812f8bb33c3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL,
1813f8bb33c3SPyun YongHyeon 			    CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL) |
1814f8bb33c3SPyun YongHyeon 			    BGE_CPMU_PADRNG_CTL_RDIV2);
1815f8bb33c3SPyun YongHyeon 		}
1816f8bb33c3SPyun YongHyeon 	}
1817f8bb33c3SPyun YongHyeon 
1818186f842bSJung-uk Kim 	/*
1819186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1820186f842bSJung-uk Kim 	 */
1821186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1822186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1823652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
182448630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
182548630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
182648630d79SPyun YongHyeon 		else
1827186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1828652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
18294c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1830186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1831186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1832186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1833186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1834186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1835186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1836cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1837cbb2b2feSPyun YongHyeon 			/*
1838cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1839cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1840cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1841cbb2b2feSPyun YongHyeon 			 * register.
1842cbb2b2feSPyun YongHyeon 			 */
1843cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1844cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1845186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1846186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1847186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1848186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1849186f842bSJung-uk Kim 		} else {
1850186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1851186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1852186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
18530c8aa4eaSJung-uk Kim 			    0x0F;
1854186f842bSJung-uk Kim 		}
1855e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1856e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
18573f74909aSGleb Smirnoff 			uint32_t tmp;
18585cba12d3SPaul Saab 
1859186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
18600c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1861186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1862186f842bSJung-uk Kim 				dma_rw_ctl |=
1863186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
18645cba12d3SPaul Saab 
1865186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1866186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1867186f842bSJung-uk Kim 		}
1868186f842bSJung-uk Kim 	} else {
1869186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1870186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1871186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1872186f842bSJung-uk Kim 
1873186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1874186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1875186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1876186f842bSJung-uk Kim 	}
1877186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1878186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1879186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1880186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1881e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1882186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
18835cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1884b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18851108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1886b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1887b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1888bbe2ca75SPyun YongHyeon 		/*
1889bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1890bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1891bbe2ca75SPyun YongHyeon 		 * disabled.
1892bbe2ca75SPyun YongHyeon 		 */
18932927f01fSPyun YongHyeon 		if (!BGE_IS_57765_PLUS(sc) &&
18942927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
18952927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5762)
1896bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1897b4a256acSPyun YongHyeon 	}
18985cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
189995d67482SBill Paul 
190095d67482SBill Paul 	/*
190195d67482SBill Paul 	 * Set up general mode register.
190295d67482SBill Paul 	 */
1903548c8f1aSPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc);
19042927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
19052927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
1906548c8f1aSPyun YongHyeon 		/* Retain Host-2-BMC settings written by APE firmware. */
1907548c8f1aSPyun YongHyeon 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1908548c8f1aSPyun YongHyeon 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1909548c8f1aSPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1910548c8f1aSPyun YongHyeon 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1911548c8f1aSPyun YongHyeon 	}
1912548c8f1aSPyun YongHyeon 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1913548c8f1aSPyun YongHyeon 	    BGE_MODECTL_TX_NO_PHDR_CSUM;
191495d67482SBill Paul 
191595d67482SBill Paul 	/*
191690447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
191790447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
191890447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
191990447aadSMarius Strobl 	 * certain bridges.
192090447aadSMarius Strobl 	 */
192190447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
192290447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
192350515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
192490447aadSMarius Strobl 
192590447aadSMarius Strobl 	/*
19268cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
19278cb1383cSDoug Ambrisko 	 */
19288cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
192950515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
193050515680SPyun YongHyeon 
193150515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
19328cb1383cSDoug Ambrisko 
19338cb1383cSDoug Ambrisko 	/*
1934ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1935c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1936c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
193795d67482SBill Paul 	 */
1938c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1939c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
194095d67482SBill Paul 
1941d7acafa1SMarius Strobl 	/* Set the timer prescaler (always 66 MHz). */
19420c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
194395d67482SBill Paul 
194438cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
194538cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
194638cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
194738cc658fSJohn Baldwin 
194838cc658fSJohn Baldwin 		/* Put PHY into ready state */
194938cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
195038cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
195138cc658fSJohn Baldwin 		DELAY(40);
195238cc658fSJohn Baldwin 	}
195338cc658fSJohn Baldwin 
195495d67482SBill Paul 	return (0);
195595d67482SBill Paul }
195695d67482SBill Paul 
195795d67482SBill Paul static int
19583f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
195995d67482SBill Paul {
196095d67482SBill Paul 	struct bge_rcb *rcb;
1961e907febfSPyun YongHyeon 	bus_size_t vrcb;
1962e907febfSPyun YongHyeon 	bge_hostaddr taddr;
19632927f01fSPyun YongHyeon 	uint32_t dmactl, rdmareg, val;
19648a315a6dSPyun YongHyeon 	int i, limit;
196595d67482SBill Paul 
196695d67482SBill Paul 	/*
196795d67482SBill Paul 	 * Initialize the memory window pointer register so that
196895d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
196995d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
197095d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
197195d67482SBill Paul 	 */
197295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
197395d67482SBill Paul 
1974822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1975822f63fcSBill Paul 
19767ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
197795d67482SBill Paul 		/* Configure mbuf memory pool */
19780dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1979822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1980822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1981822f63fcSBill Paul 		else
198295d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
198395d67482SBill Paul 
198495d67482SBill Paul 		/* Configure DMA resource pool */
19850434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
19860434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
198795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
19880434d1b8SBill Paul 	}
198995d67482SBill Paul 
199095d67482SBill Paul 	/* Configure mbuf pool watermarks */
199150515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
19921108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
19931108273aSPyun YongHyeon 		if (sc->bge_ifp->if_mtu > ETHERMTU) {
19941108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
19951108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
19961108273aSPyun YongHyeon 		} else {
19971108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
19981108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
19991108273aSPyun YongHyeon 		}
20001108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
2001fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
2002fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
2003fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
200438cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
200538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
200638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
200738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
200838cc658fSJohn Baldwin 	} else {
200938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
201038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
201138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
201238cc658fSJohn Baldwin 	}
201395d67482SBill Paul 
201495d67482SBill Paul 	/* Configure DMA resource watermarks */
201595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
201695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
201795d67482SBill Paul 
201895d67482SBill Paul 	/* Enable buffer manager */
2019bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
2020bbe2ca75SPyun YongHyeon 	/*
2021bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
2022bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
2023bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
2024bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
2025bbe2ca75SPyun YongHyeon 	 */
2026bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2027bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
2028bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
202995d67482SBill Paul 
203095d67482SBill Paul 	/* Poll for buffer manager start indication */
203195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2032d5d23857SJung-uk Kim 		DELAY(10);
20330c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
203495d67482SBill Paul 			break;
203595d67482SBill Paul 	}
203695d67482SBill Paul 
203795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
20385a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
203995d67482SBill Paul 		return (ENXIO);
204095d67482SBill Paul 	}
204195d67482SBill Paul 
204295d67482SBill Paul 	/* Enable flow-through queues */
20430c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
204495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
204595d67482SBill Paul 
204695d67482SBill Paul 	/* Wait until queue initialization is complete */
204795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2048d5d23857SJung-uk Kim 		DELAY(10);
204995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
205095d67482SBill Paul 			break;
205195d67482SBill Paul 	}
205295d67482SBill Paul 
205395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2054fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
205595d67482SBill Paul 		return (ENXIO);
205695d67482SBill Paul 	}
205795d67482SBill Paul 
20588a315a6dSPyun YongHyeon 	/*
20598a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
20608a315a6dSPyun YongHyeon 	 *
20618a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
20628a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
20638a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
20648a315a6dSPyun YongHyeon 	 *
20658a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
20668a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
20678a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
20688a315a6dSPyun YongHyeon 	 *   to the controller.
20698a315a6dSPyun YongHyeon 	 *
20708a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
20718a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
20728a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
20738a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
20748a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
20758a315a6dSPyun YongHyeon 	 *   be disabled.
20768a315a6dSPyun YongHyeon 	 *
20778a315a6dSPyun YongHyeon 	 * Receive Return Ring
20788a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
20798a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
20808a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
20818a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
20828a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
20838a315a6dSPyun YongHyeon 	 *
20848a315a6dSPyun YongHyeon 	 * Send Ring
20858a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
20868a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
20878a315a6dSPyun YongHyeon 	 */
20888a315a6dSPyun YongHyeon 
20898a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
2090f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2091f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
2092f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2093f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
2094f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2095f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2096f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
20971108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
20981108273aSPyun YongHyeon 		/*
20991108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
21001108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
21011108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
21021108273aSPyun YongHyeon 		 * Bit 0     : Reserved
21031108273aSPyun YongHyeon 		 */
21041108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
21051108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
21061108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
21078a315a6dSPyun YongHyeon 		/*
21088a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
21098a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
21108a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
21118a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21128a315a6dSPyun YongHyeon 		 */
21130434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
21148a315a6dSPyun YongHyeon 	} else {
21158a315a6dSPyun YongHyeon 		/*
21168a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
21178a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
21188a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
21198a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
21208a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21218a315a6dSPyun YongHyeon 		 */
21220434d1b8SBill Paul 		rcb->bge_maxlen_flags =
21230434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
21248a315a6dSPyun YongHyeon 	}
2125bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
212650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
212750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21281108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
21291108273aSPyun YongHyeon 	else
213095d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
21318a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
21320c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
21330c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
213467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
213567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
213695d67482SBill Paul 
21378a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
21388a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
21398a315a6dSPyun YongHyeon 
214095d67482SBill Paul 	/*
21418a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
21428a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
21438a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
214495d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
214595d67482SBill Paul 	 * high enough to require it).
214695d67482SBill Paul 	 */
21474c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2148f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
21498a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
2150f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
2151f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2152f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
2153f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2154f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2155f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2156f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
21571be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
21581be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2159bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
216050515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
216150515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21621108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
21631108273aSPyun YongHyeon 		else
216495d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
216567111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
216667111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
216767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
216867111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
21698a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
21700434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
21710434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
217267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
21738a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
21748a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
21758a315a6dSPyun YongHyeon 	}
217695d67482SBill Paul 
21778a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
21785e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
2179f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
218067111612SJohn Polstra 		rcb->bge_maxlen_flags =
218167111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
21820434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
21830434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
21848a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
21858a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
21860434d1b8SBill Paul 	}
218795d67482SBill Paul 
2188ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2189ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2190427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2191427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2192427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
21938d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
21948d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2195ca4f8986SPyun YongHyeon 	}
219695d67482SBill Paul 	/*
21978a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
21988a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
21998a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
22008a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
22018a315a6dSPyun YongHyeon 	 *
220295d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
220395d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
220495d67482SBill Paul 	 * each ring.
22059ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
22069ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
22079ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
22089ba784dbSScott Long 	 * are reports that it might not need to be so strict.
220938cc658fSJohn Baldwin 	 *
221038cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
221138cc658fSJohn Baldwin 	 * well.
221295d67482SBill Paul 	 */
22135345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
22146f8718a3SScott Long 		val = 8;
22156f8718a3SScott Long 	else
22166f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
22176f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
22182a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
22192a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
22202a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
22211108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
22221108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
22231108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
22241108273aSPyun YongHyeon 	}
222595d67482SBill Paul 
222695d67482SBill Paul 	/*
22278a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
22288a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
22298a315a6dSPyun YongHyeon 	 * located in NIC memory.
223095d67482SBill Paul 	 */
22318a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
22328a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
22338a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
22342927f01fSPyun YongHyeon 	else if (BGE_IS_57765_PLUS(sc) ||
22352927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
22362927f01fSPyun YongHyeon 		limit = 2;
22372927f01fSPyun YongHyeon 	else if (BGE_IS_5717_PLUS(sc))
22382927f01fSPyun YongHyeon 		limit = 4;
22398a315a6dSPyun YongHyeon 	else
22408a315a6dSPyun YongHyeon 		limit = 1;
2241e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
22428a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2243e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2244e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2245e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2246e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
224795d67482SBill Paul 	}
224895d67482SBill Paul 
22498a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
2250e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2251e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2252e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2253e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2254bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
225550515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
225650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
22571108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
22581108273aSPyun YongHyeon 	else
2259e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2260e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2261e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2262e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
226395d67482SBill Paul 
22648a315a6dSPyun YongHyeon 	/*
22658a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
22668a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
22678a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
22688a315a6dSPyun YongHyeon 	 */
2269bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
227050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
227150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
22721108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
22731108273aSPyun YongHyeon 		limit = 16;
22741108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
22758a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
2276b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
22772927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762 ||
2278fe26ad88SPyun YongHyeon 	    BGE_IS_57765_PLUS(sc))
22798a315a6dSPyun YongHyeon 		limit = 4;
22808a315a6dSPyun YongHyeon 	else
22818a315a6dSPyun YongHyeon 		limit = 1;
22828a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
2283e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
22848a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2285e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2286e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2287e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
22888a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
2289e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
229038cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
22913f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
2292e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
229395d67482SBill Paul 	}
229495d67482SBill Paul 
229595d67482SBill Paul 	/*
22968a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
22978a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
22988a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
229995d67482SBill Paul 	 */
2300e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2301e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2302e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2303e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
23048a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2305e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2306e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
230795d67482SBill Paul 
230895d67482SBill Paul 	/* Set random backoff seed for TX */
230995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
23100a2cc827SPyun YongHyeon 	    (IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
23114a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
23120a2cc827SPyun YongHyeon 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5]) &
231395d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
231495d67482SBill Paul 
231595d67482SBill Paul 	/* Set inter-packet gap */
231650515680SPyun YongHyeon 	val = 0x2620;
23172927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
23182927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
231950515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
232050515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
232150515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
232295d67482SBill Paul 
232395d67482SBill Paul 	/*
232495d67482SBill Paul 	 * Specify which ring to use for packets that don't match
232595d67482SBill Paul 	 * any RX rules.
232695d67482SBill Paul 	 */
232795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
232895d67482SBill Paul 
232995d67482SBill Paul 	/*
233095d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
233195d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
233295d67482SBill Paul 	 */
233395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
233495d67482SBill Paul 
233595d67482SBill Paul 	/* Inialize RX list placement stats mask. */
23360c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
233795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
233895d67482SBill Paul 
233995d67482SBill Paul 	/* Disable host coalescing until we get it set up */
234095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
234195d67482SBill Paul 
234295d67482SBill Paul 	/* Poll to make sure it's shut down. */
234395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2344d5d23857SJung-uk Kim 		DELAY(10);
234595d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
234695d67482SBill Paul 			break;
234795d67482SBill Paul 	}
234895d67482SBill Paul 
234995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2350fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2351fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
235295d67482SBill Paul 		return (ENXIO);
235395d67482SBill Paul 	}
235495d67482SBill Paul 
235595d67482SBill Paul 	/* Set up host coalescing defaults */
235695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
235795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
235895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
235995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
23607ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
236195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
236295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
23630434d1b8SBill Paul 	}
2364b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2365b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
236695d67482SBill Paul 
236795d67482SBill Paul 	/* Set up address of statistics block */
23687ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
2369f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2370f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
237195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2372f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
23730434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
237495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
23750434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
23760434d1b8SBill Paul 	}
23770434d1b8SBill Paul 
23780434d1b8SBill Paul 	/* Set up address of status block */
2379f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2380f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
238195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2382f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
238395d67482SBill Paul 
238430f57f61SPyun YongHyeon 	/* Set up status block size. */
238530f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2386864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
238730f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2388864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2389864104feSPyun YongHyeon 	} else {
239030f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2391864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2392864104feSPyun YongHyeon 	}
2393864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2394864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2395864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
239630f57f61SPyun YongHyeon 
239795d67482SBill Paul 	/* Turn on host coalescing state machine */
239830f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
239995d67482SBill Paul 
240095d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
240195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
240295d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
240395d67482SBill Paul 
240495d67482SBill Paul 	/* Turn on RX list placement state machine */
240595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
240695d67482SBill Paul 
240795d67482SBill Paul 	/* Turn on RX list selector state machine. */
24087ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
240995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
241095d67482SBill Paul 
24112246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2412ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2413ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2414ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2415ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2416ea3b4127SPyun YongHyeon 
2417ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2418ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2419ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2420ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2421ea3b4127SPyun YongHyeon 	else
2422ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2423ea3b4127SPyun YongHyeon 
2424548c8f1aSPyun YongHyeon 	/* Allow APE to send/receive frames. */
2425548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2426548c8f1aSPyun YongHyeon 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2427548c8f1aSPyun YongHyeon 
2428ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
24299b80ffe7SPyun YongHyeon 	DELAY(40);
243095d67482SBill Paul 
243195d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
243291bd90d8SPyun YongHyeon 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
243395d67482SBill Paul 
243495d67482SBill Paul #ifdef notdef
243595d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
243695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
243795d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
243895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
243995d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
244095d67482SBill Paul #endif
244195d67482SBill Paul 
244295d67482SBill Paul 	/* Turn on DMA completion state machine */
24437ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
244495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
244595d67482SBill Paul 
24466f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
24476f8718a3SScott Long 
24486f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2449a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
24503889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
24516f8718a3SScott Long 
24527aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
24537aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
24547aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
24557aa4b937SPyun YongHyeon 
245695d67482SBill Paul 	/* Turn on write DMA state machine */
24576f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
24584f09c4c7SMarius Strobl 	DELAY(40);
245995d67482SBill Paul 
246095d67482SBill Paul 	/* Turn on read DMA state machine */
24614f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
24621108273aSPyun YongHyeon 
24631108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
24641108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
24651108273aSPyun YongHyeon 
2466a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2467a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2468a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2469a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2470a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2471a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
24724f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
24734f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
24741108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2475ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
24761108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
24771108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
247855a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
247955a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
248055a24a05SPyun YongHyeon 	}
248150515680SPyun YongHyeon 
24822927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
24832927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
248450515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
248550515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2486e3215f76SPyun YongHyeon 		/*
2487e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2488e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2489e3215f76SPyun YongHyeon 		 */
2490e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2491e3215f76SPyun YongHyeon 	}
249250515680SPyun YongHyeon 
2493d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2494d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2495d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
24961108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
24972927f01fSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc) || BGE_IS_57765_PLUS(sc)) {
24982927f01fSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
24992927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL_REG2;
25002927f01fSPyun YongHyeon 		else
25012927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL;
25022927f01fSPyun YongHyeon 		dmactl = CSR_READ_4(sc, rdmareg);
2503bbe2ca75SPyun YongHyeon 		/*
2504bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2505bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2506bbe2ca75SPyun YongHyeon 		 */
25072927f01fSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5719_A0 ||
25082927f01fSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
2509bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2510bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2511bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2512bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2513bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2514bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2515bbe2ca75SPyun YongHyeon 		}
2516d255f2a9SPyun YongHyeon 		/*
2517d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2518d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2519d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2520d255f2a9SPyun YongHyeon 		 */
25212927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, rdmareg, dmactl |
2522d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2523d255f2a9SPyun YongHyeon 	}
2524bbe2ca75SPyun YongHyeon 
2525e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2526bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2527bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2528bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2529bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2530e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2531e3215f76SPyun YongHyeon 		/*
2532e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2533e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2534e3215f76SPyun YongHyeon 		 */
2535e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2536e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2537e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2538e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
25392927f01fSPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5762) {
25402927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2,
25412927f01fSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2) |
25422927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
25432927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2544bbe2ca75SPyun YongHyeon 	}
2545bbe2ca75SPyun YongHyeon 
25464f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
25474f09c4c7SMarius Strobl 	DELAY(40);
254895d67482SBill Paul 
254929b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
255029b44b09SPyun YongHyeon 		for (i = 0; i < BGE_NUM_RDMA_CHANNELS / 2; i++) {
255129b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LENGTH + i * 4);
255229b44b09SPyun YongHyeon 			if ((val & 0xFFFF) > BGE_FRAMELEN)
255329b44b09SPyun YongHyeon 				break;
255429b44b09SPyun YongHyeon 			if (((val >> 16) & 0xFFFF) > BGE_FRAMELEN)
255529b44b09SPyun YongHyeon 				break;
255629b44b09SPyun YongHyeon 		}
255729b44b09SPyun YongHyeon 		if (i != BGE_NUM_RDMA_CHANNELS / 2) {
255829b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
255929b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
256029b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5719;
256129b44b09SPyun YongHyeon 			else
256229b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5720;
256329b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
256429b44b09SPyun YongHyeon 		}
256529b44b09SPyun YongHyeon 	}
256629b44b09SPyun YongHyeon 
256795d67482SBill Paul 	/* Turn on RX data completion state machine */
256895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
256995d67482SBill Paul 
257095d67482SBill Paul 	/* Turn on RX BD initiator state machine */
257195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
257295d67482SBill Paul 
257395d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
257495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
257595d67482SBill Paul 
257695d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25777ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
257895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
257995d67482SBill Paul 
258095d67482SBill Paul 	/* Turn on send BD completion state machine */
258195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
258295d67482SBill Paul 
258395d67482SBill Paul 	/* Turn on send data completion state machine */
2584a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2585a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2586a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2587a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
258895d67482SBill Paul 
258995d67482SBill Paul 	/* Turn on send data initiator state machine */
25901108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
25911108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
25921108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2593ca3f1187SPyun YongHyeon 	else
259495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
259595d67482SBill Paul 
259695d67482SBill Paul 	/* Turn on send BD initiator state machine */
259795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
259895d67482SBill Paul 
259995d67482SBill Paul 	/* Turn on send BD selector state machine */
260095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
260195d67482SBill Paul 
26020c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
260395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
260495d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
260595d67482SBill Paul 
260695d67482SBill Paul 	/* ack/clear link change events */
260795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26080434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26090434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2610f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
261195d67482SBill Paul 
26126ede2cfaSPyun YongHyeon 	/*
26136ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
26146ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
26156ede2cfaSPyun YongHyeon 	 */
2616652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
261795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2618a1d52896SBill Paul 	} else {
26197ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
26207ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
26217ed3f0f0SPyun YongHyeon 			DELAY(80);
26227ed3f0f0SPyun YongHyeon 		}
26231f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
26244c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2625a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2626a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2627a1d52896SBill Paul 	}
262895d67482SBill Paul 
26291f313773SOleg Bulyzhin 	/*
26301f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
26311f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
26321f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
26331f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
26341f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
26351f313773SOleg Bulyzhin 	 */
26361f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26371f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26381f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
26391f313773SOleg Bulyzhin 
264095d67482SBill Paul 	/* Enable link state change attentions. */
264195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
264295d67482SBill Paul 
264395d67482SBill Paul 	return (0);
264495d67482SBill Paul }
264595d67482SBill Paul 
2646d7acafa1SMarius Strobl static const struct bge_revision *
26474c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
26484c0da0ffSGleb Smirnoff {
26494c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
26504c0da0ffSGleb Smirnoff 
26514c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
26524c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
26534c0da0ffSGleb Smirnoff 			return (br);
26544c0da0ffSGleb Smirnoff 	}
26554c0da0ffSGleb Smirnoff 
26564c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
26574c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
26584c0da0ffSGleb Smirnoff 			return (br);
26594c0da0ffSGleb Smirnoff 	}
26604c0da0ffSGleb Smirnoff 
26614c0da0ffSGleb Smirnoff 	return (NULL);
26624c0da0ffSGleb Smirnoff }
26634c0da0ffSGleb Smirnoff 
2664d7acafa1SMarius Strobl static const struct bge_vendor *
26654c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26664c0da0ffSGleb Smirnoff {
26674c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26684c0da0ffSGleb Smirnoff 
26694c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26704c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26714c0da0ffSGleb Smirnoff 			return (v);
26724c0da0ffSGleb Smirnoff 
26734c0da0ffSGleb Smirnoff 	return (NULL);
26744c0da0ffSGleb Smirnoff }
26754c0da0ffSGleb Smirnoff 
2676d7acafa1SMarius Strobl static uint32_t
2677d7acafa1SMarius Strobl bge_chipid(device_t dev)
267895d67482SBill Paul {
2679978f2704SMarius Strobl 	uint32_t id;
268095d67482SBill Paul 
2681a5779553SStanislav Sedov 	id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2682a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
26831108273aSPyun YongHyeon 	if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
26841108273aSPyun YongHyeon 		/*
2685d7acafa1SMarius Strobl 		 * Find the ASCI revision.  Different chips use different
2686d7acafa1SMarius Strobl 		 * registers.
26871108273aSPyun YongHyeon 		 */
26881108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
26891108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
26901108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2691bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
269250515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
26932927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5725:
26942927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5727:
26952927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5762:
26961108273aSPyun YongHyeon 			id = pci_read_config(dev,
26971108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
26981108273aSPyun YongHyeon 			break;
2699b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2700fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57762:
2701b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2702fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57766:
2703b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
2704b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
2705b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2706b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2707b4a256acSPyun YongHyeon 			id = pci_read_config(dev,
2708b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2709b4a256acSPyun YongHyeon 			break;
27101108273aSPyun YongHyeon 		default:
2711d7acafa1SMarius Strobl 			id = pci_read_config(dev, BGE_PCI_PRODID_ASICREV, 4);
27121108273aSPyun YongHyeon 		}
27131108273aSPyun YongHyeon 	}
2714d7acafa1SMarius Strobl 	return (id);
2715d7acafa1SMarius Strobl }
2716d7acafa1SMarius Strobl 
2717d7acafa1SMarius Strobl /*
2718d7acafa1SMarius Strobl  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2719d7acafa1SMarius Strobl  * against our list and return its name if we find a match.
2720d7acafa1SMarius Strobl  *
2721d7acafa1SMarius Strobl  * Note that since the Broadcom controller contains VPD support, we
2722d7acafa1SMarius Strobl  * try to get the device name string from the controller itself instead
2723d7acafa1SMarius Strobl  * of the compiled-in string. It guarantees we'll always announce the
2724d7acafa1SMarius Strobl  * right product name. We fall back to the compiled-in string when
2725d7acafa1SMarius Strobl  * VPD is unavailable or corrupt.
2726d7acafa1SMarius Strobl  */
2727d7acafa1SMarius Strobl static int
2728d7acafa1SMarius Strobl bge_probe(device_t dev)
2729d7acafa1SMarius Strobl {
2730d7acafa1SMarius Strobl 	char buf[96];
2731d7acafa1SMarius Strobl 	char model[64];
2732d7acafa1SMarius Strobl 	const struct bge_revision *br;
2733d7acafa1SMarius Strobl 	const char *pname;
2734d7acafa1SMarius Strobl 	struct bge_softc *sc;
2735d7acafa1SMarius Strobl 	const struct bge_type *t = bge_devs;
2736d7acafa1SMarius Strobl 	const struct bge_vendor *v;
2737d7acafa1SMarius Strobl 	uint32_t id;
2738d7acafa1SMarius Strobl 	uint16_t did, vid;
2739d7acafa1SMarius Strobl 
2740d7acafa1SMarius Strobl 	sc = device_get_softc(dev);
2741d7acafa1SMarius Strobl 	sc->bge_dev = dev;
2742d7acafa1SMarius Strobl 	vid = pci_get_vendor(dev);
2743d7acafa1SMarius Strobl 	did = pci_get_device(dev);
2744d7acafa1SMarius Strobl 	while(t->bge_vid != 0) {
2745d7acafa1SMarius Strobl 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2746d7acafa1SMarius Strobl 			id = bge_chipid(dev);
27474c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
2748852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2749852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
2750d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s", pname);
2751d7acafa1SMarius Strobl 			else {
2752d7acafa1SMarius Strobl 				v = bge_lookup_vendor(vid);
2753d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s %s",
2754d7acafa1SMarius Strobl 				    v != NULL ? v->v_name : "Unknown",
27557c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
27562ad1b396SMarius Strobl 				    "NetXtreme/NetLink Ethernet Controller");
2757d7acafa1SMarius Strobl 			}
2758d7acafa1SMarius Strobl 			snprintf(buf, sizeof(buf), "%s, %sASIC rev. %#08x",
2759d7acafa1SMarius Strobl 			    model, br != NULL ? "" : "unknown ", id);
27604c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
2761d7acafa1SMarius Strobl 			return (BUS_PROBE_DEFAULT);
276295d67482SBill Paul 		}
276395d67482SBill Paul 		t++;
276495d67482SBill Paul 	}
276595d67482SBill Paul 
276695d67482SBill Paul 	return (ENXIO);
276795d67482SBill Paul }
276895d67482SBill Paul 
2769f41ac2beSBill Paul static void
27703f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2771f41ac2beSBill Paul {
2772f41ac2beSBill Paul 	int i;
2773f41ac2beSBill Paul 
27743f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2775f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2776f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
27770ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2778f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2779f41ac2beSBill Paul 	}
2780943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2781943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2782943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2783f41ac2beSBill Paul 
27843f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2785f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2786f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2787f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2788f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2789f41ac2beSBill Paul 	}
2790943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2791943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2792943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2793f41ac2beSBill Paul 
27943f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2795f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2796f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
27970ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2798f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2799f41ac2beSBill Paul 	}
2800f41ac2beSBill Paul 
28010ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
28020ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2803c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2804c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
28050ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
28060ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2807f41ac2beSBill Paul 
28083f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2809e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2810e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2811e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2812e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2813f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2814f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2815f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2816f41ac2beSBill Paul 
2817f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2818f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2819f41ac2beSBill Paul 
28203f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2821e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2822e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2823e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2824e65bed95SPyun YongHyeon 
2825e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2826e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2827f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2828f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2829f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2830f41ac2beSBill Paul 
2831f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2832f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2833f41ac2beSBill Paul 
28343f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2835e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2836e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2837e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2838e65bed95SPyun YongHyeon 
2839e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2840e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2841f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2842f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2843f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2844f41ac2beSBill Paul 
2845f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2846f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2847f41ac2beSBill Paul 
28483f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2849e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2850e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2851e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2852e65bed95SPyun YongHyeon 
2853e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2854f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2855f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2856f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2857f41ac2beSBill Paul 
2858f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2859f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2860f41ac2beSBill Paul 
28613f74909aSGleb Smirnoff 	/* Destroy status block. */
2862e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2863e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2864e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2865e65bed95SPyun YongHyeon 
2866e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2867f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2868f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2869f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2870f41ac2beSBill Paul 
2871f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2872f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2873f41ac2beSBill Paul 
28743f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2875e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2876e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2877e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2878e65bed95SPyun YongHyeon 
2879e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2880f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2881f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2882f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2883f41ac2beSBill Paul 
2884f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2885f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2886f41ac2beSBill Paul 
28875b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
28885b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
28895b610048SPyun YongHyeon 
28903f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2891f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2892f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2893f41ac2beSBill Paul }
2894f41ac2beSBill Paul 
2895f41ac2beSBill Paul static int
28965b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
28975b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
28985b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2899f41ac2beSBill Paul {
29003f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
29015b610048SPyun YongHyeon 	int error;
2902f41ac2beSBill Paul 
29035b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2904fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
29055b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
29065b610048SPyun YongHyeon 	if (error != 0) {
29075b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29085b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
29095b610048SPyun YongHyeon 		return (ENOMEM);
29105b610048SPyun YongHyeon 	}
29115b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
29125b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
29135b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
29145b610048SPyun YongHyeon 	if (error != 0) {
29155b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29165b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
29175b610048SPyun YongHyeon 		return (ENOMEM);
29185b610048SPyun YongHyeon 	}
29195b610048SPyun YongHyeon 	/* Load the address of the ring. */
29205b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
29215b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
29225b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
29235b610048SPyun YongHyeon 	if (error != 0) {
29245b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29255b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
29265b610048SPyun YongHyeon 		return (ENOMEM);
29275b610048SPyun YongHyeon 	}
29285b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
29295b610048SPyun YongHyeon 	return (0);
29305b610048SPyun YongHyeon }
29315b610048SPyun YongHyeon 
29325b610048SPyun YongHyeon static int
29335b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
29345b610048SPyun YongHyeon {
29355b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2936fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
29375b610048SPyun YongHyeon 	int i, error;
2938f41ac2beSBill Paul 
2939f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2940f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2941f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2942f41ac2beSBill Paul 	/*
2943f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2944f41ac2beSBill Paul 	 */
29454eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2946f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
29474eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
29484eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2949e65bed95SPyun YongHyeon 	if (error != 0) {
2950fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2951fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2952e65bed95SPyun YongHyeon 		return (ENOMEM);
2953e65bed95SPyun YongHyeon 	}
2954e65bed95SPyun YongHyeon 
29555b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
29565b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
29575b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
29585b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
29595b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
29605b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
29615b610048SPyun YongHyeon 	if (error)
29625b610048SPyun YongHyeon 		return (error);
29635b610048SPyun YongHyeon 
29645b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
29655b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
29665b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
29675b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
29685b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
29695b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
29705b610048SPyun YongHyeon 	if (error)
29715b610048SPyun YongHyeon 		return (error);
29725b610048SPyun YongHyeon 
29735b610048SPyun YongHyeon 	/* Create tag for TX ring. */
29745b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
29755b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
29765b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
29775b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
29785b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
29795b610048SPyun YongHyeon 	if (error)
29805b610048SPyun YongHyeon 		return (error);
29815b610048SPyun YongHyeon 
2982f41ac2beSBill Paul 	/*
29835b610048SPyun YongHyeon 	 * Create tag for status block.
29845b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
29855b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
29865b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
29875b610048SPyun YongHyeon 	 * of configured number of ring.
2988f41ac2beSBill Paul 	 */
29895b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
29905b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
29915b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
29925b610048SPyun YongHyeon 	else
29935b610048SPyun YongHyeon 		sbsz = 32;
29945b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
29955b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
29965b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
29975b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
29985b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
29995b610048SPyun YongHyeon 	if (error)
30005b610048SPyun YongHyeon 		return (error);
30015b610048SPyun YongHyeon 
300212c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
300312c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
300412c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
300512c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
300612c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
300712c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
300812c65daeSPyun YongHyeon 	if (error)
300912c65daeSPyun YongHyeon 		return (error);
301012c65daeSPyun YongHyeon 
30115b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
30125b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30135b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
30145b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
30155b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
30165b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
30175b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
30185b610048SPyun YongHyeon 		if (error)
30195b610048SPyun YongHyeon 			return (error);
30205b610048SPyun YongHyeon 	}
30215b610048SPyun YongHyeon 
30225b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
3023d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
3024d2ffe15aSPyun YongHyeon 		/*
3025d2ffe15aSPyun YongHyeon 		 * XXX
3026d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
3027d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
3028062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
3029062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
3030d2ffe15aSPyun YongHyeon 		 */
3031062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3032d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
3033d2ffe15aSPyun YongHyeon 	}
3034fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
3035fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
3036fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
3037fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
30385b610048SPyun YongHyeon 	if (error != 0) {
30395b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
30405b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
30415b610048SPyun YongHyeon 		return (ENOMEM);
30425b610048SPyun YongHyeon 	}
30435b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
30441108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
3045ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
3046ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
3047ca3f1187SPyun YongHyeon 	} else {
3048ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
3049ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
3050ca3f1187SPyun YongHyeon 	}
30515b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
3052ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
3053ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
3054ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
3055f41ac2beSBill Paul 
3056f41ac2beSBill Paul 	if (error) {
30570ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
30580ac56796SPyun YongHyeon 		return (ENOMEM);
30590ac56796SPyun YongHyeon 	}
30600ac56796SPyun YongHyeon 
30615b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
3062f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
3063f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
3064f5459d4cSPyun YongHyeon 	else
3065f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
30665b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
3067f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
3068f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
30690ac56796SPyun YongHyeon 
30700ac56796SPyun YongHyeon 	if (error) {
30710ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3072f41ac2beSBill Paul 		return (ENOMEM);
3073f41ac2beSBill Paul 	}
3074f41ac2beSBill Paul 
30753f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3076943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3077943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3078943787f3SPyun YongHyeon 	if (error) {
3079943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3080943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3081943787f3SPyun YongHyeon 		return (ENOMEM);
3082943787f3SPyun YongHyeon 	}
3083f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
30840ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3085f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3086f41ac2beSBill Paul 		if (error) {
3087fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3088fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3089f41ac2beSBill Paul 			return (ENOMEM);
3090f41ac2beSBill Paul 		}
3091f41ac2beSBill Paul 	}
3092f41ac2beSBill Paul 
30933f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3094f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
30950ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3096f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3097f41ac2beSBill Paul 		if (error) {
3098fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30990ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3100f41ac2beSBill Paul 			return (ENOMEM);
3101f41ac2beSBill Paul 		}
3102f41ac2beSBill Paul 	}
3103f41ac2beSBill Paul 
31045b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
31054c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
31065b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
31078a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
31081be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
31091be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3110f41ac2beSBill Paul 		if (error) {
3111fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31123f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3113f41ac2beSBill Paul 			return (ENOMEM);
3114f41ac2beSBill Paul 		}
31153f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3116943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3117943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3118943787f3SPyun YongHyeon 		if (error) {
3119943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
31201b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3121943787f3SPyun YongHyeon 			return (ENOMEM);
3122943787f3SPyun YongHyeon 		}
3123f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3124f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3125f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3126f41ac2beSBill Paul 			if (error) {
3127fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
31283f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3129f41ac2beSBill Paul 				return (ENOMEM);
3130f41ac2beSBill Paul 			}
3131f41ac2beSBill Paul 		}
3132f41ac2beSBill Paul 	}
3133f41ac2beSBill Paul 
3134f41ac2beSBill Paul 	return (0);
3135f41ac2beSBill Paul }
3136f41ac2beSBill Paul 
3137bf6ef57aSJohn Polstra /*
3138bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3139bf6ef57aSJohn Polstra  */
3140bf6ef57aSJohn Polstra static int
3141bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3142bf6ef57aSJohn Polstra {
3143bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
314455aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3145bf6ef57aSJohn Polstra 
314655aaf894SMarius Strobl 	d = pci_get_domain(dev);
3147bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3148bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3149bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3150bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
315155aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3152bf6ef57aSJohn Polstra 			return (1);
3153bf6ef57aSJohn Polstra 	return (0);
3154bf6ef57aSJohn Polstra }
3155bf6ef57aSJohn Polstra 
3156bf6ef57aSJohn Polstra /*
3157bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3158bf6ef57aSJohn Polstra  */
3159bf6ef57aSJohn Polstra static int
3160bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3161bf6ef57aSJohn Polstra {
3162bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3163bf6ef57aSJohn Polstra 
3164d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
31655c952e8dSPyun YongHyeon 		return (0);
31665c952e8dSPyun YongHyeon 
31671108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
31681108273aSPyun YongHyeon #ifdef DEVICE_POLLING
31691108273aSPyun YongHyeon 	return (0);
31701108273aSPyun YongHyeon #endif
3171bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3172a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3173bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3174bf6ef57aSJohn Polstra 		/*
3175a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3176a8376f70SMarius Strobl 		 * configured in single-port mode.
3177bf6ef57aSJohn Polstra 		 */
3178bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3179bf6ef57aSJohn Polstra 			can_use_msi = 1;
3180bf6ef57aSJohn Polstra 		break;
3181bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3182bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3183bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3184bf6ef57aSJohn Polstra 			can_use_msi = 1;
3185bf6ef57aSJohn Polstra 		break;
3186a8376f70SMarius Strobl 	default:
3187a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3188bf6ef57aSJohn Polstra 			can_use_msi = 1;
3189bf6ef57aSJohn Polstra 	}
3190bf6ef57aSJohn Polstra 	return (can_use_msi);
3191bf6ef57aSJohn Polstra }
3192bf6ef57aSJohn Polstra 
319395d67482SBill Paul static int
3194062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3195062af0b0SPyun YongHyeon {
3196062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3197062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3198062af0b0SPyun YongHyeon 		const uint16_t vendor;
3199062af0b0SPyun YongHyeon 		const uint16_t device;
3200062af0b0SPyun YongHyeon 		const char *desc;
320129658c96SDimitry Andric 	} mbox_reorder_lists[] = {
3202062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3203062af0b0SPyun YongHyeon 	};
3204062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3205062af0b0SPyun YongHyeon 	device_t bus, dev;
320647f4a4dcSMarius Strobl 	int i;
3207062af0b0SPyun YongHyeon 
3208062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3209062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3210062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3211062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3212062af0b0SPyun YongHyeon 	for (;;) {
3213062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3214062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3215062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3216062af0b0SPyun YongHyeon 			break;
321747f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3218062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3219062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3220062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3221062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3222062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3223062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3224062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3225062af0b0SPyun YongHyeon 				return (1);
3226062af0b0SPyun YongHyeon 			}
3227062af0b0SPyun YongHyeon 		}
3228062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
3229062af0b0SPyun YongHyeon 			break;
3230062af0b0SPyun YongHyeon 	}
3231062af0b0SPyun YongHyeon 	return (0);
3232062af0b0SPyun YongHyeon }
3233062af0b0SPyun YongHyeon 
3234ea9c3a30SPyun YongHyeon static void
3235ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3236ea9c3a30SPyun YongHyeon {
3237ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3238ea9c3a30SPyun YongHyeon 
3239ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3240ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3241ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3242ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3243ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3244ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3245ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3246ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3247ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3248ea9c3a30SPyun YongHyeon 			clk = 133;
3249ea9c3a30SPyun YongHyeon 		else {
3250ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3251ea9c3a30SPyun YongHyeon 			switch (clk) {
3252ea9c3a30SPyun YongHyeon 			case 0:
3253ea9c3a30SPyun YongHyeon 				clk = 33;
3254ea9c3a30SPyun YongHyeon 				break;
3255ea9c3a30SPyun YongHyeon 			case 2:
3256ea9c3a30SPyun YongHyeon 				clk = 50;
3257ea9c3a30SPyun YongHyeon 				break;
3258ea9c3a30SPyun YongHyeon 			case 4:
3259ea9c3a30SPyun YongHyeon 				clk = 66;
3260ea9c3a30SPyun YongHyeon 				break;
3261ea9c3a30SPyun YongHyeon 			case 6:
3262ea9c3a30SPyun YongHyeon 				clk = 100;
3263ea9c3a30SPyun YongHyeon 				break;
3264ea9c3a30SPyun YongHyeon 			case 7:
3265ea9c3a30SPyun YongHyeon 				clk = 133;
3266ea9c3a30SPyun YongHyeon 				break;
3267ea9c3a30SPyun YongHyeon 			}
3268ea9c3a30SPyun YongHyeon 		}
3269ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3270ea9c3a30SPyun YongHyeon 	} else {
3271ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3272ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3273ea9c3a30SPyun YongHyeon 		else
3274ea9c3a30SPyun YongHyeon 			printf("PCI ");
3275ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3276ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3277ea9c3a30SPyun YongHyeon 			clk = 66;
3278ea9c3a30SPyun YongHyeon 		else
3279ea9c3a30SPyun YongHyeon 			clk = 33;
3280ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3281ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3282ea9c3a30SPyun YongHyeon 		else
3283ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3284ea9c3a30SPyun YongHyeon 	}
3285ea9c3a30SPyun YongHyeon }
3286ea9c3a30SPyun YongHyeon 
3287062af0b0SPyun YongHyeon static int
32883f74909aSGleb Smirnoff bge_attach(device_t dev)
328995d67482SBill Paul {
329095d67482SBill Paul 	struct ifnet *ifp;
329195d67482SBill Paul 	struct bge_softc *sc;
3292548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
329308013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3294daeeb75cSPyun YongHyeon 	int capmask, error, msicount, reg, rid, trys;
329595d67482SBill Paul 
329695d67482SBill Paul 	sc = device_get_softc(dev);
329795d67482SBill Paul 	sc->bge_dev = dev;
329895d67482SBill Paul 
3299e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3300dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3301e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3302dfe0df9aSPyun YongHyeon 
330395d67482SBill Paul 	/*
330495d67482SBill Paul 	 * Map control/status registers.
330595d67482SBill Paul 	 */
330695d67482SBill Paul 	pci_enable_busmaster(dev);
330795d67482SBill Paul 
3308736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
33095f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
331044f8f2fcSMarius Strobl 	    RF_ACTIVE);
331195d67482SBill Paul 
331295d67482SBill Paul 	if (sc->bge_res == NULL) {
3313548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
331495d67482SBill Paul 		error = ENXIO;
331595d67482SBill Paul 		goto fail;
331695d67482SBill Paul 	}
331795d67482SBill Paul 
33184f09c4c7SMarius Strobl 	/* Save various chip information. */
3319548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3320d7acafa1SMarius Strobl 	sc->bge_chipid = bge_chipid(dev);
3321e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3322e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3323e53d81eeSPaul Saab 
3324a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3325daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
33261108273aSPyun YongHyeon 	 /*
33271108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
33281108273aSPyun YongHyeon 	  *
33291108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
33301108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
33311108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
33321108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
33331108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3334bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
333550515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
33361108273aSPyun YongHyeon 	  *
3337548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3338548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3339548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3340548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3341548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3342548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3343548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3344548c8f1aSPyun YongHyeon 	  *
33451108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
33461108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
33471108273aSPyun YongHyeon 	  */
3348bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
334950515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
335050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3351548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
33521108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
33531108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3354daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33551108273aSPyun YongHyeon 			else
3356daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3357bbe2ca75SPyun YongHyeon 		} else {
33581108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33591108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3360daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33611108273aSPyun YongHyeon 			else
3362daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
33631108273aSPyun YongHyeon 		}
33641108273aSPyun YongHyeon 	}
3365a813ed78SPyun YongHyeon 
33665fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
33675fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
336808013fd3SMarius Strobl 
33690dae9719SJung-uk Kim 	/* Save chipset family. */
33700dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
33712927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3372fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57765:
3373fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57766:
3374fe26ad88SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_57765_PLUS;
3375fe26ad88SPyun YongHyeon 		/* FALLTHROUGH */
33761108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3377bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
337850515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
33791108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
33801108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3381b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
338229b44b09SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
338329b44b09SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
338429b44b09SPyun YongHyeon 			/*
338529b44b09SPyun YongHyeon 			 * Enable work around for DMA engine miscalculation
338629b44b09SPyun YongHyeon 			 * of TXMBUF available space.
338729b44b09SPyun YongHyeon 			 */
338829b44b09SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_RDMA_BUG;
3389bbe2ca75SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3390bbe2ca75SPyun YongHyeon 			    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3391bbe2ca75SPyun YongHyeon 				/* Jumbo frame on BCM5719 A0 does not work. */
3392463a7e27SPyun YongHyeon 				sc->bge_flags &= ~BGE_FLAG_JUMBO;
3393bbe2ca75SPyun YongHyeon 			}
339429b44b09SPyun YongHyeon 		}
33951108273aSPyun YongHyeon 		break;
3396a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3397a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3398a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3399a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3400a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3401a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3402a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3403a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3404a5779553SStanislav Sedov 		break;
34050dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
34060dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
34070dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
34080dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
34097ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
34100dae9719SJung-uk Kim 		break;
34110dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
34120dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
34130dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3414f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
34159fe569d8SXin LI 		/* FALLTHROUGH */
34160dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
34170dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
341838cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
34190dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
34209fe569d8SXin LI 		/* FALLTHROUGH */
34210dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
34220dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
34230dae9719SJung-uk Kim 		break;
34240dae9719SJung-uk Kim 	}
34250dae9719SJung-uk Kim 
3426548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3427548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3428548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3429548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3430548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3431548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
34322927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3433548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3434548c8f1aSPyun YongHyeon 		break;
3435548c8f1aSPyun YongHyeon 	}
3436548c8f1aSPyun YongHyeon 
3437548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3438548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3439548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3440548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3441548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3442548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3443548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3444548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3445548c8f1aSPyun YongHyeon 			error = ENXIO;
3446548c8f1aSPyun YongHyeon 			goto fail;
3447548c8f1aSPyun YongHyeon 		}
3448548c8f1aSPyun YongHyeon 
3449548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3450548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3451548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3452548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3453548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3454548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3455548c8f1aSPyun YongHyeon 
3456548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3457548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3458548c8f1aSPyun YongHyeon 	}
3459548c8f1aSPyun YongHyeon 
3460749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3461749a5269SMarius Strobl 	bge_add_sysctls(sc);
3462749a5269SMarius Strobl 
3463a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
34641108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
34651108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3466a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3467a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3468a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3469a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3470a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3471a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3472a813ed78SPyun YongHyeon 	else
3473a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
34747ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
34757ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
34767ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3477a813ed78SPyun YongHyeon 
3478f681b29aSPyun YongHyeon 	/*
3479d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3480f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3481f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3482f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3483f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3484f681b29aSPyun YongHyeon 	 */
3485f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
34864f0794ffSBjoern A. Zeeb 
3487d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3488d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3489d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3490d9820cd8SPyun YongHyeon 
3491a7fcfcf3SPyun YongHyeon 	/*
3492a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3493a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3494a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3495a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3496a7fcfcf3SPyun YongHyeon 	 */
3497a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3498a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3499a7fcfcf3SPyun YongHyeon 
3500ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3501fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
35024f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
35034f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
35044f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
350584ac96f8SPyun YongHyeon 	}
35064f0794ffSBjoern A. Zeeb 
3507fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3508fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3509fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3510fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3511fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3512fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3513fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3514fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3515fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3516fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3517fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3518fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3519fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3520d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57791 ||
3521d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57795 ||
3522fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3523fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3524fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3525d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3526fb772a6cSMarius Strobl 	}
3527fb772a6cSMarius Strobl 
3528e53d81eeSPaul Saab 	/*
3529ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3530ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3531ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3532ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3533d7acafa1SMarius Strobl 	 * known bug which can't handle TSO if Ethernet header + IP/TCP
3534d7acafa1SMarius Strobl 	 * header is greater than 80 bytes. A workaround for the TSO
3535ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3536ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3537ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3538ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3539ca3f1187SPyun YongHyeon 	 */
35401108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
35411108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
35421108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3543bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3544bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3545bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3546bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3547bbe2ca75SPyun YongHyeon 		}
35481108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
35494f4a16e1SPyun YongHyeon 		/*
35504f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
35514f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3552be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
35534f4a16e1SPyun YongHyeon 		 */
35544f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3555be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3556be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3557ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
35584f4a16e1SPyun YongHyeon 	}
3559ca3f1187SPyun YongHyeon 
3560ca3f1187SPyun YongHyeon 	/*
35616f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3562e53d81eeSPaul Saab 	 */
35633b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
35644c0da0ffSGleb Smirnoff 		/*
35656f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
35666f8718a3SScott Long 		 * must be a PCI Express device.
35676f8718a3SScott Long 		 */
35686f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
35690aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
357048630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
357148630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
357248630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
357348630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
357450515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
357550515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
357648630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
357748630d79SPyun YongHyeon 		else
357848630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
357948630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
35806f8718a3SScott Long 	} else {
35816f8718a3SScott Long 		/*
35826f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
35836f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
35844c0da0ffSGleb Smirnoff 		 */
35853b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
35860aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
358790447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
35884c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3589652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
35906f8718a3SScott Long 	}
35914c0da0ffSGleb Smirnoff 
3592bf6ef57aSJohn Polstra 	/*
3593fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3594fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3595fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3596fd4d32feSPyun YongHyeon 	 */
3597fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3598fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3599fd4d32feSPyun YongHyeon 	/*
3600062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3601062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3602062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3603062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3604062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3605062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3606062af0b0SPyun YongHyeon 	 */
3607062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3608062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3609062af0b0SPyun YongHyeon 	/*
3610bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3611bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3612bf6ef57aSJohn Polstra 	 * normal operation.
3613bf6ef57aSJohn Polstra 	 */
36140aaf1057SPyun YongHyeon 	rid = 0;
36153b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
36160aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3617bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
3618bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
3619bf6ef57aSJohn Polstra 			if (msicount > 1)
3620bf6ef57aSJohn Polstra 				msicount = 1;
3621bf6ef57aSJohn Polstra 		} else
3622bf6ef57aSJohn Polstra 			msicount = 0;
3623bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
3624bf6ef57aSJohn Polstra 			rid = 1;
3625bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
36260aaf1057SPyun YongHyeon 		}
36270aaf1057SPyun YongHyeon 	}
3628bf6ef57aSJohn Polstra 
36291108273aSPyun YongHyeon 	/*
36301108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
36311108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
36321108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
36331108273aSPyun YongHyeon 	 */
36341108273aSPyun YongHyeon #ifndef DEVICE_POLLING
36351108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
36361108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
36371108273aSPyun YongHyeon #endif
36381108273aSPyun YongHyeon 
3639bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3640bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
3641bf6ef57aSJohn Polstra 
3642bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3643bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3644bf6ef57aSJohn Polstra 		error = ENXIO;
3645bf6ef57aSJohn Polstra 		goto fail;
3646bf6ef57aSJohn Polstra 	}
3647bf6ef57aSJohn Polstra 
3648ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
36494f09c4c7SMarius Strobl 
36508cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3651548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3652548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3653888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3654888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3655548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3656548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
36578cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
36588cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3659d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
36608cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
36618cb1383cSDoug Ambrisko 			}
36628cb1383cSDoug Ambrisko 		}
3663548c8f1aSPyun YongHyeon 	}
36648cb1383cSDoug Ambrisko 
36658cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
36663dd76c98SPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
36678cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
36688cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
36698cb1383cSDoug Ambrisko 		error = ENXIO;
36708cb1383cSDoug Ambrisko 		goto fail;
36718cb1383cSDoug Ambrisko 	}
36728cb1383cSDoug Ambrisko 
36733dd76c98SPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
36743dd76c98SPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
367595d67482SBill Paul 
367695d67482SBill Paul 	if (bge_chipinit(sc)) {
3677fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
367895d67482SBill Paul 		error = ENXIO;
367995d67482SBill Paul 		goto fail;
368095d67482SBill Paul 	}
368195d67482SBill Paul 
368238cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
368338cc658fSJohn Baldwin 	if (error) {
368408013fd3SMarius Strobl 		device_printf(sc->bge_dev,
368508013fd3SMarius Strobl 		    "failed to read station address\n");
368695d67482SBill Paul 		error = ENXIO;
368795d67482SBill Paul 		goto fail;
368895d67482SBill Paul 	}
368995d67482SBill Paul 
3690f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
36911108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
36921108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
36931108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3694f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3695f41ac2beSBill Paul 	else
3696f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3697f41ac2beSBill Paul 
36985b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3699fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3700fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3701f41ac2beSBill Paul 		error = ENXIO;
3702f41ac2beSBill Paul 		goto fail;
3703f41ac2beSBill Paul 	}
3704f41ac2beSBill Paul 
370595d67482SBill Paul 	/* Set default tuneable values. */
370695d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
370795d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
370895d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
37096f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
37106f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
371195d67482SBill Paul 
371235f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
371335f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
371435f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
371535f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
371635f945cdSPyun YongHyeon 
371795d67482SBill Paul 	/* Set up ifnet structure */
3718fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3719fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3720fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3721fc74a9f9SBrooks Davis 		error = ENXIO;
3722fc74a9f9SBrooks Davis 		goto fail;
3723fc74a9f9SBrooks Davis 	}
372495d67482SBill Paul 	ifp->if_softc = sc;
37259bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
372695d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
372795d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
372895d67482SBill Paul 	ifp->if_start = bge_start;
372995d67482SBill Paul 	ifp->if_init = bge_init;
37304d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
37314d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
37324d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
373335f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
3734d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
37354e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
37361108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3737ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
373804bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
3739ca3f1187SPyun YongHyeon 	}
37404e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
37414e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
37424e35d186SJung-uk Kim #endif
374395d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
374475719184SGleb Smirnoff #ifdef DEVICE_POLLING
374575719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
374675719184SGleb Smirnoff #endif
374795d67482SBill Paul 
3748a1d52896SBill Paul 	/*
3749d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3750d375e524SGleb Smirnoff 	 * to hardware bugs.
3751d375e524SGleb Smirnoff 	 */
3752d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3753d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
37544d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
3755d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
3756d375e524SGleb Smirnoff 	}
3757d375e524SGleb Smirnoff 
3758d375e524SGleb Smirnoff 	/*
3759a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
376041abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
376141abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
376241abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
376341abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
376441abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
376541abcc1bSPaul Saab 	 * SK-9D41.
3766a1d52896SBill Paul 	 */
3767888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3768888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
37695fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
37705fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3771f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3772f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3773fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3774f6789fbaSPyun YongHyeon 			error = ENXIO;
3775f6789fbaSPyun YongHyeon 			goto fail;
3776f6789fbaSPyun YongHyeon 		}
377741abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
377841abcc1bSPaul Saab 	}
377941abcc1bSPaul Saab 
378095d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3781ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3782ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
378370c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3784ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
378570c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
378670c2071bSPyun YongHyeon 		} else
3787652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3788ea3b4127SPyun YongHyeon 	}
378995d67482SBill Paul 
379070c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
379170c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
379270c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
379370c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
379470c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
379570c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
379670c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
379770c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
379870c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
379970c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
380070c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
380170c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
380270c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
380370c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3804fe26ad88SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3805fe26ad88SPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc)) {
380670c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
380770c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
380870c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
380970c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
381070c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
381170c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
381270c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
381370c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
381470c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
381570c2071bSPyun YongHyeon 		} else
381670c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
381770c2071bSPyun YongHyeon 	}
381870c2071bSPyun YongHyeon 
381970c2071bSPyun YongHyeon 	/*
3820d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
382170c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
382270c2071bSPyun YongHyeon 	 */
382370c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
382470c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
382570c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3826d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
382770c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
382870c2071bSPyun YongHyeon 
3829652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
38300c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
38310c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
38320c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
38336098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
38346098821cSJung-uk Kim 		    0, NULL);
383595d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
383695d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3837da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
383895d67482SBill Paul 	} else {
383995d67482SBill Paul 		/*
38408cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
38418cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
38428cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
38438cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
38448cb1383cSDoug Ambrisko 		 * the PHY.
384595d67482SBill Paul 		 */
38464012d104SMarius Strobl 		trys = 0;
38478cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
38488cb1383cSDoug Ambrisko again:
38498cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
38508cb1383cSDoug Ambrisko 
3851fb772a6cSMarius Strobl 		error = mii_attach(dev, &sc->bge_miibus, ifp, bge_ifmedia_upd,
3852daeeb75cSPyun YongHyeon 		    bge_ifmedia_sts, capmask, sc->bge_phy_addr, MII_OFFSET_ANY,
3853fb772a6cSMarius Strobl 		    MIIF_DOPAUSE);
38548e5d93dbSMarius Strobl 		if (error != 0) {
38558cb1383cSDoug Ambrisko 			if (trys++ < 4) {
38568cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3857daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3858daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
38598cb1383cSDoug Ambrisko 				goto again;
38608cb1383cSDoug Ambrisko 			}
38618e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
386295d67482SBill Paul 			goto fail;
386395d67482SBill Paul 		}
38648cb1383cSDoug Ambrisko 
38658cb1383cSDoug Ambrisko 		/*
38668cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
38678cb1383cSDoug Ambrisko 		 */
38688cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
38698cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
387095d67482SBill Paul 	}
387195d67482SBill Paul 
387295d67482SBill Paul 	/*
3873e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3874e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3875e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3876e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3877e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3878e255b776SJohn Polstra 	 * payloads by copying the received packets.
3879e255b776SJohn Polstra 	 */
3880652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3881652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3882652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3883e255b776SJohn Polstra 
3884e255b776SJohn Polstra 	/*
388595d67482SBill Paul 	 * Call MI attach routine.
388695d67482SBill Paul 	 */
3887fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
38880f9bd73bSSam Leffler 
388961ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
389061ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
389161ccb9daSPyun YongHyeon 
38920f9bd73bSSam Leffler 	/*
38930f9bd73bSSam Leffler 	 * Hookup IRQ last.
38940f9bd73bSSam Leffler 	 */
3895dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3896dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
38977e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
38987e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3899dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3900dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3901dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3902dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3903dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3904e010b055SPyun YongHyeon 			error = ENOMEM;
3905dfe0df9aSPyun YongHyeon 			goto fail;
3906dfe0df9aSPyun YongHyeon 		}
3907d7acafa1SMarius Strobl 		error = taskqueue_start_threads(&sc->bge_tq, 1, PI_NET,
3908d7acafa1SMarius Strobl 		    "%s taskq", device_get_nameunit(sc->bge_dev));
3909d7acafa1SMarius Strobl 		if (error != 0) {
3910d7acafa1SMarius Strobl 			device_printf(dev, "could not start threads.\n");
3911d7acafa1SMarius Strobl 			ether_ifdetach(ifp);
3912d7acafa1SMarius Strobl 			goto fail;
3913d7acafa1SMarius Strobl 		}
3914dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3915dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3916dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3917dfe0df9aSPyun YongHyeon 	} else
3918dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3919dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3920dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
39210f9bd73bSSam Leffler 
39220f9bd73bSSam Leffler 	if (error) {
3923e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3924fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
39250f9bd73bSSam Leffler 	}
392695d67482SBill Paul 
392795d67482SBill Paul fail:
3928e010b055SPyun YongHyeon 	if (error)
3929e010b055SPyun YongHyeon 		bge_detach(dev);
393095d67482SBill Paul 	return (error);
393195d67482SBill Paul }
393295d67482SBill Paul 
393395d67482SBill Paul static int
39343f74909aSGleb Smirnoff bge_detach(device_t dev)
393595d67482SBill Paul {
393695d67482SBill Paul 	struct bge_softc *sc;
393795d67482SBill Paul 	struct ifnet *ifp;
393895d67482SBill Paul 
393995d67482SBill Paul 	sc = device_get_softc(dev);
3940fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
394195d67482SBill Paul 
394275719184SGleb Smirnoff #ifdef DEVICE_POLLING
394375719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
394475719184SGleb Smirnoff 		ether_poll_deregister(ifp);
394575719184SGleb Smirnoff #endif
394675719184SGleb Smirnoff 
3947e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3948e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
39490f9bd73bSSam Leffler 		BGE_LOCK(sc);
395095d67482SBill Paul 		bge_stop(sc);
39510f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
39525dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3953e010b055SPyun YongHyeon 	}
39545dda8085SOleg Bulyzhin 
3955dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3956dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
395795d67482SBill Paul 
39580aba72ddSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
395995d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
39600aba72ddSPyun YongHyeon 	else if (sc->bge_miibus != NULL) {
396195d67482SBill Paul 		bus_generic_detach(dev);
396295d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
396395d67482SBill Paul 	}
396495d67482SBill Paul 
396595d67482SBill Paul 	bge_release_resources(sc);
396695d67482SBill Paul 
396795d67482SBill Paul 	return (0);
396895d67482SBill Paul }
396995d67482SBill Paul 
397095d67482SBill Paul static void
39713f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
397295d67482SBill Paul {
397395d67482SBill Paul 	device_t dev;
397495d67482SBill Paul 
397595d67482SBill Paul 	dev = sc->bge_dev;
397695d67482SBill Paul 
3977dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3978dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3979dfe0df9aSPyun YongHyeon 
398095d67482SBill Paul 	if (sc->bge_intrhand != NULL)
398195d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
398295d67482SBill Paul 
398395d67482SBill Paul 	if (sc->bge_irq != NULL)
3984724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3985724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3986724bd939SJohn Polstra 
3987724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3988724bd939SJohn Polstra 		pci_release_msi(dev);
398995d67482SBill Paul 
399095d67482SBill Paul 	if (sc->bge_res != NULL)
399195d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3992736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
399395d67482SBill Paul 
3994548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
3995548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
3996548c8f1aSPyun YongHyeon 		    PCIR_BAR(2), sc->bge_res2);
3997548c8f1aSPyun YongHyeon 
3998ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3999ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
4000ad61f896SRuslan Ermilov 
4001f41ac2beSBill Paul 	bge_dma_free(sc);
400295d67482SBill Paul 
40030f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
40040f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
400595d67482SBill Paul }
400695d67482SBill Paul 
40078cb1383cSDoug Ambrisko static int
40083f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
400995d67482SBill Paul {
401095d67482SBill Paul 	device_t dev;
4011cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
40126f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
40130aaf1057SPyun YongHyeon 	uint16_t devctl;
40145fea260fSMarius Strobl 	int i;
401595d67482SBill Paul 
401695d67482SBill Paul 	dev = sc->bge_dev;
401795d67482SBill Paul 
4018cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
4019548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4020548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
4021cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
4022cc085b36SPyun YongHyeon 
402338cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
402438cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
40256f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
40266f8718a3SScott Long 			write_op = bge_writemem_direct;
40276f8718a3SScott Long 		else
40286f8718a3SScott Long 			write_op = bge_writemem_ind;
40299ba784dbSScott Long 	} else
40306f8718a3SScott Long 		write_op = bge_writereg_ind;
40316f8718a3SScott Long 
40323dd76c98SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
40333dd76c98SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5701) {
40343dd76c98SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
40353dd76c98SPyun YongHyeon 		for (i = 0; i < 8000; i++) {
40363dd76c98SPyun YongHyeon 			if (CSR_READ_4(sc, BGE_NVRAM_SWARB) &
40373dd76c98SPyun YongHyeon 			    BGE_NVRAMSWARB_GNT1)
40383dd76c98SPyun YongHyeon 				break;
40393dd76c98SPyun YongHyeon 			DELAY(20);
40403dd76c98SPyun YongHyeon 		}
40413dd76c98SPyun YongHyeon 		if (i == 8000) {
40423dd76c98SPyun YongHyeon 			if (bootverbose)
40433dd76c98SPyun YongHyeon 				device_printf(dev, "NVRAM lock timedout!\n");
40443dd76c98SPyun YongHyeon 		}
40453dd76c98SPyun YongHyeon 	}
4046548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
4047548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
4048548c8f1aSPyun YongHyeon 
404995d67482SBill Paul 	/* Save some important PCI state. */
405095d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
405195d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
405295d67482SBill Paul 
405395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
405495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4055e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
405695d67482SBill Paul 
40576f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
40586f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
4059a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
40606f8718a3SScott Long 		if (bootverbose)
4061333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
40626f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
40636f8718a3SScott Long 	}
40646f8718a3SScott Long 
40656f8718a3SScott Long 	/*
40666f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
40676f8718a3SScott Long 	 * When firmware finishes its initialization it will
4068888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
40696f8718a3SScott Long 	 */
4070888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
40716f8718a3SScott Long 
40720c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4073e53d81eeSPaul Saab 
4074e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4075652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4076ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4077ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
40780c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
40790c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4080ad49eccfSPyun YongHyeon 		}
4081e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4082e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
40830c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
40840c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4085e53d81eeSPaul Saab 		}
4086e53d81eeSPaul Saab 	}
4087e53d81eeSPaul Saab 
4088df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4089df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4090df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4091df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4092df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4093df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4094df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4095df4db538SPyun YongHyeon 	}
4096df4db538SPyun YongHyeon 
409721c9e407SDavid Christensen 	/*
40986f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
40996f8718a3SScott Long 	 * powered up in D0 uninitialized.
41006f8718a3SScott Long 	 */
41015512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
41025512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4103caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
41046f8718a3SScott Long 
410595d67482SBill Paul 	/* Issue global reset */
41066f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
410795d67482SBill Paul 
4108cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4109cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4110cc085b36SPyun YongHyeon 	else
411195d67482SBill Paul 		DELAY(1000);
411295d67482SBill Paul 
4113e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4114652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4115e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4116e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
41175fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
41185fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4119e53d81eeSPaul Saab 		}
41200aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4121389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
41220aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4123389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4124389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4125389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
41260aaf1057SPyun YongHyeon 		    devctl, 2);
412748630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
41280aaf1057SPyun YongHyeon 		/* Clear error status. */
4129389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4130389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4131389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4132389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4133e53d81eeSPaul Saab 	}
4134e53d81eeSPaul Saab 
41353f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
413695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
413795d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4138e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4139cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4140cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4141cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4142cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4143548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4144548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4145548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4146548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4147cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
414895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
414995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4150cbb2b2feSPyun YongHyeon 	/*
4151cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4152fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4153cbb2b2feSPyun YongHyeon 	 * read stale status block.
4154cbb2b2feSPyun YongHyeon 	 */
4155cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4156cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4157cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4158cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4159cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4160cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4161cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4162cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4163cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4164cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4165cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4166cbb2b2feSPyun YongHyeon 		}
4167cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4168cbb2b2feSPyun YongHyeon 		    devctl, 2);
4169cbb2b2feSPyun YongHyeon 	}
417022a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
41714c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4172bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4173bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
41740aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
41750aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
41760aaf1057SPyun YongHyeon 			pci_write_config(dev,
41770aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4178bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4179bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4180bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4181bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4182bf6ef57aSJohn Polstra 		}
41834c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
41844c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
41854c0da0ffSGleb Smirnoff 	} else
4186a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4187a7b0c314SPaul Saab 
4188cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4189cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4190cc085b36SPyun YongHyeon 
4191cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4192cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4193cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4194cc085b36SPyun YongHyeon 	DELAY(40);
4195cc085b36SPyun YongHyeon 
4196548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4197548c8f1aSPyun YongHyeon 
419838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
419938cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
420038cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
420138cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
420238cc658fSJohn Baldwin 				break;
420338cc658fSJohn Baldwin 			DELAY(100);
420438cc658fSJohn Baldwin 		}
420538cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4206333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
420738cc658fSJohn Baldwin 			return (1);
420838cc658fSJohn Baldwin 		}
420938cc658fSJohn Baldwin 	} else {
421095d67482SBill Paul 		/*
42116f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
421208013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
42135fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
42145fea260fSMarius Strobl 		 * address is fitted though.
421595d67482SBill Paul 		 */
421695d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4217d5d23857SJung-uk Kim 			DELAY(10);
4218888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4219888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
422095d67482SBill Paul 				break;
422195d67482SBill Paul 		}
422295d67482SBill Paul 
42235fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4224333704a3SPyun YongHyeon 			device_printf(dev,
4225333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4226333704a3SPyun YongHyeon 			    val);
4227b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4228b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4229b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
423038cc658fSJohn Baldwin 	}
423195d67482SBill Paul 
423295d67482SBill Paul 	/*
4233da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4234da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4235da3003f0SBill Paul 	 * to 1.2V.
4236da3003f0SBill Paul 	 */
4237652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4238652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
42395fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
42405fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
42415fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4242da3003f0SBill Paul 	}
4243da3003f0SBill Paul 
4244e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4245652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4246b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4247a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4248a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4249a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
42505fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
42515fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4252e53d81eeSPaul Saab 	}
42538cb1383cSDoug Ambrisko 
425450515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
425550515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
425650515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
425750515680SPyun YongHyeon 
42588cb1383cSDoug Ambrisko 	return (0);
425995d67482SBill Paul }
426095d67482SBill Paul 
4261e0b7b101SPyun YongHyeon static __inline void
4262e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4263e0b7b101SPyun YongHyeon {
4264e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4265e0b7b101SPyun YongHyeon 
4266e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4267e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4268e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4269e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4270e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4271e0b7b101SPyun YongHyeon }
4272e0b7b101SPyun YongHyeon 
4273e0b7b101SPyun YongHyeon static __inline void
4274e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4275e0b7b101SPyun YongHyeon {
4276e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4277e0b7b101SPyun YongHyeon 
4278e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4279e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4280e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4281e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4282e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4283e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4284e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4285e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4286e0b7b101SPyun YongHyeon }
4287e0b7b101SPyun YongHyeon 
428895d67482SBill Paul /*
428995d67482SBill Paul  * Frame reception handling. This is called if there's a frame
429095d67482SBill Paul  * on the receive return list.
429195d67482SBill Paul  *
429295d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
42931be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
429495d67482SBill Paul  * 2) the frame is from the standard receive ring
429595d67482SBill Paul  */
429695d67482SBill Paul 
42971abcdbd1SAttilio Rao static int
4298dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
429995d67482SBill Paul {
430095d67482SBill Paul 	struct ifnet *ifp;
43011abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4302b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
430395d67482SBill Paul 
43047f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
43050f9bd73bSSam Leffler 
43063f74909aSGleb Smirnoff 	/* Nothing to do. */
43077f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
43081abcdbd1SAttilio Rao 		return (rx_npkts);
4309cfcb5025SOleg Bulyzhin 
4310fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
431195d67482SBill Paul 
4312f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4313e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4314f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
431515eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4316f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4317f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4318c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
4319f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
432015eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4321f41ac2beSBill Paul 
43227f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
432395d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
43243f74909aSGleb Smirnoff 		uint32_t		rxidx;
432595d67482SBill Paul 		struct mbuf		*m = NULL;
43263f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
432795d67482SBill Paul 		int			have_tag = 0;
432895d67482SBill Paul 
432975719184SGleb Smirnoff #ifdef DEVICE_POLLING
433075719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
433175719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
433275719184SGleb Smirnoff 				break;
433375719184SGleb Smirnoff 			sc->rxcycles--;
433475719184SGleb Smirnoff 		}
433575719184SGleb Smirnoff #endif
433675719184SGleb Smirnoff 
43377f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
433895d67482SBill Paul 
433995d67482SBill Paul 		rxidx = cur_rx->bge_idx;
43407f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
434195d67482SBill Paul 
4342cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
4343cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
434495d67482SBill Paul 			have_tag = 1;
434595d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
434695d67482SBill Paul 		}
434795d67482SBill Paul 
434895d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
434995d67482SBill Paul 			jumbocnt++;
4350943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
435195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4352e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
435395d67482SBill Paul 				continue;
435495d67482SBill Paul 			}
4355943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4356e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4357943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
435895d67482SBill Paul 				continue;
435995d67482SBill Paul 			}
436003e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
436195d67482SBill Paul 		} else {
436295d67482SBill Paul 			stdcnt++;
4363e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
436495d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4365e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
436695d67482SBill Paul 				continue;
436795d67482SBill Paul 			}
4368943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4369e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4370943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
437195d67482SBill Paul 				continue;
437295d67482SBill Paul 			}
437303e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
437495d67482SBill Paul 		}
437595d67482SBill Paul 
437695d67482SBill Paul 		ifp->if_ipackets++;
4377e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4378e255b776SJohn Polstra 		/*
4379e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4380e65bed95SPyun YongHyeon 		 * the payload is aligned.
4381e255b776SJohn Polstra 		 */
4382652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4383e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4384e255b776SJohn Polstra 			    cur_rx->bge_len);
4385e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4386e255b776SJohn Polstra 		}
4387e255b776SJohn Polstra #endif
4388473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
438995d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
439095d67482SBill Paul 
43911108273aSPyun YongHyeon 		if (ifp->if_capenable & IFCAP_RXCSUM)
43921108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
439395d67482SBill Paul 
439495d67482SBill Paul 		/*
4395673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4396673d9191SSam Leffler 		 * attach that information to the packet.
439795d67482SBill Paul 		 */
4398d147662cSGleb Smirnoff 		if (have_tag) {
439978ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
440078ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4401d147662cSGleb Smirnoff 		}
440295d67482SBill Paul 
4403dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
44040f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4405673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
44060f9bd73bSSam Leffler 			BGE_LOCK(sc);
4407dfe0df9aSPyun YongHyeon 		} else
4408dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
4409d4da719cSAttilio Rao 		rx_npkts++;
441025e13e68SXin LI 
441125e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
44128cf7d13dSAttilio Rao 			return (rx_npkts);
441395d67482SBill Paul 	}
441495d67482SBill Paul 
441515eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
441615eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4417e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4418f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4419e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
44204c0da0ffSGleb Smirnoff 
4421c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4422f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
44234c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4424f41ac2beSBill Paul 
44257f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
442638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
442795d67482SBill Paul 	if (stdcnt)
4428767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4429767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
443095d67482SBill Paul 	if (jumbocnt)
4431767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4432767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4433f5a034f9SPyun YongHyeon #ifdef notyet
4434f5a034f9SPyun YongHyeon 	/*
4435f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4436f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4437f5a034f9SPyun YongHyeon 	 */
4438f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4439f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
4440f5a034f9SPyun YongHyeon #endif
44411abcdbd1SAttilio Rao 	return (rx_npkts);
444295d67482SBill Paul }
444395d67482SBill Paul 
444495d67482SBill Paul static void
44451108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
44461108273aSPyun YongHyeon {
44471108273aSPyun YongHyeon 
44481108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
44491108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
44501108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44511108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44521108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
44531108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
44541108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44551108273aSPyun YongHyeon 			}
44561108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
44571108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
44581108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
44591108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44601108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
44611108273aSPyun YongHyeon 			}
44621108273aSPyun YongHyeon 		}
44631108273aSPyun YongHyeon 	} else {
44641108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44651108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44661108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
44671108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44681108273aSPyun YongHyeon 		}
44691108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
44701108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
44711108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
44721108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
44731108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44741108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
44751108273aSPyun YongHyeon 		}
44761108273aSPyun YongHyeon 	}
44771108273aSPyun YongHyeon }
44781108273aSPyun YongHyeon 
44791108273aSPyun YongHyeon static void
4480b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
448195d67482SBill Paul {
448295a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
448395d67482SBill Paul 	struct ifnet *ifp;
448495d67482SBill Paul 
44850f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
44860f9bd73bSSam Leffler 
44873f74909aSGleb Smirnoff 	/* Nothing to do. */
4488b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4489cfcb5025SOleg Bulyzhin 		return;
4490cfcb5025SOleg Bulyzhin 
4491fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
449295d67482SBill Paul 
4493e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
44945c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
449595d67482SBill Paul 	/*
449695d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
449795d67482SBill Paul 	 * frames that have been sent.
449895d67482SBill Paul 	 */
4499b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
450095a0a340SPyun YongHyeon 		uint32_t		idx;
450195d67482SBill Paul 
450295d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4503f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
450495d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
450595d67482SBill Paul 			ifp->if_opackets++;
450695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
45070ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4508e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4509e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
45100ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4511f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4512e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4513e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
451495d67482SBill Paul 		}
451595d67482SBill Paul 		sc->bge_txcnt--;
451695d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
451795d67482SBill Paul 	}
451895d67482SBill Paul 
451913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
45205b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
45215b01e77cSBruce Evans 		sc->bge_timer = 0;
452295d67482SBill Paul }
452395d67482SBill Paul 
452475719184SGleb Smirnoff #ifdef DEVICE_POLLING
45251abcdbd1SAttilio Rao static int
452675719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
452775719184SGleb Smirnoff {
452875719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
4529b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4530366454f2SOleg Bulyzhin 	uint32_t statusword;
45311abcdbd1SAttilio Rao 	int rx_npkts = 0;
453275719184SGleb Smirnoff 
45333f74909aSGleb Smirnoff 	BGE_LOCK(sc);
45343f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
45353f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
45361abcdbd1SAttilio Rao 		return (rx_npkts);
45373f74909aSGleb Smirnoff 	}
453875719184SGleb Smirnoff 
4539dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4540b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4541b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
45422246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4543b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4544b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4545dab5cd05SOleg Bulyzhin 
4546175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
45472246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4548175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4549dab5cd05SOleg Bulyzhin 
4550dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4551b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4552b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4553366454f2SOleg Bulyzhin 
45540c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4555366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4556366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4557366454f2SOleg Bulyzhin 
4558366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4559366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
45604c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4561652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4562366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4563366454f2SOleg Bulyzhin 
4564366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4565dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
456625e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
456725e13e68SXin LI 		BGE_UNLOCK(sc);
45688cf7d13dSAttilio Rao 		return (rx_npkts);
456925e13e68SXin LI 	}
4570b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4571366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4572366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
45733f74909aSGleb Smirnoff 
45743f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
45751abcdbd1SAttilio Rao 	return (rx_npkts);
457675719184SGleb Smirnoff }
457775719184SGleb Smirnoff #endif /* DEVICE_POLLING */
457875719184SGleb Smirnoff 
4579dfe0df9aSPyun YongHyeon static int
4580dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4581dfe0df9aSPyun YongHyeon {
4582dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4583dfe0df9aSPyun YongHyeon 
4584dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4585dfe0df9aSPyun YongHyeon 	/*
4586dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4587dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4588dfe0df9aSPyun YongHyeon 	 */
4589dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4590dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4591dfe0df9aSPyun YongHyeon }
4592dfe0df9aSPyun YongHyeon 
4593dfe0df9aSPyun YongHyeon static void
4594dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4595dfe0df9aSPyun YongHyeon {
4596dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4597dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
45981108273aSPyun YongHyeon 	uint32_t status, status_tag;
4599dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4600dfe0df9aSPyun YongHyeon 
4601dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4602dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4603dfe0df9aSPyun YongHyeon 
460466151edfSPyun YongHyeon 	BGE_LOCK(sc);
460566151edfSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
460666151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4607dfe0df9aSPyun YongHyeon 		return;
460866151edfSPyun YongHyeon 	}
4609dfe0df9aSPyun YongHyeon 
4610dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4611dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4612dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4613dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4614dfe0df9aSPyun YongHyeon 
46152246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4616dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4617dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4618dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
46191108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
46202246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4621dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4622dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4623dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4624dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
46251108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
46261108273aSPyun YongHyeon 		status_tag = 0;
462766151edfSPyun YongHyeon 
462866151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
462966151edfSPyun YongHyeon 		bge_link_upd(sc);
463066151edfSPyun YongHyeon 
4631dfe0df9aSPyun YongHyeon 	/* Let controller work. */
46321108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4633dfe0df9aSPyun YongHyeon 
463466151edfSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
463566151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4636dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
463766151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4638dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
463966151edfSPyun YongHyeon 		BGE_LOCK(sc);
4640dfe0df9aSPyun YongHyeon 	}
4641dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4642dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4643dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4644dfe0df9aSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4645dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4646dfe0df9aSPyun YongHyeon 	}
464766151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4648dfe0df9aSPyun YongHyeon }
4649dfe0df9aSPyun YongHyeon 
465095d67482SBill Paul static void
46513f74909aSGleb Smirnoff bge_intr(void *xsc)
465295d67482SBill Paul {
465395d67482SBill Paul 	struct bge_softc *sc;
465495d67482SBill Paul 	struct ifnet *ifp;
4655dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4656b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
465795d67482SBill Paul 
465895d67482SBill Paul 	sc = xsc;
4659f41ac2beSBill Paul 
46600f9bd73bSSam Leffler 	BGE_LOCK(sc);
46610f9bd73bSSam Leffler 
4662dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4663dab5cd05SOleg Bulyzhin 
466475719184SGleb Smirnoff #ifdef DEVICE_POLLING
466575719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
466675719184SGleb Smirnoff 		BGE_UNLOCK(sc);
466775719184SGleb Smirnoff 		return;
466875719184SGleb Smirnoff 	}
466975719184SGleb Smirnoff #endif
467075719184SGleb Smirnoff 
4671f30cbfc6SScott Long 	/*
4672b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4673b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4674b848e032SBruce Evans 	 * our current organization this just gives complications and
4675b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4676b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4677b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4678b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4679b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4680b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4681b848e032SBruce Evans 	 *
4682b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4683b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4684b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4685b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4686b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4687b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4688b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4689b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4690b848e032SBruce Evans 	 */
469138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4692b848e032SBruce Evans 
4693f584dfd1SPyun YongHyeon 	/*
4694f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4695f584dfd1SPyun YongHyeon 	 */
4696f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4697f584dfd1SPyun YongHyeon 
4698f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4699f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4700f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4701f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4702f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4703f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4704f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4705f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4706f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4707f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4708f584dfd1SPyun YongHyeon 
47091f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
47104c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4711f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4712dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
471395d67482SBill Paul 
471413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
47153f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4716dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
471725e13e68SXin LI 	}
471895d67482SBill Paul 
471925e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
47203f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4721b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
472295d67482SBill Paul 	}
472395d67482SBill Paul 
472413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
472513f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
47260f9bd73bSSam Leffler 		bge_start_locked(ifp);
47270f9bd73bSSam Leffler 
47280f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
472995d67482SBill Paul }
473095d67482SBill Paul 
473195d67482SBill Paul static void
47328cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
47338cb1383cSDoug Ambrisko {
47348cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
47358cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
47368cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
47378cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
47388cb1383cSDoug Ambrisko 		else {
4739899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4740888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
47413c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4742888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4743941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4744941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
47453fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
47469931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
47479931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
47488cb1383cSDoug Ambrisko 		}
47498cb1383cSDoug Ambrisko 	}
47508cb1383cSDoug Ambrisko }
47518cb1383cSDoug Ambrisko 
47528cb1383cSDoug Ambrisko static void
4753b74e67fbSGleb Smirnoff bge_tick(void *xsc)
47540f9bd73bSSam Leffler {
4755b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
475695d67482SBill Paul 	struct mii_data *mii = NULL;
475795d67482SBill Paul 
47580f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
475995d67482SBill Paul 
47605dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
47615dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
47625dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
47635dda8085SOleg Bulyzhin 		return;
47645dda8085SOleg Bulyzhin 
47657ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
47660434d1b8SBill Paul 		bge_stats_update_regs(sc);
47670434d1b8SBill Paul 	else
476895d67482SBill Paul 		bge_stats_update(sc);
476995d67482SBill Paul 
4770548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4771548c8f1aSPyun YongHyeon 
4772652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
477395d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
477482b67c01SOleg Bulyzhin 		/*
477582b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
477682b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
477782b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
477882b67c01SOleg Bulyzhin 		 */
477982b67c01SOleg Bulyzhin 		if (!sc->bge_link)
478095d67482SBill Paul 			mii_tick(mii);
47817b97099dSOleg Bulyzhin 	} else {
47827b97099dSOleg Bulyzhin 		/*
47837b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
47847b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
47857b97099dSOleg Bulyzhin 		 * and trigger interrupt.
47867b97099dSOleg Bulyzhin 		 */
47877b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
47883f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
47897b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
47907b97099dSOleg Bulyzhin #endif
47917b97099dSOleg Bulyzhin 		{
47927b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
47934f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
47944f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
47957b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
47964f0794ffSBjoern A. Zeeb 		else
47974f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
47987b97099dSOleg Bulyzhin 		}
4799dab5cd05SOleg Bulyzhin 	}
480095d67482SBill Paul 
48018cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4802b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
48038cb1383cSDoug Ambrisko 
4804dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
480595d67482SBill Paul }
480695d67482SBill Paul 
480795d67482SBill Paul static void
48083f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
48090434d1b8SBill Paul {
48103f74909aSGleb Smirnoff 	struct ifnet *ifp;
48112280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
481229b44b09SPyun YongHyeon 	uint32_t val;
48130434d1b8SBill Paul 
4814fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
48152280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
48160434d1b8SBill Paul 
48172280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
48182280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48192280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
48202280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48212280c16bSPyun YongHyeon 	stats->outXonSent +=
48222280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48232280c16bSPyun YongHyeon 	stats->outXoffSent +=
48242280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48252280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
48262280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48272280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
48282280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
48292280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
48302280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
48312280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
48322280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
48332280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
48342280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
48352280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
48362280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
48372280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
48382280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
48392280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
48402280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
48412280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
48422280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48437e6e2507SJung-uk Kim 
48442280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
48452280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48462280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
48472280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48482280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
48492280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48502280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
48512280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48522280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
48532280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48542280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
48552280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48562280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
48572280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48582280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
48592280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48602280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
48612280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48622280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
48632280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
48642280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
48652280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
48662280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
48672280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
48682280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
48692280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48702280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
48712280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48722280c16bSPyun YongHyeon 
48732280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
48742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48752280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
48762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48772280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
48782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48792280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
48802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4881f78094a5SPyun YongHyeon 	/*
4882f78094a5SPyun YongHyeon 	 * XXX
4883f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4884f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4885f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4886f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4887f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4888f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4889f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4890f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4891f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4892f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4893f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4894f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4895f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4896f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4897f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4898f78094a5SPyun YongHyeon 	 * silicon bug.
4899f78094a5SPyun YongHyeon 	 */
4900f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4901f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4902f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
49032280c16bSPyun YongHyeon 		stats->InputDiscards +=
49042280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49052280c16bSPyun YongHyeon 	stats->InputErrors +=
49062280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49072280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
49082280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49092280c16bSPyun YongHyeon 
49102280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
49112280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
49122280c16bSPyun YongHyeon 	    stats->InputErrors);
491329b44b09SPyun YongHyeon 
491429b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
491529b44b09SPyun YongHyeon 		/*
491629b44b09SPyun YongHyeon 		 * If controller transmitted more than BGE_NUM_RDMA_CHANNELS
491729b44b09SPyun YongHyeon 		 * frames, it's safe to disable workaround for DMA engine's
491829b44b09SPyun YongHyeon 		 * miscalculation of TXMBUF space.
491929b44b09SPyun YongHyeon 		 */
492029b44b09SPyun YongHyeon 		if (stats->ifHCOutUcastPkts + stats->ifHCOutMulticastPkts +
492129b44b09SPyun YongHyeon 		    stats->ifHCOutBroadcastPkts > BGE_NUM_RDMA_CHANNELS) {
492229b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
492329b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
492429b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5719;
492529b44b09SPyun YongHyeon 			else
492629b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5720;
492729b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
492829b44b09SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_RDMA_BUG;
492929b44b09SPyun YongHyeon 		}
493029b44b09SPyun YongHyeon 	}
49312280c16bSPyun YongHyeon }
49322280c16bSPyun YongHyeon 
49332280c16bSPyun YongHyeon static void
49342280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
49352280c16bSPyun YongHyeon {
49362280c16bSPyun YongHyeon 
49372280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
49382280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
49392280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
49402280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
49412280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
49422280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
49432280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
49442280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
49452280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
49462280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
49472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
49482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
49492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
49502280c16bSPyun YongHyeon 
49512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
49522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
49532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
49542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
49552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
49562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
49572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
49582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
49592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
49602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
49612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
49622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
49632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
49642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
49652280c16bSPyun YongHyeon 
49662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
49672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
49682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
49692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
49702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49730434d1b8SBill Paul }
49740434d1b8SBill Paul 
49750434d1b8SBill Paul static void
49763f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
497795d67482SBill Paul {
497895d67482SBill Paul 	struct ifnet *ifp;
4979e907febfSPyun YongHyeon 	bus_size_t stats;
49807e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
498195d67482SBill Paul 
4982fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
498395d67482SBill Paul 
4984e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4985e907febfSPyun YongHyeon 
4986e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4987e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
498895d67482SBill Paul 
49898634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
49906b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
49916fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
49926fb34dd2SOleg Bulyzhin 
499337ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
499437ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_nobds);
499537ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
499637ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
499737ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_inerrs);
499837ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
49996fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
50006b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
50016fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
50026fb34dd2SOleg Bulyzhin 
50036fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
50046b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
50056fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
500695d67482SBill Paul 
5007e907febfSPyun YongHyeon #undef	READ_STAT
500895d67482SBill Paul }
500995d67482SBill Paul 
501095d67482SBill Paul /*
5011d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
5012d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
5013d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
5014d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
5015d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
5016d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
5017d375e524SGleb Smirnoff  */
5018d375e524SGleb Smirnoff static __inline int
5019d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
5020d375e524SGleb Smirnoff {
5021d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
5022d375e524SGleb Smirnoff 	struct mbuf *last;
5023d375e524SGleb Smirnoff 
5024d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
5025d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
5026d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
5027d375e524SGleb Smirnoff 		last = m;
5028d375e524SGleb Smirnoff 	} else {
5029d375e524SGleb Smirnoff 		/*
5030d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
5031d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
5032d375e524SGleb Smirnoff 		 */
5033d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
5034d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
5035d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
5036d375e524SGleb Smirnoff 			struct mbuf *n;
5037d375e524SGleb Smirnoff 
5038c6499eccSGleb Smirnoff 			MGET(n, M_NOWAIT, MT_DATA);
5039d375e524SGleb Smirnoff 			if (n == NULL)
5040d375e524SGleb Smirnoff 				return (ENOBUFS);
5041d375e524SGleb Smirnoff 			n->m_len = 0;
5042d375e524SGleb Smirnoff 			last->m_next = n;
5043d375e524SGleb Smirnoff 			last = n;
5044d375e524SGleb Smirnoff 		}
5045d375e524SGleb Smirnoff 	}
5046d375e524SGleb Smirnoff 
5047d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
5048d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
5049d375e524SGleb Smirnoff 	last->m_len += padlen;
5050d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
5051d375e524SGleb Smirnoff 
5052d375e524SGleb Smirnoff 	return (0);
5053d375e524SGleb Smirnoff }
5054d375e524SGleb Smirnoff 
5055ca3f1187SPyun YongHyeon static struct mbuf *
5056d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
5057d598b626SPyun YongHyeon {
5058d598b626SPyun YongHyeon 	struct mbuf *n;
5059d598b626SPyun YongHyeon 	int found;
5060d598b626SPyun YongHyeon 
5061d598b626SPyun YongHyeon 	/*
5062d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
5063d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
5064d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
5065d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
5066d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
5067d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
5068d598b626SPyun YongHyeon 	 */
5069d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
5070d598b626SPyun YongHyeon 		if (n->m_len < 8) {
5071d598b626SPyun YongHyeon 			found++;
5072d598b626SPyun YongHyeon 			if (found > 1)
5073d598b626SPyun YongHyeon 				break;
5074d598b626SPyun YongHyeon 			continue;
5075d598b626SPyun YongHyeon 		}
5076d598b626SPyun YongHyeon 		found = 0;
5077d598b626SPyun YongHyeon 	}
5078d598b626SPyun YongHyeon 
5079d598b626SPyun YongHyeon 	if (found > 1) {
5080c6499eccSGleb Smirnoff 		n = m_defrag(m, M_NOWAIT);
5081d598b626SPyun YongHyeon 		if (n == NULL)
5082d598b626SPyun YongHyeon 			m_freem(m);
5083d598b626SPyun YongHyeon 	} else
5084d598b626SPyun YongHyeon 		n = m;
5085d598b626SPyun YongHyeon 	return (n);
5086d598b626SPyun YongHyeon }
5087d598b626SPyun YongHyeon 
5088d598b626SPyun YongHyeon static struct mbuf *
50891108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
50901108273aSPyun YongHyeon     uint16_t *flags)
5091ca3f1187SPyun YongHyeon {
5092ca3f1187SPyun YongHyeon 	struct ip *ip;
5093ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
5094ca3f1187SPyun YongHyeon 	struct mbuf *n;
5095ca3f1187SPyun YongHyeon 	uint16_t hlen;
50965b355c4fSPyun YongHyeon 	uint32_t poff;
5097ca3f1187SPyun YongHyeon 
5098ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5099ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5100c6499eccSGleb Smirnoff 		n = m_dup(m, M_NOWAIT);
5101ca3f1187SPyun YongHyeon 		m_freem(m);
5102ca3f1187SPyun YongHyeon 		if (n == NULL)
5103ca3f1187SPyun YongHyeon 			return (NULL);
5104ca3f1187SPyun YongHyeon 		m = n;
5105ca3f1187SPyun YongHyeon 	}
51065b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5107ca3f1187SPyun YongHyeon 	if (m == NULL)
5108ca3f1187SPyun YongHyeon 		return (NULL);
51095b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
51105b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5111ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5112ca3f1187SPyun YongHyeon 	if (m == NULL)
5113ca3f1187SPyun YongHyeon 		return (NULL);
5114ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
51155b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5116ca3f1187SPyun YongHyeon 	if (m == NULL)
5117ca3f1187SPyun YongHyeon 		return (NULL);
5118ca3f1187SPyun YongHyeon 	/*
5119ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5120ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5121ca3f1187SPyun YongHyeon 	 */
5122ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
512396486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5124ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5125ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5126ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
512796486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5128ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5129ca3f1187SPyun YongHyeon 	/*
5130ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5131ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5132ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
51331108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5134ca3f1187SPyun YongHyeon 	 */
51351108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5136ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
51371108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
51381108273aSPyun YongHyeon 		/*
51391108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
51401108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
51411108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
51421108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
51431108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
51441108273aSPyun YongHyeon 		 * frames are supported.
51451108273aSPyun YongHyeon 		 */
51461108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
51471108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
51481108273aSPyun YongHyeon 	} else {
51491108273aSPyun YongHyeon 		/*
51501108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
51511108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
51521108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
51531108273aSPyun YongHyeon 		 * supported.
51541108273aSPyun YongHyeon 		 */
5155ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
51561108273aSPyun YongHyeon 	}
5157ca3f1187SPyun YongHyeon 	return (m);
5158ca3f1187SPyun YongHyeon }
5159ca3f1187SPyun YongHyeon 
5160d375e524SGleb Smirnoff /*
516195d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
516295d67482SBill Paul  * pointers to descriptors.
516395d67482SBill Paul  */
516495d67482SBill Paul static int
5165676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
516695d67482SBill Paul {
51677e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5168f41ac2beSBill Paul 	bus_dmamap_t		map;
5169676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5170676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
51717e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5172ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
51737e27542aSGleb Smirnoff 	int			nsegs, i, error;
517495d67482SBill Paul 
51756909dc43SGleb Smirnoff 	csum_flags = 0;
5176ca3f1187SPyun YongHyeon 	mss = 0;
5177ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5178d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5179d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5180d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5181d598b626SPyun YongHyeon 		if (*m_head == NULL)
5182d598b626SPyun YongHyeon 			return (ENOBUFS);
5183d598b626SPyun YongHyeon 		m = *m_head;
5184d598b626SPyun YongHyeon 	}
5185ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
51861108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5187ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5188ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5189ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5190ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
519135f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
51926909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
51936909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
51946909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
51956909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
51966909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
51976909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
51986909dc43SGleb Smirnoff 				m_freem(m);
51996909dc43SGleb Smirnoff 				*m_head = NULL;
52006909dc43SGleb Smirnoff 				return (error);
52016909dc43SGleb Smirnoff 			}
52026909dc43SGleb Smirnoff 		}
52036909dc43SGleb Smirnoff 	}
52046909dc43SGleb Smirnoff 
52051108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
52061108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
52071108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
52081108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
52091108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5210beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5211d94f2b85SPyun YongHyeon 			/*
5212d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5213d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5214d94f2b85SPyun YongHyeon 			 * DMA read operation.
5215d94f2b85SPyun YongHyeon 			 */
5216beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5217c6499eccSGleb Smirnoff 				m = m_defrag(m, M_NOWAIT);
5218d94f2b85SPyun YongHyeon 			else
5219c6499eccSGleb Smirnoff 				m = m_collapse(m, M_NOWAIT,
52201108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5221261f04d6SPyun YongHyeon 			if (m == NULL)
5222261f04d6SPyun YongHyeon 				m = *m_head;
5223d94f2b85SPyun YongHyeon 			*m_head = m;
5224d94f2b85SPyun YongHyeon 		}
52251108273aSPyun YongHyeon 	}
5226d94f2b85SPyun YongHyeon 
52277e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
52280ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5229676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
52307e27542aSGleb Smirnoff 	if (error == EFBIG) {
5231c6499eccSGleb Smirnoff 		m = m_collapse(m, M_NOWAIT, BGE_NSEG_NEW);
5232676ad2c9SGleb Smirnoff 		if (m == NULL) {
5233676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5234676ad2c9SGleb Smirnoff 			*m_head = NULL;
52357e27542aSGleb Smirnoff 			return (ENOBUFS);
52367e27542aSGleb Smirnoff 		}
5237676ad2c9SGleb Smirnoff 		*m_head = m;
52380ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
52390ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5240676ad2c9SGleb Smirnoff 		if (error) {
5241676ad2c9SGleb Smirnoff 			m_freem(m);
5242676ad2c9SGleb Smirnoff 			*m_head = NULL;
52437e27542aSGleb Smirnoff 			return (error);
52447e27542aSGleb Smirnoff 		}
5245676ad2c9SGleb Smirnoff 	} else if (error != 0)
5246676ad2c9SGleb Smirnoff 		return (error);
52477e27542aSGleb Smirnoff 
5248167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5249167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
52500ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
525195d67482SBill Paul 		return (ENOBUFS);
52527e27542aSGleb Smirnoff 	}
52537e27542aSGleb Smirnoff 
52540ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5255e65bed95SPyun YongHyeon 
5256ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5257ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5258ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5259ca3f1187SPyun YongHyeon 	}
5260*b77d3a3bSPyun YongHyeon 
5261*b77d3a3bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762 &&
5262*b77d3a3bSPyun YongHyeon 	    (m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
5263*b77d3a3bSPyun YongHyeon 		/*
5264*b77d3a3bSPyun YongHyeon 		 * 5725 family of devices corrupts TSO packets when TSO DMA
5265*b77d3a3bSPyun YongHyeon 		 * buffers cross into regions which are within MSS bytes of
5266*b77d3a3bSPyun YongHyeon 		 * a 4GB boundary.  If we encounter the condition, drop the
5267*b77d3a3bSPyun YongHyeon 		 * packet.
5268*b77d3a3bSPyun YongHyeon 		 */
5269*b77d3a3bSPyun YongHyeon 		for (i = 0; ; i++) {
5270*b77d3a3bSPyun YongHyeon 			d = &sc->bge_ldata.bge_tx_ring[idx];
5271*b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
5272*b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
5273*b77d3a3bSPyun YongHyeon 			d->bge_len = segs[i].ds_len;
5274*b77d3a3bSPyun YongHyeon 			if (d->bge_addr.bge_addr_lo + segs[i].ds_len + mss <
5275*b77d3a3bSPyun YongHyeon 			    d->bge_addr.bge_addr_lo)
5276*b77d3a3bSPyun YongHyeon 				break;
5277*b77d3a3bSPyun YongHyeon 			d->bge_flags = csum_flags;
5278*b77d3a3bSPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5279*b77d3a3bSPyun YongHyeon 			d->bge_mss = mss;
5280*b77d3a3bSPyun YongHyeon 			if (i == nsegs - 1)
5281*b77d3a3bSPyun YongHyeon 				break;
5282*b77d3a3bSPyun YongHyeon 			BGE_INC(idx, BGE_TX_RING_CNT);
5283*b77d3a3bSPyun YongHyeon 		}
5284*b77d3a3bSPyun YongHyeon 		if (i != nsegs - 1) {
5285*b77d3a3bSPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map,
5286*b77d3a3bSPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
5287*b77d3a3bSPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
5288*b77d3a3bSPyun YongHyeon 			m_freem(*m_head);
5289*b77d3a3bSPyun YongHyeon 			*m_head = NULL;
5290*b77d3a3bSPyun YongHyeon 			return (EIO);
5291*b77d3a3bSPyun YongHyeon 		}
5292*b77d3a3bSPyun YongHyeon 	} else {
52937e27542aSGleb Smirnoff 		for (i = 0; ; i++) {
52947e27542aSGleb Smirnoff 			d = &sc->bge_ldata.bge_tx_ring[idx];
52957e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
52967e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
52977e27542aSGleb Smirnoff 			d->bge_len = segs[i].ds_len;
52987e27542aSGleb Smirnoff 			d->bge_flags = csum_flags;
5299ca3f1187SPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5300ca3f1187SPyun YongHyeon 			d->bge_mss = mss;
53017e27542aSGleb Smirnoff 			if (i == nsegs - 1)
53027e27542aSGleb Smirnoff 				break;
53037e27542aSGleb Smirnoff 			BGE_INC(idx, BGE_TX_RING_CNT);
53047e27542aSGleb Smirnoff 		}
5305*b77d3a3bSPyun YongHyeon 	}
53067e27542aSGleb Smirnoff 
53077e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
53087e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5309676ad2c9SGleb Smirnoff 
5310f41ac2beSBill Paul 	/*
5311f41ac2beSBill Paul 	 * Insure that the map for this transmission
5312f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5313f41ac2beSBill Paul 	 * in this chain.
5314f41ac2beSBill Paul 	 */
53157e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
53167e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5317676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
53187e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
531995d67482SBill Paul 
53207e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
53217e27542aSGleb Smirnoff 	*txidx = idx;
532295d67482SBill Paul 
532395d67482SBill Paul 	return (0);
532495d67482SBill Paul }
532595d67482SBill Paul 
532695d67482SBill Paul /*
532795d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
532895d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
532995d67482SBill Paul  */
533095d67482SBill Paul static void
53313f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
533295d67482SBill Paul {
533395d67482SBill Paul 	struct bge_softc *sc;
5334167fdb62SPyun YongHyeon 	struct mbuf *m_head;
533514bbd30fSGleb Smirnoff 	uint32_t prodidx;
5336167fdb62SPyun YongHyeon 	int count;
533795d67482SBill Paul 
533895d67482SBill Paul 	sc = ifp->if_softc;
5339167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
534095d67482SBill Paul 
5341167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5342167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5343167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
534495d67482SBill Paul 		return;
534595d67482SBill Paul 
534614bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
534795d67482SBill Paul 
5348167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
5349167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5350167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5351167fdb62SPyun YongHyeon 			break;
5352167fdb62SPyun YongHyeon 		}
53534d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
535495d67482SBill Paul 		if (m_head == NULL)
535595d67482SBill Paul 			break;
535695d67482SBill Paul 
535795d67482SBill Paul 		/*
535895d67482SBill Paul 		 * Pack the data into the transmit ring. If we
535995d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
536095d67482SBill Paul 		 * for the NIC to drain the ring.
536195d67482SBill Paul 		 */
5362676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5363676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5364676ad2c9SGleb Smirnoff 				break;
53654d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
536613f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
536795d67482SBill Paul 			break;
536895d67482SBill Paul 		}
5369303a718cSDag-Erling Smørgrav 		++count;
537095d67482SBill Paul 
537195d67482SBill Paul 		/*
537295d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
537395d67482SBill Paul 		 * to him.
537495d67482SBill Paul 		 */
53754e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
537645ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
53774e35d186SJung-uk Kim #else
53784e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
53794e35d186SJung-uk Kim #endif
538095d67482SBill Paul 	}
538195d67482SBill Paul 
5382167fdb62SPyun YongHyeon 	if (count > 0) {
5383aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
53845c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
53853f74909aSGleb Smirnoff 		/* Transmit. */
538638cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
53873927098fSPaul Saab 		/* 5700 b2 errata */
5388e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
538938cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
539095d67482SBill Paul 
539114bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
539214bbd30fSGleb Smirnoff 
539395d67482SBill Paul 		/*
539495d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
539595d67482SBill Paul 		 */
5396b584d2b3SPyun YongHyeon 		sc->bge_timer = BGE_TX_TIMEOUT;
539795d67482SBill Paul 	}
5398167fdb62SPyun YongHyeon }
539995d67482SBill Paul 
54000f9bd73bSSam Leffler /*
54010f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
54020f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
54030f9bd73bSSam Leffler  */
540495d67482SBill Paul static void
54053f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
540695d67482SBill Paul {
54070f9bd73bSSam Leffler 	struct bge_softc *sc;
54080f9bd73bSSam Leffler 
54090f9bd73bSSam Leffler 	sc = ifp->if_softc;
54100f9bd73bSSam Leffler 	BGE_LOCK(sc);
54110f9bd73bSSam Leffler 	bge_start_locked(ifp);
54120f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
54130f9bd73bSSam Leffler }
54140f9bd73bSSam Leffler 
54150f9bd73bSSam Leffler static void
54163f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
54170f9bd73bSSam Leffler {
541895d67482SBill Paul 	struct ifnet *ifp;
54193f74909aSGleb Smirnoff 	uint16_t *m;
5420f6a65488SPyun YongHyeon 	uint32_t mode;
542195d67482SBill Paul 
54220f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
542395d67482SBill Paul 
5424fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
542595d67482SBill Paul 
542613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
542795d67482SBill Paul 		return;
542895d67482SBill Paul 
542995d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
543095d67482SBill Paul 	bge_stop(sc);
54318cb1383cSDoug Ambrisko 
54328cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
54338cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
543495d67482SBill Paul 	bge_reset(sc);
54358cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
54368cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
54378cb1383cSDoug Ambrisko 
543895d67482SBill Paul 	bge_chipinit(sc);
543995d67482SBill Paul 
544095d67482SBill Paul 	/*
544195d67482SBill Paul 	 * Init the various state machines, ring
544295d67482SBill Paul 	 * control blocks and firmware.
544395d67482SBill Paul 	 */
544495d67482SBill Paul 	if (bge_blockinit(sc)) {
5445fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
544695d67482SBill Paul 		return;
544795d67482SBill Paul 	}
544895d67482SBill Paul 
5449fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
545095d67482SBill Paul 
545195d67482SBill Paul 	/* Specify MTU. */
545295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
5453cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5454cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
545595d67482SBill Paul 
545695d67482SBill Paul 	/* Load our MAC address. */
54573f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
545895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
545995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
546095d67482SBill Paul 
54613e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
54623e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
546395d67482SBill Paul 
546495d67482SBill Paul 	/* Program multicast filter. */
546595d67482SBill Paul 	bge_setmulti(sc);
546695d67482SBill Paul 
5467cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5468cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5469cb2eacc7SYaroslav Tykhiy 
547035f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
547135f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
547235f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
547335f945cdSPyun YongHyeon 	else
547435f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
547535f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
547635f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
547735f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
547835f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
547935f945cdSPyun YongHyeon 	}
548035f945cdSPyun YongHyeon 
548195d67482SBill Paul 	/* Init RX ring. */
54823ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
54833ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
54843ee5d7daSPyun YongHyeon 		bge_stop(sc);
54853ee5d7daSPyun YongHyeon 		return;
54863ee5d7daSPyun YongHyeon 	}
548795d67482SBill Paul 
54880434d1b8SBill Paul 	/*
54890434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
54900434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
54910434d1b8SBill Paul 	 * entry of the ring.
54920434d1b8SBill Paul 	 */
54930434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
54943f74909aSGleb Smirnoff 		uint32_t		v, i;
54950434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
54960434d1b8SBill Paul 			DELAY(20);
54970434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
54980434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
54990434d1b8SBill Paul 				break;
55000434d1b8SBill Paul 		}
55010434d1b8SBill Paul 		if (i == 10)
5502fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5503fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
55040434d1b8SBill Paul 	}
55050434d1b8SBill Paul 
550695d67482SBill Paul 	/* Init jumbo RX ring. */
5507f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5508f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
5509c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
55103ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5511333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5512b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
55133ee5d7daSPyun YongHyeon 			bge_stop(sc);
55143ee5d7daSPyun YongHyeon 			return;
55153ee5d7daSPyun YongHyeon 		}
55163ee5d7daSPyun YongHyeon 	}
551795d67482SBill Paul 
55183f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
551995d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
552095d67482SBill Paul 
55217e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
55227e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
55237e6e2507SJung-uk Kim 
552495d67482SBill Paul 	/* Init TX ring. */
552595d67482SBill Paul 	bge_init_tx_ring(sc);
552695d67482SBill Paul 
5527f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5528f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5529f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5530f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
55312927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
55322927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
553350515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
553450515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
553550515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
553650515680SPyun YongHyeon 	}
55373f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5538f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5539a6e66cd2SPyun YongHyeon 	DELAY(100);
554095d67482SBill Paul 
55413f74909aSGleb Smirnoff 	/* Turn on receiver. */
5542548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5543548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5544548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
5545548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5546a6e66cd2SPyun YongHyeon 	DELAY(10);
554795d67482SBill Paul 
5548dedcdf57SPyun YongHyeon 	/*
5549dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5550dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5551dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5552dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5553dedcdf57SPyun YongHyeon 	 */
55543fc5fbfbSPyun YongHyeon 	if (BGE_IS_57765_PLUS(sc))
5555b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5556b4a256acSPyun YongHyeon 	else
5557dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5558dedcdf57SPyun YongHyeon 
55592280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
55602280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
55612280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
55622280c16bSPyun YongHyeon 
556395d67482SBill Paul 	/* Tell firmware we're alive. */
556495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
556595d67482SBill Paul 
556675719184SGleb Smirnoff #ifdef DEVICE_POLLING
556775719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
556875719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
556975719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
557075719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
557138cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
557275719184SGleb Smirnoff 	} else
557375719184SGleb Smirnoff #endif
557475719184SGleb Smirnoff 
557595d67482SBill Paul 	/* Enable host interrupts. */
557675719184SGleb Smirnoff 	{
557795d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
557895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
557938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
558075719184SGleb Smirnoff 	}
558195d67482SBill Paul 
558213f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
558313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
558495d67482SBill Paul 
5585e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5586e4146b95SPyun YongHyeon 
55870f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
55880f9bd73bSSam Leffler }
55890f9bd73bSSam Leffler 
55900f9bd73bSSam Leffler static void
55913f74909aSGleb Smirnoff bge_init(void *xsc)
55920f9bd73bSSam Leffler {
55930f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
55940f9bd73bSSam Leffler 
55950f9bd73bSSam Leffler 	BGE_LOCK(sc);
55960f9bd73bSSam Leffler 	bge_init_locked(sc);
55970f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
559895d67482SBill Paul }
559995d67482SBill Paul 
560095d67482SBill Paul /*
560195d67482SBill Paul  * Set media options.
560295d67482SBill Paul  */
560395d67482SBill Paul static int
56043f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
560595d67482SBill Paul {
560667d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
560767d5e043SOleg Bulyzhin 	int res;
560867d5e043SOleg Bulyzhin 
560967d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
561067d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
561167d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
561267d5e043SOleg Bulyzhin 
561367d5e043SOleg Bulyzhin 	return (res);
561467d5e043SOleg Bulyzhin }
561567d5e043SOleg Bulyzhin 
561667d5e043SOleg Bulyzhin static int
561767d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
561867d5e043SOleg Bulyzhin {
561967d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
562095d67482SBill Paul 	struct mii_data *mii;
56214f09c4c7SMarius Strobl 	struct mii_softc *miisc;
562295d67482SBill Paul 	struct ifmedia *ifm;
562395d67482SBill Paul 
562467d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
562567d5e043SOleg Bulyzhin 
562695d67482SBill Paul 	ifm = &sc->bge_ifmedia;
562795d67482SBill Paul 
562895d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5629652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
563095d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
563195d67482SBill Paul 			return (EINVAL);
563295d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
563395d67482SBill Paul 		case IFM_AUTO:
5634ff50922bSDoug White 			/*
5635ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5636ff50922bSDoug White 			 * mechanism for programming the autoneg
5637ff50922bSDoug White 			 * advertisement registers in TBI mode.
5638ff50922bSDoug White 			 */
56390f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5640ff50922bSDoug White 				uint32_t sgdig;
56410f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
56420f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5643ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5644ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5645ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5646ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5647ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5648ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5649ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5650ff50922bSDoug White 					DELAY(5);
5651ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5652ff50922bSDoug White 				}
56530f89fde2SJung-uk Kim 			}
565495d67482SBill Paul 			break;
565595d67482SBill Paul 		case IFM_1000_SX:
565695d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
565795d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
565895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
565995d67482SBill Paul 			} else {
566095d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
566195d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
566295d67482SBill Paul 			}
56639b80ffe7SPyun YongHyeon 			DELAY(40);
566495d67482SBill Paul 			break;
566595d67482SBill Paul 		default:
566695d67482SBill Paul 			return (EINVAL);
566795d67482SBill Paul 		}
566895d67482SBill Paul 		return (0);
566995d67482SBill Paul 	}
567095d67482SBill Paul 
56711493e883SOleg Bulyzhin 	sc->bge_link_evt++;
567295d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
56734f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
56743fcb7a53SMarius Strobl 		PHY_RESET(miisc);
567595d67482SBill Paul 	mii_mediachg(mii);
567695d67482SBill Paul 
5677902827f6SBjoern A. Zeeb 	/*
5678902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5679902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5680902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5681902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5682902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5683902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5684902827f6SBjoern A. Zeeb 	 * get an RX intr.
5685902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5686902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5687902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5688902827f6SBjoern A. Zeeb 	 */
56894f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
56904f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5691902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
56924f0794ffSBjoern A. Zeeb 	else
569363ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5694902827f6SBjoern A. Zeeb 
569595d67482SBill Paul 	return (0);
569695d67482SBill Paul }
569795d67482SBill Paul 
569895d67482SBill Paul /*
569995d67482SBill Paul  * Report current media status.
570095d67482SBill Paul  */
570195d67482SBill Paul static void
57023f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
570395d67482SBill Paul {
570467d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
570595d67482SBill Paul 	struct mii_data *mii;
570695d67482SBill Paul 
570767d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
570895d67482SBill Paul 
5709b9d2edd7SPyun YongHyeon 	if ((ifp->if_flags & IFF_UP) == 0) {
5710b9d2edd7SPyun YongHyeon 		BGE_UNLOCK(sc);
5711b9d2edd7SPyun YongHyeon 		return;
5712b9d2edd7SPyun YongHyeon 	}
5713652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
571495d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
571595d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
571695d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
571795d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
571895d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
57194c0da0ffSGleb Smirnoff 		else {
57204c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
572167d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
57224c0da0ffSGleb Smirnoff 			return;
57234c0da0ffSGleb Smirnoff 		}
572495d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
572595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
572695d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
572795d67482SBill Paul 		else
572895d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
572967d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
573095d67482SBill Paul 		return;
573195d67482SBill Paul 	}
573295d67482SBill Paul 
573395d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
573495d67482SBill Paul 	mii_pollstat(mii);
573595d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
573695d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
573767d5e043SOleg Bulyzhin 
573867d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
573995d67482SBill Paul }
574095d67482SBill Paul 
574195d67482SBill Paul static int
57423f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
574395d67482SBill Paul {
574495d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
574595d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
574695d67482SBill Paul 	struct mii_data *mii;
5747f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
574895d67482SBill Paul 
574995d67482SBill Paul 	switch (command) {
575095d67482SBill Paul 	case SIOCSIFMTU:
5751f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5752f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
57534c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5754f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
575595d67482SBill Paul 				error = EINVAL;
5756f5459d4cSPyun YongHyeon 				break;
5757f5459d4cSPyun YongHyeon 			}
5758f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5759f5459d4cSPyun YongHyeon 			error = EINVAL;
5760f5459d4cSPyun YongHyeon 			break;
5761f5459d4cSPyun YongHyeon 		}
5762f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5763f5459d4cSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
576495d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
57653a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
576613f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
57673a429c8fSPyun YongHyeon 				bge_init_locked(sc);
576895d67482SBill Paul 			}
57693a429c8fSPyun YongHyeon 		}
57703a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
577195d67482SBill Paul 		break;
577295d67482SBill Paul 	case SIOCSIFFLAGS:
57730f9bd73bSSam Leffler 		BGE_LOCK(sc);
577495d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
577595d67482SBill Paul 			/*
577695d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
577795d67482SBill Paul 			 * then just use the 'set promisc mode' command
577895d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
577995d67482SBill Paul 			 * a full re-init means reloading the firmware and
578095d67482SBill Paul 			 * waiting for it to start up, which may take a
5781d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
578295d67482SBill Paul 			 */
5783f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5784f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
57853e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
57863e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5787f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5788d183af7fSRuslan Ermilov 					bge_setmulti(sc);
578995d67482SBill Paul 			} else
57900f9bd73bSSam Leffler 				bge_init_locked(sc);
579195d67482SBill Paul 		} else {
579213f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
579395d67482SBill Paul 				bge_stop(sc);
579495d67482SBill Paul 			}
579595d67482SBill Paul 		}
579695d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
57970f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
579895d67482SBill Paul 		error = 0;
579995d67482SBill Paul 		break;
580095d67482SBill Paul 	case SIOCADDMULTI:
580195d67482SBill Paul 	case SIOCDELMULTI:
580213f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
58030f9bd73bSSam Leffler 			BGE_LOCK(sc);
580495d67482SBill Paul 			bge_setmulti(sc);
58050f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
580695d67482SBill Paul 			error = 0;
580795d67482SBill Paul 		}
580895d67482SBill Paul 		break;
580995d67482SBill Paul 	case SIOCSIFMEDIA:
581095d67482SBill Paul 	case SIOCGIFMEDIA:
5811652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
581295d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
581395d67482SBill Paul 			    &sc->bge_ifmedia, command);
581495d67482SBill Paul 		} else {
581595d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
581695d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
581795d67482SBill Paul 			    &mii->mii_media, command);
581895d67482SBill Paul 		}
581995d67482SBill Paul 		break;
582095d67482SBill Paul 	case SIOCSIFCAP:
582195d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
582275719184SGleb Smirnoff #ifdef DEVICE_POLLING
582375719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
582475719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
582575719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
582675719184SGleb Smirnoff 				if (error)
582775719184SGleb Smirnoff 					return (error);
582875719184SGleb Smirnoff 				BGE_LOCK(sc);
582975719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
583075719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
583138cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
583275719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
583375719184SGleb Smirnoff 				BGE_UNLOCK(sc);
583475719184SGleb Smirnoff 			} else {
583575719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
583675719184SGleb Smirnoff 				/* Enable interrupt even in error case */
583775719184SGleb Smirnoff 				BGE_LOCK(sc);
583875719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
583975719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
584038cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
584175719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
584275719184SGleb Smirnoff 				BGE_UNLOCK(sc);
584375719184SGleb Smirnoff 			}
584475719184SGleb Smirnoff 		}
584575719184SGleb Smirnoff #endif
5846d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5847d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
5848d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
5849d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
585035f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
585195d67482SBill Paul 			else
585235f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
585395d67482SBill Paul 		}
5854cb2eacc7SYaroslav Tykhiy 
5855d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5856d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
5857d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
5858d8b57f98SPyun YongHyeon 
5859ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5860ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
5861ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
5862ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
5863ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
5864ca3f1187SPyun YongHyeon 			else
5865ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
5866ca3f1187SPyun YongHyeon 		}
5867ca3f1187SPyun YongHyeon 
5868cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5869cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
5870cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5871cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5872cb2eacc7SYaroslav Tykhiy 		}
5873cb2eacc7SYaroslav Tykhiy 
587404bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
587504bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
587604bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
587704bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
587804bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
5879cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
588004bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
588104bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
5882cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5883cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5884cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
588504bde852SPyun YongHyeon 		}
5886cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5887cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
5888cb2eacc7SYaroslav Tykhiy #endif
588995d67482SBill Paul 		break;
589095d67482SBill Paul 	default:
5891673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
589295d67482SBill Paul 		break;
589395d67482SBill Paul 	}
589495d67482SBill Paul 
589595d67482SBill Paul 	return (error);
589695d67482SBill Paul }
589795d67482SBill Paul 
589895d67482SBill Paul static void
5899b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
590095d67482SBill Paul {
5901b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
5902b584d2b3SPyun YongHyeon 	uint32_t status;
590395d67482SBill Paul 
5904b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5905b74e67fbSGleb Smirnoff 
5906b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5907b74e67fbSGleb Smirnoff 		return;
5908b74e67fbSGleb Smirnoff 
5909b584d2b3SPyun YongHyeon 	/* If pause frames are active then don't reset the hardware. */
5910b584d2b3SPyun YongHyeon 	if ((CSR_READ_4(sc, BGE_RX_MODE) & BGE_RXMODE_FLOWCTL_ENABLE) != 0) {
5911b584d2b3SPyun YongHyeon 		status = CSR_READ_4(sc, BGE_RX_STS);
5912b584d2b3SPyun YongHyeon 		if ((status & BGE_RXSTAT_REMOTE_XOFFED) != 0) {
5913b584d2b3SPyun YongHyeon 			/*
5914b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5915b584d2b3SPyun YongHyeon 			 * the condition to clear.
5916b584d2b3SPyun YongHyeon 			 */
5917b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5918b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5919b584d2b3SPyun YongHyeon 			return;
5920b584d2b3SPyun YongHyeon 		} else if ((status & BGE_RXSTAT_RCVD_XOFF) != 0 &&
5921b584d2b3SPyun YongHyeon 		    (status & BGE_RXSTAT_RCVD_XON) != 0) {
5922b584d2b3SPyun YongHyeon 			/*
5923b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5924b584d2b3SPyun YongHyeon 			 * the condition to clear.
5925b584d2b3SPyun YongHyeon 			 */
5926b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5927b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5928b584d2b3SPyun YongHyeon 			return;
5929b584d2b3SPyun YongHyeon 		}
5930b584d2b3SPyun YongHyeon 		/*
5931b584d2b3SPyun YongHyeon 		 * Any other condition is unexpected and the controller
5932b584d2b3SPyun YongHyeon 		 * should be reset.
5933b584d2b3SPyun YongHyeon 		 */
5934b584d2b3SPyun YongHyeon 	}
5935b584d2b3SPyun YongHyeon 
5936b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
593795d67482SBill Paul 
5938fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
593995d67482SBill Paul 
594013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5941426742bfSGleb Smirnoff 	bge_init_locked(sc);
594295d67482SBill Paul 
594395d67482SBill Paul 	ifp->if_oerrors++;
594495d67482SBill Paul }
594595d67482SBill Paul 
59465a147ba6SPyun YongHyeon static void
59475a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
59485a147ba6SPyun YongHyeon {
59495a147ba6SPyun YongHyeon 	int i;
59505a147ba6SPyun YongHyeon 
59515a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
59525a147ba6SPyun YongHyeon 
59535a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
59545a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
59555a147ba6SPyun YongHyeon 			return;
59565a147ba6SPyun YongHyeon 		DELAY(100);
59575a147ba6SPyun YongHyeon         }
59585a147ba6SPyun YongHyeon }
59595a147ba6SPyun YongHyeon 
596095d67482SBill Paul /*
596195d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
596295d67482SBill Paul  * RX and TX lists.
596395d67482SBill Paul  */
596495d67482SBill Paul static void
59653f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
596695d67482SBill Paul {
596795d67482SBill Paul 	struct ifnet *ifp;
596895d67482SBill Paul 
59690f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
59700f9bd73bSSam Leffler 
5971fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
597295d67482SBill Paul 
59730f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
597495d67482SBill Paul 
597544b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
597644b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
597744b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
597844b63691SBjoern A. Zeeb 
597944b63691SBjoern A. Zeeb 	/*
598044b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
598144b63691SBjoern A. Zeeb 	 */
598244b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
5983548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
598444b63691SBjoern A. Zeeb 
598595d67482SBill Paul 	/*
59863f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
598795d67482SBill Paul 	 */
59885a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
59895a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
59905a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
59915a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
59925a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
59935a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
59945a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
59955a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
599695d67482SBill Paul 
599795d67482SBill Paul 	/*
59983f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
599995d67482SBill Paul 	 */
60005a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
60015a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
60025a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
60035a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
60045a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
60055a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60065a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
60075a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
600895d67482SBill Paul 
600995d67482SBill Paul 	/*
601095d67482SBill Paul 	 * Shut down all of the memory managers and related
601195d67482SBill Paul 	 * state machines.
601295d67482SBill Paul 	 */
60135a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
60145a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
60155a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60165a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
60175a147ba6SPyun YongHyeon 
60180c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
601995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
60207ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
602195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
602295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
60230434d1b8SBill Paul 	}
60242280c16bSPyun YongHyeon 	/* Update MAC statistics. */
60252280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
60262280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
602795d67482SBill Paul 
60288cb1383cSDoug Ambrisko 	bge_reset(sc);
6029548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
6030548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
60318cb1383cSDoug Ambrisko 
60328cb1383cSDoug Ambrisko 	/*
60338cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
60348cb1383cSDoug Ambrisko 	 */
60358cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
60368cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
60378cb1383cSDoug Ambrisko 	else
603895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
603995d67482SBill Paul 
604095d67482SBill Paul 	/* Free the RX lists. */
604195d67482SBill Paul 	bge_free_rx_ring_std(sc);
604295d67482SBill Paul 
604395d67482SBill Paul 	/* Free jumbo RX list. */
60444c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
604595d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
604695d67482SBill Paul 
604795d67482SBill Paul 	/* Free TX buffers. */
604895d67482SBill Paul 	bge_free_tx_ring(sc);
604995d67482SBill Paul 
605095d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
605195d67482SBill Paul 
60525dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
60531493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
60541493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
60551493e883SOleg Bulyzhin 	sc->bge_link = 0;
605695d67482SBill Paul 
60571493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
605895d67482SBill Paul }
605995d67482SBill Paul 
606095d67482SBill Paul /*
606195d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
606295d67482SBill Paul  * get confused by errant DMAs when rebooting.
606395d67482SBill Paul  */
6064b6c974e8SWarner Losh static int
60653f74909aSGleb Smirnoff bge_shutdown(device_t dev)
606695d67482SBill Paul {
606795d67482SBill Paul 	struct bge_softc *sc;
606895d67482SBill Paul 
606995d67482SBill Paul 	sc = device_get_softc(dev);
60700f9bd73bSSam Leffler 	BGE_LOCK(sc);
607195d67482SBill Paul 	bge_stop(sc);
60720f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
6073b6c974e8SWarner Losh 
6074b6c974e8SWarner Losh 	return (0);
607595d67482SBill Paul }
607614afefa3SPawel Jakub Dawidek 
607714afefa3SPawel Jakub Dawidek static int
607814afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
607914afefa3SPawel Jakub Dawidek {
608014afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
608114afefa3SPawel Jakub Dawidek 
608214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
608314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
608414afefa3SPawel Jakub Dawidek 	bge_stop(sc);
608514afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
608614afefa3SPawel Jakub Dawidek 
608714afefa3SPawel Jakub Dawidek 	return (0);
608814afefa3SPawel Jakub Dawidek }
608914afefa3SPawel Jakub Dawidek 
609014afefa3SPawel Jakub Dawidek static int
609114afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
609214afefa3SPawel Jakub Dawidek {
609314afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
609414afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
609514afefa3SPawel Jakub Dawidek 
609614afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
609714afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
609814afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
609914afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
610014afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
610114afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
610214afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
610314afefa3SPawel Jakub Dawidek 	}
610414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
610514afefa3SPawel Jakub Dawidek 
610614afefa3SPawel Jakub Dawidek 	return (0);
610714afefa3SPawel Jakub Dawidek }
6108dab5cd05SOleg Bulyzhin 
6109dab5cd05SOleg Bulyzhin static void
61103f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
6111dab5cd05SOleg Bulyzhin {
61121f313773SOleg Bulyzhin 	struct mii_data *mii;
61131f313773SOleg Bulyzhin 	uint32_t link, status;
6114dab5cd05SOleg Bulyzhin 
6115dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
61161f313773SOleg Bulyzhin 
61173f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
61187b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
61197b97099dSOleg Bulyzhin 
6120dab5cd05SOleg Bulyzhin 	/*
6121dab5cd05SOleg Bulyzhin 	 * Process link state changes.
6122dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
6123dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
6124dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
6125dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
6126dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
6127dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
6128dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
6129dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
61301f313773SOleg Bulyzhin 	 *
61311f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
61324c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
6133dab5cd05SOleg Bulyzhin 	 */
6134dab5cd05SOleg Bulyzhin 
61351f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
61364c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
6137dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
6138dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
61391f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
61405dda8085SOleg Bulyzhin 			mii_pollstat(mii);
61411f313773SOleg Bulyzhin 			if (!sc->bge_link &&
61421f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
61431f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
61441f313773SOleg Bulyzhin 				sc->bge_link++;
61451f313773SOleg Bulyzhin 				if (bootverbose)
61461f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61471f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
61481f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
61491f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
61501f313773SOleg Bulyzhin 				sc->bge_link = 0;
61511f313773SOleg Bulyzhin 				if (bootverbose)
61521f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
61531f313773SOleg Bulyzhin 			}
61541f313773SOleg Bulyzhin 
61553f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6156dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6157dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6158daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6159daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6160daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6161daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6162dab5cd05SOleg Bulyzhin 		}
6163dab5cd05SOleg Bulyzhin 		return;
6164dab5cd05SOleg Bulyzhin 	}
6165dab5cd05SOleg Bulyzhin 
6166652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
61671f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
61687b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
61697b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
61701f313773SOleg Bulyzhin 				sc->bge_link++;
61719b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
61721f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
61731f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
61749b80ffe7SPyun YongHyeon 					DELAY(40);
61759b80ffe7SPyun YongHyeon 				}
61760c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
61771f313773SOleg Bulyzhin 				if (bootverbose)
61781f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61793f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
61803f74909aSGleb Smirnoff 				    LINK_STATE_UP);
61817b97099dSOleg Bulyzhin 			}
61821f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6183dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
61841f313773SOleg Bulyzhin 			if (bootverbose)
61851f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
61867b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
61871f313773SOleg Bulyzhin 		}
61886ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
61891f313773SOleg Bulyzhin 		/*
61900c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
61910c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
61920c8aa4eaSJung-uk Kim 		 * PHY link status directly.
61931f313773SOleg Bulyzhin 		 */
61941f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
61951f313773SOleg Bulyzhin 
61961f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
61971f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
61981f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
61995dda8085SOleg Bulyzhin 			mii_pollstat(mii);
62001f313773SOleg Bulyzhin 			if (!sc->bge_link &&
62011f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
62021f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
62031f313773SOleg Bulyzhin 				sc->bge_link++;
62041f313773SOleg Bulyzhin 				if (bootverbose)
62051f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
62061f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
62071f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
62081f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
62091f313773SOleg Bulyzhin 				sc->bge_link = 0;
62101f313773SOleg Bulyzhin 				if (bootverbose)
62111f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
62121f313773SOleg Bulyzhin 			}
62131f313773SOleg Bulyzhin 		}
62140c8aa4eaSJung-uk Kim 	} else {
62150c8aa4eaSJung-uk Kim 		/*
62166ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
62176ede2cfaSPyun YongHyeon 		 * link status.
62180c8aa4eaSJung-uk Kim 		 */
62196ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
62206ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
62216ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6222dab5cd05SOleg Bulyzhin 	}
6223dab5cd05SOleg Bulyzhin 
62242246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6225dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6226dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6227dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6228dab5cd05SOleg Bulyzhin }
62296f8718a3SScott Long 
62306f8718a3SScott Long static void
62316f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
62326f8718a3SScott Long {
62336f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
62342280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
62357e32f79aSPyun YongHyeon 	char tn[32];
62367e32f79aSPyun YongHyeon 	int unit;
62376f8718a3SScott Long 
62386f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
62396f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
62406f8718a3SScott Long 
62416f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
62426f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
62436f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
62446f8718a3SScott Long 	    "Debug Information");
62456f8718a3SScott Long 
62466f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
62476f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
6248548c8f1aSPyun YongHyeon 	    "MAC Register Read");
6249548c8f1aSPyun YongHyeon 
6250548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
6251548c8f1aSPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_ape_read, "I",
6252548c8f1aSPyun YongHyeon 	    "APE Register Read");
62536f8718a3SScott Long 
62546f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
62556f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
62566f8718a3SScott Long 	    "Memory Read");
62576f8718a3SScott Long 
62586f8718a3SScott Long #endif
6259763757b2SScott Long 
62607e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
6261beaa2ae1SPyun YongHyeon 	/*
6262beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6263beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6264beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6265beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6266beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6267beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6268beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6269beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6270beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6271beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6272beaa2ae1SPyun YongHyeon 	 */
62737e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
62747e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
62757e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
6276beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6277beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
6278beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6279beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6280beaa2ae1SPyun YongHyeon 
62812ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
62822ae7f64bSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.msi", unit);
62832ae7f64bSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_msi);
62842ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
62852ae7f64bSPyun YongHyeon 	    CTLFLAG_RD, &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 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
629935f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
630035f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
630135f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
630235f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
630335f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
630435f945cdSPyun YongHyeon 
6305d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
63062280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
63072280c16bSPyun YongHyeon 	else
63082280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
63092280c16bSPyun YongHyeon }
6310d949071dSJung-uk Kim 
63112280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
63122280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
63132280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
63142280c16bSPyun YongHyeon 	    desc)
63152280c16bSPyun YongHyeon 
63162280c16bSPyun YongHyeon static void
63172280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
63182280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
63192280c16bSPyun YongHyeon {
63202280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
63212280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
63222280c16bSPyun YongHyeon 
63232280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6324763757b2SScott Long 	    NULL, "BGE Statistics");
6325763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6326763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6327763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6328763757b2SScott Long 	    "FramesDroppedDueToFilters");
6329763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6330763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6331763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6332763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6333763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6334763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
633506e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
633606e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
633706e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
633806e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6339763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6340763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6341763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6342763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6343763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6344763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6345763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6346763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6347763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6348763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6349763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6350763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6351763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6352763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6353763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6354763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6355763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6356763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6357763757b2SScott Long 
6358763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
6359763757b2SScott Long 	    NULL, "BGE RX Statistics");
6360763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6361763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
63621cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6363763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6364763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6365763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
63661cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6367763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6368763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6369763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6370763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6371763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6372763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6373763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6374763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6375763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6376763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6377763757b2SScott Long 	    "xoffPauseFramesReceived");
6378763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6379763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6380763757b2SScott Long 	    "ControlFramesReceived");
6381763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6382763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6383763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6384763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6385763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6386763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6387763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6388763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6389763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
639006e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6391763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
639206e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6393763757b2SScott Long 
6394763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
6395763757b2SScott Long 	    NULL, "BGE TX Statistics");
6396763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6397763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
63981cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6399763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6400763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6401763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6402763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6403763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6404763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6405763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6406763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6407763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6408763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6409763757b2SScott Long 	    "InternalMacTransmitErrors");
6410763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6411763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6412763757b2SScott Long 	    "SingleCollisionFrames");
6413763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6414763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6415763757b2SScott Long 	    "MultipleCollisionFrames");
6416763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6417763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6418763757b2SScott Long 	    "DeferredTransmissions");
6419763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6420763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6421763757b2SScott Long 	    "ExcessiveCollisions");
6422763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
642306e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
642406e83c7eSScott Long 	    "LateCollisions");
6425763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
64261cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6427763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6428763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6429763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6430763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6431763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6432763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6433763757b2SScott Long 	    "CarrierSenseErrors");
6434763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6435763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6436763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6437763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6438763757b2SScott Long }
6439763757b2SScott Long 
64402280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
64412280c16bSPyun YongHyeon 
64422280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
64436dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
64442280c16bSPyun YongHyeon 
64452280c16bSPyun YongHyeon static void
64462280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
64472280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
64482280c16bSPyun YongHyeon {
64492280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
64502280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
64512280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
64522280c16bSPyun YongHyeon 
64532280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
64542280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
64552280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
64562280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
64572280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
64582280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
64592280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
64602280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
64612280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
64622280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
64632280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
64642280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
64652280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
64662280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
64672280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
64682280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
64692280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
64702280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
64712280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
64722280c16bSPyun YongHyeon 
64732280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
64742280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
64752280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
64762280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
64772280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
64782280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
64792280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
64801cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
64812280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
64822280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
64832280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
64842280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
64852280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
64862280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
64872280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
64882280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
64892280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
64902280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
64912280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
64922280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
64932280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
64942280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
64952280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
64962280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
64972280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
64982280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
64992280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
65002280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
65012280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
65022280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
65032280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
65042280c16bSPyun YongHyeon 
65052280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
65062280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
65072280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
65081cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
65092280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
65102280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
65112280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
65122280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
65132280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
65142280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
65152280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
65162280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
65172280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
65182280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
65192280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
65202280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
65212280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
65222280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
65232280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
65242280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
65252280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
65262280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
65272280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
65282280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
65292280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
65301cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
65312280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
65321cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
65332280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
65341cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
65352280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
65362280c16bSPyun YongHyeon }
65372280c16bSPyun YongHyeon 
65382280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
65392280c16bSPyun YongHyeon 
6540763757b2SScott Long static int
6541763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6542763757b2SScott Long {
6543763757b2SScott Long 	struct bge_softc *sc;
654406e83c7eSScott Long 	uint32_t result;
6545d949071dSJung-uk Kim 	int offset;
6546763757b2SScott Long 
6547763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6548763757b2SScott Long 	offset = arg2;
6549d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6550d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6551041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
65526f8718a3SScott Long }
65536f8718a3SScott Long 
65546f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
65556f8718a3SScott Long static int
65566f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
65576f8718a3SScott Long {
65586f8718a3SScott Long 	struct bge_softc *sc;
65596f8718a3SScott Long 	uint16_t *sbdata;
656028276ad6SPyun YongHyeon 	int error, result, sbsz;
65616f8718a3SScott Long 	int i, j;
65626f8718a3SScott Long 
65636f8718a3SScott Long 	result = -1;
65646f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65656f8718a3SScott Long 	if (error || (req->newptr == NULL))
65666f8718a3SScott Long 		return (error);
65676f8718a3SScott Long 
65686f8718a3SScott Long 	if (result == 1) {
65696f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65706f8718a3SScott Long 
657128276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
657228276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
657328276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
657428276ad6SPyun YongHyeon 		else
657528276ad6SPyun YongHyeon 			sbsz = 32;
65766f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
65776f8718a3SScott Long 		printf("Status Block:\n");
657828276ad6SPyun YongHyeon 		BGE_LOCK(sc);
657928276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
658028276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
658128276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
658228276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
65836f8718a3SScott Long 			printf("%06x:", i);
658428276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
658528276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
65866f8718a3SScott Long 			printf("\n");
65876f8718a3SScott Long 		}
65886f8718a3SScott Long 
65896f8718a3SScott Long 		printf("Registers:\n");
65900c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
65916f8718a3SScott Long 			printf("%06x:", i);
65926f8718a3SScott Long 			for (j = 0; j < 8; j++) {
65936f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
65946f8718a3SScott Long 				i += 4;
65956f8718a3SScott Long 			}
65966f8718a3SScott Long 			printf("\n");
65976f8718a3SScott Long 		}
659828276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
65996f8718a3SScott Long 
66006f8718a3SScott Long 		printf("Hardware Flags:\n");
660128276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
660228276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6603a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6604a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
66055345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
66066f8718a3SScott Long 			printf(" - 575X Plus\n");
66075345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
66086f8718a3SScott Long 			printf(" - 5705 Plus\n");
66095345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
66105345bad0SScott Long 			printf(" - 5714 Family\n");
66115345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
66125345bad0SScott Long 			printf(" - 5700 Family\n");
66136f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
66146f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
66156f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
66166f8718a3SScott Long 			printf(" - PCI-X Bus\n");
66176f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
66186f8718a3SScott Long 			printf(" - PCI Express Bus\n");
66197d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
66206f8718a3SScott Long 			printf(" - No 3 LEDs\n");
66216f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
66226f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
66236f8718a3SScott Long 	}
66246f8718a3SScott Long 
66256f8718a3SScott Long 	return (error);
66266f8718a3SScott Long }
66276f8718a3SScott Long 
66286f8718a3SScott Long static int
66296f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
66306f8718a3SScott Long {
66316f8718a3SScott Long 	struct bge_softc *sc;
66326f8718a3SScott Long 	int error;
66336f8718a3SScott Long 	uint16_t result;
66346f8718a3SScott Long 	uint32_t val;
66356f8718a3SScott Long 
66366f8718a3SScott Long 	result = -1;
66376f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66386f8718a3SScott Long 	if (error || (req->newptr == NULL))
66396f8718a3SScott Long 		return (error);
66406f8718a3SScott Long 
66416f8718a3SScott Long 	if (result < 0x8000) {
66426f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66436f8718a3SScott Long 		val = CSR_READ_4(sc, result);
66446f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
66456f8718a3SScott Long 	}
66466f8718a3SScott Long 
66476f8718a3SScott Long 	return (error);
66486f8718a3SScott Long }
66496f8718a3SScott Long 
66506f8718a3SScott Long static int
6651548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6652548c8f1aSPyun YongHyeon {
6653548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6654548c8f1aSPyun YongHyeon 	int error;
6655548c8f1aSPyun YongHyeon 	uint16_t result;
6656548c8f1aSPyun YongHyeon 	uint32_t val;
6657548c8f1aSPyun YongHyeon 
6658548c8f1aSPyun YongHyeon 	result = -1;
6659548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6660548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6661548c8f1aSPyun YongHyeon 		return (error);
6662548c8f1aSPyun YongHyeon 
6663548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6664548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6665548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6666548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6667548c8f1aSPyun YongHyeon 	}
6668548c8f1aSPyun YongHyeon 
6669548c8f1aSPyun YongHyeon 	return (error);
6670548c8f1aSPyun YongHyeon }
6671548c8f1aSPyun YongHyeon 
6672548c8f1aSPyun YongHyeon static int
66736f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
66746f8718a3SScott Long {
66756f8718a3SScott Long 	struct bge_softc *sc;
66766f8718a3SScott Long 	int error;
66776f8718a3SScott Long 	uint16_t result;
66786f8718a3SScott Long 	uint32_t val;
66796f8718a3SScott Long 
66806f8718a3SScott Long 	result = -1;
66816f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66826f8718a3SScott Long 	if (error || (req->newptr == NULL))
66836f8718a3SScott Long 		return (error);
66846f8718a3SScott Long 
66856f8718a3SScott Long 	if (result < 0x8000) {
66866f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66876f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
66886f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
66896f8718a3SScott Long 	}
66906f8718a3SScott Long 
66916f8718a3SScott Long 	return (error);
66926f8718a3SScott Long }
66936f8718a3SScott Long #endif
669438cc658fSJohn Baldwin 
669538cc658fSJohn Baldwin static int
66965fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
66975fea260fSMarius Strobl {
66985fea260fSMarius Strobl 
66995fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
67005fea260fSMarius Strobl 		return (1);
67015fea260fSMarius Strobl 
67025fea260fSMarius Strobl #ifdef __sparc64__
67035fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
67045fea260fSMarius Strobl 	return (0);
67055fea260fSMarius Strobl #endif
67065fea260fSMarius Strobl 	return (1);
67075fea260fSMarius Strobl }
67085fea260fSMarius Strobl 
67095fea260fSMarius Strobl static int
671038cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
671138cc658fSJohn Baldwin {
671238cc658fSJohn Baldwin 	uint32_t mac_addr;
671338cc658fSJohn Baldwin 
671473635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
671538cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
671638cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
671738cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
671873635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
671938cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
672038cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
672138cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
672238cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
67235fea260fSMarius Strobl 		return (0);
672438cc658fSJohn Baldwin 	}
67255fea260fSMarius Strobl 	return (1);
672638cc658fSJohn Baldwin }
672738cc658fSJohn Baldwin 
672838cc658fSJohn Baldwin static int
672938cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
673038cc658fSJohn Baldwin {
673138cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
673238cc658fSJohn Baldwin 
673338cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
673438cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
673538cc658fSJohn Baldwin 
67365fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
67375fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
673838cc658fSJohn Baldwin }
673938cc658fSJohn Baldwin 
674038cc658fSJohn Baldwin static int
674138cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
674238cc658fSJohn Baldwin {
674338cc658fSJohn Baldwin 
67445fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
67455fea260fSMarius Strobl 		return (1);
67465fea260fSMarius Strobl 
67475fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
67485fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
674938cc658fSJohn Baldwin }
675038cc658fSJohn Baldwin 
675138cc658fSJohn Baldwin static int
675238cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
675338cc658fSJohn Baldwin {
675438cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
675538cc658fSJohn Baldwin 		/* NOTE: Order is critical */
67565fea260fSMarius Strobl 		bge_get_eaddr_fw,
675738cc658fSJohn Baldwin 		bge_get_eaddr_mem,
675838cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
675938cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
676038cc658fSJohn Baldwin 		NULL
676138cc658fSJohn Baldwin 	};
676238cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
676338cc658fSJohn Baldwin 
676438cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
676538cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
676638cc658fSJohn Baldwin 			break;
676738cc658fSJohn Baldwin 	}
676838cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
676938cc658fSJohn Baldwin }
6770