xref: /freebsd/sys/dev/bge/if_bge.c (revision d73ea7c6d30c64ee1daa33a691fd17152c66375c)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
3895d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4195d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4222a4ecedSMarius Strobl  * MAC chips. The BCM5700, sometimes referred to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
82f1a7e6d5SScott Long #include <sys/sysctl.h>
83dfe0df9aSPyun YongHyeon #include <sys/taskqueue.h>
8495d67482SBill Paul 
8595d67482SBill Paul #include <net/if.h>
8695d67482SBill Paul #include <net/if_arp.h>
8795d67482SBill Paul #include <net/ethernet.h>
8895d67482SBill Paul #include <net/if_dl.h>
8995d67482SBill Paul #include <net/if_media.h>
9095d67482SBill Paul 
9195d67482SBill Paul #include <net/bpf.h>
9295d67482SBill Paul 
9395d67482SBill Paul #include <net/if_types.h>
9495d67482SBill Paul #include <net/if_vlan_var.h>
9595d67482SBill Paul 
9695d67482SBill Paul #include <netinet/in_systm.h>
9795d67482SBill Paul #include <netinet/in.h>
9895d67482SBill Paul #include <netinet/ip.h>
99ca3f1187SPyun YongHyeon #include <netinet/tcp.h>
10095d67482SBill Paul 
10195d67482SBill Paul #include <machine/bus.h>
10295d67482SBill Paul #include <machine/resource.h>
10395d67482SBill Paul #include <sys/bus.h>
10495d67482SBill Paul #include <sys/rman.h>
10595d67482SBill Paul 
10695d67482SBill Paul #include <dev/mii/mii.h>
10795d67482SBill Paul #include <dev/mii/miivar.h>
1082d3ce713SDavid E. O'Brien #include "miidevs.h"
10995d67482SBill Paul #include <dev/mii/brgphyreg.h>
11095d67482SBill Paul 
11108013fd3SMarius Strobl #ifdef __sparc64__
11208013fd3SMarius Strobl #include <dev/ofw/ofw_bus.h>
11308013fd3SMarius Strobl #include <dev/ofw/openfirm.h>
11408013fd3SMarius Strobl #include <machine/ofw_machdep.h>
11508013fd3SMarius Strobl #include <machine/ver.h>
11608013fd3SMarius Strobl #endif
11708013fd3SMarius Strobl 
1184fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1194fbd232cSWarner Losh #include <dev/pci/pcivar.h>
12095d67482SBill Paul 
12195d67482SBill Paul #include <dev/bge/if_bgereg.h>
12295d67482SBill Paul 
12335f945cdSPyun YongHyeon #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP)
124d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12595d67482SBill Paul 
126f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
127f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12895d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12995d67482SBill Paul 
1307b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
13195d67482SBill Paul #include "miibus_if.h"
13295d67482SBill Paul 
13395d67482SBill Paul /*
13495d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13595d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13695d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13795d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13895d67482SBill Paul  */
139852c67f9SMarius Strobl static const struct bge_type {
1404c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1414c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
142978f2704SMarius Strobl } const bge_devs[] = {
1434c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1444c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
14595d67482SBill Paul 
1464c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1474c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1484c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1494c0da0ffSGleb Smirnoff 
1504c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1514c0da0ffSGleb Smirnoff 
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1721108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5717 },
1731108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5718 },
174bbe2ca75SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5719 },
1754c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1764c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
177effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
178a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5723 },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1834c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1844c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1854c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1899e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1909e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1919e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1929e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
193f7d1b2ebSXin LI 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5756 },
194a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761 },
195a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761E },
196a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761S },
197a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761SE },
198a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5764 },
1994c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
2004c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
2014c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
2024c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
203a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5784 },
204a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785F },
205a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785G },
2069e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
2079e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
208a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787F },
2099e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
2104c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
2114c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
2124c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
2134c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
2144c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
21538cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
21638cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
217a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57760 },
218b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57761 },
219b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57765 },
220a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
221b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57781 },
222b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57785 },
223a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
224a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
225b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57791 },
226b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57795 },
2274c0da0ffSGleb Smirnoff 
2284c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2294c0da0ffSGleb Smirnoff 
2304c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2314c0da0ffSGleb Smirnoff 
232a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
233a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
234a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PP250450 },
235a5779553SStanislav Sedov 
2364c0da0ffSGleb Smirnoff 	{ 0, 0 }
23795d67482SBill Paul };
23895d67482SBill Paul 
2394c0da0ffSGleb Smirnoff static const struct bge_vendor {
2404c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2414c0da0ffSGleb Smirnoff 	const char	*v_name;
242978f2704SMarius Strobl } const bge_vendors[] = {
2434c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2444c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2454c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2464c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2474c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2484c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
249a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2504c0da0ffSGleb Smirnoff 
2514c0da0ffSGleb Smirnoff 	{ 0, NULL }
2524c0da0ffSGleb Smirnoff };
2534c0da0ffSGleb Smirnoff 
2544c0da0ffSGleb Smirnoff static const struct bge_revision {
2554c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2564c0da0ffSGleb Smirnoff 	const char	*br_name;
257978f2704SMarius Strobl } const bge_revisions[] = {
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2594c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2604c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2614c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2624c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2654c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2674c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2684c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2694c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2704c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2714c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2724c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2749e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2764c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2794c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2824c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2844c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2854c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2884c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2894c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2904c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
29142787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2924c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2934c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2944c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2954c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2964c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2974c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2984c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2994c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
3000c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
3011108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_A0,	"BCM5717 A0" },
3021108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_B0,	"BCM5717 B0" },
303bbe2ca75SPyun YongHyeon 	{ BGE_CHIPID_BCM5719_A0,	"BCM5719 A0" },
30450515680SPyun YongHyeon 	{ BGE_CHIPID_BCM5720_A0,	"BCM5720 A0" },
3050c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
3060c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
3070c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
308bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
309a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
310a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
311a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
312a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
31381179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3146f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
3156f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3166f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
31738cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
31838cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
319b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_A0,	"BCM57765 A0" },
320b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_B0,	"BCM57765 B0" },
321a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
322a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3234c0da0ffSGleb Smirnoff 
3244c0da0ffSGleb Smirnoff 	{ 0, NULL }
3254c0da0ffSGleb Smirnoff };
3264c0da0ffSGleb Smirnoff 
3274c0da0ffSGleb Smirnoff /*
3284c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3294c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3304c0da0ffSGleb Smirnoff  */
331978f2704SMarius Strobl static const struct bge_revision const bge_majorrevs[] = {
3329e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3339e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3349e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3359e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3369e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3379e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3389e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3399e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3409e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3419e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3429e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
343a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
344a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
345a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
34681179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3476f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
34838cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
349b4a256acSPyun YongHyeon 	{ BGE_ASICREV_BCM57765,		"unknown BCM57765" },
350a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3511108273aSPyun YongHyeon 	{ BGE_ASICREV_BCM5717,		"unknown BCM5717" },
352bbe2ca75SPyun YongHyeon 	{ BGE_ASICREV_BCM5719,		"unknown BCM5719" },
35350515680SPyun YongHyeon 	{ BGE_ASICREV_BCM5720,		"unknown BCM5720" },
3544c0da0ffSGleb Smirnoff 
3554c0da0ffSGleb Smirnoff 	{ 0, NULL }
3564c0da0ffSGleb Smirnoff };
3574c0da0ffSGleb Smirnoff 
3580c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3590c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3600c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3610c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3620c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
363a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3641108273aSPyun YongHyeon #define	BGE_IS_5717_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5717_PLUS)
3654c0da0ffSGleb Smirnoff 
3664c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3674c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
36838cc658fSJohn Baldwin 
36938cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
37038cc658fSJohn Baldwin 
371e51a25f8SAlfred Perlstein static int bge_probe(device_t);
372e51a25f8SAlfred Perlstein static int bge_attach(device_t);
373e51a25f8SAlfred Perlstein static int bge_detach(device_t);
37414afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
37514afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3763f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
377f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3785b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
379f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
3805b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
3815b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
382f41ac2beSBill Paul 
383ea9c3a30SPyun YongHyeon static void bge_devinfo(struct bge_softc *);
384062af0b0SPyun YongHyeon static int bge_mbox_reorder(struct bge_softc *);
385062af0b0SPyun YongHyeon 
3865fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
38738cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
38838cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
38938cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
39038cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
39138cc658fSJohn Baldwin 
392b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
3931108273aSPyun YongHyeon static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
394dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
39595d67482SBill Paul 
3968cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
397e51a25f8SAlfred Perlstein static void bge_tick(void *);
3982280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
399e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
4003f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
401d598b626SPyun YongHyeon static struct mbuf *bge_check_short_dma(struct mbuf *);
4022e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
4031108273aSPyun YongHyeon     uint16_t *, uint16_t *);
404676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
40595d67482SBill Paul 
406e51a25f8SAlfred Perlstein static void bge_intr(void *);
407dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
408dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
4090f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
410e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
411e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
4120f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
413e51a25f8SAlfred Perlstein static void bge_init(void *);
4145a147ba6SPyun YongHyeon static void bge_stop_block(struct bge_softc *, bus_size_t, uint32_t);
415e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
416b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
417b6c974e8SWarner Losh static int bge_shutdown(device_t);
41867d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
419e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
420e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
42195d67482SBill Paul 
42238cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
42338cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
42438cc658fSJohn Baldwin 
4253f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
426e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
42795d67482SBill Paul 
4283e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
429e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
430cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
43195d67482SBill Paul 
432e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
433e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
434943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
435943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
436e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
437e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
438e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
439e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
440e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
441e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
44295d67482SBill Paul 
443e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
444e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
44550515680SPyun YongHyeon static uint32_t bge_dma_swap_options(struct bge_softc *);
44695d67482SBill Paul 
4475fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4483f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
449e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
45038cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
45195d67482SBill Paul #ifdef notdef
4523f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
45395d67482SBill Paul #endif
4549ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
455e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
45695d67482SBill Paul 
457e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
458e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
459e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
46075719184SGleb Smirnoff #ifdef DEVICE_POLLING
4611abcdbd1SAttilio Rao static int bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
46275719184SGleb Smirnoff #endif
46395d67482SBill Paul 
464548c8f1aSPyun YongHyeon #define	BGE_RESET_SHUTDOWN	0
4658cb1383cSDoug Ambrisko #define	BGE_RESET_START		1
466548c8f1aSPyun YongHyeon #define	BGE_RESET_SUSPEND	2
4678cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4688cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4698cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
470797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4718cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
472dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
47395d67482SBill Paul 
474548c8f1aSPyun YongHyeon static void bge_ape_lock_init(struct bge_softc *);
475548c8f1aSPyun YongHyeon static void bge_ape_read_fw_ver(struct bge_softc *);
476548c8f1aSPyun YongHyeon static int bge_ape_lock(struct bge_softc *, int);
477548c8f1aSPyun YongHyeon static void bge_ape_unlock(struct bge_softc *, int);
478548c8f1aSPyun YongHyeon static void bge_ape_send_event(struct bge_softc *, uint32_t);
479548c8f1aSPyun YongHyeon static void bge_ape_driver_state_change(struct bge_softc *, int);
480548c8f1aSPyun YongHyeon 
4816f8718a3SScott Long /*
4826f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4836f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4846f8718a3SScott Long  * traps on certain architectures.
4856f8718a3SScott Long  */
4866f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4876f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4886f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
489548c8f1aSPyun YongHyeon static int bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS);
4906f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
4916f8718a3SScott Long #endif
4926f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
4932280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
4942280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
4952280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
4962280c16bSPyun YongHyeon     struct sysctl_oid_list *);
497763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
4986f8718a3SScott Long 
49995d67482SBill Paul static device_method_t bge_methods[] = {
50095d67482SBill Paul 	/* Device interface */
50195d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
50295d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
50395d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
50495d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
50514afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
50614afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
50795d67482SBill Paul 
50895d67482SBill Paul 	/* MII interface */
50995d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
51095d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
51195d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
51295d67482SBill Paul 
5134b7ec270SMarius Strobl 	DEVMETHOD_END
51495d67482SBill Paul };
51595d67482SBill Paul 
51695d67482SBill Paul static driver_t bge_driver = {
51795d67482SBill Paul 	"bge",
51895d67482SBill Paul 	bge_methods,
51995d67482SBill Paul 	sizeof(struct bge_softc)
52095d67482SBill Paul };
52195d67482SBill Paul 
52295d67482SBill Paul static devclass_t bge_devclass;
52395d67482SBill Paul 
524f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
52595d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
52695d67482SBill Paul 
527f1a7e6d5SScott Long static int bge_allow_asf = 1;
528f1a7e6d5SScott Long 
529f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
530f1a7e6d5SScott Long 
5316472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
532f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
533f1a7e6d5SScott Long 	"Allow ASF mode if available");
534c4529f41SMichael Reifenberger 
53508013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
53608013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
53708013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
53808013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
53908013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
54008013fd3SMarius Strobl 
54108013fd3SMarius Strobl static int
5425fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
54308013fd3SMarius Strobl {
54408013fd3SMarius Strobl #ifdef __sparc64__
54508013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
54608013fd3SMarius Strobl 	device_t dev;
54708013fd3SMarius Strobl 	uint32_t subvendor;
54808013fd3SMarius Strobl 
54908013fd3SMarius Strobl 	dev = sc->bge_dev;
55008013fd3SMarius Strobl 
55108013fd3SMarius Strobl 	/*
55208013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
55308013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
55408013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
55508013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
55608013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
55708013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
55808013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
55908013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
56008013fd3SMarius Strobl 	 * there.
56108013fd3SMarius Strobl 	 */
56208013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
56308013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5642d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
56508013fd3SMarius Strobl 		return (0);
56608013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
56708013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
56808013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
56908013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
57008013fd3SMarius Strobl 			return (0);
57108013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
57208013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
57308013fd3SMarius Strobl 			return (0);
57408013fd3SMarius Strobl 	}
57508013fd3SMarius Strobl #endif
57608013fd3SMarius Strobl 	return (1);
57708013fd3SMarius Strobl }
57808013fd3SMarius Strobl 
5793f74909aSGleb Smirnoff static uint32_t
5803f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
58195d67482SBill Paul {
58295d67482SBill Paul 	device_t dev;
5836f8718a3SScott Long 	uint32_t val;
58495d67482SBill Paul 
585a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
586a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
587a4431ebaSPyun YongHyeon 		return (0);
588a4431ebaSPyun YongHyeon 
58995d67482SBill Paul 	dev = sc->bge_dev;
59095d67482SBill Paul 
59195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5926f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5936f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5946f8718a3SScott Long 	return (val);
59595d67482SBill Paul }
59695d67482SBill Paul 
59795d67482SBill Paul static void
5983f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
59995d67482SBill Paul {
60095d67482SBill Paul 	device_t dev;
60195d67482SBill Paul 
602a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
603a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
604a4431ebaSPyun YongHyeon 		return;
605a4431ebaSPyun YongHyeon 
60695d67482SBill Paul 	dev = sc->bge_dev;
60795d67482SBill Paul 
60895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
60995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
6106f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
61195d67482SBill Paul }
61295d67482SBill Paul 
61395d67482SBill Paul #ifdef notdef
6143f74909aSGleb Smirnoff static uint32_t
6153f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
61695d67482SBill Paul {
61795d67482SBill Paul 	device_t dev;
61895d67482SBill Paul 
61995d67482SBill Paul 	dev = sc->bge_dev;
62095d67482SBill Paul 
62195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
62295d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
62395d67482SBill Paul }
62495d67482SBill Paul #endif
62595d67482SBill Paul 
62695d67482SBill Paul static void
6273f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
62895d67482SBill Paul {
62995d67482SBill Paul 	device_t dev;
63095d67482SBill Paul 
63195d67482SBill Paul 	dev = sc->bge_dev;
63295d67482SBill Paul 
63395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
63495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
63595d67482SBill Paul }
63695d67482SBill Paul 
6376f8718a3SScott Long static void
6386f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6396f8718a3SScott Long {
6406f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6416f8718a3SScott Long }
6426f8718a3SScott Long 
64338cc658fSJohn Baldwin static void
64438cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
64538cc658fSJohn Baldwin {
64638cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
64738cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
64838cc658fSJohn Baldwin 
64938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
650062af0b0SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
651062af0b0SPyun YongHyeon 		CSR_READ_4(sc, off);
65238cc658fSJohn Baldwin }
65338cc658fSJohn Baldwin 
654f41ac2beSBill Paul /*
655548c8f1aSPyun YongHyeon  * Clear all stale locks and select the lock for this driver instance.
656548c8f1aSPyun YongHyeon  */
657548c8f1aSPyun YongHyeon static void
658548c8f1aSPyun YongHyeon bge_ape_lock_init(struct bge_softc *sc)
659548c8f1aSPyun YongHyeon {
660548c8f1aSPyun YongHyeon 	uint32_t bit, regbase;
661548c8f1aSPyun YongHyeon 	int i;
662548c8f1aSPyun YongHyeon 
663548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
664548c8f1aSPyun YongHyeon 		regbase = BGE_APE_LOCK_GRANT;
665548c8f1aSPyun YongHyeon 	else
666548c8f1aSPyun YongHyeon 		regbase = BGE_APE_PER_LOCK_GRANT;
667548c8f1aSPyun YongHyeon 
668548c8f1aSPyun YongHyeon 	/* Clear any stale locks. */
669548c8f1aSPyun YongHyeon 	for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
670548c8f1aSPyun YongHyeon 		switch (i) {
671548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY0:
672548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY1:
673548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY2:
674548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY3:
675548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
676548c8f1aSPyun YongHyeon 			break;
677548c8f1aSPyun YongHyeon 		default:
678548c8f1aSPyun YongHyeon 			if (sc->bge_func_addr != 0)
679548c8f1aSPyun YongHyeon 				bit = BGE_APE_LOCK_GRANT_DRIVER0;
680548c8f1aSPyun YongHyeon 			else
681548c8f1aSPyun YongHyeon 				bit = (1 << sc->bge_func_addr);
682548c8f1aSPyun YongHyeon 		}
683548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, regbase + 4 * i, bit);
684548c8f1aSPyun YongHyeon 	}
685548c8f1aSPyun YongHyeon 
686548c8f1aSPyun YongHyeon 	/* Select the PHY lock based on the device's function number. */
687548c8f1aSPyun YongHyeon 	switch (sc->bge_func_addr) {
688548c8f1aSPyun YongHyeon 	case 0:
689548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY0;
690548c8f1aSPyun YongHyeon 		break;
691548c8f1aSPyun YongHyeon 	case 1:
692548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY1;
693548c8f1aSPyun YongHyeon 		break;
694548c8f1aSPyun YongHyeon 	case 2:
695548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY2;
696548c8f1aSPyun YongHyeon 		break;
697548c8f1aSPyun YongHyeon 	case 3:
698548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY3;
699548c8f1aSPyun YongHyeon 		break;
700548c8f1aSPyun YongHyeon 	default:
701548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev,
702548c8f1aSPyun YongHyeon 		    "PHY lock not supported on this function\n");
703548c8f1aSPyun YongHyeon 	}
704548c8f1aSPyun YongHyeon }
705548c8f1aSPyun YongHyeon 
706548c8f1aSPyun YongHyeon /*
707548c8f1aSPyun YongHyeon  * Check for APE firmware, set flags, and print version info.
708548c8f1aSPyun YongHyeon  */
709548c8f1aSPyun YongHyeon static void
710548c8f1aSPyun YongHyeon bge_ape_read_fw_ver(struct bge_softc *sc)
711548c8f1aSPyun YongHyeon {
712548c8f1aSPyun YongHyeon 	const char *fwtype;
713548c8f1aSPyun YongHyeon 	uint32_t apedata, features;
714548c8f1aSPyun YongHyeon 
715548c8f1aSPyun YongHyeon 	/* Check for a valid APE signature in shared memory. */
716548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
717548c8f1aSPyun YongHyeon 	if (apedata != BGE_APE_SEG_SIG_MAGIC) {
718548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags &= ~ BGE_MFW_ON_APE;
719548c8f1aSPyun YongHyeon 		return;
720548c8f1aSPyun YongHyeon 	}
721548c8f1aSPyun YongHyeon 
722548c8f1aSPyun YongHyeon 	/* Check if APE firmware is running. */
723548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
724548c8f1aSPyun YongHyeon 	if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
725548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE signature found "
726548c8f1aSPyun YongHyeon 		    "but FW status not ready! 0x%08x\n", apedata);
727548c8f1aSPyun YongHyeon 		return;
728548c8f1aSPyun YongHyeon 	}
729548c8f1aSPyun YongHyeon 
730548c8f1aSPyun YongHyeon 	sc->bge_mfw_flags |= BGE_MFW_ON_APE;
731548c8f1aSPyun YongHyeon 
732548c8f1aSPyun YongHyeon 	/* Fetch the APE firwmare type and version. */
733548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
734548c8f1aSPyun YongHyeon 	features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
735548c8f1aSPyun YongHyeon 	if ((features & BGE_APE_FW_FEATURE_NCSI) != 0) {
736548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_NCSI;
737548c8f1aSPyun YongHyeon 		fwtype = "NCSI";
738548c8f1aSPyun YongHyeon 	} else if ((features & BGE_APE_FW_FEATURE_DASH) != 0) {
739548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_DASH;
740548c8f1aSPyun YongHyeon 		fwtype = "DASH";
741548c8f1aSPyun YongHyeon 	} else
742548c8f1aSPyun YongHyeon 		fwtype = "UNKN";
743548c8f1aSPyun YongHyeon 
744548c8f1aSPyun YongHyeon 	/* Print the APE firmware version. */
745548c8f1aSPyun YongHyeon 	device_printf(sc->bge_dev, "APE FW version: %s v%d.%d.%d.%d\n",
746548c8f1aSPyun YongHyeon 	    fwtype,
747548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
748548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
749548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
750548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_BLDMSK));
751548c8f1aSPyun YongHyeon }
752548c8f1aSPyun YongHyeon 
753548c8f1aSPyun YongHyeon static int
754548c8f1aSPyun YongHyeon bge_ape_lock(struct bge_softc *sc, int locknum)
755548c8f1aSPyun YongHyeon {
756548c8f1aSPyun YongHyeon 	uint32_t bit, gnt, req, status;
757548c8f1aSPyun YongHyeon 	int i, off;
758548c8f1aSPyun YongHyeon 
759548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
760548c8f1aSPyun YongHyeon 		return (0);
761548c8f1aSPyun YongHyeon 
762548c8f1aSPyun YongHyeon 	/* Lock request/grant registers have different bases. */
763548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761) {
764548c8f1aSPyun YongHyeon 		req = BGE_APE_LOCK_REQ;
765548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
766548c8f1aSPyun YongHyeon 	} else {
767548c8f1aSPyun YongHyeon 		req = BGE_APE_PER_LOCK_REQ;
768548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
769548c8f1aSPyun YongHyeon 	}
770548c8f1aSPyun YongHyeon 
771548c8f1aSPyun YongHyeon 	off = 4 * locknum;
772548c8f1aSPyun YongHyeon 
773548c8f1aSPyun YongHyeon 	switch (locknum) {
774548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
775548c8f1aSPyun YongHyeon 		/* Lock required when using GPIO. */
776548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
777548c8f1aSPyun YongHyeon 			return (0);
778548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
779548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
780548c8f1aSPyun YongHyeon 		else
781548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
782548c8f1aSPyun YongHyeon 		break;
783548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
784548c8f1aSPyun YongHyeon 		/* Lock required to reset the device. */
785548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
786548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
787548c8f1aSPyun YongHyeon 		else
788548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
789548c8f1aSPyun YongHyeon 		break;
790548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
791548c8f1aSPyun YongHyeon 		/* Lock required when accessing certain APE memory. */
792548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
793548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
794548c8f1aSPyun YongHyeon 		else
795548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
796548c8f1aSPyun YongHyeon 		break;
797548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
798548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
799548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
800548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
801548c8f1aSPyun YongHyeon 		/* Lock required when accessing PHYs. */
802548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_REQ_DRIVER0;
803548c8f1aSPyun YongHyeon 		break;
804548c8f1aSPyun YongHyeon 	default:
805548c8f1aSPyun YongHyeon 		return (EINVAL);
806548c8f1aSPyun YongHyeon 	}
807548c8f1aSPyun YongHyeon 
808548c8f1aSPyun YongHyeon 	/* Request a lock. */
809548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, req + off, bit);
810548c8f1aSPyun YongHyeon 
811548c8f1aSPyun YongHyeon 	/* Wait up to 1 second to acquire lock. */
812548c8f1aSPyun YongHyeon 	for (i = 0; i < 20000; i++) {
813548c8f1aSPyun YongHyeon 		status = APE_READ_4(sc, gnt + off);
814548c8f1aSPyun YongHyeon 		if (status == bit)
815548c8f1aSPyun YongHyeon 			break;
816548c8f1aSPyun YongHyeon 		DELAY(50);
817548c8f1aSPyun YongHyeon 	}
818548c8f1aSPyun YongHyeon 
819548c8f1aSPyun YongHyeon 	/* Handle any errors. */
820548c8f1aSPyun YongHyeon 	if (status != bit) {
821548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE lock %d request failed! "
822548c8f1aSPyun YongHyeon 		    "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
823548c8f1aSPyun YongHyeon 		    locknum, req + off, bit & 0xFFFF, gnt + off,
824548c8f1aSPyun YongHyeon 		    status & 0xFFFF);
825548c8f1aSPyun YongHyeon 		/* Revoke the lock request. */
826548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, gnt + off, bit);
827548c8f1aSPyun YongHyeon 		return (EBUSY);
828548c8f1aSPyun YongHyeon 	}
829548c8f1aSPyun YongHyeon 
830548c8f1aSPyun YongHyeon 	return (0);
831548c8f1aSPyun YongHyeon }
832548c8f1aSPyun YongHyeon 
833548c8f1aSPyun YongHyeon static void
834548c8f1aSPyun YongHyeon bge_ape_unlock(struct bge_softc *sc, int locknum)
835548c8f1aSPyun YongHyeon {
836548c8f1aSPyun YongHyeon 	uint32_t bit, gnt;
837548c8f1aSPyun YongHyeon 	int off;
838548c8f1aSPyun YongHyeon 
839548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
840548c8f1aSPyun YongHyeon 		return;
841548c8f1aSPyun YongHyeon 
842548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
843548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
844548c8f1aSPyun YongHyeon 	else
845548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
846548c8f1aSPyun YongHyeon 
847548c8f1aSPyun YongHyeon 	off = 4 * locknum;
848548c8f1aSPyun YongHyeon 
849548c8f1aSPyun YongHyeon 	switch (locknum) {
850548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
851548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
852548c8f1aSPyun YongHyeon 			return;
853548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
854548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
855548c8f1aSPyun YongHyeon 		else
856548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
857548c8f1aSPyun YongHyeon 		break;
858548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
859548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
860548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
861548c8f1aSPyun YongHyeon 		else
862548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
863548c8f1aSPyun YongHyeon 		break;
864548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
865548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
866548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
867548c8f1aSPyun YongHyeon 		else
868548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
869548c8f1aSPyun YongHyeon 		break;
870548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
871548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
872548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
873548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
874548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_GRANT_DRIVER0;
875548c8f1aSPyun YongHyeon 		break;
876548c8f1aSPyun YongHyeon 	default:
877548c8f1aSPyun YongHyeon 		return;
878548c8f1aSPyun YongHyeon 	}
879548c8f1aSPyun YongHyeon 
880548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, gnt + off, bit);
881548c8f1aSPyun YongHyeon }
882548c8f1aSPyun YongHyeon 
883548c8f1aSPyun YongHyeon /*
884548c8f1aSPyun YongHyeon  * Send an event to the APE firmware.
885548c8f1aSPyun YongHyeon  */
886548c8f1aSPyun YongHyeon static void
887548c8f1aSPyun YongHyeon bge_ape_send_event(struct bge_softc *sc, uint32_t event)
888548c8f1aSPyun YongHyeon {
889548c8f1aSPyun YongHyeon 	uint32_t apedata;
890548c8f1aSPyun YongHyeon 	int i;
891548c8f1aSPyun YongHyeon 
892548c8f1aSPyun YongHyeon 	/* NCSI does not support APE events. */
893548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
894548c8f1aSPyun YongHyeon 		return;
895548c8f1aSPyun YongHyeon 
896548c8f1aSPyun YongHyeon 	/* Wait up to 1ms for APE to service previous event. */
897548c8f1aSPyun YongHyeon 	for (i = 10; i > 0; i--) {
898548c8f1aSPyun YongHyeon 		if (bge_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
899548c8f1aSPyun YongHyeon 			break;
900548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
901548c8f1aSPyun YongHyeon 		if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
902548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
903548c8f1aSPyun YongHyeon 			    BGE_APE_EVENT_STATUS_EVENT_PENDING);
904548c8f1aSPyun YongHyeon 			bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
905548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
906548c8f1aSPyun YongHyeon 			break;
907548c8f1aSPyun YongHyeon 		}
908548c8f1aSPyun YongHyeon 		bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
909548c8f1aSPyun YongHyeon 		DELAY(100);
910548c8f1aSPyun YongHyeon 	}
911548c8f1aSPyun YongHyeon 	if (i == 0)
912548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE event 0x%08x send timed out\n",
913548c8f1aSPyun YongHyeon 		    event);
914548c8f1aSPyun YongHyeon }
915548c8f1aSPyun YongHyeon 
916548c8f1aSPyun YongHyeon static void
917548c8f1aSPyun YongHyeon bge_ape_driver_state_change(struct bge_softc *sc, int kind)
918548c8f1aSPyun YongHyeon {
919548c8f1aSPyun YongHyeon 	uint32_t apedata, event;
920548c8f1aSPyun YongHyeon 
921548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
922548c8f1aSPyun YongHyeon 		return;
923548c8f1aSPyun YongHyeon 
924548c8f1aSPyun YongHyeon 	switch (kind) {
925548c8f1aSPyun YongHyeon 	case BGE_RESET_START:
926548c8f1aSPyun YongHyeon 		/* If this is the first load, clear the load counter. */
927548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
928548c8f1aSPyun YongHyeon 		if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC)
929548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
930548c8f1aSPyun YongHyeon 		else {
931548c8f1aSPyun YongHyeon 			apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
932548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
933548c8f1aSPyun YongHyeon 		}
934548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
935548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_SIG_MAGIC);
936548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
937548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_LEN_MAGIC);
938548c8f1aSPyun YongHyeon 
939548c8f1aSPyun YongHyeon 		/* Add some version info if bge(4) supports it. */
940548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
941548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
942548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
943548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_BEHAV_NO_PHYLOCK);
944548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
945548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
946548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
947548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_START);
948548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_START;
949548c8f1aSPyun YongHyeon 		break;
950548c8f1aSPyun YongHyeon 	case BGE_RESET_SHUTDOWN:
951548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
952548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_UNLOAD);
953548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
954548c8f1aSPyun YongHyeon 		break;
955548c8f1aSPyun YongHyeon 	case BGE_RESET_SUSPEND:
956548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
957548c8f1aSPyun YongHyeon 		break;
958548c8f1aSPyun YongHyeon 	default:
959548c8f1aSPyun YongHyeon 		return;
960548c8f1aSPyun YongHyeon 	}
961548c8f1aSPyun YongHyeon 
962548c8f1aSPyun YongHyeon 	bge_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
963548c8f1aSPyun YongHyeon 	    BGE_APE_EVENT_STATUS_STATE_CHNGE);
964548c8f1aSPyun YongHyeon }
965548c8f1aSPyun YongHyeon 
966548c8f1aSPyun YongHyeon /*
967f41ac2beSBill Paul  * Map a single buffer address.
968f41ac2beSBill Paul  */
969f41ac2beSBill Paul 
970f41ac2beSBill Paul static void
9713f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
972f41ac2beSBill Paul {
973f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
974f41ac2beSBill Paul 
975f41ac2beSBill Paul 	if (error)
976f41ac2beSBill Paul 		return;
977f41ac2beSBill Paul 
9785b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
9795b610048SPyun YongHyeon 
980f41ac2beSBill Paul 	ctx = arg;
981f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
982f41ac2beSBill Paul }
983f41ac2beSBill Paul 
98438cc658fSJohn Baldwin static uint8_t
98538cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
98638cc658fSJohn Baldwin {
98738cc658fSJohn Baldwin 	uint32_t access, byte = 0;
98838cc658fSJohn Baldwin 	int i;
98938cc658fSJohn Baldwin 
99038cc658fSJohn Baldwin 	/* Lock. */
99138cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
99238cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
99338cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
99438cc658fSJohn Baldwin 			break;
99538cc658fSJohn Baldwin 		DELAY(20);
99638cc658fSJohn Baldwin 	}
99738cc658fSJohn Baldwin 	if (i == 8000)
99838cc658fSJohn Baldwin 		return (1);
99938cc658fSJohn Baldwin 
100038cc658fSJohn Baldwin 	/* Enable access. */
100138cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
100238cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
100338cc658fSJohn Baldwin 
100438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
100538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
100638cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
100738cc658fSJohn Baldwin 		DELAY(10);
100838cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
100938cc658fSJohn Baldwin 			DELAY(10);
101038cc658fSJohn Baldwin 			break;
101138cc658fSJohn Baldwin 		}
101238cc658fSJohn Baldwin 	}
101338cc658fSJohn Baldwin 
101438cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
101538cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
101638cc658fSJohn Baldwin 		return (1);
101738cc658fSJohn Baldwin 	}
101838cc658fSJohn Baldwin 
101938cc658fSJohn Baldwin 	/* Get result. */
102038cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
102138cc658fSJohn Baldwin 
102238cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
102338cc658fSJohn Baldwin 
102438cc658fSJohn Baldwin 	/* Disable access. */
102538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
102638cc658fSJohn Baldwin 
102738cc658fSJohn Baldwin 	/* Unlock. */
102838cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
102938cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
103038cc658fSJohn Baldwin 
103138cc658fSJohn Baldwin 	return (0);
103238cc658fSJohn Baldwin }
103338cc658fSJohn Baldwin 
103438cc658fSJohn Baldwin /*
103538cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
103638cc658fSJohn Baldwin  */
103738cc658fSJohn Baldwin static int
103838cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
103938cc658fSJohn Baldwin {
104038cc658fSJohn Baldwin 	int err = 0, i;
104138cc658fSJohn Baldwin 	uint8_t byte = 0;
104238cc658fSJohn Baldwin 
104338cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
104438cc658fSJohn Baldwin 		return (1);
104538cc658fSJohn Baldwin 
104638cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
104738cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
104838cc658fSJohn Baldwin 		if (err)
104938cc658fSJohn Baldwin 			break;
105038cc658fSJohn Baldwin 		*(dest + i) = byte;
105138cc658fSJohn Baldwin 	}
105238cc658fSJohn Baldwin 
105338cc658fSJohn Baldwin 	return (err ? 1 : 0);
105438cc658fSJohn Baldwin }
105538cc658fSJohn Baldwin 
105695d67482SBill Paul /*
105795d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
105895d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
105995d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
106095d67482SBill Paul  * access method.
106195d67482SBill Paul  */
10623f74909aSGleb Smirnoff static uint8_t
10633f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
106495d67482SBill Paul {
106595d67482SBill Paul 	int i;
10663f74909aSGleb Smirnoff 	uint32_t byte = 0;
106795d67482SBill Paul 
106895d67482SBill Paul 	/*
106995d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
107095d67482SBill Paul 	 * having to use the bitbang method.
107195d67482SBill Paul 	 */
107295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
107395d67482SBill Paul 
107495d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
107595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
107695d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
107795d67482SBill Paul 	DELAY(20);
107895d67482SBill Paul 
107995d67482SBill Paul 	/* Issue the read EEPROM command. */
108095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
108195d67482SBill Paul 
108295d67482SBill Paul 	/* Wait for completion */
108395d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
108495d67482SBill Paul 		DELAY(10);
108595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
108695d67482SBill Paul 			break;
108795d67482SBill Paul 	}
108895d67482SBill Paul 
1089d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
1090fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
1091f6789fbaSPyun YongHyeon 		return (1);
109295d67482SBill Paul 	}
109395d67482SBill Paul 
109495d67482SBill Paul 	/* Get result. */
109595d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
109695d67482SBill Paul 
10970c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
109895d67482SBill Paul 
109995d67482SBill Paul 	return (0);
110095d67482SBill Paul }
110195d67482SBill Paul 
110295d67482SBill Paul /*
110395d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
110495d67482SBill Paul  */
110595d67482SBill Paul static int
11063f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
110795d67482SBill Paul {
11083f74909aSGleb Smirnoff 	int i, error = 0;
11093f74909aSGleb Smirnoff 	uint8_t byte = 0;
111095d67482SBill Paul 
111195d67482SBill Paul 	for (i = 0; i < cnt; i++) {
11123f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
11133f74909aSGleb Smirnoff 		if (error)
111495d67482SBill Paul 			break;
111595d67482SBill Paul 		*(dest + i) = byte;
111695d67482SBill Paul 	}
111795d67482SBill Paul 
11183f74909aSGleb Smirnoff 	return (error ? 1 : 0);
111995d67482SBill Paul }
112095d67482SBill Paul 
112195d67482SBill Paul static int
11223f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
112395d67482SBill Paul {
112495d67482SBill Paul 	struct bge_softc *sc;
1125a813ed78SPyun YongHyeon 	uint32_t val;
112695d67482SBill Paul 	int i;
112795d67482SBill Paul 
112895d67482SBill Paul 	sc = device_get_softc(dev);
112995d67482SBill Paul 
1130548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1131548c8f1aSPyun YongHyeon 		return (0);
1132548c8f1aSPyun YongHyeon 
1133a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1134a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1135a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1136a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1137a813ed78SPyun YongHyeon 		DELAY(80);
113837ceeb4dSPaul Saab 	}
113937ceeb4dSPaul Saab 
114095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
114195d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
114295d67482SBill Paul 
1143a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
114495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1145d5d23857SJung-uk Kim 		DELAY(10);
114695d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
1147a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
1148a813ed78SPyun YongHyeon 			DELAY(5);
1149a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
115095d67482SBill Paul 			break;
115195d67482SBill Paul 		}
1152a813ed78SPyun YongHyeon 	}
115395d67482SBill Paul 
115495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
11555fea260fSMarius Strobl 		device_printf(sc->bge_dev,
11565fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
11575fea260fSMarius Strobl 		    phy, reg, val);
115837ceeb4dSPaul Saab 		val = 0;
115995d67482SBill Paul 	}
116095d67482SBill Paul 
1161a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1162a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1163a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1164a813ed78SPyun YongHyeon 		DELAY(80);
116537ceeb4dSPaul Saab 	}
116637ceeb4dSPaul Saab 
1167548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1168548c8f1aSPyun YongHyeon 
116995d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
117095d67482SBill Paul 		return (0);
117195d67482SBill Paul 
11720c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
117395d67482SBill Paul }
117495d67482SBill Paul 
117595d67482SBill Paul static int
11763f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
117795d67482SBill Paul {
117895d67482SBill Paul 	struct bge_softc *sc;
117995d67482SBill Paul 	int i;
118095d67482SBill Paul 
118195d67482SBill Paul 	sc = device_get_softc(dev);
118295d67482SBill Paul 
118338cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
118438cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
118538cc658fSJohn Baldwin 		return (0);
118638cc658fSJohn Baldwin 
1187548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1188548c8f1aSPyun YongHyeon 		return (0);
1189548c8f1aSPyun YongHyeon 
1190a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1191a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1192a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1193a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1194a813ed78SPyun YongHyeon 		DELAY(80);
119537ceeb4dSPaul Saab 	}
119637ceeb4dSPaul Saab 
119795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
119895d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
119995d67482SBill Paul 
120095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1201d5d23857SJung-uk Kim 		DELAY(10);
120238cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
120338cc658fSJohn Baldwin 			DELAY(5);
120438cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
120595d67482SBill Paul 			break;
1206d5d23857SJung-uk Kim 		}
120738cc658fSJohn Baldwin 	}
1208d5d23857SJung-uk Kim 
1209a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1210a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1211a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1212a813ed78SPyun YongHyeon 		DELAY(80);
1213a813ed78SPyun YongHyeon 	}
1214a813ed78SPyun YongHyeon 
1215548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1216548c8f1aSPyun YongHyeon 
1217a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
121838cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
12192246e8c6SPyun YongHyeon 		    "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
122038cc658fSJohn Baldwin 		    phy, reg, val);
122137ceeb4dSPaul Saab 
122295d67482SBill Paul 	return (0);
122395d67482SBill Paul }
122495d67482SBill Paul 
122595d67482SBill Paul static void
12263f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
122795d67482SBill Paul {
122895d67482SBill Paul 	struct bge_softc *sc;
122995d67482SBill Paul 	struct mii_data *mii;
1230a0a03d1eSPyun YongHyeon 	uint32_t mac_mode, rx_mode, tx_mode;
1231e4146b95SPyun YongHyeon 
123295d67482SBill Paul 	sc = device_get_softc(dev);
1233e4146b95SPyun YongHyeon 	if ((sc->bge_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1234e4146b95SPyun YongHyeon 		return;
123595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
123695d67482SBill Paul 
1237d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1238d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
1239d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
1240d4f5240aSPyun YongHyeon 		case IFM_10_T:
1241d4f5240aSPyun YongHyeon 		case IFM_100_TX:
1242d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
1243d4f5240aSPyun YongHyeon 			break;
1244d4f5240aSPyun YongHyeon 		case IFM_1000_T:
1245d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
1246d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
1247d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1248d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
1249d4f5240aSPyun YongHyeon 			else
1250d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
1251d4f5240aSPyun YongHyeon 			break;
1252d4f5240aSPyun YongHyeon 		default:
1253d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
1254d4f5240aSPyun YongHyeon 			break;
1255d4f5240aSPyun YongHyeon 		}
1256d4f5240aSPyun YongHyeon 	} else
1257d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
1258d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
1259d4f5240aSPyun YongHyeon 		return;
1260a0a03d1eSPyun YongHyeon 
1261a0a03d1eSPyun YongHyeon 	/*
1262a0a03d1eSPyun YongHyeon 	 * APE firmware touches these registers to keep the MAC
1263a0a03d1eSPyun YongHyeon 	 * connected to the outside world.  Try to keep the
1264a0a03d1eSPyun YongHyeon 	 * accesses atomic.
1265a0a03d1eSPyun YongHyeon 	 */
1266a0a03d1eSPyun YongHyeon 
1267a0a03d1eSPyun YongHyeon 	/* Set the port mode (MII/GMII) to match the link speed. */
1268a0a03d1eSPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
1269a0a03d1eSPyun YongHyeon 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
1270a0a03d1eSPyun YongHyeon 	tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
1271a0a03d1eSPyun YongHyeon 	rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
1272a0a03d1eSPyun YongHyeon 
1273ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1274ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1275a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_GMII;
12763f74909aSGleb Smirnoff 	else
1277a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_MII;
127895d67482SBill Paul 
1279a0a03d1eSPyun YongHyeon 	/* Set MAC flow control behavior to match link flow control settings. */
1280a0a03d1eSPyun YongHyeon 	tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
1281a0a03d1eSPyun YongHyeon 	rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
12826854be25SPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0) {
1283a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1284a0a03d1eSPyun YongHyeon 			tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
1285a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1286a0a03d1eSPyun YongHyeon 			rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
1287a0a03d1eSPyun YongHyeon 	} else
1288a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
1289a0a03d1eSPyun YongHyeon 
1290a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
12919b80ffe7SPyun YongHyeon 	DELAY(40);
1292a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
1293a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
129495d67482SBill Paul }
129595d67482SBill Paul 
129695d67482SBill Paul /*
129795d67482SBill Paul  * Intialize a standard receive ring descriptor.
129895d67482SBill Paul  */
129995d67482SBill Paul static int
1300943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
130195d67482SBill Paul {
1302943787f3SPyun YongHyeon 	struct mbuf *m;
130395d67482SBill Paul 	struct bge_rx_bd *r;
1304a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
1305943787f3SPyun YongHyeon 	bus_dmamap_t map;
1306a23634a1SPyun YongHyeon 	int error, nsegs;
130795d67482SBill Paul 
1308f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
1309f5459d4cSPyun YongHyeon 	    (sc->bge_ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
1310f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
1311f5459d4cSPyun YongHyeon 		m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1312f5459d4cSPyun YongHyeon 		if (m == NULL)
1313f5459d4cSPyun YongHyeon 			return (ENOBUFS);
1314f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1315f5459d4cSPyun YongHyeon 	} else {
1316943787f3SPyun YongHyeon 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1317943787f3SPyun YongHyeon 		if (m == NULL)
131895d67482SBill Paul 			return (ENOBUFS);
1319943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
1320f5459d4cSPyun YongHyeon 	}
1321652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1322943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
1323943787f3SPyun YongHyeon 
13240ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
1325943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
1326a23634a1SPyun YongHyeon 	if (error != 0) {
1327943787f3SPyun YongHyeon 		m_freem(m);
1328a23634a1SPyun YongHyeon 		return (error);
1329f41ac2beSBill Paul 	}
1330943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1331943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1332943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1333943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1334943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
1335943787f3SPyun YongHyeon 	}
1336943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
1337943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1338943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
1339943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
1340e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1341943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1342a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1343a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1344e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1345a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1346e907febfSPyun YongHyeon 	r->bge_idx = i;
1347f41ac2beSBill Paul 
13480ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1349943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
135095d67482SBill Paul 
135195d67482SBill Paul 	return (0);
135295d67482SBill Paul }
135395d67482SBill Paul 
135495d67482SBill Paul /*
135595d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
135695d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
135795d67482SBill Paul  */
135895d67482SBill Paul static int
1359943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
136095d67482SBill Paul {
13611be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1362943787f3SPyun YongHyeon 	bus_dmamap_t map;
13631be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1364943787f3SPyun YongHyeon 	struct mbuf *m;
1365943787f3SPyun YongHyeon 	int error, nsegs;
136695d67482SBill Paul 
1367943787f3SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1368943787f3SPyun YongHyeon 	if (m == NULL)
136995d67482SBill Paul 		return (ENOBUFS);
137095d67482SBill Paul 
1371943787f3SPyun YongHyeon 	m_cljget(m, M_DONTWAIT, MJUM9BYTES);
1372943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
1373943787f3SPyun YongHyeon 		m_freem(m);
137495d67482SBill Paul 		return (ENOBUFS);
137595d67482SBill Paul 	}
1376943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1377652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1378943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
13791be6acb7SGleb Smirnoff 
13801be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1381943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1382943787f3SPyun YongHyeon 	if (error != 0) {
1383943787f3SPyun YongHyeon 		m_freem(m);
13841be6acb7SGleb Smirnoff 		return (error);
1385f7cea149SGleb Smirnoff 	}
13861be6acb7SGleb Smirnoff 
1387aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1388943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1389943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1390943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1391943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1392943787f3SPyun YongHyeon 	}
1393943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1394943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1395943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1396943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1397943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1398e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1399e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1400e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1401e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1402e0b7b101SPyun YongHyeon 
14031be6acb7SGleb Smirnoff 	/*
14041be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
14051be6acb7SGleb Smirnoff 	 */
1406943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
14074e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
14084e7ba1abSGleb Smirnoff 	r->bge_idx = i;
14094e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
14104e7ba1abSGleb Smirnoff 	switch (nsegs) {
14114e7ba1abSGleb Smirnoff 	case 4:
14124e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
14134e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
14144e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1415e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
14164e7ba1abSGleb Smirnoff 	case 3:
1417e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1418e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1419e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1420e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
14214e7ba1abSGleb Smirnoff 	case 2:
14224e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
14234e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
14244e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1425e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
14264e7ba1abSGleb Smirnoff 	case 1:
14274e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
14284e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
14294e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1430e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
14314e7ba1abSGleb Smirnoff 		break;
14324e7ba1abSGleb Smirnoff 	default:
14334e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
14344e7ba1abSGleb Smirnoff 	}
1435f41ac2beSBill Paul 
1436a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1437943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
143895d67482SBill Paul 
143995d67482SBill Paul 	return (0);
144095d67482SBill Paul }
144195d67482SBill Paul 
144295d67482SBill Paul static int
14433f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
144495d67482SBill Paul {
14453ee5d7daSPyun YongHyeon 	int error, i;
144695d67482SBill Paul 
1447e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
144803e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1449e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1450943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
14513ee5d7daSPyun YongHyeon 			return (error);
145203e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
14531888f324SPyun YongHyeon 	}
145495d67482SBill Paul 
1455f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1456d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1457f41ac2beSBill Paul 
1458e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1459e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
146095d67482SBill Paul 
146195d67482SBill Paul 	return (0);
146295d67482SBill Paul }
146395d67482SBill Paul 
146495d67482SBill Paul static void
14653f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
146695d67482SBill Paul {
146795d67482SBill Paul 	int i;
146895d67482SBill Paul 
146995d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
147095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
14710ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1472e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1473e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
14740ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1475f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1476e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1477e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
147895d67482SBill Paul 		}
1479f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
148095d67482SBill Paul 		    sizeof(struct bge_rx_bd));
148195d67482SBill Paul 	}
148295d67482SBill Paul }
148395d67482SBill Paul 
148495d67482SBill Paul static int
14853f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
148695d67482SBill Paul {
148795d67482SBill Paul 	struct bge_rcb *rcb;
14883ee5d7daSPyun YongHyeon 	int error, i;
148995d67482SBill Paul 
1490e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
149103e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
149295d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1493943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
14943ee5d7daSPyun YongHyeon 			return (error);
149503e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
14961888f324SPyun YongHyeon 	}
149795d67482SBill Paul 
1498f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1499d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1500f41ac2beSBill Paul 
1501e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
150295d67482SBill Paul 
15038a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1504f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
15058a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
15068a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
150767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
150895d67482SBill Paul 
1509e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
151095d67482SBill Paul 
151195d67482SBill Paul 	return (0);
151295d67482SBill Paul }
151395d67482SBill Paul 
151495d67482SBill Paul static void
15153f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
151695d67482SBill Paul {
151795d67482SBill Paul 	int i;
151895d67482SBill Paul 
151995d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
152095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1521e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1522e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1523e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1524f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1525f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1526e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1527e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
152895d67482SBill Paul 		}
1529f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
15301be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
153195d67482SBill Paul 	}
153295d67482SBill Paul }
153395d67482SBill Paul 
153495d67482SBill Paul static void
15353f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
153695d67482SBill Paul {
153795d67482SBill Paul 	int i;
153895d67482SBill Paul 
1539f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
154095d67482SBill Paul 		return;
154195d67482SBill Paul 
154295d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
154395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
15440ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1545e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1546e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15470ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1548f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1549e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1550e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
155195d67482SBill Paul 		}
1552f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
155395d67482SBill Paul 		    sizeof(struct bge_tx_bd));
155495d67482SBill Paul 	}
155595d67482SBill Paul }
155695d67482SBill Paul 
155795d67482SBill Paul static int
15583f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
155995d67482SBill Paul {
156095d67482SBill Paul 	sc->bge_txcnt = 0;
156195d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
15623927098fSPaul Saab 
1563e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1564e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
15655c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1566e6bf277eSPyun YongHyeon 
156714bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
156814bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
156938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
157014bbd30fSGleb Smirnoff 
15713927098fSPaul Saab 	/* 5700 b2 errata */
1572e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
157338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
15743927098fSPaul Saab 
157514bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
157638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
15773927098fSPaul Saab 	/* 5700 b2 errata */
1578e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
157938cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
158095d67482SBill Paul 
158195d67482SBill Paul 	return (0);
158295d67482SBill Paul }
158395d67482SBill Paul 
158495d67482SBill Paul static void
15853e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
15863e9b1bcaSJung-uk Kim {
15873e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
15883e9b1bcaSJung-uk Kim 
15893e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
15903e9b1bcaSJung-uk Kim 
15913e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
15923e9b1bcaSJung-uk Kim 
159345ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
15943e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
159545ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
15963e9b1bcaSJung-uk Kim 	else
159745ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
15983e9b1bcaSJung-uk Kim }
15993e9b1bcaSJung-uk Kim 
16003e9b1bcaSJung-uk Kim static void
16013f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
160295d67482SBill Paul {
160395d67482SBill Paul 	struct ifnet *ifp;
160495d67482SBill Paul 	struct ifmultiaddr *ifma;
16053f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
160695d67482SBill Paul 	int h, i;
160795d67482SBill Paul 
16080f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
16090f9bd73bSSam Leffler 
1610fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
161195d67482SBill Paul 
161295d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
161395d67482SBill Paul 		for (i = 0; i < 4; i++)
16140c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
161595d67482SBill Paul 		return;
161695d67482SBill Paul 	}
161795d67482SBill Paul 
161895d67482SBill Paul 	/* First, zot all the existing filters. */
161995d67482SBill Paul 	for (i = 0; i < 4; i++)
162095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
162195d67482SBill Paul 
162295d67482SBill Paul 	/* Now program new ones. */
1623eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
162495d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
162595d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
162695d67482SBill Paul 			continue;
16270e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
16280c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
16290c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
163095d67482SBill Paul 	}
1631eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
163295d67482SBill Paul 
163395d67482SBill Paul 	for (i = 0; i < 4; i++)
163495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
163595d67482SBill Paul }
163695d67482SBill Paul 
16378cb1383cSDoug Ambrisko static void
1638cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1639cb2eacc7SYaroslav Tykhiy {
1640cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1641cb2eacc7SYaroslav Tykhiy 
1642cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1643cb2eacc7SYaroslav Tykhiy 
1644cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1645cb2eacc7SYaroslav Tykhiy 
1646cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1647cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1648cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1649cb2eacc7SYaroslav Tykhiy 	else
1650cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1651cb2eacc7SYaroslav Tykhiy }
1652cb2eacc7SYaroslav Tykhiy 
1653cb2eacc7SYaroslav Tykhiy static void
1654797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
16558cb1383cSDoug Ambrisko {
1656797ab05eSPyun YongHyeon 
16578cb1383cSDoug Ambrisko 	/*
16588cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
16598cb1383cSDoug Ambrisko 	 */
16608cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1661888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
16628cb1383cSDoug Ambrisko 
16638cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16648cb1383cSDoug Ambrisko 		switch (type) {
16658cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1666224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1667224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
16688cb1383cSDoug Ambrisko 			break;
1669548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1670224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1671224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
16728cb1383cSDoug Ambrisko 			break;
1673548c8f1aSPyun YongHyeon 		case BGE_RESET_SUSPEND:
1674548c8f1aSPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1675548c8f1aSPyun YongHyeon 			    BGE_FW_DRV_STATE_SUSPEND);
1676548c8f1aSPyun YongHyeon 			break;
16778cb1383cSDoug Ambrisko 		}
16788cb1383cSDoug Ambrisko 	}
1679548c8f1aSPyun YongHyeon 
1680548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1681548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
16828cb1383cSDoug Ambrisko }
16838cb1383cSDoug Ambrisko 
16848cb1383cSDoug Ambrisko static void
1685797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
16868cb1383cSDoug Ambrisko {
1687797ab05eSPyun YongHyeon 
16888cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16898cb1383cSDoug Ambrisko 		switch (type) {
16908cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1691224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1692224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
16938cb1383cSDoug Ambrisko 			/* START DONE */
16948cb1383cSDoug Ambrisko 			break;
1695548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1696224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1697224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
16988cb1383cSDoug Ambrisko 			break;
16998cb1383cSDoug Ambrisko 		}
17008cb1383cSDoug Ambrisko 	}
1701548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_SHUTDOWN)
1702548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17038cb1383cSDoug Ambrisko }
17048cb1383cSDoug Ambrisko 
17058cb1383cSDoug Ambrisko static void
1706797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
17078cb1383cSDoug Ambrisko {
1708797ab05eSPyun YongHyeon 
17098cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17108cb1383cSDoug Ambrisko 		switch (type) {
17118cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1712224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1713224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
17148cb1383cSDoug Ambrisko 			break;
1715548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1716224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1717224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
17188cb1383cSDoug Ambrisko 			break;
17198cb1383cSDoug Ambrisko 		}
17208cb1383cSDoug Ambrisko 	}
17218cb1383cSDoug Ambrisko }
17228cb1383cSDoug Ambrisko 
1723797ab05eSPyun YongHyeon static void
1724797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
17258cb1383cSDoug Ambrisko {
17268cb1383cSDoug Ambrisko 	int i;
17278cb1383cSDoug Ambrisko 
17288cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17293c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
17303fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
17319931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
17328cb1383cSDoug Ambrisko 
17338cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
17349931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
17359931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
17368cb1383cSDoug Ambrisko 				break;
17378cb1383cSDoug Ambrisko 			DELAY(10);
17388cb1383cSDoug Ambrisko 		}
17398cb1383cSDoug Ambrisko 	}
17408cb1383cSDoug Ambrisko }
17418cb1383cSDoug Ambrisko 
174250515680SPyun YongHyeon static uint32_t
174350515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
174450515680SPyun YongHyeon {
174550515680SPyun YongHyeon 	uint32_t dma_options;
174650515680SPyun YongHyeon 
174750515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
174850515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
174950515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
175050515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
175150515680SPyun YongHyeon #endif
175250515680SPyun YongHyeon 	return (dma_options);
175350515680SPyun YongHyeon }
175450515680SPyun YongHyeon 
175595d67482SBill Paul /*
1756c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
175795d67482SBill Paul  */
175895d67482SBill Paul static int
17593f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
176095d67482SBill Paul {
176150515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1762fbc374afSPyun YongHyeon 	uint16_t val;
176395d67482SBill Paul 	int i;
176495d67482SBill Paul 
17658cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
17661108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
17671108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
17681108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
17691108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
177095d67482SBill Paul 
177195d67482SBill Paul 	/*
177295d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
177395d67482SBill Paul 	 * internal memory.
177495d67482SBill Paul 	 */
177595d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
17763f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
177795d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
177895d67482SBill Paul 
177995d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
17803f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
178195d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
178295d67482SBill Paul 
1783fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1784fbc374afSPyun YongHyeon 		/*
1785d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1786fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1787fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1788fbc374afSPyun YongHyeon 		 */
1789fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1790fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1791fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1792fbc374afSPyun YongHyeon 	}
1793fbc374afSPyun YongHyeon 
1794186f842bSJung-uk Kim 	/*
1795186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1796186f842bSJung-uk Kim 	 */
1797186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1798186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1799652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
180048630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
180148630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
180248630d79SPyun YongHyeon 		else
1803186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1804652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
18054c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1806186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1807186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1808186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1809186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1810186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1811186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1812cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1813cbb2b2feSPyun YongHyeon 			/*
1814cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1815cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1816cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1817cbb2b2feSPyun YongHyeon 			 * register.
1818cbb2b2feSPyun YongHyeon 			 */
1819cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1820cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1821186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1822186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1823186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1824186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1825186f842bSJung-uk Kim 		} else {
1826186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1827186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1828186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
18290c8aa4eaSJung-uk Kim 			    0x0F;
1830186f842bSJung-uk Kim 		}
1831e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1832e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
18333f74909aSGleb Smirnoff 			uint32_t tmp;
18345cba12d3SPaul Saab 
1835186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
18360c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1837186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1838186f842bSJung-uk Kim 				dma_rw_ctl |=
1839186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
18405cba12d3SPaul Saab 
1841186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1842186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1843186f842bSJung-uk Kim 		}
1844186f842bSJung-uk Kim 	} else {
1845186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1846186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1847186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1848186f842bSJung-uk Kim 
1849186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1850186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1851186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1852186f842bSJung-uk Kim 	}
1853186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1854186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1855186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1856186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1857e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1858186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
18595cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1860b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18611108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1862b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1863b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1864bbe2ca75SPyun YongHyeon 		/*
1865bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1866bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1867bbe2ca75SPyun YongHyeon 		 * disabled.
1868bbe2ca75SPyun YongHyeon 		 */
1869bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
1870bbe2ca75SPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM57765)
1871bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1872b4a256acSPyun YongHyeon 	}
18735cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
187495d67482SBill Paul 
187595d67482SBill Paul 	/*
187695d67482SBill Paul 	 * Set up general mode register.
187795d67482SBill Paul 	 */
1878548c8f1aSPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc);
1879548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
1880548c8f1aSPyun YongHyeon 		/* Retain Host-2-BMC settings written by APE firmware. */
1881548c8f1aSPyun YongHyeon 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1882548c8f1aSPyun YongHyeon 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1883548c8f1aSPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1884548c8f1aSPyun YongHyeon 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1885548c8f1aSPyun YongHyeon 	}
1886548c8f1aSPyun YongHyeon 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1887548c8f1aSPyun YongHyeon 	    BGE_MODECTL_TX_NO_PHDR_CSUM;
188895d67482SBill Paul 
188995d67482SBill Paul 	/*
189090447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
189190447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
189290447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
189390447aadSMarius Strobl 	 * certain bridges.
189490447aadSMarius Strobl 	 */
189590447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
189690447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
189750515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
189890447aadSMarius Strobl 
189990447aadSMarius Strobl 	/*
19008cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
19018cb1383cSDoug Ambrisko 	 */
19028cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
190350515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
190450515680SPyun YongHyeon 
190550515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
19068cb1383cSDoug Ambrisko 
19078cb1383cSDoug Ambrisko 	/*
1908ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1909c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1910c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
191195d67482SBill Paul 	 */
1912c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1913c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
191495d67482SBill Paul 
191595d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
19160c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
191795d67482SBill Paul 
191838cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
191938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
192038cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
192138cc658fSJohn Baldwin 
192238cc658fSJohn Baldwin 		/* Put PHY into ready state */
192338cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
192438cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
192538cc658fSJohn Baldwin 		DELAY(40);
192638cc658fSJohn Baldwin 	}
192738cc658fSJohn Baldwin 
192895d67482SBill Paul 	return (0);
192995d67482SBill Paul }
193095d67482SBill Paul 
193195d67482SBill Paul static int
19323f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
193395d67482SBill Paul {
193495d67482SBill Paul 	struct bge_rcb *rcb;
1935e907febfSPyun YongHyeon 	bus_size_t vrcb;
1936e907febfSPyun YongHyeon 	bge_hostaddr taddr;
1937bbe2ca75SPyun YongHyeon 	uint32_t dmactl, val;
19388a315a6dSPyun YongHyeon 	int i, limit;
193995d67482SBill Paul 
194095d67482SBill Paul 	/*
194195d67482SBill Paul 	 * Initialize the memory window pointer register so that
194295d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
194395d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
194495d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
194595d67482SBill Paul 	 */
194695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
194795d67482SBill Paul 
1948822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1949822f63fcSBill Paul 
19507ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
195195d67482SBill Paul 		/* Configure mbuf memory pool */
19520dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1953822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1954822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1955822f63fcSBill Paul 		else
195695d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
195795d67482SBill Paul 
195895d67482SBill Paul 		/* Configure DMA resource pool */
19590434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
19600434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
196195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
19620434d1b8SBill Paul 	}
196395d67482SBill Paul 
196495d67482SBill Paul 	/* Configure mbuf pool watermarks */
196550515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
19661108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
19671108273aSPyun YongHyeon 		if (sc->bge_ifp->if_mtu > ETHERMTU) {
19681108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
19691108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
19701108273aSPyun YongHyeon 		} else {
19711108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
19721108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
19731108273aSPyun YongHyeon 		}
19741108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
1975fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1976fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1977fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
197838cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
197938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
198038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
198138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
198238cc658fSJohn Baldwin 	} else {
198338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
198438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
198538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
198638cc658fSJohn Baldwin 	}
198795d67482SBill Paul 
198895d67482SBill Paul 	/* Configure DMA resource watermarks */
198995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
199095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
199195d67482SBill Paul 
199295d67482SBill Paul 	/* Enable buffer manager */
1993bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1994bbe2ca75SPyun YongHyeon 	/*
1995bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
1996bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
1997bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
1998bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
1999bbe2ca75SPyun YongHyeon 	 */
2000bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2001bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
2002bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
200395d67482SBill Paul 
200495d67482SBill Paul 	/* Poll for buffer manager start indication */
200595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2006d5d23857SJung-uk Kim 		DELAY(10);
20070c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
200895d67482SBill Paul 			break;
200995d67482SBill Paul 	}
201095d67482SBill Paul 
201195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
20125a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
201395d67482SBill Paul 		return (ENXIO);
201495d67482SBill Paul 	}
201595d67482SBill Paul 
201695d67482SBill Paul 	/* Enable flow-through queues */
20170c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
201895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
201995d67482SBill Paul 
202095d67482SBill Paul 	/* Wait until queue initialization is complete */
202195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2022d5d23857SJung-uk Kim 		DELAY(10);
202395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
202495d67482SBill Paul 			break;
202595d67482SBill Paul 	}
202695d67482SBill Paul 
202795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2028fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
202995d67482SBill Paul 		return (ENXIO);
203095d67482SBill Paul 	}
203195d67482SBill Paul 
20328a315a6dSPyun YongHyeon 	/*
20338a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
20348a315a6dSPyun YongHyeon 	 *
20358a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
20368a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
20378a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
20388a315a6dSPyun YongHyeon 	 *
20398a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
20408a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
20418a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
20428a315a6dSPyun YongHyeon 	 *   to the controller.
20438a315a6dSPyun YongHyeon 	 *
20448a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
20458a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
20468a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
20478a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
20488a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
20498a315a6dSPyun YongHyeon 	 *   be disabled.
20508a315a6dSPyun YongHyeon 	 *
20518a315a6dSPyun YongHyeon 	 * Receive Return Ring
20528a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
20538a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
20548a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
20558a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
20568a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
20578a315a6dSPyun YongHyeon 	 *
20588a315a6dSPyun YongHyeon 	 * Send Ring
20598a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
20608a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
20618a315a6dSPyun YongHyeon 	 */
20628a315a6dSPyun YongHyeon 
20638a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
2064f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2065f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
2066f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2067f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
2068f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2069f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2070f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
20711108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
20721108273aSPyun YongHyeon 		/*
20731108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
20741108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
20751108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
20761108273aSPyun YongHyeon 		 * Bit 0     : Reserved
20771108273aSPyun YongHyeon 		 */
20781108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
20791108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
20801108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
20818a315a6dSPyun YongHyeon 		/*
20828a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
20838a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
20848a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
20858a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
20868a315a6dSPyun YongHyeon 		 */
20870434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
20888a315a6dSPyun YongHyeon 	} else {
20898a315a6dSPyun YongHyeon 		/*
20908a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
20918a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
20928a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
20938a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
20948a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
20958a315a6dSPyun YongHyeon 		 */
20960434d1b8SBill Paul 		rcb->bge_maxlen_flags =
20970434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
20988a315a6dSPyun YongHyeon 	}
2099bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
210050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
210150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21021108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
21031108273aSPyun YongHyeon 	else
210495d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
21058a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
21060c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
21070c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
210867111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
210967111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
211095d67482SBill Paul 
21118a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
21128a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
21138a315a6dSPyun YongHyeon 
211495d67482SBill Paul 	/*
21158a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
21168a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
21178a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
211895d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
211995d67482SBill Paul 	 * high enough to require it).
212095d67482SBill Paul 	 */
21214c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2122f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
21238a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
2124f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
2125f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2126f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
2127f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2128f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2129f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2130f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
21311be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
21321be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2133bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
213450515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
213550515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21361108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
21371108273aSPyun YongHyeon 		else
213895d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
213967111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
214067111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
214167111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
214267111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
21438a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
21440434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
21450434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
214667111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
21478a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
21488a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
21498a315a6dSPyun YongHyeon 	}
215095d67482SBill Paul 
21518a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
21525e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
2153f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
215467111612SJohn Polstra 		rcb->bge_maxlen_flags =
215567111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
21560434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
21570434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
21588a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
21598a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
21600434d1b8SBill Paul 	}
216195d67482SBill Paul 
2162ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2163ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2164427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2165427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2166427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
21678d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
21688d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2169ca4f8986SPyun YongHyeon 	}
217095d67482SBill Paul 	/*
21718a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
21728a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
21738a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
21748a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
21758a315a6dSPyun YongHyeon 	 *
217695d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
217795d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
217895d67482SBill Paul 	 * each ring.
21799ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
21809ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
21819ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
21829ba784dbSScott Long 	 * are reports that it might not need to be so strict.
218338cc658fSJohn Baldwin 	 *
218438cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
218538cc658fSJohn Baldwin 	 * well.
218695d67482SBill Paul 	 */
21875345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
21886f8718a3SScott Long 		val = 8;
21896f8718a3SScott Long 	else
21906f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
21916f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
21922a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
21932a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
21942a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
21951108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
21961108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
21971108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
21981108273aSPyun YongHyeon 	}
219995d67482SBill Paul 
220095d67482SBill Paul 	/*
22018a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
22028a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
22038a315a6dSPyun YongHyeon 	 * located in NIC memory.
220495d67482SBill Paul 	 */
22058a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
22068a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
22078a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
22088a315a6dSPyun YongHyeon 	else
22098a315a6dSPyun YongHyeon 		limit = 1;
2210e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
22118a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2212e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2213e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2214e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2215e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
221695d67482SBill Paul 	}
221795d67482SBill Paul 
22188a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
2219e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2220e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2221e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2222e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2223bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
222450515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
222550515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
22261108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
22271108273aSPyun YongHyeon 	else
2228e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2229e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2230e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2231e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
223295d67482SBill Paul 
22338a315a6dSPyun YongHyeon 	/*
22348a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
22358a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
22368a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
22378a315a6dSPyun YongHyeon 	 */
2238bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
223950515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
224050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
22411108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
22421108273aSPyun YongHyeon 		limit = 16;
22431108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
22448a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
2245b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2246b4a256acSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57765)
22478a315a6dSPyun YongHyeon 		limit = 4;
22488a315a6dSPyun YongHyeon 	else
22498a315a6dSPyun YongHyeon 		limit = 1;
22508a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
2251e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
22528a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2253e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2254e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2255e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
22568a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
2257e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
225838cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
22593f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
2260e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
226195d67482SBill Paul 	}
226295d67482SBill Paul 
226395d67482SBill Paul 	/*
22648a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
22658a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
22668a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
226795d67482SBill Paul 	 */
2268e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2269e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2270e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2271e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
22728a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2273e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2274e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
227595d67482SBill Paul 
227695d67482SBill Paul 	/* Set random backoff seed for TX */
227795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
22784a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
22794a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
22804a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
228195d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
228295d67482SBill Paul 
228395d67482SBill Paul 	/* Set inter-packet gap */
228450515680SPyun YongHyeon 	val = 0x2620;
228550515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
228650515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
228750515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
228850515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
228995d67482SBill Paul 
229095d67482SBill Paul 	/*
229195d67482SBill Paul 	 * Specify which ring to use for packets that don't match
229295d67482SBill Paul 	 * any RX rules.
229395d67482SBill Paul 	 */
229495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
229595d67482SBill Paul 
229695d67482SBill Paul 	/*
229795d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
229895d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
229995d67482SBill Paul 	 */
230095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
230195d67482SBill Paul 
230295d67482SBill Paul 	/* Inialize RX list placement stats mask. */
23030c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
230495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
230595d67482SBill Paul 
230695d67482SBill Paul 	/* Disable host coalescing until we get it set up */
230795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
230895d67482SBill Paul 
230995d67482SBill Paul 	/* Poll to make sure it's shut down. */
231095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2311d5d23857SJung-uk Kim 		DELAY(10);
231295d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
231395d67482SBill Paul 			break;
231495d67482SBill Paul 	}
231595d67482SBill Paul 
231695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2317fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2318fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
231995d67482SBill Paul 		return (ENXIO);
232095d67482SBill Paul 	}
232195d67482SBill Paul 
232295d67482SBill Paul 	/* Set up host coalescing defaults */
232395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
232495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
232595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
232695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
23277ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
232895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
232995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
23300434d1b8SBill Paul 	}
2331b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2332b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
233395d67482SBill Paul 
233495d67482SBill Paul 	/* Set up address of statistics block */
23357ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
2336f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2337f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
233895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2339f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
23400434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
234195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
23420434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
23430434d1b8SBill Paul 	}
23440434d1b8SBill Paul 
23450434d1b8SBill Paul 	/* Set up address of status block */
2346f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2347f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
234895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2349f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
235095d67482SBill Paul 
235130f57f61SPyun YongHyeon 	/* Set up status block size. */
235230f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2353864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
235430f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2355864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2356864104feSPyun YongHyeon 	} else {
235730f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2358864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2359864104feSPyun YongHyeon 	}
2360864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2361864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2362864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
236330f57f61SPyun YongHyeon 
236495d67482SBill Paul 	/* Turn on host coalescing state machine */
236530f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
236695d67482SBill Paul 
236795d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
236895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
236995d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
237095d67482SBill Paul 
237195d67482SBill Paul 	/* Turn on RX list placement state machine */
237295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
237395d67482SBill Paul 
237495d67482SBill Paul 	/* Turn on RX list selector state machine. */
23757ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
237695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
237795d67482SBill Paul 
23782246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2379ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2380ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2381ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2382ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2383ea3b4127SPyun YongHyeon 
2384ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2385ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2386ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2387ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2388ea3b4127SPyun YongHyeon 	else
2389ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2390ea3b4127SPyun YongHyeon 
2391548c8f1aSPyun YongHyeon 	/* Allow APE to send/receive frames. */
2392548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2393548c8f1aSPyun YongHyeon 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2394548c8f1aSPyun YongHyeon 
2395ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
23969b80ffe7SPyun YongHyeon 	DELAY(40);
239795d67482SBill Paul 
239895d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
239995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
240095d67482SBill Paul 
240195d67482SBill Paul #ifdef notdef
240295d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
240395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
240495d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
240595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
240695d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
240795d67482SBill Paul #endif
240895d67482SBill Paul 
240995d67482SBill Paul 	/* Turn on DMA completion state machine */
24107ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
241195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
241295d67482SBill Paul 
24136f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
24146f8718a3SScott Long 
24156f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2416a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
24173889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
24186f8718a3SScott Long 
24197aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
24207aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
24217aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
24227aa4b937SPyun YongHyeon 
242395d67482SBill Paul 	/* Turn on write DMA state machine */
24246f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
24254f09c4c7SMarius Strobl 	DELAY(40);
242695d67482SBill Paul 
242795d67482SBill Paul 	/* Turn on read DMA state machine */
24284f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
24291108273aSPyun YongHyeon 
24301108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
24311108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
24321108273aSPyun YongHyeon 
2433a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2434a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2435a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2436a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2437a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2438a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
24394f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
24404f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
24411108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2442ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
24431108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
24441108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
244555a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
244655a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
244755a24a05SPyun YongHyeon 	}
244850515680SPyun YongHyeon 
2449e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
245050515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
245150515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2452e3215f76SPyun YongHyeon 		/*
2453e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2454e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2455e3215f76SPyun YongHyeon 		 */
2456e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2457e3215f76SPyun YongHyeon 	}
245850515680SPyun YongHyeon 
2459d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2460d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2461d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
24621108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
24631108273aSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc)) {
2464bbe2ca75SPyun YongHyeon 		dmactl = CSR_READ_4(sc, BGE_RDMA_RSRVCTRL);
2465bbe2ca75SPyun YongHyeon 		/*
2466bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2467bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2468bbe2ca75SPyun YongHyeon 		 */
2469f7add34cSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
2470f7add34cSPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
2471bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2472bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2473bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2474bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2475bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2476bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2477bbe2ca75SPyun YongHyeon 		}
2478d255f2a9SPyun YongHyeon 		/*
2479d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2480d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2481d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2482d255f2a9SPyun YongHyeon 		 */
2483bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL, dmactl |
2484d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2485d255f2a9SPyun YongHyeon 	}
2486bbe2ca75SPyun YongHyeon 
2487e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2488bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2489bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2490bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2491bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2492e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2493e3215f76SPyun YongHyeon 		/*
2494e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2495e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2496e3215f76SPyun YongHyeon 		 */
2497e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2498e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2499e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2500e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2501bbe2ca75SPyun YongHyeon 	}
2502bbe2ca75SPyun YongHyeon 
25034f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
25044f09c4c7SMarius Strobl 	DELAY(40);
250595d67482SBill Paul 
250695d67482SBill Paul 	/* Turn on RX data completion state machine */
250795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
250895d67482SBill Paul 
250995d67482SBill Paul 	/* Turn on RX BD initiator state machine */
251095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
251195d67482SBill Paul 
251295d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
251395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
251495d67482SBill Paul 
251595d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25167ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
251795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
251895d67482SBill Paul 
251995d67482SBill Paul 	/* Turn on send BD completion state machine */
252095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
252195d67482SBill Paul 
252295d67482SBill Paul 	/* Turn on send data completion state machine */
2523a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2524a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2525a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2526a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
252795d67482SBill Paul 
252895d67482SBill Paul 	/* Turn on send data initiator state machine */
25291108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
25301108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
25311108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2532ca3f1187SPyun YongHyeon 	else
253395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
253495d67482SBill Paul 
253595d67482SBill Paul 	/* Turn on send BD initiator state machine */
253695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
253795d67482SBill Paul 
253895d67482SBill Paul 	/* Turn on send BD selector state machine */
253995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
254095d67482SBill Paul 
25410c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
254295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
254395d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
254495d67482SBill Paul 
254595d67482SBill Paul 	/* ack/clear link change events */
254695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
25470434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
25480434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2549f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
255095d67482SBill Paul 
25516ede2cfaSPyun YongHyeon 	/*
25526ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
25536ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
25546ede2cfaSPyun YongHyeon 	 */
2555652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
255695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2557a1d52896SBill Paul 	} else {
25587ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
25597ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
25607ed3f0f0SPyun YongHyeon 			DELAY(80);
25617ed3f0f0SPyun YongHyeon 		}
25621f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
25634c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2564a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2565a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2566a1d52896SBill Paul 	}
256795d67482SBill Paul 
25681f313773SOleg Bulyzhin 	/*
25691f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
25701f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
25711f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
25721f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
25731f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
25741f313773SOleg Bulyzhin 	 */
25751f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
25761f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
25771f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
25781f313773SOleg Bulyzhin 
257995d67482SBill Paul 	/* Enable link state change attentions. */
258095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
258195d67482SBill Paul 
258295d67482SBill Paul 	return (0);
258395d67482SBill Paul }
258495d67482SBill Paul 
25854c0da0ffSGleb Smirnoff const struct bge_revision *
25864c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
25874c0da0ffSGleb Smirnoff {
25884c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
25894c0da0ffSGleb Smirnoff 
25904c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
25914c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
25924c0da0ffSGleb Smirnoff 			return (br);
25934c0da0ffSGleb Smirnoff 	}
25944c0da0ffSGleb Smirnoff 
25954c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
25964c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
25974c0da0ffSGleb Smirnoff 			return (br);
25984c0da0ffSGleb Smirnoff 	}
25994c0da0ffSGleb Smirnoff 
26004c0da0ffSGleb Smirnoff 	return (NULL);
26014c0da0ffSGleb Smirnoff }
26024c0da0ffSGleb Smirnoff 
26034c0da0ffSGleb Smirnoff const struct bge_vendor *
26044c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26054c0da0ffSGleb Smirnoff {
26064c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26074c0da0ffSGleb Smirnoff 
26084c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26094c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26104c0da0ffSGleb Smirnoff 			return (v);
26114c0da0ffSGleb Smirnoff 
26124c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
26134c0da0ffSGleb Smirnoff 	return (NULL);
26144c0da0ffSGleb Smirnoff }
26154c0da0ffSGleb Smirnoff 
261695d67482SBill Paul /*
261795d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
26184c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
26194c0da0ffSGleb Smirnoff  *
26204c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
26217c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
26227c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
26237c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
26247c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
262595d67482SBill Paul  */
262695d67482SBill Paul static int
26273f74909aSGleb Smirnoff bge_probe(device_t dev)
262895d67482SBill Paul {
2629978f2704SMarius Strobl 	char buf[96];
2630978f2704SMarius Strobl 	char model[64];
2631978f2704SMarius Strobl 	const struct bge_revision *br;
2632978f2704SMarius Strobl 	const char *pname;
26334c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
2634978f2704SMarius Strobl 	const struct bge_type *t = bge_devs;
2635978f2704SMarius Strobl 	const struct bge_vendor *v;
2636978f2704SMarius Strobl 	uint32_t id;
2637978f2704SMarius Strobl 	uint16_t did, vid;
263895d67482SBill Paul 
263995d67482SBill Paul 	sc->bge_dev = dev;
26407c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
26417c929cf9SJung-uk Kim 	did = pci_get_device(dev);
26424c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
26437c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2644a5779553SStanislav Sedov 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2645a5779553SStanislav Sedov 			    BGE_PCIMISCCTL_ASICREV_SHIFT;
26461108273aSPyun YongHyeon 			if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
26471108273aSPyun YongHyeon 				/*
26481108273aSPyun YongHyeon 				 * Find the ASCI revision.  Different chips
26491108273aSPyun YongHyeon 				 * use different registers.
26501108273aSPyun YongHyeon 				 */
26511108273aSPyun YongHyeon 				switch (pci_get_device(dev)) {
26521108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5717:
26531108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5718:
2654bbe2ca75SPyun YongHyeon 				case BCOM_DEVICEID_BCM5719:
265550515680SPyun YongHyeon 				case BCOM_DEVICEID_BCM5720:
26561108273aSPyun YongHyeon 					id = pci_read_config(dev,
26571108273aSPyun YongHyeon 					    BGE_PCI_GEN2_PRODID_ASICREV, 4);
26581108273aSPyun YongHyeon 					break;
2659b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57761:
2660b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57765:
2661b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57781:
2662b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57785:
2663b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57791:
2664b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57795:
2665b4a256acSPyun YongHyeon 					id = pci_read_config(dev,
2666b4a256acSPyun YongHyeon 					    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2667b4a256acSPyun YongHyeon 					break;
26681108273aSPyun YongHyeon 				default:
2669a5779553SStanislav Sedov 					id = pci_read_config(dev,
2670a5779553SStanislav Sedov 					    BGE_PCI_PRODID_ASICREV, 4);
26711108273aSPyun YongHyeon 				}
26721108273aSPyun YongHyeon 			}
26734c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
26747c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
2675852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2676852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
26774e35d186SJung-uk Kim 				snprintf(model, 64, "%s", pname);
26784e35d186SJung-uk Kim 			else
2679978f2704SMarius Strobl 				snprintf(model, 64, "%s %s", v->v_name,
26807c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
26817c929cf9SJung-uk Kim 				    "NetXtreme Ethernet Controller");
2682a5779553SStanislav Sedov 			snprintf(buf, 96, "%s, %sASIC rev. %#08x", model,
2683a5779553SStanislav Sedov 			    br != NULL ? "" : "unknown ", id);
26844c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
268595d67482SBill Paul 			return (0);
268695d67482SBill Paul 		}
268795d67482SBill Paul 		t++;
268895d67482SBill Paul 	}
268995d67482SBill Paul 
269095d67482SBill Paul 	return (ENXIO);
269195d67482SBill Paul }
269295d67482SBill Paul 
2693f41ac2beSBill Paul static void
26943f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2695f41ac2beSBill Paul {
2696f41ac2beSBill Paul 	int i;
2697f41ac2beSBill Paul 
26983f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2699f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2700f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
27010ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2702f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2703f41ac2beSBill Paul 	}
2704943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2705943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2706943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2707f41ac2beSBill Paul 
27083f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2709f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2710f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2711f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2712f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2713f41ac2beSBill Paul 	}
2714943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2715943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2716943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2717f41ac2beSBill Paul 
27183f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2719f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2720f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
27210ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2722f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2723f41ac2beSBill Paul 	}
2724f41ac2beSBill Paul 
27250ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
27260ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2727c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2728c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
27290ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
27300ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2731f41ac2beSBill Paul 
27323f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2733e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2734e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2735e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2736e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2737f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2738f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2739f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2740f41ac2beSBill Paul 
2741f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2742f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2743f41ac2beSBill Paul 
27443f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2745e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2746e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2747e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2748e65bed95SPyun YongHyeon 
2749e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2750e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2751f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2752f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2753f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2754f41ac2beSBill Paul 
2755f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2756f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2757f41ac2beSBill Paul 
27583f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2759e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2760e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2761e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2762e65bed95SPyun YongHyeon 
2763e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2764e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2765f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2766f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2767f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2768f41ac2beSBill Paul 
2769f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2770f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2771f41ac2beSBill Paul 
27723f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2773e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2774e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2775e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2776e65bed95SPyun YongHyeon 
2777e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2778f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2779f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2780f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2781f41ac2beSBill Paul 
2782f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2783f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2784f41ac2beSBill Paul 
27853f74909aSGleb Smirnoff 	/* Destroy status block. */
2786e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2787e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2788e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2789e65bed95SPyun YongHyeon 
2790e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2791f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2792f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2793f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2794f41ac2beSBill Paul 
2795f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2796f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2797f41ac2beSBill Paul 
27983f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2799e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2800e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2801e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2802e65bed95SPyun YongHyeon 
2803e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2804f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2805f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2806f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2807f41ac2beSBill Paul 
2808f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2809f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2810f41ac2beSBill Paul 
28115b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
28125b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
28135b610048SPyun YongHyeon 
28143f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2815f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2816f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2817f41ac2beSBill Paul }
2818f41ac2beSBill Paul 
2819f41ac2beSBill Paul static int
28205b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
28215b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
28225b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2823f41ac2beSBill Paul {
28243f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
28255b610048SPyun YongHyeon 	int error;
2826f41ac2beSBill Paul 
28275b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2828fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
28295b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
28305b610048SPyun YongHyeon 	if (error != 0) {
28315b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28325b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
28335b610048SPyun YongHyeon 		return (ENOMEM);
28345b610048SPyun YongHyeon 	}
28355b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
28365b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
28375b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
28385b610048SPyun YongHyeon 	if (error != 0) {
28395b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28405b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
28415b610048SPyun YongHyeon 		return (ENOMEM);
28425b610048SPyun YongHyeon 	}
28435b610048SPyun YongHyeon 	/* Load the address of the ring. */
28445b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
28455b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
28465b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
28475b610048SPyun YongHyeon 	if (error != 0) {
28485b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28495b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
28505b610048SPyun YongHyeon 		return (ENOMEM);
28515b610048SPyun YongHyeon 	}
28525b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
28535b610048SPyun YongHyeon 	return (0);
28545b610048SPyun YongHyeon }
28555b610048SPyun YongHyeon 
28565b610048SPyun YongHyeon static int
28575b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
28585b610048SPyun YongHyeon {
28595b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2860fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
28615b610048SPyun YongHyeon 	int i, error;
2862f41ac2beSBill Paul 
2863f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2864f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2865f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2866f41ac2beSBill Paul 	/*
2867f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2868f41ac2beSBill Paul 	 */
28694eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2870f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
28714eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
28724eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2873e65bed95SPyun YongHyeon 	if (error != 0) {
2874fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2875fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2876e65bed95SPyun YongHyeon 		return (ENOMEM);
2877e65bed95SPyun YongHyeon 	}
2878e65bed95SPyun YongHyeon 
28795b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
28805b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
28815b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
28825b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
28835b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
28845b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
28855b610048SPyun YongHyeon 	if (error)
28865b610048SPyun YongHyeon 		return (error);
28875b610048SPyun YongHyeon 
28885b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
28895b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
28905b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
28915b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
28925b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
28935b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
28945b610048SPyun YongHyeon 	if (error)
28955b610048SPyun YongHyeon 		return (error);
28965b610048SPyun YongHyeon 
28975b610048SPyun YongHyeon 	/* Create tag for TX ring. */
28985b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
28995b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
29005b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
29015b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
29025b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
29035b610048SPyun YongHyeon 	if (error)
29045b610048SPyun YongHyeon 		return (error);
29055b610048SPyun YongHyeon 
2906f41ac2beSBill Paul 	/*
29075b610048SPyun YongHyeon 	 * Create tag for status block.
29085b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
29095b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
29105b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
29115b610048SPyun YongHyeon 	 * of configured number of ring.
2912f41ac2beSBill Paul 	 */
29135b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
29145b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
29155b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
29165b610048SPyun YongHyeon 	else
29175b610048SPyun YongHyeon 		sbsz = 32;
29185b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
29195b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
29205b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
29215b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
29225b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
29235b610048SPyun YongHyeon 	if (error)
29245b610048SPyun YongHyeon 		return (error);
29255b610048SPyun YongHyeon 
292612c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
292712c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
292812c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
292912c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
293012c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
293112c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
293212c65daeSPyun YongHyeon 	if (error)
293312c65daeSPyun YongHyeon 		return (error);
293412c65daeSPyun YongHyeon 
29355b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
29365b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
29375b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
29385b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
29395b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
29405b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
29415b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
29425b610048SPyun YongHyeon 		if (error)
29435b610048SPyun YongHyeon 			return (error);
29445b610048SPyun YongHyeon 	}
29455b610048SPyun YongHyeon 
29465b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
2947d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
2948d2ffe15aSPyun YongHyeon 		/*
2949d2ffe15aSPyun YongHyeon 		 * XXX
2950d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
2951d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
2952062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
2953062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
2954d2ffe15aSPyun YongHyeon 		 */
2955062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
2956d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
2957d2ffe15aSPyun YongHyeon 	}
2958fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
2959fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
2960fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
2961fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
29625b610048SPyun YongHyeon 	if (error != 0) {
29635b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29645b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
29655b610048SPyun YongHyeon 		return (ENOMEM);
29665b610048SPyun YongHyeon 	}
29675b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
29681108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2969ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
2970ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
2971ca3f1187SPyun YongHyeon 	} else {
2972ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
2973ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
2974ca3f1187SPyun YongHyeon 	}
29755b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
2976ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2977ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
2978ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
2979f41ac2beSBill Paul 
2980f41ac2beSBill Paul 	if (error) {
29810ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
29820ac56796SPyun YongHyeon 		return (ENOMEM);
29830ac56796SPyun YongHyeon 	}
29840ac56796SPyun YongHyeon 
29855b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
2986f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
2987f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
2988f5459d4cSPyun YongHyeon 	else
2989f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
29905b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
2991f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
2992f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
29930ac56796SPyun YongHyeon 
29940ac56796SPyun YongHyeon 	if (error) {
29950ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
2996f41ac2beSBill Paul 		return (ENOMEM);
2997f41ac2beSBill Paul 	}
2998f41ac2beSBill Paul 
29993f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3000943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3001943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3002943787f3SPyun YongHyeon 	if (error) {
3003943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3004943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3005943787f3SPyun YongHyeon 		return (ENOMEM);
3006943787f3SPyun YongHyeon 	}
3007f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
30080ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3009f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3010f41ac2beSBill Paul 		if (error) {
3011fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3012fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3013f41ac2beSBill Paul 			return (ENOMEM);
3014f41ac2beSBill Paul 		}
3015f41ac2beSBill Paul 	}
3016f41ac2beSBill Paul 
30173f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3018f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
30190ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3020f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3021f41ac2beSBill Paul 		if (error) {
3022fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30230ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3024f41ac2beSBill Paul 			return (ENOMEM);
3025f41ac2beSBill Paul 		}
3026f41ac2beSBill Paul 	}
3027f41ac2beSBill Paul 
30285b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
30294c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30305b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
30318a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
30321be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
30331be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3034f41ac2beSBill Paul 		if (error) {
3035fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30363f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3037f41ac2beSBill Paul 			return (ENOMEM);
3038f41ac2beSBill Paul 		}
30393f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3040943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3041943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3042943787f3SPyun YongHyeon 		if (error) {
3043943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
30441b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3045943787f3SPyun YongHyeon 			return (ENOMEM);
3046943787f3SPyun YongHyeon 		}
3047f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3048f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3049f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3050f41ac2beSBill Paul 			if (error) {
3051fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
30523f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3053f41ac2beSBill Paul 				return (ENOMEM);
3054f41ac2beSBill Paul 			}
3055f41ac2beSBill Paul 		}
3056f41ac2beSBill Paul 	}
3057f41ac2beSBill Paul 
3058f41ac2beSBill Paul 	return (0);
3059f41ac2beSBill Paul }
3060f41ac2beSBill Paul 
3061bf6ef57aSJohn Polstra /*
3062bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3063bf6ef57aSJohn Polstra  */
3064bf6ef57aSJohn Polstra static int
3065bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3066bf6ef57aSJohn Polstra {
3067bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
306855aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3069bf6ef57aSJohn Polstra 
307055aaf894SMarius Strobl 	d = pci_get_domain(dev);
3071bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3072bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3073bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3074bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
307555aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3076bf6ef57aSJohn Polstra 			return (1);
3077bf6ef57aSJohn Polstra 	return (0);
3078bf6ef57aSJohn Polstra }
3079bf6ef57aSJohn Polstra 
3080bf6ef57aSJohn Polstra /*
3081bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3082bf6ef57aSJohn Polstra  */
3083bf6ef57aSJohn Polstra static int
3084bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3085bf6ef57aSJohn Polstra {
3086bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3087bf6ef57aSJohn Polstra 
3088d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
30895c952e8dSPyun YongHyeon 		return (0);
30905c952e8dSPyun YongHyeon 
30911108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
30921108273aSPyun YongHyeon #ifdef DEVICE_POLLING
30931108273aSPyun YongHyeon 	return (0);
30941108273aSPyun YongHyeon #endif
3095bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3096a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3097bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3098bf6ef57aSJohn Polstra 		/*
3099a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3100a8376f70SMarius Strobl 		 * configured in single-port mode.
3101bf6ef57aSJohn Polstra 		 */
3102bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3103bf6ef57aSJohn Polstra 			can_use_msi = 1;
3104bf6ef57aSJohn Polstra 		break;
3105bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3106bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3107bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3108bf6ef57aSJohn Polstra 			can_use_msi = 1;
3109bf6ef57aSJohn Polstra 		break;
3110a8376f70SMarius Strobl 	default:
3111a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3112bf6ef57aSJohn Polstra 			can_use_msi = 1;
3113bf6ef57aSJohn Polstra 	}
3114bf6ef57aSJohn Polstra 	return (can_use_msi);
3115bf6ef57aSJohn Polstra }
3116bf6ef57aSJohn Polstra 
311795d67482SBill Paul static int
3118062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3119062af0b0SPyun YongHyeon {
3120062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3121062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3122062af0b0SPyun YongHyeon 		const uint16_t vendor;
3123062af0b0SPyun YongHyeon 		const uint16_t device;
3124062af0b0SPyun YongHyeon 		const char *desc;
3125062af0b0SPyun YongHyeon 	} const mbox_reorder_lists[] = {
3126062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3127062af0b0SPyun YongHyeon 	};
3128062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3129062af0b0SPyun YongHyeon 	device_t bus, dev;
313047f4a4dcSMarius Strobl 	int i;
3131062af0b0SPyun YongHyeon 
3132062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3133062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3134062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3135062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3136062af0b0SPyun YongHyeon 	for (;;) {
3137062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3138062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3139062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3140062af0b0SPyun YongHyeon 			break;
314147f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3142062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3143062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3144062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3145062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3146062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3147062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3148062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3149062af0b0SPyun YongHyeon 				return (1);
3150062af0b0SPyun YongHyeon 			}
3151062af0b0SPyun YongHyeon 		}
3152062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
3153062af0b0SPyun YongHyeon 			break;
3154062af0b0SPyun YongHyeon 	}
3155062af0b0SPyun YongHyeon 	return (0);
3156062af0b0SPyun YongHyeon }
3157062af0b0SPyun YongHyeon 
3158ea9c3a30SPyun YongHyeon static void
3159ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3160ea9c3a30SPyun YongHyeon {
3161ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3162ea9c3a30SPyun YongHyeon 
3163ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3164ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3165ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3166ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3167ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3168ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3169ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3170ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3171ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3172ea9c3a30SPyun YongHyeon 			clk = 133;
3173ea9c3a30SPyun YongHyeon 		else {
3174ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3175ea9c3a30SPyun YongHyeon 			switch (clk) {
3176ea9c3a30SPyun YongHyeon 			case 0:
3177ea9c3a30SPyun YongHyeon 				clk = 33;
3178ea9c3a30SPyun YongHyeon 				break;
3179ea9c3a30SPyun YongHyeon 			case 2:
3180ea9c3a30SPyun YongHyeon 				clk = 50;
3181ea9c3a30SPyun YongHyeon 				break;
3182ea9c3a30SPyun YongHyeon 			case 4:
3183ea9c3a30SPyun YongHyeon 				clk = 66;
3184ea9c3a30SPyun YongHyeon 				break;
3185ea9c3a30SPyun YongHyeon 			case 6:
3186ea9c3a30SPyun YongHyeon 				clk = 100;
3187ea9c3a30SPyun YongHyeon 				break;
3188ea9c3a30SPyun YongHyeon 			case 7:
3189ea9c3a30SPyun YongHyeon 				clk = 133;
3190ea9c3a30SPyun YongHyeon 				break;
3191ea9c3a30SPyun YongHyeon 			}
3192ea9c3a30SPyun YongHyeon 		}
3193ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3194ea9c3a30SPyun YongHyeon 	} else {
3195ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3196ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3197ea9c3a30SPyun YongHyeon 		else
3198ea9c3a30SPyun YongHyeon 			printf("PCI ");
3199ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3200ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3201ea9c3a30SPyun YongHyeon 			clk = 66;
3202ea9c3a30SPyun YongHyeon 		else
3203ea9c3a30SPyun YongHyeon 			clk = 33;
3204ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3205ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3206ea9c3a30SPyun YongHyeon 		else
3207ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3208ea9c3a30SPyun YongHyeon 	}
3209ea9c3a30SPyun YongHyeon }
3210ea9c3a30SPyun YongHyeon 
3211062af0b0SPyun YongHyeon static int
32123f74909aSGleb Smirnoff bge_attach(device_t dev)
321395d67482SBill Paul {
321495d67482SBill Paul 	struct ifnet *ifp;
321595d67482SBill Paul 	struct bge_softc *sc;
3216548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
321708013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3218daeeb75cSPyun YongHyeon 	int capmask, error, msicount, reg, rid, trys;
321995d67482SBill Paul 
322095d67482SBill Paul 	sc = device_get_softc(dev);
322195d67482SBill Paul 	sc->bge_dev = dev;
322295d67482SBill Paul 
3223e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3224dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3225e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3226dfe0df9aSPyun YongHyeon 
322795d67482SBill Paul 	/*
322895d67482SBill Paul 	 * Map control/status registers.
322995d67482SBill Paul 	 */
323095d67482SBill Paul 	pci_enable_busmaster(dev);
323195d67482SBill Paul 
3232736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
32335f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
323444f8f2fcSMarius Strobl 	    RF_ACTIVE);
323595d67482SBill Paul 
323695d67482SBill Paul 	if (sc->bge_res == NULL) {
3237548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
323895d67482SBill Paul 		error = ENXIO;
323995d67482SBill Paul 		goto fail;
324095d67482SBill Paul 	}
324195d67482SBill Paul 
32424f09c4c7SMarius Strobl 	/* Save various chip information. */
3243548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3244e53d81eeSPaul Saab 	sc->bge_chipid =
3245a5779553SStanislav Sedov 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
3246a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
32471108273aSPyun YongHyeon 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG) {
32481108273aSPyun YongHyeon 		/*
32491108273aSPyun YongHyeon 		 * Find the ASCI revision.  Different chips use different
32501108273aSPyun YongHyeon 		 * registers.
32511108273aSPyun YongHyeon 		 */
32521108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
32531108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
32541108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
3255bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
325650515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
32571108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
32581108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
32591108273aSPyun YongHyeon 			break;
3260b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
3261b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
3262b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
3263b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
3264b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
3265b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
3266b4a256acSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
3267b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
3268b4a256acSPyun YongHyeon 			break;
32691108273aSPyun YongHyeon 		default:
32701108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
32711108273aSPyun YongHyeon 			    BGE_PCI_PRODID_ASICREV, 4);
32721108273aSPyun YongHyeon 		}
32731108273aSPyun YongHyeon 	}
3274e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3275e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3276e53d81eeSPaul Saab 
3277a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3278daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
32791108273aSPyun YongHyeon 	 /*
32801108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
32811108273aSPyun YongHyeon 	  *
32821108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
32831108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
32841108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
32851108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
32861108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3287bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
328850515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
32891108273aSPyun YongHyeon 	  *
3290548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3291548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3292548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3293548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3294548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3295548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3296548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3297548c8f1aSPyun YongHyeon 	  *
32981108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
32991108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
33001108273aSPyun YongHyeon 	  */
3301bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
330250515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
330350515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3304548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
33051108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
33061108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3307daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33081108273aSPyun YongHyeon 			else
3309daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3310bbe2ca75SPyun YongHyeon 		} else {
33111108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33121108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3313daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33141108273aSPyun YongHyeon 			else
3315daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
33161108273aSPyun YongHyeon 		}
33171108273aSPyun YongHyeon 	}
3318a813ed78SPyun YongHyeon 
33195fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
33205fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
332108013fd3SMarius Strobl 
33220dae9719SJung-uk Kim 	/* Save chipset family. */
33230dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
33241108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3325bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
332650515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3327b4a256acSPyun YongHyeon 	case BGE_ASICREV_BCM57765:
33281108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
33291108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3330b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
3331bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3332bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3333bbe2ca75SPyun YongHyeon 			/* Jumbo frame on BCM5719 A0 does not work. */
3334463a7e27SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_JUMBO;
3335bbe2ca75SPyun YongHyeon 		}
33361108273aSPyun YongHyeon 		break;
3337a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3338a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3339a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3340a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3341a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3342a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3343a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3344a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3345a5779553SStanislav Sedov 		break;
33460dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
33470dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
33480dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
33490dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
33507ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
33510dae9719SJung-uk Kim 		break;
33520dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
33530dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
33540dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3355f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
33569fe569d8SXin LI 		/* FALLTHROUGH */
33570dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
33580dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
335938cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
33600dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
33619fe569d8SXin LI 		/* FALLTHROUGH */
33620dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
33630dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
33640dae9719SJung-uk Kim 		break;
33650dae9719SJung-uk Kim 	}
33660dae9719SJung-uk Kim 
3367548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3368548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3369548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3370548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3371548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3372548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
3373548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3374548c8f1aSPyun YongHyeon 		break;
3375548c8f1aSPyun YongHyeon 	}
3376548c8f1aSPyun YongHyeon 
3377548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3378548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3379548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3380548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3381548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3382548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3383548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3384548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3385548c8f1aSPyun YongHyeon 			error = ENXIO;
3386548c8f1aSPyun YongHyeon 			goto fail;
3387548c8f1aSPyun YongHyeon 		}
3388548c8f1aSPyun YongHyeon 
3389548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3390548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3391548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3392548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3393548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3394548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3395548c8f1aSPyun YongHyeon 
3396548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3397548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3398548c8f1aSPyun YongHyeon 	}
3399548c8f1aSPyun YongHyeon 
3400749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3401749a5269SMarius Strobl 	bge_add_sysctls(sc);
3402749a5269SMarius Strobl 
3403a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
34041108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
34051108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3406a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3407a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3408a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3409a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3410a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3411a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3412a813ed78SPyun YongHyeon 	else
3413a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
34147ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
34157ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
34167ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3417a813ed78SPyun YongHyeon 
3418f681b29aSPyun YongHyeon 	/*
3419d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3420f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3421f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3422f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3423f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3424f681b29aSPyun YongHyeon 	 */
3425f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
34264f0794ffSBjoern A. Zeeb 
3427d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3428d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3429d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3430d9820cd8SPyun YongHyeon 
3431a7fcfcf3SPyun YongHyeon 	/*
3432a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3433a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3434a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3435a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3436a7fcfcf3SPyun YongHyeon 	 */
3437a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3438a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3439a7fcfcf3SPyun YongHyeon 
3440ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3441fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
34424f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
34434f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
34444f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
344584ac96f8SPyun YongHyeon 	}
34464f0794ffSBjoern A. Zeeb 
3447fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3448fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3449fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3450fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3451fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3452fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3453fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3454fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3455fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3456fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3457fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3458fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3459fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3460fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3461fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3462fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3463*d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3464fb772a6cSMarius Strobl 	}
3465fb772a6cSMarius Strobl 
3466e53d81eeSPaul Saab 	/*
3467ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3468ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3469ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3470ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3471ca3f1187SPyun YongHyeon 	 * known bug which can't handle TSO if ethernet header + IP/TCP
3472ca3f1187SPyun YongHyeon 	 * header is greater than 80 bytes. The workaround for the TSO
3473ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3474ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3475ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3476ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3477ca3f1187SPyun YongHyeon 	 */
34781108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
34791108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
34801108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3481bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3482bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3483bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3484bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3485bbe2ca75SPyun YongHyeon 		}
34861108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
34874f4a16e1SPyun YongHyeon 		/*
34884f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
34894f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3490be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
34914f4a16e1SPyun YongHyeon 		 */
34924f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3493be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3494be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3495ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
34964f4a16e1SPyun YongHyeon 	}
3497ca3f1187SPyun YongHyeon 
3498ca3f1187SPyun YongHyeon 	/*
34996f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3500e53d81eeSPaul Saab 	 */
35013b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
35024c0da0ffSGleb Smirnoff 		/*
35036f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
35046f8718a3SScott Long 		 * must be a PCI Express device.
35056f8718a3SScott Long 		 */
35066f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
35070aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
350848630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
350948630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
351048630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
351148630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
351250515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
351350515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
351448630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
351548630d79SPyun YongHyeon 		else
351648630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
351748630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
35186f8718a3SScott Long 	} else {
35196f8718a3SScott Long 		/*
35206f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
35216f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
35224c0da0ffSGleb Smirnoff 		 */
35233b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
35240aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
352590447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
35264c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3527652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
35286f8718a3SScott Long 	}
35294c0da0ffSGleb Smirnoff 
3530bf6ef57aSJohn Polstra 	/*
3531fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3532fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3533fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3534fd4d32feSPyun YongHyeon 	 */
3535fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3536fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3537fd4d32feSPyun YongHyeon 	/*
3538062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3539062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3540062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3541062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3542062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3543062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3544062af0b0SPyun YongHyeon 	 */
3545062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3546062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3547062af0b0SPyun YongHyeon 	/*
3548bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3549bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3550bf6ef57aSJohn Polstra 	 * normal operation.
3551bf6ef57aSJohn Polstra 	 */
35520aaf1057SPyun YongHyeon 	rid = 0;
35533b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
35540aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3555bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
3556bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
3557bf6ef57aSJohn Polstra 			if (msicount > 1)
3558bf6ef57aSJohn Polstra 				msicount = 1;
3559bf6ef57aSJohn Polstra 		} else
3560bf6ef57aSJohn Polstra 			msicount = 0;
3561bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
3562bf6ef57aSJohn Polstra 			rid = 1;
3563bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
35640aaf1057SPyun YongHyeon 		}
35650aaf1057SPyun YongHyeon 	}
3566bf6ef57aSJohn Polstra 
35671108273aSPyun YongHyeon 	/*
35681108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
35691108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
35701108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
35711108273aSPyun YongHyeon 	 */
35721108273aSPyun YongHyeon #ifndef DEVICE_POLLING
35731108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
35741108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
35751108273aSPyun YongHyeon #endif
35761108273aSPyun YongHyeon 
3577bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3578bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
3579bf6ef57aSJohn Polstra 
3580bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3581bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3582bf6ef57aSJohn Polstra 		error = ENXIO;
3583bf6ef57aSJohn Polstra 		goto fail;
3584bf6ef57aSJohn Polstra 	}
3585bf6ef57aSJohn Polstra 
3586ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
35874f09c4c7SMarius Strobl 
35888cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3589548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3590548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3591888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3592888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3593548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3594548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
35958cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
35968cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3597d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
35988cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
35998cb1383cSDoug Ambrisko 			}
36008cb1383cSDoug Ambrisko 		}
3601548c8f1aSPyun YongHyeon 	}
36028cb1383cSDoug Ambrisko 
36038cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
3604548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_START);
36058cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
36068cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
36078cb1383cSDoug Ambrisko 		error = ENXIO;
36088cb1383cSDoug Ambrisko 		goto fail;
36098cb1383cSDoug Ambrisko 	}
36108cb1383cSDoug Ambrisko 
3611548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_START);
3612548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_START);
361395d67482SBill Paul 
361495d67482SBill Paul 	if (bge_chipinit(sc)) {
3615fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
361695d67482SBill Paul 		error = ENXIO;
361795d67482SBill Paul 		goto fail;
361895d67482SBill Paul 	}
361995d67482SBill Paul 
362038cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
362138cc658fSJohn Baldwin 	if (error) {
362208013fd3SMarius Strobl 		device_printf(sc->bge_dev,
362308013fd3SMarius Strobl 		    "failed to read station address\n");
362495d67482SBill Paul 		error = ENXIO;
362595d67482SBill Paul 		goto fail;
362695d67482SBill Paul 	}
362795d67482SBill Paul 
3628f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
36291108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
36301108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
36311108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3632f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3633f41ac2beSBill Paul 	else
3634f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3635f41ac2beSBill Paul 
36365b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3637fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3638fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3639f41ac2beSBill Paul 		error = ENXIO;
3640f41ac2beSBill Paul 		goto fail;
3641f41ac2beSBill Paul 	}
3642f41ac2beSBill Paul 
364395d67482SBill Paul 	/* Set default tuneable values. */
364495d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
364595d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
364695d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
36476f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
36486f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
364995d67482SBill Paul 
365035f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
365135f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
365235f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
365335f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
365435f945cdSPyun YongHyeon 
365595d67482SBill Paul 	/* Set up ifnet structure */
3656fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3657fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3658fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3659fc74a9f9SBrooks Davis 		error = ENXIO;
3660fc74a9f9SBrooks Davis 		goto fail;
3661fc74a9f9SBrooks Davis 	}
366295d67482SBill Paul 	ifp->if_softc = sc;
36639bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
366495d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
366595d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
366695d67482SBill Paul 	ifp->if_start = bge_start;
366795d67482SBill Paul 	ifp->if_init = bge_init;
36684d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
36694d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
36704d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
367135f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
3672d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
36734e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
36741108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3675ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
367604bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
3677ca3f1187SPyun YongHyeon 	}
36784e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
36794e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
36804e35d186SJung-uk Kim #endif
368195d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
368275719184SGleb Smirnoff #ifdef DEVICE_POLLING
368375719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
368475719184SGleb Smirnoff #endif
368595d67482SBill Paul 
3686a1d52896SBill Paul 	/*
3687d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3688d375e524SGleb Smirnoff 	 * to hardware bugs.
3689d375e524SGleb Smirnoff 	 */
3690d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3691d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
36924d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
3693d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
3694d375e524SGleb Smirnoff 	}
3695d375e524SGleb Smirnoff 
3696d375e524SGleb Smirnoff 	/*
3697a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
369841abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
369941abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
370041abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
370141abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
370241abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
370341abcc1bSPaul Saab 	 * SK-9D41.
3704a1d52896SBill Paul 	 */
3705888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3706888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
37075fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
37085fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3709f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3710f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3711fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3712f6789fbaSPyun YongHyeon 			error = ENXIO;
3713f6789fbaSPyun YongHyeon 			goto fail;
3714f6789fbaSPyun YongHyeon 		}
371541abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
371641abcc1bSPaul Saab 	}
371741abcc1bSPaul Saab 
371895d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3719ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3720ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
372170c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3722ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
372370c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
372470c2071bSPyun YongHyeon 		} else
3725652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3726ea3b4127SPyun YongHyeon 	}
372795d67482SBill Paul 
372870c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
372970c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
373070c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
373170c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
373270c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
373370c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
373470c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
373570c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
373670c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
373770c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
373870c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
373970c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
374070c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
374170c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
374270c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5719 &&
374370c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5720 &&
374470c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
374570c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57765 &&
374670c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780) {
374770c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
374870c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
374970c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
375070c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
375170c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
375270c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
375370c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
375470c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
375570c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
375670c2071bSPyun YongHyeon 		} else
375770c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
375870c2071bSPyun YongHyeon 	}
375970c2071bSPyun YongHyeon 
376070c2071bSPyun YongHyeon 	/*
3761*d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
376270c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
376370c2071bSPyun YongHyeon 	 */
376470c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
376570c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
376670c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3767*d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
376870c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
376970c2071bSPyun YongHyeon 
3770652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
37710c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
37720c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
37730c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
37746098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
37756098821cSJung-uk Kim 		    0, NULL);
377695d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
377795d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3778da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
377995d67482SBill Paul 	} else {
378095d67482SBill Paul 		/*
37818cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
37828cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
37838cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
37848cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
37858cb1383cSDoug Ambrisko 		 * the PHY.
378695d67482SBill Paul 		 */
37874012d104SMarius Strobl 		trys = 0;
37888cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
37898cb1383cSDoug Ambrisko again:
37908cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
37918cb1383cSDoug Ambrisko 
3792fb772a6cSMarius Strobl 		error = mii_attach(dev, &sc->bge_miibus, ifp, bge_ifmedia_upd,
3793daeeb75cSPyun YongHyeon 		    bge_ifmedia_sts, capmask, sc->bge_phy_addr, MII_OFFSET_ANY,
3794fb772a6cSMarius Strobl 		    MIIF_DOPAUSE);
37958e5d93dbSMarius Strobl 		if (error != 0) {
37968cb1383cSDoug Ambrisko 			if (trys++ < 4) {
37978cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3798daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3799daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
38008cb1383cSDoug Ambrisko 				goto again;
38018cb1383cSDoug Ambrisko 			}
38028e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
380395d67482SBill Paul 			goto fail;
380495d67482SBill Paul 		}
38058cb1383cSDoug Ambrisko 
38068cb1383cSDoug Ambrisko 		/*
38078cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
38088cb1383cSDoug Ambrisko 		 */
38098cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
38108cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
381195d67482SBill Paul 	}
381295d67482SBill Paul 
381395d67482SBill Paul 	/*
3814e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3815e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3816e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3817e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3818e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3819e255b776SJohn Polstra 	 * payloads by copying the received packets.
3820e255b776SJohn Polstra 	 */
3821652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3822652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3823652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3824e255b776SJohn Polstra 
3825e255b776SJohn Polstra 	/*
382695d67482SBill Paul 	 * Call MI attach routine.
382795d67482SBill Paul 	 */
3828fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
38290f9bd73bSSam Leffler 
383061ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
383161ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
383261ccb9daSPyun YongHyeon 
38330f9bd73bSSam Leffler 	/*
38340f9bd73bSSam Leffler 	 * Hookup IRQ last.
38350f9bd73bSSam Leffler 	 */
3836dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3837dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
38387e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
38397e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3840dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3841dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3842dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3843dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3844dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3845e010b055SPyun YongHyeon 			error = ENOMEM;
3846dfe0df9aSPyun YongHyeon 			goto fail;
3847dfe0df9aSPyun YongHyeon 		}
3848dfe0df9aSPyun YongHyeon 		taskqueue_start_threads(&sc->bge_tq, 1, PI_NET, "%s taskq",
3849dfe0df9aSPyun YongHyeon 		    device_get_nameunit(sc->bge_dev));
3850dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3851dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3852dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3853dfe0df9aSPyun YongHyeon 	} else
3854dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3855dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3856dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
38570f9bd73bSSam Leffler 
38580f9bd73bSSam Leffler 	if (error) {
3859e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3860fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
38610f9bd73bSSam Leffler 	}
386295d67482SBill Paul 
386395d67482SBill Paul fail:
3864e010b055SPyun YongHyeon 	if (error)
3865e010b055SPyun YongHyeon 		bge_detach(dev);
386695d67482SBill Paul 	return (error);
386795d67482SBill Paul }
386895d67482SBill Paul 
386995d67482SBill Paul static int
38703f74909aSGleb Smirnoff bge_detach(device_t dev)
387195d67482SBill Paul {
387295d67482SBill Paul 	struct bge_softc *sc;
387395d67482SBill Paul 	struct ifnet *ifp;
387495d67482SBill Paul 
387595d67482SBill Paul 	sc = device_get_softc(dev);
3876fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
387795d67482SBill Paul 
387875719184SGleb Smirnoff #ifdef DEVICE_POLLING
387975719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
388075719184SGleb Smirnoff 		ether_poll_deregister(ifp);
388175719184SGleb Smirnoff #endif
388275719184SGleb Smirnoff 
3883e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3884e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
38850f9bd73bSSam Leffler 		BGE_LOCK(sc);
388695d67482SBill Paul 		bge_stop(sc);
38870f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
38885dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3889e010b055SPyun YongHyeon 	}
38905dda8085SOleg Bulyzhin 
3891dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3892dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
389395d67482SBill Paul 
3894652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
389595d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
389695d67482SBill Paul 	} else {
389795d67482SBill Paul 		bus_generic_detach(dev);
389895d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
389995d67482SBill Paul 	}
390095d67482SBill Paul 
390195d67482SBill Paul 	bge_release_resources(sc);
390295d67482SBill Paul 
390395d67482SBill Paul 	return (0);
390495d67482SBill Paul }
390595d67482SBill Paul 
390695d67482SBill Paul static void
39073f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
390895d67482SBill Paul {
390995d67482SBill Paul 	device_t dev;
391095d67482SBill Paul 
391195d67482SBill Paul 	dev = sc->bge_dev;
391295d67482SBill Paul 
3913dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3914dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3915dfe0df9aSPyun YongHyeon 
391695d67482SBill Paul 	if (sc->bge_intrhand != NULL)
391795d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
391895d67482SBill Paul 
391995d67482SBill Paul 	if (sc->bge_irq != NULL)
3920724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3921724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3922724bd939SJohn Polstra 
3923724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3924724bd939SJohn Polstra 		pci_release_msi(dev);
392595d67482SBill Paul 
392695d67482SBill Paul 	if (sc->bge_res != NULL)
392795d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3928736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
392995d67482SBill Paul 
3930548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
3931548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
3932548c8f1aSPyun YongHyeon 		    PCIR_BAR(2), sc->bge_res2);
3933548c8f1aSPyun YongHyeon 
3934ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3935ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3936ad61f896SRuslan Ermilov 
3937f41ac2beSBill Paul 	bge_dma_free(sc);
393895d67482SBill Paul 
39390f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
39400f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
394195d67482SBill Paul }
394295d67482SBill Paul 
39438cb1383cSDoug Ambrisko static int
39443f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
394595d67482SBill Paul {
394695d67482SBill Paul 	device_t dev;
3947cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
39486f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
39490aaf1057SPyun YongHyeon 	uint16_t devctl;
39505fea260fSMarius Strobl 	int i;
395195d67482SBill Paul 
395295d67482SBill Paul 	dev = sc->bge_dev;
395395d67482SBill Paul 
3954cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
3955548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
3956548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
3957cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
3958cc085b36SPyun YongHyeon 
395938cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
396038cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
39616f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
39626f8718a3SScott Long 			write_op = bge_writemem_direct;
39636f8718a3SScott Long 		else
39646f8718a3SScott Long 			write_op = bge_writemem_ind;
39659ba784dbSScott Long 	} else
39666f8718a3SScott Long 		write_op = bge_writereg_ind;
39676f8718a3SScott Long 
3968548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
3969548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
3970548c8f1aSPyun YongHyeon 
397195d67482SBill Paul 	/* Save some important PCI state. */
397295d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
397395d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
397495d67482SBill Paul 
397595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
397695d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3977e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
397895d67482SBill Paul 
39796f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
39806f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
3981a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
39826f8718a3SScott Long 		if (bootverbose)
3983333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
39846f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
39856f8718a3SScott Long 	}
39866f8718a3SScott Long 
39876f8718a3SScott Long 	/*
39886f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
39896f8718a3SScott Long 	 * When firmware finishes its initialization it will
3990888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
39916f8718a3SScott Long 	 */
3992888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
39936f8718a3SScott Long 
39940c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
3995e53d81eeSPaul Saab 
3996e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3997652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
3998ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3999ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
40000c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
40010c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4002ad49eccfSPyun YongHyeon 		}
4003e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4004e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
40050c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
40060c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4007e53d81eeSPaul Saab 		}
4008e53d81eeSPaul Saab 	}
4009e53d81eeSPaul Saab 
4010df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4011df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4012df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4013df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4014df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4015df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4016df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4017df4db538SPyun YongHyeon 	}
4018df4db538SPyun YongHyeon 
401921c9e407SDavid Christensen 	/*
40206f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
40216f8718a3SScott Long 	 * powered up in D0 uninitialized.
40226f8718a3SScott Long 	 */
40235512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
40245512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4025caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
40266f8718a3SScott Long 
402795d67482SBill Paul 	/* Issue global reset */
40286f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
402995d67482SBill Paul 
4030cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4031cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4032cc085b36SPyun YongHyeon 	else
403395d67482SBill Paul 		DELAY(1000);
403495d67482SBill Paul 
4035e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4036652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4037e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4038e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
40395fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
40405fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4041e53d81eeSPaul Saab 		}
40420aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4043389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
40440aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4045389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4046389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4047389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
40480aaf1057SPyun YongHyeon 		    devctl, 2);
404948630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
40500aaf1057SPyun YongHyeon 		/* Clear error status. */
4051389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4052389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4053389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4054389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4055e53d81eeSPaul Saab 	}
4056e53d81eeSPaul Saab 
40573f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
405895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
405995d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4060e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4061cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4062cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4063cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4064cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4065548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4066548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4067548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4068548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4069cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
407095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
407195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4072cbb2b2feSPyun YongHyeon 	/*
4073cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4074fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4075cbb2b2feSPyun YongHyeon 	 * read stale status block.
4076cbb2b2feSPyun YongHyeon 	 */
4077cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4078cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4079cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4080cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4081cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4082cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4083cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4084cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4085cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4086cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4087cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4088cbb2b2feSPyun YongHyeon 		}
4089cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4090cbb2b2feSPyun YongHyeon 		    devctl, 2);
4091cbb2b2feSPyun YongHyeon 	}
409222a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
40934c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4094bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4095bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
40960aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
40970aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
40980aaf1057SPyun YongHyeon 			pci_write_config(dev,
40990aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4100bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4101bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4102bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4103bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4104bf6ef57aSJohn Polstra 		}
41054c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
41064c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
41074c0da0ffSGleb Smirnoff 	} else
4108a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4109a7b0c314SPaul Saab 
4110cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4111cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4112cc085b36SPyun YongHyeon 
4113cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4114cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4115cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4116cc085b36SPyun YongHyeon 	DELAY(40);
4117cc085b36SPyun YongHyeon 
4118548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4119548c8f1aSPyun YongHyeon 
412038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
412138cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
412238cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
412338cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
412438cc658fSJohn Baldwin 				break;
412538cc658fSJohn Baldwin 			DELAY(100);
412638cc658fSJohn Baldwin 		}
412738cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4128333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
412938cc658fSJohn Baldwin 			return (1);
413038cc658fSJohn Baldwin 		}
413138cc658fSJohn Baldwin 	} else {
413295d67482SBill Paul 		/*
41336f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
413408013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
41355fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
41365fea260fSMarius Strobl 		 * address is fitted though.
413795d67482SBill Paul 		 */
413895d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4139d5d23857SJung-uk Kim 			DELAY(10);
4140888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4141888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
414295d67482SBill Paul 				break;
414395d67482SBill Paul 		}
414495d67482SBill Paul 
41455fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4146333704a3SPyun YongHyeon 			device_printf(dev,
4147333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4148333704a3SPyun YongHyeon 			    val);
4149b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4150b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4151b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
415238cc658fSJohn Baldwin 	}
415395d67482SBill Paul 
415495d67482SBill Paul 	/*
4155da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4156da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4157da3003f0SBill Paul 	 * to 1.2V.
4158da3003f0SBill Paul 	 */
4159652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4160652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
41615fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
41625fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
41635fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4164da3003f0SBill Paul 	}
4165da3003f0SBill Paul 
4166e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4167652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4168b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4169a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4170a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4171a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
41725fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
41735fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4174e53d81eeSPaul Saab 	}
41758cb1383cSDoug Ambrisko 
417650515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
417750515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
417850515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
417950515680SPyun YongHyeon 
41808cb1383cSDoug Ambrisko 	return (0);
418195d67482SBill Paul }
418295d67482SBill Paul 
4183e0b7b101SPyun YongHyeon static __inline void
4184e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4185e0b7b101SPyun YongHyeon {
4186e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4187e0b7b101SPyun YongHyeon 
4188e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4189e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4190e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4191e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4192e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4193e0b7b101SPyun YongHyeon }
4194e0b7b101SPyun YongHyeon 
4195e0b7b101SPyun YongHyeon static __inline void
4196e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4197e0b7b101SPyun YongHyeon {
4198e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4199e0b7b101SPyun YongHyeon 
4200e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4201e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4202e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4203e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4204e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4205e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4206e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4207e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4208e0b7b101SPyun YongHyeon }
4209e0b7b101SPyun YongHyeon 
421095d67482SBill Paul /*
421195d67482SBill Paul  * Frame reception handling. This is called if there's a frame
421295d67482SBill Paul  * on the receive return list.
421395d67482SBill Paul  *
421495d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
42151be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
421695d67482SBill Paul  * 2) the frame is from the standard receive ring
421795d67482SBill Paul  */
421895d67482SBill Paul 
42191abcdbd1SAttilio Rao static int
4220dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
422195d67482SBill Paul {
422295d67482SBill Paul 	struct ifnet *ifp;
42231abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4224b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
422595d67482SBill Paul 
42267f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
42270f9bd73bSSam Leffler 
42283f74909aSGleb Smirnoff 	/* Nothing to do. */
42297f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
42301abcdbd1SAttilio Rao 		return (rx_npkts);
4231cfcb5025SOleg Bulyzhin 
4232fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
423395d67482SBill Paul 
4234f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4235e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4236f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
423715eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4238f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4239f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4240c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
4241f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
424215eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4243f41ac2beSBill Paul 
42447f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
424595d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
42463f74909aSGleb Smirnoff 		uint32_t		rxidx;
424795d67482SBill Paul 		struct mbuf		*m = NULL;
42483f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
424995d67482SBill Paul 		int			have_tag = 0;
425095d67482SBill Paul 
425175719184SGleb Smirnoff #ifdef DEVICE_POLLING
425275719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
425375719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
425475719184SGleb Smirnoff 				break;
425575719184SGleb Smirnoff 			sc->rxcycles--;
425675719184SGleb Smirnoff 		}
425775719184SGleb Smirnoff #endif
425875719184SGleb Smirnoff 
42597f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
426095d67482SBill Paul 
426195d67482SBill Paul 		rxidx = cur_rx->bge_idx;
42627f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
426395d67482SBill Paul 
4264cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
4265cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
426695d67482SBill Paul 			have_tag = 1;
426795d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
426895d67482SBill Paul 		}
426995d67482SBill Paul 
427095d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
427195d67482SBill Paul 			jumbocnt++;
4272943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
427395d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4274e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
427595d67482SBill Paul 				continue;
427695d67482SBill Paul 			}
4277943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4278e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4279943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
428095d67482SBill Paul 				continue;
428195d67482SBill Paul 			}
428203e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
428395d67482SBill Paul 		} else {
428495d67482SBill Paul 			stdcnt++;
4285e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
428695d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4287e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
428895d67482SBill Paul 				continue;
428995d67482SBill Paul 			}
4290943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4291e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4292943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
429395d67482SBill Paul 				continue;
429495d67482SBill Paul 			}
429503e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
429695d67482SBill Paul 		}
429795d67482SBill Paul 
429895d67482SBill Paul 		ifp->if_ipackets++;
4299e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4300e255b776SJohn Polstra 		/*
4301e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4302e65bed95SPyun YongHyeon 		 * the payload is aligned.
4303e255b776SJohn Polstra 		 */
4304652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4305e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4306e255b776SJohn Polstra 			    cur_rx->bge_len);
4307e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4308e255b776SJohn Polstra 		}
4309e255b776SJohn Polstra #endif
4310473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
431195d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
431295d67482SBill Paul 
43131108273aSPyun YongHyeon 		if (ifp->if_capenable & IFCAP_RXCSUM)
43141108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
431595d67482SBill Paul 
431695d67482SBill Paul 		/*
4317673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4318673d9191SSam Leffler 		 * attach that information to the packet.
431995d67482SBill Paul 		 */
4320d147662cSGleb Smirnoff 		if (have_tag) {
432178ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
432278ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4323d147662cSGleb Smirnoff 		}
432495d67482SBill Paul 
4325dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
43260f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4327673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
43280f9bd73bSSam Leffler 			BGE_LOCK(sc);
4329dfe0df9aSPyun YongHyeon 		} else
4330dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
4331d4da719cSAttilio Rao 		rx_npkts++;
433225e13e68SXin LI 
433325e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
43348cf7d13dSAttilio Rao 			return (rx_npkts);
433595d67482SBill Paul 	}
433695d67482SBill Paul 
433715eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
433815eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4339e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4340f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4341e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
43424c0da0ffSGleb Smirnoff 
4343c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4344f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
43454c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4346f41ac2beSBill Paul 
43477f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
434838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
434995d67482SBill Paul 	if (stdcnt)
4350767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4351767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
435295d67482SBill Paul 	if (jumbocnt)
4353767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4354767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4355f5a034f9SPyun YongHyeon #ifdef notyet
4356f5a034f9SPyun YongHyeon 	/*
4357f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4358f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4359f5a034f9SPyun YongHyeon 	 */
4360f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4361f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
4362f5a034f9SPyun YongHyeon #endif
43631abcdbd1SAttilio Rao 	return (rx_npkts);
436495d67482SBill Paul }
436595d67482SBill Paul 
436695d67482SBill Paul static void
43671108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
43681108273aSPyun YongHyeon {
43691108273aSPyun YongHyeon 
43701108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
43711108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
43721108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
43731108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
43741108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
43751108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
43761108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
43771108273aSPyun YongHyeon 			}
43781108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
43791108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
43801108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
43811108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
43821108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
43831108273aSPyun YongHyeon 			}
43841108273aSPyun YongHyeon 		}
43851108273aSPyun YongHyeon 	} else {
43861108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
43871108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
43881108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
43891108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
43901108273aSPyun YongHyeon 		}
43911108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
43921108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
43931108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
43941108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
43951108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
43961108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
43971108273aSPyun YongHyeon 		}
43981108273aSPyun YongHyeon 	}
43991108273aSPyun YongHyeon }
44001108273aSPyun YongHyeon 
44011108273aSPyun YongHyeon static void
4402b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
440395d67482SBill Paul {
440495a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
440595d67482SBill Paul 	struct ifnet *ifp;
440695d67482SBill Paul 
44070f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
44080f9bd73bSSam Leffler 
44093f74909aSGleb Smirnoff 	/* Nothing to do. */
4410b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4411cfcb5025SOleg Bulyzhin 		return;
4412cfcb5025SOleg Bulyzhin 
4413fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
441495d67482SBill Paul 
4415e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
44165c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
441795d67482SBill Paul 	/*
441895d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
441995d67482SBill Paul 	 * frames that have been sent.
442095d67482SBill Paul 	 */
4421b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
442295a0a340SPyun YongHyeon 		uint32_t		idx;
442395d67482SBill Paul 
442495d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4425f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
442695d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
442795d67482SBill Paul 			ifp->if_opackets++;
442895d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
44290ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4430e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4431e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
44320ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4433f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4434e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4435e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
443695d67482SBill Paul 		}
443795d67482SBill Paul 		sc->bge_txcnt--;
443895d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
443995d67482SBill Paul 	}
444095d67482SBill Paul 
444113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
44425b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
44435b01e77cSBruce Evans 		sc->bge_timer = 0;
444495d67482SBill Paul }
444595d67482SBill Paul 
444675719184SGleb Smirnoff #ifdef DEVICE_POLLING
44471abcdbd1SAttilio Rao static int
444875719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
444975719184SGleb Smirnoff {
445075719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
4451b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4452366454f2SOleg Bulyzhin 	uint32_t statusword;
44531abcdbd1SAttilio Rao 	int rx_npkts = 0;
445475719184SGleb Smirnoff 
44553f74909aSGleb Smirnoff 	BGE_LOCK(sc);
44563f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
44573f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
44581abcdbd1SAttilio Rao 		return (rx_npkts);
44593f74909aSGleb Smirnoff 	}
446075719184SGleb Smirnoff 
4461dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4462b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4463b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
44642246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4465b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4466b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4467dab5cd05SOleg Bulyzhin 
4468175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
44692246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4470175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4471dab5cd05SOleg Bulyzhin 
4472dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4473b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4474b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4475366454f2SOleg Bulyzhin 
44760c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4477366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4478366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4479366454f2SOleg Bulyzhin 
4480366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4481366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
44824c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4483652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4484366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4485366454f2SOleg Bulyzhin 
4486366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4487dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
448825e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
448925e13e68SXin LI 		BGE_UNLOCK(sc);
44908cf7d13dSAttilio Rao 		return (rx_npkts);
449125e13e68SXin LI 	}
4492b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4493366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4494366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
44953f74909aSGleb Smirnoff 
44963f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
44971abcdbd1SAttilio Rao 	return (rx_npkts);
449875719184SGleb Smirnoff }
449975719184SGleb Smirnoff #endif /* DEVICE_POLLING */
450075719184SGleb Smirnoff 
4501dfe0df9aSPyun YongHyeon static int
4502dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4503dfe0df9aSPyun YongHyeon {
4504dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4505dfe0df9aSPyun YongHyeon 
4506dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4507dfe0df9aSPyun YongHyeon 	/*
4508dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4509dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4510dfe0df9aSPyun YongHyeon 	 */
4511dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4512dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4513dfe0df9aSPyun YongHyeon }
4514dfe0df9aSPyun YongHyeon 
4515dfe0df9aSPyun YongHyeon static void
4516dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4517dfe0df9aSPyun YongHyeon {
4518dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4519dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
45201108273aSPyun YongHyeon 	uint32_t status, status_tag;
4521dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4522dfe0df9aSPyun YongHyeon 
4523dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4524dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4525dfe0df9aSPyun YongHyeon 
452666151edfSPyun YongHyeon 	BGE_LOCK(sc);
452766151edfSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
452866151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4529dfe0df9aSPyun YongHyeon 		return;
453066151edfSPyun YongHyeon 	}
4531dfe0df9aSPyun YongHyeon 
4532dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4533dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4534dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4535dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4536dfe0df9aSPyun YongHyeon 
45372246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4538dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4539dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4540dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
45411108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
45422246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4543dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4544dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4545dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4546dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
45471108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
45481108273aSPyun YongHyeon 		status_tag = 0;
454966151edfSPyun YongHyeon 
455066151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
455166151edfSPyun YongHyeon 		bge_link_upd(sc);
455266151edfSPyun YongHyeon 
4553dfe0df9aSPyun YongHyeon 	/* Let controller work. */
45541108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4555dfe0df9aSPyun YongHyeon 
455666151edfSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
455766151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4558dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
455966151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4560dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
456166151edfSPyun YongHyeon 		BGE_LOCK(sc);
4562dfe0df9aSPyun YongHyeon 	}
4563dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4564dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4565dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4566dfe0df9aSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4567dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4568dfe0df9aSPyun YongHyeon 	}
456966151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4570dfe0df9aSPyun YongHyeon }
4571dfe0df9aSPyun YongHyeon 
457295d67482SBill Paul static void
45733f74909aSGleb Smirnoff bge_intr(void *xsc)
457495d67482SBill Paul {
457595d67482SBill Paul 	struct bge_softc *sc;
457695d67482SBill Paul 	struct ifnet *ifp;
4577dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4578b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
457995d67482SBill Paul 
458095d67482SBill Paul 	sc = xsc;
4581f41ac2beSBill Paul 
45820f9bd73bSSam Leffler 	BGE_LOCK(sc);
45830f9bd73bSSam Leffler 
4584dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4585dab5cd05SOleg Bulyzhin 
458675719184SGleb Smirnoff #ifdef DEVICE_POLLING
458775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
458875719184SGleb Smirnoff 		BGE_UNLOCK(sc);
458975719184SGleb Smirnoff 		return;
459075719184SGleb Smirnoff 	}
459175719184SGleb Smirnoff #endif
459275719184SGleb Smirnoff 
4593f30cbfc6SScott Long 	/*
4594b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4595b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4596b848e032SBruce Evans 	 * our current organization this just gives complications and
4597b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4598b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4599b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4600b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4601b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4602b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4603b848e032SBruce Evans 	 *
4604b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4605b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4606b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4607b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4608b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4609b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4610b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4611b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4612b848e032SBruce Evans 	 */
461338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4614b848e032SBruce Evans 
4615f584dfd1SPyun YongHyeon 	/*
4616f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4617f584dfd1SPyun YongHyeon 	 */
4618f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4619f584dfd1SPyun YongHyeon 
4620f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4621f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4622f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4623f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4624f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4625f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4626f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4627f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4628f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4629f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4630f584dfd1SPyun YongHyeon 
46311f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
46324c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4633f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4634dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
463595d67482SBill Paul 
463613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46373f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4638dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
463925e13e68SXin LI 	}
464095d67482SBill Paul 
464125e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46423f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4643b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
464495d67482SBill Paul 	}
464595d67482SBill Paul 
464613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
464713f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
46480f9bd73bSSam Leffler 		bge_start_locked(ifp);
46490f9bd73bSSam Leffler 
46500f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
465195d67482SBill Paul }
465295d67482SBill Paul 
465395d67482SBill Paul static void
46548cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
46558cb1383cSDoug Ambrisko {
46568cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
46578cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
46588cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
46598cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
46608cb1383cSDoug Ambrisko 		else {
4661899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4662888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
46633c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4664888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4665941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4666941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
46673fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
46689931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
46699931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
46708cb1383cSDoug Ambrisko 		}
46718cb1383cSDoug Ambrisko 	}
46728cb1383cSDoug Ambrisko }
46738cb1383cSDoug Ambrisko 
46748cb1383cSDoug Ambrisko static void
4675b74e67fbSGleb Smirnoff bge_tick(void *xsc)
46760f9bd73bSSam Leffler {
4677b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
467895d67482SBill Paul 	struct mii_data *mii = NULL;
467995d67482SBill Paul 
46800f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
468195d67482SBill Paul 
46825dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
46835dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
46845dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
46855dda8085SOleg Bulyzhin 		return;
46865dda8085SOleg Bulyzhin 
46877ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
46880434d1b8SBill Paul 		bge_stats_update_regs(sc);
46890434d1b8SBill Paul 	else
469095d67482SBill Paul 		bge_stats_update(sc);
469195d67482SBill Paul 
4692548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4693548c8f1aSPyun YongHyeon 
4694652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
469595d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
469682b67c01SOleg Bulyzhin 		/*
469782b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
469882b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
469982b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
470082b67c01SOleg Bulyzhin 		 */
470182b67c01SOleg Bulyzhin 		if (!sc->bge_link)
470295d67482SBill Paul 			mii_tick(mii);
47037b97099dSOleg Bulyzhin 	} else {
47047b97099dSOleg Bulyzhin 		/*
47057b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
47067b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
47077b97099dSOleg Bulyzhin 		 * and trigger interrupt.
47087b97099dSOleg Bulyzhin 		 */
47097b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
47103f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
47117b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
47127b97099dSOleg Bulyzhin #endif
47137b97099dSOleg Bulyzhin 		{
47147b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
47154f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
47164f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
47177b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
47184f0794ffSBjoern A. Zeeb 		else
47194f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
47207b97099dSOleg Bulyzhin 		}
4721dab5cd05SOleg Bulyzhin 	}
472295d67482SBill Paul 
47238cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4724b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
47258cb1383cSDoug Ambrisko 
4726dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
472795d67482SBill Paul }
472895d67482SBill Paul 
472995d67482SBill Paul static void
47303f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
47310434d1b8SBill Paul {
47323f74909aSGleb Smirnoff 	struct ifnet *ifp;
47332280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
47340434d1b8SBill Paul 
4735fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
47362280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
47370434d1b8SBill Paul 
47382280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
47392280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
47402280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
47412280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
47422280c16bSPyun YongHyeon 	stats->outXonSent +=
47432280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
47442280c16bSPyun YongHyeon 	stats->outXoffSent +=
47452280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
47462280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
47472280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
47482280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
47492280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
47502280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
47512280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
47522280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
47532280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
47542280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
47552280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
47562280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
47572280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
47582280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
47592280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
47602280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
47612280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
47622280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
47632280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
47647e6e2507SJung-uk Kim 
47652280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
47662280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
47672280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
47682280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
47692280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
47702280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
47712280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
47722280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
47732280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
47742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
47752280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
47762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
47772280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
47782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
47792280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
47802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
47812280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
47822280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
47832280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
47842280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
47852280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
47862280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
47872280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
47882280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
47892280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
47902280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
47912280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
47922280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
47932280c16bSPyun YongHyeon 
47942280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
47952280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
47962280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
47972280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
47982280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
47992280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48002280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
48012280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4802f78094a5SPyun YongHyeon 	/*
4803f78094a5SPyun YongHyeon 	 * XXX
4804f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4805f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4806f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4807f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4808f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4809f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4810f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4811f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4812f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4813f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4814f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4815f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4816f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4817f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4818f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4819f78094a5SPyun YongHyeon 	 * silicon bug.
4820f78094a5SPyun YongHyeon 	 */
4821f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4822f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4823f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
48242280c16bSPyun YongHyeon 		stats->InputDiscards +=
48252280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
48262280c16bSPyun YongHyeon 	stats->InputErrors +=
48272280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
48282280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
48292280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
48302280c16bSPyun YongHyeon 
48312280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
48322280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
48332280c16bSPyun YongHyeon 	    stats->InputErrors);
48342280c16bSPyun YongHyeon }
48352280c16bSPyun YongHyeon 
48362280c16bSPyun YongHyeon static void
48372280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
48382280c16bSPyun YongHyeon {
48392280c16bSPyun YongHyeon 
48402280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48412280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48422280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48432280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48442280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48452280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
48462280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
48472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
48482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
48492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
48502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
48512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
48522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48532280c16bSPyun YongHyeon 
48542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
48642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
48652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
48662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48682280c16bSPyun YongHyeon 
48692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
48732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
48742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
48752280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
48760434d1b8SBill Paul }
48770434d1b8SBill Paul 
48780434d1b8SBill Paul static void
48793f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
488095d67482SBill Paul {
488195d67482SBill Paul 	struct ifnet *ifp;
4882e907febfSPyun YongHyeon 	bus_size_t stats;
48837e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
488495d67482SBill Paul 
4885fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
488695d67482SBill Paul 
4887e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4888e907febfSPyun YongHyeon 
4889e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4890e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
489195d67482SBill Paul 
48928634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
48936b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
48946fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
48956fb34dd2SOleg Bulyzhin 
489637ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
489737ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_nobds);
489837ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
489937ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
490037ee7cc7SPyun YongHyeon 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_inerrs);
490137ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
49026fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
49036b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
49046fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
49056fb34dd2SOleg Bulyzhin 
49066fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
49076b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
49086fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
490995d67482SBill Paul 
4910e907febfSPyun YongHyeon #undef	READ_STAT
491195d67482SBill Paul }
491295d67482SBill Paul 
491395d67482SBill Paul /*
4914d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
4915d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
4916d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
4917d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
4918d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
4919d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
4920d375e524SGleb Smirnoff  */
4921d375e524SGleb Smirnoff static __inline int
4922d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
4923d375e524SGleb Smirnoff {
4924d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
4925d375e524SGleb Smirnoff 	struct mbuf *last;
4926d375e524SGleb Smirnoff 
4927d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
4928d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
4929d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
4930d375e524SGleb Smirnoff 		last = m;
4931d375e524SGleb Smirnoff 	} else {
4932d375e524SGleb Smirnoff 		/*
4933d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
4934d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
4935d375e524SGleb Smirnoff 		 */
4936d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
4937d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
4938d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
4939d375e524SGleb Smirnoff 			struct mbuf *n;
4940d375e524SGleb Smirnoff 
4941d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
4942d375e524SGleb Smirnoff 			if (n == NULL)
4943d375e524SGleb Smirnoff 				return (ENOBUFS);
4944d375e524SGleb Smirnoff 			n->m_len = 0;
4945d375e524SGleb Smirnoff 			last->m_next = n;
4946d375e524SGleb Smirnoff 			last = n;
4947d375e524SGleb Smirnoff 		}
4948d375e524SGleb Smirnoff 	}
4949d375e524SGleb Smirnoff 
4950d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
4951d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
4952d375e524SGleb Smirnoff 	last->m_len += padlen;
4953d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
4954d375e524SGleb Smirnoff 
4955d375e524SGleb Smirnoff 	return (0);
4956d375e524SGleb Smirnoff }
4957d375e524SGleb Smirnoff 
4958ca3f1187SPyun YongHyeon static struct mbuf *
4959d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
4960d598b626SPyun YongHyeon {
4961d598b626SPyun YongHyeon 	struct mbuf *n;
4962d598b626SPyun YongHyeon 	int found;
4963d598b626SPyun YongHyeon 
4964d598b626SPyun YongHyeon 	/*
4965d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
4966d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
4967d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
4968d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
4969d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
4970d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
4971d598b626SPyun YongHyeon 	 */
4972d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
4973d598b626SPyun YongHyeon 		if (n->m_len < 8) {
4974d598b626SPyun YongHyeon 			found++;
4975d598b626SPyun YongHyeon 			if (found > 1)
4976d598b626SPyun YongHyeon 				break;
4977d598b626SPyun YongHyeon 			continue;
4978d598b626SPyun YongHyeon 		}
4979d598b626SPyun YongHyeon 		found = 0;
4980d598b626SPyun YongHyeon 	}
4981d598b626SPyun YongHyeon 
4982d598b626SPyun YongHyeon 	if (found > 1) {
4983d598b626SPyun YongHyeon 		n = m_defrag(m, M_DONTWAIT);
4984d598b626SPyun YongHyeon 		if (n == NULL)
4985d598b626SPyun YongHyeon 			m_freem(m);
4986d598b626SPyun YongHyeon 	} else
4987d598b626SPyun YongHyeon 		n = m;
4988d598b626SPyun YongHyeon 	return (n);
4989d598b626SPyun YongHyeon }
4990d598b626SPyun YongHyeon 
4991d598b626SPyun YongHyeon static struct mbuf *
49921108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
49931108273aSPyun YongHyeon     uint16_t *flags)
4994ca3f1187SPyun YongHyeon {
4995ca3f1187SPyun YongHyeon 	struct ip *ip;
4996ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
4997ca3f1187SPyun YongHyeon 	struct mbuf *n;
4998ca3f1187SPyun YongHyeon 	uint16_t hlen;
49995b355c4fSPyun YongHyeon 	uint32_t poff;
5000ca3f1187SPyun YongHyeon 
5001ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5002ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5003ca3f1187SPyun YongHyeon 		n = m_dup(m, M_DONTWAIT);
5004ca3f1187SPyun YongHyeon 		m_freem(m);
5005ca3f1187SPyun YongHyeon 		if (n == NULL)
5006ca3f1187SPyun YongHyeon 			return (NULL);
5007ca3f1187SPyun YongHyeon 		m = n;
5008ca3f1187SPyun YongHyeon 	}
50095b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5010ca3f1187SPyun YongHyeon 	if (m == NULL)
5011ca3f1187SPyun YongHyeon 		return (NULL);
50125b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
50135b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5014ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5015ca3f1187SPyun YongHyeon 	if (m == NULL)
5016ca3f1187SPyun YongHyeon 		return (NULL);
5017ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
50185b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5019ca3f1187SPyun YongHyeon 	if (m == NULL)
5020ca3f1187SPyun YongHyeon 		return (NULL);
5021ca3f1187SPyun YongHyeon 	/*
5022ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5023ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5024ca3f1187SPyun YongHyeon 	 */
5025ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
502696486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5027ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5028ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5029ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
503096486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5031ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5032ca3f1187SPyun YongHyeon 	/*
5033ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5034ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5035ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
50361108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5037ca3f1187SPyun YongHyeon 	 */
50381108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5039ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
50401108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
50411108273aSPyun YongHyeon 		/*
50421108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
50431108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
50441108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
50451108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
50461108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
50471108273aSPyun YongHyeon 		 * frames are supported.
50481108273aSPyun YongHyeon 		 */
50491108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
50501108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
50511108273aSPyun YongHyeon 	} else {
50521108273aSPyun YongHyeon 		/*
50531108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
50541108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
50551108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
50561108273aSPyun YongHyeon 		 * supported.
50571108273aSPyun YongHyeon 		 */
5058ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
50591108273aSPyun YongHyeon 	}
5060ca3f1187SPyun YongHyeon 	return (m);
5061ca3f1187SPyun YongHyeon }
5062ca3f1187SPyun YongHyeon 
5063d375e524SGleb Smirnoff /*
506495d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
506595d67482SBill Paul  * pointers to descriptors.
506695d67482SBill Paul  */
506795d67482SBill Paul static int
5068676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
506995d67482SBill Paul {
50707e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5071f41ac2beSBill Paul 	bus_dmamap_t		map;
5072676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5073676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
50747e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5075ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
50767e27542aSGleb Smirnoff 	int			nsegs, i, error;
507795d67482SBill Paul 
50786909dc43SGleb Smirnoff 	csum_flags = 0;
5079ca3f1187SPyun YongHyeon 	mss = 0;
5080ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5081d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5082d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5083d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5084d598b626SPyun YongHyeon 		if (*m_head == NULL)
5085d598b626SPyun YongHyeon 			return (ENOBUFS);
5086d598b626SPyun YongHyeon 		m = *m_head;
5087d598b626SPyun YongHyeon 	}
5088ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
50891108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5090ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5091ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5092ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5093ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
509435f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
50956909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
50966909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
50976909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
50986909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
50996909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
51006909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
51016909dc43SGleb Smirnoff 				m_freem(m);
51026909dc43SGleb Smirnoff 				*m_head = NULL;
51036909dc43SGleb Smirnoff 				return (error);
51046909dc43SGleb Smirnoff 			}
51056909dc43SGleb Smirnoff 		}
51066909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
51076909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
51086909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
51096909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
51106909dc43SGleb Smirnoff 	}
51116909dc43SGleb Smirnoff 
51121108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
51131108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
51141108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
51151108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
51161108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5117beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5118d94f2b85SPyun YongHyeon 			/*
5119d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5120d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5121d94f2b85SPyun YongHyeon 			 * DMA read operation.
5122d94f2b85SPyun YongHyeon 			 */
5123beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5124d94f2b85SPyun YongHyeon 				m = m_defrag(m, M_DONTWAIT);
5125d94f2b85SPyun YongHyeon 			else
51261108273aSPyun YongHyeon 				m = m_collapse(m, M_DONTWAIT,
51271108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5128261f04d6SPyun YongHyeon 			if (m == NULL)
5129261f04d6SPyun YongHyeon 				m = *m_head;
5130d94f2b85SPyun YongHyeon 			*m_head = m;
5131d94f2b85SPyun YongHyeon 		}
51321108273aSPyun YongHyeon 	}
5133d94f2b85SPyun YongHyeon 
51347e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
51350ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5136676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
51377e27542aSGleb Smirnoff 	if (error == EFBIG) {
51384eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
5139676ad2c9SGleb Smirnoff 		if (m == NULL) {
5140676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5141676ad2c9SGleb Smirnoff 			*m_head = NULL;
51427e27542aSGleb Smirnoff 			return (ENOBUFS);
51437e27542aSGleb Smirnoff 		}
5144676ad2c9SGleb Smirnoff 		*m_head = m;
51450ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
51460ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5147676ad2c9SGleb Smirnoff 		if (error) {
5148676ad2c9SGleb Smirnoff 			m_freem(m);
5149676ad2c9SGleb Smirnoff 			*m_head = NULL;
51507e27542aSGleb Smirnoff 			return (error);
51517e27542aSGleb Smirnoff 		}
5152676ad2c9SGleb Smirnoff 	} else if (error != 0)
5153676ad2c9SGleb Smirnoff 		return (error);
51547e27542aSGleb Smirnoff 
5155167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5156167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
51570ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
515895d67482SBill Paul 		return (ENOBUFS);
51597e27542aSGleb Smirnoff 	}
51607e27542aSGleb Smirnoff 
51610ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5162e65bed95SPyun YongHyeon 
5163ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5164ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5165ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5166ca3f1187SPyun YongHyeon 	}
51677e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
51687e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
51697e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
51707e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
51717e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
51727e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
5173ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
5174ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
51757e27542aSGleb Smirnoff 		if (i == nsegs - 1)
51767e27542aSGleb Smirnoff 			break;
51777e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
51787e27542aSGleb Smirnoff 	}
51797e27542aSGleb Smirnoff 
51807e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
51817e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5182676ad2c9SGleb Smirnoff 
5183f41ac2beSBill Paul 	/*
5184f41ac2beSBill Paul 	 * Insure that the map for this transmission
5185f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5186f41ac2beSBill Paul 	 * in this chain.
5187f41ac2beSBill Paul 	 */
51887e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
51897e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5190676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
51917e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
519295d67482SBill Paul 
51937e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
51947e27542aSGleb Smirnoff 	*txidx = idx;
519595d67482SBill Paul 
519695d67482SBill Paul 	return (0);
519795d67482SBill Paul }
519895d67482SBill Paul 
519995d67482SBill Paul /*
520095d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
520195d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
520295d67482SBill Paul  */
520395d67482SBill Paul static void
52043f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
520595d67482SBill Paul {
520695d67482SBill Paul 	struct bge_softc *sc;
5207167fdb62SPyun YongHyeon 	struct mbuf *m_head;
520814bbd30fSGleb Smirnoff 	uint32_t prodidx;
5209167fdb62SPyun YongHyeon 	int count;
521095d67482SBill Paul 
521195d67482SBill Paul 	sc = ifp->if_softc;
5212167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
521395d67482SBill Paul 
5214167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5215167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5216167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
521795d67482SBill Paul 		return;
521895d67482SBill Paul 
521914bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
522095d67482SBill Paul 
5221167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
5222167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5223167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5224167fdb62SPyun YongHyeon 			break;
5225167fdb62SPyun YongHyeon 		}
52264d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
522795d67482SBill Paul 		if (m_head == NULL)
522895d67482SBill Paul 			break;
522995d67482SBill Paul 
523095d67482SBill Paul 		/*
523195d67482SBill Paul 		 * XXX
5232b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
5233b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
5234b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
5235b874fdd4SYaroslav Tykhiy 		 *
5236b874fdd4SYaroslav Tykhiy 		 * XXX
523795d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
523895d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
523995d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
524095d67482SBill Paul 		 * chain at once.
524195d67482SBill Paul 		 * (paranoia -- may not actually be needed)
524295d67482SBill Paul 		 */
524395d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
524495d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
524595d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
524695d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
52474d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
524813f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
524995d67482SBill Paul 				break;
525095d67482SBill Paul 			}
525195d67482SBill Paul 		}
525295d67482SBill Paul 
525395d67482SBill Paul 		/*
525495d67482SBill Paul 		 * Pack the data into the transmit ring. If we
525595d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
525695d67482SBill Paul 		 * for the NIC to drain the ring.
525795d67482SBill Paul 		 */
5258676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5259676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5260676ad2c9SGleb Smirnoff 				break;
52614d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
526213f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
526395d67482SBill Paul 			break;
526495d67482SBill Paul 		}
5265303a718cSDag-Erling Smørgrav 		++count;
526695d67482SBill Paul 
526795d67482SBill Paul 		/*
526895d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
526995d67482SBill Paul 		 * to him.
527095d67482SBill Paul 		 */
52714e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
527245ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
52734e35d186SJung-uk Kim #else
52744e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
52754e35d186SJung-uk Kim #endif
527695d67482SBill Paul 	}
527795d67482SBill Paul 
5278167fdb62SPyun YongHyeon 	if (count > 0) {
5279aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
52805c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
52813f74909aSGleb Smirnoff 		/* Transmit. */
528238cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
52833927098fSPaul Saab 		/* 5700 b2 errata */
5284e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
528538cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
528695d67482SBill Paul 
528714bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
528814bbd30fSGleb Smirnoff 
528995d67482SBill Paul 		/*
529095d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
529195d67482SBill Paul 		 */
5292b74e67fbSGleb Smirnoff 		sc->bge_timer = 5;
529395d67482SBill Paul 	}
5294167fdb62SPyun YongHyeon }
529595d67482SBill Paul 
52960f9bd73bSSam Leffler /*
52970f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
52980f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
52990f9bd73bSSam Leffler  */
530095d67482SBill Paul static void
53013f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
530295d67482SBill Paul {
53030f9bd73bSSam Leffler 	struct bge_softc *sc;
53040f9bd73bSSam Leffler 
53050f9bd73bSSam Leffler 	sc = ifp->if_softc;
53060f9bd73bSSam Leffler 	BGE_LOCK(sc);
53070f9bd73bSSam Leffler 	bge_start_locked(ifp);
53080f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
53090f9bd73bSSam Leffler }
53100f9bd73bSSam Leffler 
53110f9bd73bSSam Leffler static void
53123f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
53130f9bd73bSSam Leffler {
531495d67482SBill Paul 	struct ifnet *ifp;
53153f74909aSGleb Smirnoff 	uint16_t *m;
5316f6a65488SPyun YongHyeon 	uint32_t mode;
531795d67482SBill Paul 
53180f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
531995d67482SBill Paul 
5320fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
532195d67482SBill Paul 
532213f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
532395d67482SBill Paul 		return;
532495d67482SBill Paul 
532595d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
532695d67482SBill Paul 	bge_stop(sc);
53278cb1383cSDoug Ambrisko 
53288cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
53298cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
533095d67482SBill Paul 	bge_reset(sc);
53318cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
53328cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
53338cb1383cSDoug Ambrisko 
533495d67482SBill Paul 	bge_chipinit(sc);
533595d67482SBill Paul 
533695d67482SBill Paul 	/*
533795d67482SBill Paul 	 * Init the various state machines, ring
533895d67482SBill Paul 	 * control blocks and firmware.
533995d67482SBill Paul 	 */
534095d67482SBill Paul 	if (bge_blockinit(sc)) {
5341fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
534295d67482SBill Paul 		return;
534395d67482SBill Paul 	}
534495d67482SBill Paul 
5345fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
534695d67482SBill Paul 
534795d67482SBill Paul 	/* Specify MTU. */
534895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
5349cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5350cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
535195d67482SBill Paul 
535295d67482SBill Paul 	/* Load our MAC address. */
53533f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
535495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
535595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
535695d67482SBill Paul 
53573e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
53583e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
535995d67482SBill Paul 
536095d67482SBill Paul 	/* Program multicast filter. */
536195d67482SBill Paul 	bge_setmulti(sc);
536295d67482SBill Paul 
5363cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5364cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5365cb2eacc7SYaroslav Tykhiy 
536635f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
536735f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
536835f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
536935f945cdSPyun YongHyeon 	else
537035f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
537135f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
537235f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
537335f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
537435f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
537535f945cdSPyun YongHyeon 	}
537635f945cdSPyun YongHyeon 
537795d67482SBill Paul 	/* Init RX ring. */
53783ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
53793ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
53803ee5d7daSPyun YongHyeon 		bge_stop(sc);
53813ee5d7daSPyun YongHyeon 		return;
53823ee5d7daSPyun YongHyeon 	}
538395d67482SBill Paul 
53840434d1b8SBill Paul 	/*
53850434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
53860434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
53870434d1b8SBill Paul 	 * entry of the ring.
53880434d1b8SBill Paul 	 */
53890434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
53903f74909aSGleb Smirnoff 		uint32_t		v, i;
53910434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
53920434d1b8SBill Paul 			DELAY(20);
53930434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
53940434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
53950434d1b8SBill Paul 				break;
53960434d1b8SBill Paul 		}
53970434d1b8SBill Paul 		if (i == 10)
5398fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5399fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
54000434d1b8SBill Paul 	}
54010434d1b8SBill Paul 
540295d67482SBill Paul 	/* Init jumbo RX ring. */
5403f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5404f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
5405c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
54063ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5407333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5408b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
54093ee5d7daSPyun YongHyeon 			bge_stop(sc);
54103ee5d7daSPyun YongHyeon 			return;
54113ee5d7daSPyun YongHyeon 		}
54123ee5d7daSPyun YongHyeon 	}
541395d67482SBill Paul 
54143f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
541595d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
541695d67482SBill Paul 
54177e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
54187e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
54197e6e2507SJung-uk Kim 
542095d67482SBill Paul 	/* Init TX ring. */
542195d67482SBill Paul 	bge_init_tx_ring(sc);
542295d67482SBill Paul 
5423f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5424f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5425f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5426f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
542750515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
542850515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
542950515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
543050515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
543150515680SPyun YongHyeon 	}
54323f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5433f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5434a6e66cd2SPyun YongHyeon 	DELAY(100);
543595d67482SBill Paul 
54363f74909aSGleb Smirnoff 	/* Turn on receiver. */
5437548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5438548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5439548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
5440548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5441a6e66cd2SPyun YongHyeon 	DELAY(10);
544295d67482SBill Paul 
5443dedcdf57SPyun YongHyeon 	/*
5444dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5445dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5446dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5447dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5448dedcdf57SPyun YongHyeon 	 */
5449b4a256acSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765)
5450b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5451b4a256acSPyun YongHyeon 	else
5452dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5453dedcdf57SPyun YongHyeon 
54542280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
54552280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
54562280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
54572280c16bSPyun YongHyeon 
545895d67482SBill Paul 	/* Tell firmware we're alive. */
545995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
546095d67482SBill Paul 
546175719184SGleb Smirnoff #ifdef DEVICE_POLLING
546275719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
546375719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
546475719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
546575719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
546638cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
546775719184SGleb Smirnoff 	} else
546875719184SGleb Smirnoff #endif
546975719184SGleb Smirnoff 
547095d67482SBill Paul 	/* Enable host interrupts. */
547175719184SGleb Smirnoff 	{
547295d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
547395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
547438cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
547575719184SGleb Smirnoff 	}
547695d67482SBill Paul 
547713f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
547813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
547995d67482SBill Paul 
5480e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5481e4146b95SPyun YongHyeon 
54820f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
54830f9bd73bSSam Leffler }
54840f9bd73bSSam Leffler 
54850f9bd73bSSam Leffler static void
54863f74909aSGleb Smirnoff bge_init(void *xsc)
54870f9bd73bSSam Leffler {
54880f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
54890f9bd73bSSam Leffler 
54900f9bd73bSSam Leffler 	BGE_LOCK(sc);
54910f9bd73bSSam Leffler 	bge_init_locked(sc);
54920f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
549395d67482SBill Paul }
549495d67482SBill Paul 
549595d67482SBill Paul /*
549695d67482SBill Paul  * Set media options.
549795d67482SBill Paul  */
549895d67482SBill Paul static int
54993f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
550095d67482SBill Paul {
550167d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
550267d5e043SOleg Bulyzhin 	int res;
550367d5e043SOleg Bulyzhin 
550467d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
550567d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
550667d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
550767d5e043SOleg Bulyzhin 
550867d5e043SOleg Bulyzhin 	return (res);
550967d5e043SOleg Bulyzhin }
551067d5e043SOleg Bulyzhin 
551167d5e043SOleg Bulyzhin static int
551267d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
551367d5e043SOleg Bulyzhin {
551467d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
551595d67482SBill Paul 	struct mii_data *mii;
55164f09c4c7SMarius Strobl 	struct mii_softc *miisc;
551795d67482SBill Paul 	struct ifmedia *ifm;
551895d67482SBill Paul 
551967d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
552067d5e043SOleg Bulyzhin 
552195d67482SBill Paul 	ifm = &sc->bge_ifmedia;
552295d67482SBill Paul 
552395d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5524652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
552595d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
552695d67482SBill Paul 			return (EINVAL);
552795d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
552895d67482SBill Paul 		case IFM_AUTO:
5529ff50922bSDoug White 			/*
5530ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5531ff50922bSDoug White 			 * mechanism for programming the autoneg
5532ff50922bSDoug White 			 * advertisement registers in TBI mode.
5533ff50922bSDoug White 			 */
55340f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5535ff50922bSDoug White 				uint32_t sgdig;
55360f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
55370f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5538ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5539ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5540ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5541ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5542ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5543ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5544ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5545ff50922bSDoug White 					DELAY(5);
5546ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5547ff50922bSDoug White 				}
55480f89fde2SJung-uk Kim 			}
554995d67482SBill Paul 			break;
555095d67482SBill Paul 		case IFM_1000_SX:
555195d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
555295d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
555395d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
555495d67482SBill Paul 			} else {
555595d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
555695d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
555795d67482SBill Paul 			}
55589b80ffe7SPyun YongHyeon 			DELAY(40);
555995d67482SBill Paul 			break;
556095d67482SBill Paul 		default:
556195d67482SBill Paul 			return (EINVAL);
556295d67482SBill Paul 		}
556395d67482SBill Paul 		return (0);
556495d67482SBill Paul 	}
556595d67482SBill Paul 
55661493e883SOleg Bulyzhin 	sc->bge_link_evt++;
556795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
55684f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
55693fcb7a53SMarius Strobl 		PHY_RESET(miisc);
557095d67482SBill Paul 	mii_mediachg(mii);
557195d67482SBill Paul 
5572902827f6SBjoern A. Zeeb 	/*
5573902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5574902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5575902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5576902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5577902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5578902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5579902827f6SBjoern A. Zeeb 	 * get an RX intr.
5580902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5581902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5582902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5583902827f6SBjoern A. Zeeb 	 */
55844f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
55854f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5586902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
55874f0794ffSBjoern A. Zeeb 	else
558863ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5589902827f6SBjoern A. Zeeb 
559095d67482SBill Paul 	return (0);
559195d67482SBill Paul }
559295d67482SBill Paul 
559395d67482SBill Paul /*
559495d67482SBill Paul  * Report current media status.
559595d67482SBill Paul  */
559695d67482SBill Paul static void
55973f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
559895d67482SBill Paul {
559967d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
560095d67482SBill Paul 	struct mii_data *mii;
560195d67482SBill Paul 
560267d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
560395d67482SBill Paul 
5604652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
560595d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
560695d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
560795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
560895d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
560995d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
56104c0da0ffSGleb Smirnoff 		else {
56114c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
561267d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
56134c0da0ffSGleb Smirnoff 			return;
56144c0da0ffSGleb Smirnoff 		}
561595d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
561695d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
561795d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
561895d67482SBill Paul 		else
561995d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
562067d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
562195d67482SBill Paul 		return;
562295d67482SBill Paul 	}
562395d67482SBill Paul 
562495d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
562595d67482SBill Paul 	mii_pollstat(mii);
562695d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
562795d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
562867d5e043SOleg Bulyzhin 
562967d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
563095d67482SBill Paul }
563195d67482SBill Paul 
563295d67482SBill Paul static int
56333f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
563495d67482SBill Paul {
563595d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
563695d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
563795d67482SBill Paul 	struct mii_data *mii;
5638f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
563995d67482SBill Paul 
564095d67482SBill Paul 	switch (command) {
564195d67482SBill Paul 	case SIOCSIFMTU:
5642f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5643f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
56444c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5645f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
564695d67482SBill Paul 				error = EINVAL;
5647f5459d4cSPyun YongHyeon 				break;
5648f5459d4cSPyun YongHyeon 			}
5649f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5650f5459d4cSPyun YongHyeon 			error = EINVAL;
5651f5459d4cSPyun YongHyeon 			break;
5652f5459d4cSPyun YongHyeon 		}
5653f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5654f5459d4cSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
565595d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
56563a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
565713f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
56583a429c8fSPyun YongHyeon 				bge_init_locked(sc);
565995d67482SBill Paul 			}
56603a429c8fSPyun YongHyeon 		}
56613a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
566295d67482SBill Paul 		break;
566395d67482SBill Paul 	case SIOCSIFFLAGS:
56640f9bd73bSSam Leffler 		BGE_LOCK(sc);
566595d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
566695d67482SBill Paul 			/*
566795d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
566895d67482SBill Paul 			 * then just use the 'set promisc mode' command
566995d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
567095d67482SBill Paul 			 * a full re-init means reloading the firmware and
567195d67482SBill Paul 			 * waiting for it to start up, which may take a
5672d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
567395d67482SBill Paul 			 */
5674f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5675f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
56763e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
56773e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5678f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5679d183af7fSRuslan Ermilov 					bge_setmulti(sc);
568095d67482SBill Paul 			} else
56810f9bd73bSSam Leffler 				bge_init_locked(sc);
568295d67482SBill Paul 		} else {
568313f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
568495d67482SBill Paul 				bge_stop(sc);
568595d67482SBill Paul 			}
568695d67482SBill Paul 		}
568795d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
56880f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
568995d67482SBill Paul 		error = 0;
569095d67482SBill Paul 		break;
569195d67482SBill Paul 	case SIOCADDMULTI:
569295d67482SBill Paul 	case SIOCDELMULTI:
569313f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
56940f9bd73bSSam Leffler 			BGE_LOCK(sc);
569595d67482SBill Paul 			bge_setmulti(sc);
56960f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
569795d67482SBill Paul 			error = 0;
569895d67482SBill Paul 		}
569995d67482SBill Paul 		break;
570095d67482SBill Paul 	case SIOCSIFMEDIA:
570195d67482SBill Paul 	case SIOCGIFMEDIA:
5702652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
570395d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
570495d67482SBill Paul 			    &sc->bge_ifmedia, command);
570595d67482SBill Paul 		} else {
570695d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
570795d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
570895d67482SBill Paul 			    &mii->mii_media, command);
570995d67482SBill Paul 		}
571095d67482SBill Paul 		break;
571195d67482SBill Paul 	case SIOCSIFCAP:
571295d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
571375719184SGleb Smirnoff #ifdef DEVICE_POLLING
571475719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
571575719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
571675719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
571775719184SGleb Smirnoff 				if (error)
571875719184SGleb Smirnoff 					return (error);
571975719184SGleb Smirnoff 				BGE_LOCK(sc);
572075719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
572175719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
572238cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
572375719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
572475719184SGleb Smirnoff 				BGE_UNLOCK(sc);
572575719184SGleb Smirnoff 			} else {
572675719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
572775719184SGleb Smirnoff 				/* Enable interrupt even in error case */
572875719184SGleb Smirnoff 				BGE_LOCK(sc);
572975719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
573075719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
573138cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
573275719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
573375719184SGleb Smirnoff 				BGE_UNLOCK(sc);
573475719184SGleb Smirnoff 			}
573575719184SGleb Smirnoff 		}
573675719184SGleb Smirnoff #endif
5737d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5738d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
5739d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
5740d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
574135f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
574295d67482SBill Paul 			else
574335f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
574495d67482SBill Paul 		}
5745cb2eacc7SYaroslav Tykhiy 
5746d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5747d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
5748d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
5749d8b57f98SPyun YongHyeon 
5750ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5751ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
5752ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
5753ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
5754ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
5755ca3f1187SPyun YongHyeon 			else
5756ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
5757ca3f1187SPyun YongHyeon 		}
5758ca3f1187SPyun YongHyeon 
5759cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5760cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
5761cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5762cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5763cb2eacc7SYaroslav Tykhiy 		}
5764cb2eacc7SYaroslav Tykhiy 
576504bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
576604bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
576704bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
576804bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
576904bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
5770cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
577104bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
577204bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
5773cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5774cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5775cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
577604bde852SPyun YongHyeon 		}
5777cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5778cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
5779cb2eacc7SYaroslav Tykhiy #endif
578095d67482SBill Paul 		break;
578195d67482SBill Paul 	default:
5782673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
578395d67482SBill Paul 		break;
578495d67482SBill Paul 	}
578595d67482SBill Paul 
578695d67482SBill Paul 	return (error);
578795d67482SBill Paul }
578895d67482SBill Paul 
578995d67482SBill Paul static void
5790b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
579195d67482SBill Paul {
5792b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
579395d67482SBill Paul 
5794b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5795b74e67fbSGleb Smirnoff 
5796b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5797b74e67fbSGleb Smirnoff 		return;
5798b74e67fbSGleb Smirnoff 
5799b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
580095d67482SBill Paul 
5801fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
580295d67482SBill Paul 
580313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5804426742bfSGleb Smirnoff 	bge_init_locked(sc);
580595d67482SBill Paul 
580695d67482SBill Paul 	ifp->if_oerrors++;
580795d67482SBill Paul }
580895d67482SBill Paul 
58095a147ba6SPyun YongHyeon static void
58105a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
58115a147ba6SPyun YongHyeon {
58125a147ba6SPyun YongHyeon 	int i;
58135a147ba6SPyun YongHyeon 
58145a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
58155a147ba6SPyun YongHyeon 
58165a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
58175a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
58185a147ba6SPyun YongHyeon 			return;
58195a147ba6SPyun YongHyeon 		DELAY(100);
58205a147ba6SPyun YongHyeon         }
58215a147ba6SPyun YongHyeon }
58225a147ba6SPyun YongHyeon 
582395d67482SBill Paul /*
582495d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
582595d67482SBill Paul  * RX and TX lists.
582695d67482SBill Paul  */
582795d67482SBill Paul static void
58283f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
582995d67482SBill Paul {
583095d67482SBill Paul 	struct ifnet *ifp;
583195d67482SBill Paul 
58320f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
58330f9bd73bSSam Leffler 
5834fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
583595d67482SBill Paul 
58360f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
583795d67482SBill Paul 
583844b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
583944b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
584044b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
584144b63691SBjoern A. Zeeb 
584244b63691SBjoern A. Zeeb 	/*
584344b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
584444b63691SBjoern A. Zeeb 	 */
584544b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
5846548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
584744b63691SBjoern A. Zeeb 
584895d67482SBill Paul 	/*
58493f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
585095d67482SBill Paul 	 */
58515a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
58525a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
58535a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
58545a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
58555a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
58565a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
58575a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
58585a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
585995d67482SBill Paul 
586095d67482SBill Paul 	/*
58613f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
586295d67482SBill Paul 	 */
58635a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
58645a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
58655a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
58665a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
58675a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
58685a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
58695a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
58705a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
587195d67482SBill Paul 
587295d67482SBill Paul 	/*
587395d67482SBill Paul 	 * Shut down all of the memory managers and related
587495d67482SBill Paul 	 * state machines.
587595d67482SBill Paul 	 */
58765a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
58775a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
58785a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
58795a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
58805a147ba6SPyun YongHyeon 
58810c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
588295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
58837ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
588495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
588595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
58860434d1b8SBill Paul 	}
58872280c16bSPyun YongHyeon 	/* Update MAC statistics. */
58882280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
58892280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
589095d67482SBill Paul 
58918cb1383cSDoug Ambrisko 	bge_reset(sc);
5892548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
5893548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
58948cb1383cSDoug Ambrisko 
58958cb1383cSDoug Ambrisko 	/*
58968cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
58978cb1383cSDoug Ambrisko 	 */
58988cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
58998cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
59008cb1383cSDoug Ambrisko 	else
590195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
590295d67482SBill Paul 
590395d67482SBill Paul 	/* Free the RX lists. */
590495d67482SBill Paul 	bge_free_rx_ring_std(sc);
590595d67482SBill Paul 
590695d67482SBill Paul 	/* Free jumbo RX list. */
59074c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
590895d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
590995d67482SBill Paul 
591095d67482SBill Paul 	/* Free TX buffers. */
591195d67482SBill Paul 	bge_free_tx_ring(sc);
591295d67482SBill Paul 
591395d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
591495d67482SBill Paul 
59155dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
59161493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
59171493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
59181493e883SOleg Bulyzhin 	sc->bge_link = 0;
591995d67482SBill Paul 
59201493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
592195d67482SBill Paul }
592295d67482SBill Paul 
592395d67482SBill Paul /*
592495d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
592595d67482SBill Paul  * get confused by errant DMAs when rebooting.
592695d67482SBill Paul  */
5927b6c974e8SWarner Losh static int
59283f74909aSGleb Smirnoff bge_shutdown(device_t dev)
592995d67482SBill Paul {
593095d67482SBill Paul 	struct bge_softc *sc;
593195d67482SBill Paul 
593295d67482SBill Paul 	sc = device_get_softc(dev);
59330f9bd73bSSam Leffler 	BGE_LOCK(sc);
593495d67482SBill Paul 	bge_stop(sc);
59350f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
5936b6c974e8SWarner Losh 
5937b6c974e8SWarner Losh 	return (0);
593895d67482SBill Paul }
593914afefa3SPawel Jakub Dawidek 
594014afefa3SPawel Jakub Dawidek static int
594114afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
594214afefa3SPawel Jakub Dawidek {
594314afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
594414afefa3SPawel Jakub Dawidek 
594514afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
594614afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
594714afefa3SPawel Jakub Dawidek 	bge_stop(sc);
594814afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
594914afefa3SPawel Jakub Dawidek 
595014afefa3SPawel Jakub Dawidek 	return (0);
595114afefa3SPawel Jakub Dawidek }
595214afefa3SPawel Jakub Dawidek 
595314afefa3SPawel Jakub Dawidek static int
595414afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
595514afefa3SPawel Jakub Dawidek {
595614afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
595714afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
595814afefa3SPawel Jakub Dawidek 
595914afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
596014afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
596114afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
596214afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
596314afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
596414afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
596514afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
596614afefa3SPawel Jakub Dawidek 	}
596714afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
596814afefa3SPawel Jakub Dawidek 
596914afefa3SPawel Jakub Dawidek 	return (0);
597014afefa3SPawel Jakub Dawidek }
5971dab5cd05SOleg Bulyzhin 
5972dab5cd05SOleg Bulyzhin static void
59733f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
5974dab5cd05SOleg Bulyzhin {
59751f313773SOleg Bulyzhin 	struct mii_data *mii;
59761f313773SOleg Bulyzhin 	uint32_t link, status;
5977dab5cd05SOleg Bulyzhin 
5978dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
59791f313773SOleg Bulyzhin 
59803f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
59817b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
59827b97099dSOleg Bulyzhin 
5983dab5cd05SOleg Bulyzhin 	/*
5984dab5cd05SOleg Bulyzhin 	 * Process link state changes.
5985dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
5986dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
5987dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
5988dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
5989dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
5990dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
5991dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
5992dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
59931f313773SOleg Bulyzhin 	 *
59941f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
59954c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
5996dab5cd05SOleg Bulyzhin 	 */
5997dab5cd05SOleg Bulyzhin 
59981f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
59994c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
6000dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
6001dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
60021f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
60035dda8085SOleg Bulyzhin 			mii_pollstat(mii);
60041f313773SOleg Bulyzhin 			if (!sc->bge_link &&
60051f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
60061f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
60071f313773SOleg Bulyzhin 				sc->bge_link++;
60081f313773SOleg Bulyzhin 				if (bootverbose)
60091f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
60101f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
60111f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
60121f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
60131f313773SOleg Bulyzhin 				sc->bge_link = 0;
60141f313773SOleg Bulyzhin 				if (bootverbose)
60151f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
60161f313773SOleg Bulyzhin 			}
60171f313773SOleg Bulyzhin 
60183f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6019dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6020dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6021daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6022daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6023daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6024daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6025dab5cd05SOleg Bulyzhin 		}
6026dab5cd05SOleg Bulyzhin 		return;
6027dab5cd05SOleg Bulyzhin 	}
6028dab5cd05SOleg Bulyzhin 
6029652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
60301f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
60317b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
60327b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
60331f313773SOleg Bulyzhin 				sc->bge_link++;
60349b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
60351f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
60361f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
60379b80ffe7SPyun YongHyeon 					DELAY(40);
60389b80ffe7SPyun YongHyeon 				}
60390c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
60401f313773SOleg Bulyzhin 				if (bootverbose)
60411f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
60423f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
60433f74909aSGleb Smirnoff 				    LINK_STATE_UP);
60447b97099dSOleg Bulyzhin 			}
60451f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6046dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
60471f313773SOleg Bulyzhin 			if (bootverbose)
60481f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
60497b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
60501f313773SOleg Bulyzhin 		}
60516ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
60521f313773SOleg Bulyzhin 		/*
60530c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
60540c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
60550c8aa4eaSJung-uk Kim 		 * PHY link status directly.
60561f313773SOleg Bulyzhin 		 */
60571f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
60581f313773SOleg Bulyzhin 
60591f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
60601f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
60611f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
60625dda8085SOleg Bulyzhin 			mii_pollstat(mii);
60631f313773SOleg Bulyzhin 			if (!sc->bge_link &&
60641f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
60651f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
60661f313773SOleg Bulyzhin 				sc->bge_link++;
60671f313773SOleg Bulyzhin 				if (bootverbose)
60681f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
60691f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
60701f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
60711f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
60721f313773SOleg Bulyzhin 				sc->bge_link = 0;
60731f313773SOleg Bulyzhin 				if (bootverbose)
60741f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
60751f313773SOleg Bulyzhin 			}
60761f313773SOleg Bulyzhin 		}
60770c8aa4eaSJung-uk Kim 	} else {
60780c8aa4eaSJung-uk Kim 		/*
60796ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
60806ede2cfaSPyun YongHyeon 		 * link status.
60810c8aa4eaSJung-uk Kim 		 */
60826ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
60836ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
60846ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6085dab5cd05SOleg Bulyzhin 	}
6086dab5cd05SOleg Bulyzhin 
60872246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6088dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6089dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6090dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6091dab5cd05SOleg Bulyzhin }
60926f8718a3SScott Long 
60936f8718a3SScott Long static void
60946f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
60956f8718a3SScott Long {
60966f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
60972280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
60987e32f79aSPyun YongHyeon 	char tn[32];
60997e32f79aSPyun YongHyeon 	int unit;
61006f8718a3SScott Long 
61016f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
61026f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
61036f8718a3SScott Long 
61046f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
61056f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
61066f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
61076f8718a3SScott Long 	    "Debug Information");
61086f8718a3SScott Long 
61096f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
61106f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
6111548c8f1aSPyun YongHyeon 	    "MAC Register Read");
6112548c8f1aSPyun YongHyeon 
6113548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
6114548c8f1aSPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_ape_read, "I",
6115548c8f1aSPyun YongHyeon 	    "APE Register Read");
61166f8718a3SScott Long 
61176f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
61186f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
61196f8718a3SScott Long 	    "Memory Read");
61206f8718a3SScott Long 
61216f8718a3SScott Long #endif
6122763757b2SScott Long 
61237e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
6124beaa2ae1SPyun YongHyeon 	/*
6125beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6126beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6127beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6128beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6129beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6130beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6131beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6132beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6133beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6134beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6135beaa2ae1SPyun YongHyeon 	 */
61367e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
61377e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
61387e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
6139beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6140beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
6141beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6142beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6143beaa2ae1SPyun YongHyeon 
61442ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
61452ae7f64bSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.msi", unit);
61462ae7f64bSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_msi);
61472ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
61482ae7f64bSPyun YongHyeon 	    CTLFLAG_RD, &sc->bge_msi, 0, "Enable MSI");
61495c952e8dSPyun YongHyeon 
615035f945cdSPyun YongHyeon 	/*
615135f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
615235f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
615335f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
615435f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
615535f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
615635f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
615735f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
615835f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
615935f945cdSPyun YongHyeon 	 */
616035f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
616135f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
616235f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
616335f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
616435f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
616535f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
616635f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
616735f945cdSPyun YongHyeon 
6168d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
61692280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
61702280c16bSPyun YongHyeon 	else
61712280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
61722280c16bSPyun YongHyeon }
6173d949071dSJung-uk Kim 
61742280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
61752280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
61762280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
61772280c16bSPyun YongHyeon 	    desc)
61782280c16bSPyun YongHyeon 
61792280c16bSPyun YongHyeon static void
61802280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
61812280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
61822280c16bSPyun YongHyeon {
61832280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
61842280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
61852280c16bSPyun YongHyeon 
61862280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6187763757b2SScott Long 	    NULL, "BGE Statistics");
6188763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6189763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6190763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6191763757b2SScott Long 	    "FramesDroppedDueToFilters");
6192763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6193763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6194763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6195763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6196763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6197763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
619806e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
619906e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
620006e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
620106e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6202763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6203763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6204763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6205763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6206763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6207763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6208763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6209763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6210763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6211763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6212763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6213763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6214763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6215763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6216763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6217763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6218763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6219763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6220763757b2SScott Long 
6221763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
6222763757b2SScott Long 	    NULL, "BGE RX Statistics");
6223763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6224763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
62251cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6226763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6227763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6228763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
62291cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6230763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6231763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6232763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6233763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6234763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6235763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6236763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6237763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6238763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6239763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6240763757b2SScott Long 	    "xoffPauseFramesReceived");
6241763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6242763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6243763757b2SScott Long 	    "ControlFramesReceived");
6244763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6245763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6246763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6247763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6248763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6249763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6250763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6251763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6252763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
625306e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6254763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
625506e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6256763757b2SScott Long 
6257763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
6258763757b2SScott Long 	    NULL, "BGE TX Statistics");
6259763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6260763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
62611cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6262763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6263763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6264763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6265763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6266763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6267763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6268763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6269763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6270763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6271763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6272763757b2SScott Long 	    "InternalMacTransmitErrors");
6273763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6274763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6275763757b2SScott Long 	    "SingleCollisionFrames");
6276763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6277763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6278763757b2SScott Long 	    "MultipleCollisionFrames");
6279763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6280763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6281763757b2SScott Long 	    "DeferredTransmissions");
6282763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6283763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6284763757b2SScott Long 	    "ExcessiveCollisions");
6285763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
628606e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
628706e83c7eSScott Long 	    "LateCollisions");
6288763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
62891cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6290763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6291763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6292763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6293763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6294763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6295763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6296763757b2SScott Long 	    "CarrierSenseErrors");
6297763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6298763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6299763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6300763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6301763757b2SScott Long }
6302763757b2SScott Long 
63032280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
63042280c16bSPyun YongHyeon 
63052280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
63066dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
63072280c16bSPyun YongHyeon 
63082280c16bSPyun YongHyeon static void
63092280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
63102280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
63112280c16bSPyun YongHyeon {
63122280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
63132280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
63142280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
63152280c16bSPyun YongHyeon 
63162280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
63172280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
63182280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
63192280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
63202280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
63212280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
63222280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
63232280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
63242280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
63252280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
63262280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
63272280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
63282280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
63292280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
63302280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
63312280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
63322280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
63332280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
63342280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
63352280c16bSPyun YongHyeon 
63362280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
63372280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
63382280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
63392280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
63402280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
63412280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
63422280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
63431cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
63442280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
63452280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
63462280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
63472280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
63482280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
63492280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
63502280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
63512280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
63522280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
63532280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
63542280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
63552280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
63562280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
63572280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
63582280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
63592280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
63602280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
63612280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
63622280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
63632280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
63642280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
63652280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
63662280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
63672280c16bSPyun YongHyeon 
63682280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
63692280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
63702280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
63711cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
63722280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
63732280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
63742280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
63752280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
63762280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
63772280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
63782280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
63792280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
63802280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
63812280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
63822280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
63832280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
63842280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
63852280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
63862280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
63872280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
63882280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
63892280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
63902280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
63912280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
63922280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
63931cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
63942280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
63951cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
63962280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
63971cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
63982280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
63992280c16bSPyun YongHyeon }
64002280c16bSPyun YongHyeon 
64012280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
64022280c16bSPyun YongHyeon 
6403763757b2SScott Long static int
6404763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6405763757b2SScott Long {
6406763757b2SScott Long 	struct bge_softc *sc;
640706e83c7eSScott Long 	uint32_t result;
6408d949071dSJung-uk Kim 	int offset;
6409763757b2SScott Long 
6410763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6411763757b2SScott Long 	offset = arg2;
6412d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6413d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6414041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
64156f8718a3SScott Long }
64166f8718a3SScott Long 
64176f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
64186f8718a3SScott Long static int
64196f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
64206f8718a3SScott Long {
64216f8718a3SScott Long 	struct bge_softc *sc;
64226f8718a3SScott Long 	uint16_t *sbdata;
642328276ad6SPyun YongHyeon 	int error, result, sbsz;
64246f8718a3SScott Long 	int i, j;
64256f8718a3SScott Long 
64266f8718a3SScott Long 	result = -1;
64276f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
64286f8718a3SScott Long 	if (error || (req->newptr == NULL))
64296f8718a3SScott Long 		return (error);
64306f8718a3SScott Long 
64316f8718a3SScott Long 	if (result == 1) {
64326f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
64336f8718a3SScott Long 
643428276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
643528276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
643628276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
643728276ad6SPyun YongHyeon 		else
643828276ad6SPyun YongHyeon 			sbsz = 32;
64396f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
64406f8718a3SScott Long 		printf("Status Block:\n");
644128276ad6SPyun YongHyeon 		BGE_LOCK(sc);
644228276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
644328276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
644428276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
644528276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
64466f8718a3SScott Long 			printf("%06x:", i);
644728276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
644828276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
64496f8718a3SScott Long 			printf("\n");
64506f8718a3SScott Long 		}
64516f8718a3SScott Long 
64526f8718a3SScott Long 		printf("Registers:\n");
64530c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
64546f8718a3SScott Long 			printf("%06x:", i);
64556f8718a3SScott Long 			for (j = 0; j < 8; j++) {
64566f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
64576f8718a3SScott Long 				i += 4;
64586f8718a3SScott Long 			}
64596f8718a3SScott Long 			printf("\n");
64606f8718a3SScott Long 		}
646128276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
64626f8718a3SScott Long 
64636f8718a3SScott Long 		printf("Hardware Flags:\n");
646428276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
646528276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6466a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6467a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
64685345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
64696f8718a3SScott Long 			printf(" - 575X Plus\n");
64705345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
64716f8718a3SScott Long 			printf(" - 5705 Plus\n");
64725345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
64735345bad0SScott Long 			printf(" - 5714 Family\n");
64745345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
64755345bad0SScott Long 			printf(" - 5700 Family\n");
64766f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
64776f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
64786f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
64796f8718a3SScott Long 			printf(" - PCI-X Bus\n");
64806f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
64816f8718a3SScott Long 			printf(" - PCI Express Bus\n");
64827d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
64836f8718a3SScott Long 			printf(" - No 3 LEDs\n");
64846f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
64856f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
64866f8718a3SScott Long 	}
64876f8718a3SScott Long 
64886f8718a3SScott Long 	return (error);
64896f8718a3SScott Long }
64906f8718a3SScott Long 
64916f8718a3SScott Long static int
64926f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
64936f8718a3SScott Long {
64946f8718a3SScott Long 	struct bge_softc *sc;
64956f8718a3SScott Long 	int error;
64966f8718a3SScott Long 	uint16_t result;
64976f8718a3SScott Long 	uint32_t val;
64986f8718a3SScott Long 
64996f8718a3SScott Long 	result = -1;
65006f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65016f8718a3SScott Long 	if (error || (req->newptr == NULL))
65026f8718a3SScott Long 		return (error);
65036f8718a3SScott Long 
65046f8718a3SScott Long 	if (result < 0x8000) {
65056f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65066f8718a3SScott Long 		val = CSR_READ_4(sc, result);
65076f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
65086f8718a3SScott Long 	}
65096f8718a3SScott Long 
65106f8718a3SScott Long 	return (error);
65116f8718a3SScott Long }
65126f8718a3SScott Long 
65136f8718a3SScott Long static int
6514548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6515548c8f1aSPyun YongHyeon {
6516548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6517548c8f1aSPyun YongHyeon 	int error;
6518548c8f1aSPyun YongHyeon 	uint16_t result;
6519548c8f1aSPyun YongHyeon 	uint32_t val;
6520548c8f1aSPyun YongHyeon 
6521548c8f1aSPyun YongHyeon 	result = -1;
6522548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6523548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6524548c8f1aSPyun YongHyeon 		return (error);
6525548c8f1aSPyun YongHyeon 
6526548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6527548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6528548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6529548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6530548c8f1aSPyun YongHyeon 	}
6531548c8f1aSPyun YongHyeon 
6532548c8f1aSPyun YongHyeon 	return (error);
6533548c8f1aSPyun YongHyeon }
6534548c8f1aSPyun YongHyeon 
6535548c8f1aSPyun YongHyeon static int
65366f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
65376f8718a3SScott Long {
65386f8718a3SScott Long 	struct bge_softc *sc;
65396f8718a3SScott Long 	int error;
65406f8718a3SScott Long 	uint16_t result;
65416f8718a3SScott Long 	uint32_t val;
65426f8718a3SScott Long 
65436f8718a3SScott Long 	result = -1;
65446f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65456f8718a3SScott Long 	if (error || (req->newptr == NULL))
65466f8718a3SScott Long 		return (error);
65476f8718a3SScott Long 
65486f8718a3SScott Long 	if (result < 0x8000) {
65496f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65506f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
65516f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
65526f8718a3SScott Long 	}
65536f8718a3SScott Long 
65546f8718a3SScott Long 	return (error);
65556f8718a3SScott Long }
65566f8718a3SScott Long #endif
655738cc658fSJohn Baldwin 
655838cc658fSJohn Baldwin static int
65595fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
65605fea260fSMarius Strobl {
65615fea260fSMarius Strobl 
65625fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
65635fea260fSMarius Strobl 		return (1);
65645fea260fSMarius Strobl 
65655fea260fSMarius Strobl #ifdef __sparc64__
65665fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
65675fea260fSMarius Strobl 	return (0);
65685fea260fSMarius Strobl #endif
65695fea260fSMarius Strobl 	return (1);
65705fea260fSMarius Strobl }
65715fea260fSMarius Strobl 
65725fea260fSMarius Strobl static int
657338cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
657438cc658fSJohn Baldwin {
657538cc658fSJohn Baldwin 	uint32_t mac_addr;
657638cc658fSJohn Baldwin 
657773635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
657838cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
657938cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
658038cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
658173635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
658238cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
658338cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
658438cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
658538cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
65865fea260fSMarius Strobl 		return (0);
658738cc658fSJohn Baldwin 	}
65885fea260fSMarius Strobl 	return (1);
658938cc658fSJohn Baldwin }
659038cc658fSJohn Baldwin 
659138cc658fSJohn Baldwin static int
659238cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
659338cc658fSJohn Baldwin {
659438cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
659538cc658fSJohn Baldwin 
659638cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
659738cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
659838cc658fSJohn Baldwin 
65995fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
66005fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
660138cc658fSJohn Baldwin }
660238cc658fSJohn Baldwin 
660338cc658fSJohn Baldwin static int
660438cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
660538cc658fSJohn Baldwin {
660638cc658fSJohn Baldwin 
66075fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
66085fea260fSMarius Strobl 		return (1);
66095fea260fSMarius Strobl 
66105fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
66115fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
661238cc658fSJohn Baldwin }
661338cc658fSJohn Baldwin 
661438cc658fSJohn Baldwin static int
661538cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
661638cc658fSJohn Baldwin {
661738cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
661838cc658fSJohn Baldwin 		/* NOTE: Order is critical */
66195fea260fSMarius Strobl 		bge_get_eaddr_fw,
662038cc658fSJohn Baldwin 		bge_get_eaddr_mem,
662138cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
662238cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
662338cc658fSJohn Baldwin 		NULL
662438cc658fSJohn Baldwin 	};
662538cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
662638cc658fSJohn Baldwin 
662738cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
662838cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
662938cc658fSJohn Baldwin 			break;
663038cc658fSJohn Baldwin 	}
663138cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
663238cc658fSJohn Baldwin }
6633