xref: /freebsd/sys/dev/bge/if_bge.c (revision d4f5240abd4fc94ddf521ce8a60e0c763b4ddf31)
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
4295d67482SBill Paul  * MAC chips. The BCM5700, sometimes refered 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;
1424c0da0ffSGleb Smirnoff } 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 },
1724c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1734c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
174effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
175a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5723 },
1764c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1774c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1784c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1834c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1844c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1854c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1869e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1879e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1889e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1899e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
190f7d1b2ebSXin LI 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5756 },
191a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761 },
192a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761E },
193a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761S },
194a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761SE },
195a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5764 },
1964c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
1974c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
1984c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
1994c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
200a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5784 },
201a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785F },
202a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785G },
2039e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
2049e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
205a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787F },
2069e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
2074c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
2084c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
2094c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
2104c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
2114c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
21238cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
21338cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
214a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57760 },
215a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
216a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
217a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
2184c0da0ffSGleb Smirnoff 
2194c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2204c0da0ffSGleb Smirnoff 
2214c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2224c0da0ffSGleb Smirnoff 
223a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
224a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
225a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PP250450 },
226a5779553SStanislav Sedov 
2274c0da0ffSGleb Smirnoff 	{ 0, 0 }
22895d67482SBill Paul };
22995d67482SBill Paul 
2304c0da0ffSGleb Smirnoff static const struct bge_vendor {
2314c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2324c0da0ffSGleb Smirnoff 	const char	*v_name;
2334c0da0ffSGleb Smirnoff } bge_vendors[] = {
2344c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2354c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2364c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2374c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2384c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2394c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
240a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2414c0da0ffSGleb Smirnoff 
2424c0da0ffSGleb Smirnoff 	{ 0, NULL }
2434c0da0ffSGleb Smirnoff };
2444c0da0ffSGleb Smirnoff 
2454c0da0ffSGleb Smirnoff static const struct bge_revision {
2464c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2474c0da0ffSGleb Smirnoff 	const char	*br_name;
2484c0da0ffSGleb Smirnoff } bge_revisions[] = {
2494c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2504c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2514c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2524c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2534c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2544c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2554c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2564c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2574c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2594c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2604c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2614c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2624c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2659e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2674c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2684c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2694c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2704c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2714c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2724c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2744c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2764c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2794c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
28242787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2844c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2854c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2884c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2894c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2904c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
2910c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
2920c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
2930c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
2940c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
295bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
296a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
297a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
298a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
299a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
30081179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3016f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
3026f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3036f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
30438cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
30538cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
306a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
307a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3084c0da0ffSGleb Smirnoff 
3094c0da0ffSGleb Smirnoff 	{ 0, NULL }
3104c0da0ffSGleb Smirnoff };
3114c0da0ffSGleb Smirnoff 
3124c0da0ffSGleb Smirnoff /*
3134c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3144c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3154c0da0ffSGleb Smirnoff  */
3164c0da0ffSGleb Smirnoff static const struct bge_revision bge_majorrevs[] = {
3179e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3189e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3199e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3209e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3219e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3229e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3239e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3249e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3259e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3269e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3279e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
328a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
329a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
330a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
33181179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3326f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
33338cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
334a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3354c0da0ffSGleb Smirnoff 
3364c0da0ffSGleb Smirnoff 	{ 0, NULL }
3374c0da0ffSGleb Smirnoff };
3384c0da0ffSGleb Smirnoff 
3390c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3400c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3410c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3420c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3430c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
344a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3454c0da0ffSGleb Smirnoff 
3464c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3474c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
34838cc658fSJohn Baldwin 
34938cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
35038cc658fSJohn Baldwin 
351e51a25f8SAlfred Perlstein static int bge_probe(device_t);
352e51a25f8SAlfred Perlstein static int bge_attach(device_t);
353e51a25f8SAlfred Perlstein static int bge_detach(device_t);
35414afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
35514afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3563f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
357f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3585b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
359f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
3605b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
3615b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
362f41ac2beSBill Paul 
3635fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
36438cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
36538cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
36638cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
36738cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
36838cc658fSJohn Baldwin 
369b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
370dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
37195d67482SBill Paul 
3728cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
373e51a25f8SAlfred Perlstein static void bge_tick(void *);
3742280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
375e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3763f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
3772e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
3782e1d4df4SPyun YongHyeon     uint16_t *);
379676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
38095d67482SBill Paul 
381e51a25f8SAlfred Perlstein static void bge_intr(void *);
382dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
383dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
3840f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
385e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
386e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
3870f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
388e51a25f8SAlfred Perlstein static void bge_init(void *);
389e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
390b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
391b6c974e8SWarner Losh static int bge_shutdown(device_t);
39267d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
393e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
394e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
39595d67482SBill Paul 
39638cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
39738cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
39838cc658fSJohn Baldwin 
3993f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
400e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
40195d67482SBill Paul 
4023e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
403e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
404cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
40595d67482SBill Paul 
406e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
407e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
408943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
409943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
410e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
411e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
412e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
413e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
414e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
415e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
41695d67482SBill Paul 
417e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
418e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
41995d67482SBill Paul 
4205fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4213f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
422e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
42338cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
42495d67482SBill Paul #ifdef notdef
4253f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
42695d67482SBill Paul #endif
4279ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
428e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
42995d67482SBill Paul 
430e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
431e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
432e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
43375719184SGleb Smirnoff #ifdef DEVICE_POLLING
4341abcdbd1SAttilio Rao static int bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
43575719184SGleb Smirnoff #endif
43695d67482SBill Paul 
4378cb1383cSDoug Ambrisko #define	BGE_RESET_START 1
4388cb1383cSDoug Ambrisko #define	BGE_RESET_STOP  2
4398cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4408cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4418cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
442797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4438cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
444dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
44595d67482SBill Paul 
4466f8718a3SScott Long /*
4476f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4486f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4496f8718a3SScott Long  * traps on certain architectures.
4506f8718a3SScott Long  */
4516f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4526f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4536f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
4546f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
4556f8718a3SScott Long #endif
4566f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
4572280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
4582280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
4592280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
4602280c16bSPyun YongHyeon     struct sysctl_oid_list *);
461763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
4626f8718a3SScott Long 
46395d67482SBill Paul static device_method_t bge_methods[] = {
46495d67482SBill Paul 	/* Device interface */
46595d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
46695d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
46795d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
46895d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
46914afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
47014afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
47195d67482SBill Paul 
47295d67482SBill Paul 	/* bus interface */
47395d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
47495d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
47595d67482SBill Paul 
47695d67482SBill Paul 	/* MII interface */
47795d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
47895d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
47995d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
48095d67482SBill Paul 
48195d67482SBill Paul 	{ 0, 0 }
48295d67482SBill Paul };
48395d67482SBill Paul 
48495d67482SBill Paul static driver_t bge_driver = {
48595d67482SBill Paul 	"bge",
48695d67482SBill Paul 	bge_methods,
48795d67482SBill Paul 	sizeof(struct bge_softc)
48895d67482SBill Paul };
48995d67482SBill Paul 
49095d67482SBill Paul static devclass_t bge_devclass;
49195d67482SBill Paul 
492f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
49395d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
49495d67482SBill Paul 
495f1a7e6d5SScott Long static int bge_allow_asf = 1;
496f1a7e6d5SScott Long 
497f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
498f1a7e6d5SScott Long 
499f1a7e6d5SScott Long SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
500f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
501f1a7e6d5SScott Long 	"Allow ASF mode if available");
502c4529f41SMichael Reifenberger 
50308013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
50408013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
50508013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
50608013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
50708013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
50808013fd3SMarius Strobl 
50908013fd3SMarius Strobl static int
5105fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
51108013fd3SMarius Strobl {
51208013fd3SMarius Strobl #ifdef __sparc64__
51308013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
51408013fd3SMarius Strobl 	device_t dev;
51508013fd3SMarius Strobl 	uint32_t subvendor;
51608013fd3SMarius Strobl 
51708013fd3SMarius Strobl 	dev = sc->bge_dev;
51808013fd3SMarius Strobl 
51908013fd3SMarius Strobl 	/*
52008013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
52108013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
52208013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
52308013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
52408013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
52508013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
52608013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
52708013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
52808013fd3SMarius Strobl 	 * there.
52908013fd3SMarius Strobl 	 */
53008013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
53108013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5322d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
53308013fd3SMarius Strobl 		return (0);
53408013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
53508013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
53608013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
53708013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
53808013fd3SMarius Strobl 			return (0);
53908013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
54008013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
54108013fd3SMarius Strobl 			return (0);
54208013fd3SMarius Strobl 	}
54308013fd3SMarius Strobl #endif
54408013fd3SMarius Strobl 	return (1);
54508013fd3SMarius Strobl }
54608013fd3SMarius Strobl 
5473f74909aSGleb Smirnoff static uint32_t
5483f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
54995d67482SBill Paul {
55095d67482SBill Paul 	device_t dev;
5516f8718a3SScott Long 	uint32_t val;
55295d67482SBill Paul 
553a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
554a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
555a4431ebaSPyun YongHyeon 		return (0);
556a4431ebaSPyun YongHyeon 
55795d67482SBill Paul 	dev = sc->bge_dev;
55895d67482SBill Paul 
55995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5606f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5616f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5626f8718a3SScott Long 	return (val);
56395d67482SBill Paul }
56495d67482SBill Paul 
56595d67482SBill Paul static void
5663f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
56795d67482SBill Paul {
56895d67482SBill Paul 	device_t dev;
56995d67482SBill Paul 
570a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
571a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
572a4431ebaSPyun YongHyeon 		return;
573a4431ebaSPyun YongHyeon 
57495d67482SBill Paul 	dev = sc->bge_dev;
57595d67482SBill Paul 
57695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
57795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
5786f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
57995d67482SBill Paul }
58095d67482SBill Paul 
58195d67482SBill Paul #ifdef notdef
5823f74909aSGleb Smirnoff static uint32_t
5833f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
58495d67482SBill Paul {
58595d67482SBill Paul 	device_t dev;
58695d67482SBill Paul 
58795d67482SBill Paul 	dev = sc->bge_dev;
58895d67482SBill Paul 
58995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
59095d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
59195d67482SBill Paul }
59295d67482SBill Paul #endif
59395d67482SBill Paul 
59495d67482SBill Paul static void
5953f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
59695d67482SBill Paul {
59795d67482SBill Paul 	device_t dev;
59895d67482SBill Paul 
59995d67482SBill Paul 	dev = sc->bge_dev;
60095d67482SBill Paul 
60195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
60295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
60395d67482SBill Paul }
60495d67482SBill Paul 
6056f8718a3SScott Long static void
6066f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6076f8718a3SScott Long {
6086f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6096f8718a3SScott Long }
6106f8718a3SScott Long 
61138cc658fSJohn Baldwin static void
61238cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
61338cc658fSJohn Baldwin {
61438cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
61538cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
61638cc658fSJohn Baldwin 
61738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
61838cc658fSJohn Baldwin }
61938cc658fSJohn Baldwin 
620f41ac2beSBill Paul /*
621f41ac2beSBill Paul  * Map a single buffer address.
622f41ac2beSBill Paul  */
623f41ac2beSBill Paul 
624f41ac2beSBill Paul static void
6253f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
626f41ac2beSBill Paul {
627f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
628f41ac2beSBill Paul 
629f41ac2beSBill Paul 	if (error)
630f41ac2beSBill Paul 		return;
631f41ac2beSBill Paul 
6325b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
6335b610048SPyun YongHyeon 
634f41ac2beSBill Paul 	ctx = arg;
635f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
636f41ac2beSBill Paul }
637f41ac2beSBill Paul 
63838cc658fSJohn Baldwin static uint8_t
63938cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
64038cc658fSJohn Baldwin {
64138cc658fSJohn Baldwin 	uint32_t access, byte = 0;
64238cc658fSJohn Baldwin 	int i;
64338cc658fSJohn Baldwin 
64438cc658fSJohn Baldwin 	/* Lock. */
64538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
64638cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
64738cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
64838cc658fSJohn Baldwin 			break;
64938cc658fSJohn Baldwin 		DELAY(20);
65038cc658fSJohn Baldwin 	}
65138cc658fSJohn Baldwin 	if (i == 8000)
65238cc658fSJohn Baldwin 		return (1);
65338cc658fSJohn Baldwin 
65438cc658fSJohn Baldwin 	/* Enable access. */
65538cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
65638cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
65738cc658fSJohn Baldwin 
65838cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
65938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
66038cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
66138cc658fSJohn Baldwin 		DELAY(10);
66238cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
66338cc658fSJohn Baldwin 			DELAY(10);
66438cc658fSJohn Baldwin 			break;
66538cc658fSJohn Baldwin 		}
66638cc658fSJohn Baldwin 	}
66738cc658fSJohn Baldwin 
66838cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
66938cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
67038cc658fSJohn Baldwin 		return (1);
67138cc658fSJohn Baldwin 	}
67238cc658fSJohn Baldwin 
67338cc658fSJohn Baldwin 	/* Get result. */
67438cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
67538cc658fSJohn Baldwin 
67638cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
67738cc658fSJohn Baldwin 
67838cc658fSJohn Baldwin 	/* Disable access. */
67938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
68038cc658fSJohn Baldwin 
68138cc658fSJohn Baldwin 	/* Unlock. */
68238cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
68338cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
68438cc658fSJohn Baldwin 
68538cc658fSJohn Baldwin 	return (0);
68638cc658fSJohn Baldwin }
68738cc658fSJohn Baldwin 
68838cc658fSJohn Baldwin /*
68938cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
69038cc658fSJohn Baldwin  */
69138cc658fSJohn Baldwin static int
69238cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
69338cc658fSJohn Baldwin {
69438cc658fSJohn Baldwin 	int err = 0, i;
69538cc658fSJohn Baldwin 	uint8_t byte = 0;
69638cc658fSJohn Baldwin 
69738cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
69838cc658fSJohn Baldwin 		return (1);
69938cc658fSJohn Baldwin 
70038cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
70138cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
70238cc658fSJohn Baldwin 		if (err)
70338cc658fSJohn Baldwin 			break;
70438cc658fSJohn Baldwin 		*(dest + i) = byte;
70538cc658fSJohn Baldwin 	}
70638cc658fSJohn Baldwin 
70738cc658fSJohn Baldwin 	return (err ? 1 : 0);
70838cc658fSJohn Baldwin }
70938cc658fSJohn Baldwin 
71095d67482SBill Paul /*
71195d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
71295d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
71395d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
71495d67482SBill Paul  * access method.
71595d67482SBill Paul  */
7163f74909aSGleb Smirnoff static uint8_t
7173f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
71895d67482SBill Paul {
71995d67482SBill Paul 	int i;
7203f74909aSGleb Smirnoff 	uint32_t byte = 0;
72195d67482SBill Paul 
72295d67482SBill Paul 	/*
72395d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
72495d67482SBill Paul 	 * having to use the bitbang method.
72595d67482SBill Paul 	 */
72695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
72795d67482SBill Paul 
72895d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
72995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
73095d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
73195d67482SBill Paul 	DELAY(20);
73295d67482SBill Paul 
73395d67482SBill Paul 	/* Issue the read EEPROM command. */
73495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
73595d67482SBill Paul 
73695d67482SBill Paul 	/* Wait for completion */
73795d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
73895d67482SBill Paul 		DELAY(10);
73995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
74095d67482SBill Paul 			break;
74195d67482SBill Paul 	}
74295d67482SBill Paul 
743d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
744fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
745f6789fbaSPyun YongHyeon 		return (1);
74695d67482SBill Paul 	}
74795d67482SBill Paul 
74895d67482SBill Paul 	/* Get result. */
74995d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
75095d67482SBill Paul 
7510c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
75295d67482SBill Paul 
75395d67482SBill Paul 	return (0);
75495d67482SBill Paul }
75595d67482SBill Paul 
75695d67482SBill Paul /*
75795d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
75895d67482SBill Paul  */
75995d67482SBill Paul static int
7603f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
76195d67482SBill Paul {
7623f74909aSGleb Smirnoff 	int i, error = 0;
7633f74909aSGleb Smirnoff 	uint8_t byte = 0;
76495d67482SBill Paul 
76595d67482SBill Paul 	for (i = 0; i < cnt; i++) {
7663f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
7673f74909aSGleb Smirnoff 		if (error)
76895d67482SBill Paul 			break;
76995d67482SBill Paul 		*(dest + i) = byte;
77095d67482SBill Paul 	}
77195d67482SBill Paul 
7723f74909aSGleb Smirnoff 	return (error ? 1 : 0);
77395d67482SBill Paul }
77495d67482SBill Paul 
77595d67482SBill Paul static int
7763f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
77795d67482SBill Paul {
77895d67482SBill Paul 	struct bge_softc *sc;
779a813ed78SPyun YongHyeon 	uint32_t val;
78095d67482SBill Paul 	int i;
78195d67482SBill Paul 
78295d67482SBill Paul 	sc = device_get_softc(dev);
78395d67482SBill Paul 
784a813ed78SPyun YongHyeon 	/* Prevent the probe from finding incorrect devices. */
785a813ed78SPyun YongHyeon 	if (phy != sc->bge_phy_addr)
78698b28ee5SBill Paul 		return (0);
78798b28ee5SBill Paul 
788a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
789a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
790a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
791a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
792a813ed78SPyun YongHyeon 		DELAY(80);
79337ceeb4dSPaul Saab 	}
79437ceeb4dSPaul Saab 
79595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
79695d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
79795d67482SBill Paul 
798a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
79995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
800d5d23857SJung-uk Kim 		DELAY(10);
80195d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
802a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
803a813ed78SPyun YongHyeon 			DELAY(5);
804a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
80595d67482SBill Paul 			break;
80695d67482SBill Paul 		}
807a813ed78SPyun YongHyeon 	}
80895d67482SBill Paul 
80995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
8105fea260fSMarius Strobl 		device_printf(sc->bge_dev,
8115fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
8125fea260fSMarius Strobl 		    phy, reg, val);
81337ceeb4dSPaul Saab 		val = 0;
81495d67482SBill Paul 	}
81595d67482SBill Paul 
816a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
817a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
818a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
819a813ed78SPyun YongHyeon 		DELAY(80);
82037ceeb4dSPaul Saab 	}
82137ceeb4dSPaul Saab 
82295d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
82395d67482SBill Paul 		return (0);
82495d67482SBill Paul 
8250c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
82695d67482SBill Paul }
82795d67482SBill Paul 
82895d67482SBill Paul static int
8293f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
83095d67482SBill Paul {
83195d67482SBill Paul 	struct bge_softc *sc;
83295d67482SBill Paul 	int i;
83395d67482SBill Paul 
83495d67482SBill Paul 	sc = device_get_softc(dev);
83595d67482SBill Paul 
83638cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
83738cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
83838cc658fSJohn Baldwin 		return (0);
83938cc658fSJohn Baldwin 
840a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
841a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
842a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
843a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
844a813ed78SPyun YongHyeon 		DELAY(80);
84537ceeb4dSPaul Saab 	}
84637ceeb4dSPaul Saab 
84795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
84895d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
84995d67482SBill Paul 
85095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
851d5d23857SJung-uk Kim 		DELAY(10);
85238cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
85338cc658fSJohn Baldwin 			DELAY(5);
85438cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
85595d67482SBill Paul 			break;
856d5d23857SJung-uk Kim 		}
85738cc658fSJohn Baldwin 	}
858d5d23857SJung-uk Kim 
859a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
860a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
861a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
862a813ed78SPyun YongHyeon 		DELAY(80);
863a813ed78SPyun YongHyeon 	}
864a813ed78SPyun YongHyeon 
865a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
86638cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
86738cc658fSJohn Baldwin 		    "PHY write timed out (phy %d, reg %d, val %d)\n",
86838cc658fSJohn Baldwin 		    phy, reg, val);
86937ceeb4dSPaul Saab 
87095d67482SBill Paul 	return (0);
87195d67482SBill Paul }
87295d67482SBill Paul 
87395d67482SBill Paul static void
8743f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
87595d67482SBill Paul {
87695d67482SBill Paul 	struct bge_softc *sc;
87795d67482SBill Paul 	struct mii_data *mii;
87895d67482SBill Paul 	sc = device_get_softc(dev);
87995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
88095d67482SBill Paul 
881*d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
882*d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
883*d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
884*d4f5240aSPyun YongHyeon 		case IFM_10_T:
885*d4f5240aSPyun YongHyeon 		case IFM_100_TX:
886*d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
887*d4f5240aSPyun YongHyeon 			break;
888*d4f5240aSPyun YongHyeon 		case IFM_1000_T:
889*d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
890*d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
891*d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
892*d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
893*d4f5240aSPyun YongHyeon 			else
894*d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
895*d4f5240aSPyun YongHyeon 			break;
896*d4f5240aSPyun YongHyeon 		default:
897*d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
898*d4f5240aSPyun YongHyeon 			break;
899*d4f5240aSPyun YongHyeon 		}
900*d4f5240aSPyun YongHyeon 	} else
901*d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
902*d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
903*d4f5240aSPyun YongHyeon 		return;
90495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
905ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
906ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
90795d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
9083f74909aSGleb Smirnoff 	else
90995d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
91095d67482SBill Paul 
9116854be25SPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0) {
91295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
9136854be25SPyun YongHyeon 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_FLAG1)
9146854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
9153f74909aSGleb Smirnoff 		else
9166854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
9176854be25SPyun YongHyeon 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_FLAG0)
9186854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9196854be25SPyun YongHyeon 		else
9206854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9216854be25SPyun YongHyeon 	} else {
92295d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
9236854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
9246854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9256854be25SPyun YongHyeon 	}
92695d67482SBill Paul }
92795d67482SBill Paul 
92895d67482SBill Paul /*
92995d67482SBill Paul  * Intialize a standard receive ring descriptor.
93095d67482SBill Paul  */
93195d67482SBill Paul static int
932943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
93395d67482SBill Paul {
934943787f3SPyun YongHyeon 	struct mbuf *m;
93595d67482SBill Paul 	struct bge_rx_bd *r;
936a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
937943787f3SPyun YongHyeon 	bus_dmamap_t map;
938a23634a1SPyun YongHyeon 	int error, nsegs;
93995d67482SBill Paul 
940943787f3SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
941943787f3SPyun YongHyeon 	if (m == NULL)
94295d67482SBill Paul 		return (ENOBUFS);
943943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
944652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
945943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
946943787f3SPyun YongHyeon 
9470ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
948943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
949a23634a1SPyun YongHyeon 	if (error != 0) {
950943787f3SPyun YongHyeon 		m_freem(m);
951a23634a1SPyun YongHyeon 		return (error);
952f41ac2beSBill Paul 	}
953943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
954943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
955943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
956943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
957943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
958943787f3SPyun YongHyeon 	}
959943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
960943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
961943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
962943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
963e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
964943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
965a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
966a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
967e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
968a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
969e907febfSPyun YongHyeon 	r->bge_idx = i;
970f41ac2beSBill Paul 
9710ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
972943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
97395d67482SBill Paul 
97495d67482SBill Paul 	return (0);
97595d67482SBill Paul }
97695d67482SBill Paul 
97795d67482SBill Paul /*
97895d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
97995d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
98095d67482SBill Paul  */
98195d67482SBill Paul static int
982943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
98395d67482SBill Paul {
9841be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
985943787f3SPyun YongHyeon 	bus_dmamap_t map;
9861be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
987943787f3SPyun YongHyeon 	struct mbuf *m;
988943787f3SPyun YongHyeon 	int error, nsegs;
98995d67482SBill Paul 
990943787f3SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
991943787f3SPyun YongHyeon 	if (m == NULL)
99295d67482SBill Paul 		return (ENOBUFS);
99395d67482SBill Paul 
994943787f3SPyun YongHyeon 	m_cljget(m, M_DONTWAIT, MJUM9BYTES);
995943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
996943787f3SPyun YongHyeon 		m_freem(m);
99795d67482SBill Paul 		return (ENOBUFS);
99895d67482SBill Paul 	}
999943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1000652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1001943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
10021be6acb7SGleb Smirnoff 
10031be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1004943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1005943787f3SPyun YongHyeon 	if (error != 0) {
1006943787f3SPyun YongHyeon 		m_freem(m);
10071be6acb7SGleb Smirnoff 		return (error);
1008f7cea149SGleb Smirnoff 	}
10091be6acb7SGleb Smirnoff 
1010943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] == NULL) {
1011943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1012943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1013943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1014943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1015943787f3SPyun YongHyeon 	}
1016943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1017943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1018943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1019943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1020943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1021e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1022e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1023e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1024e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1025e0b7b101SPyun YongHyeon 
10261be6acb7SGleb Smirnoff 	/*
10271be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
10281be6acb7SGleb Smirnoff 	 */
1029943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
10304e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
10314e7ba1abSGleb Smirnoff 	r->bge_idx = i;
10324e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
10334e7ba1abSGleb Smirnoff 	switch (nsegs) {
10344e7ba1abSGleb Smirnoff 	case 4:
10354e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
10364e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
10374e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1038e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
10394e7ba1abSGleb Smirnoff 	case 3:
1040e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1041e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1042e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1043e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
10444e7ba1abSGleb Smirnoff 	case 2:
10454e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
10464e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
10474e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1048e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
10494e7ba1abSGleb Smirnoff 	case 1:
10504e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
10514e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
10524e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1053e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
10544e7ba1abSGleb Smirnoff 		break;
10554e7ba1abSGleb Smirnoff 	default:
10564e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
10574e7ba1abSGleb Smirnoff 	}
1058f41ac2beSBill Paul 
1059a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1060943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
106195d67482SBill Paul 
106295d67482SBill Paul 	return (0);
106395d67482SBill Paul }
106495d67482SBill Paul 
106595d67482SBill Paul static int
10663f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
106795d67482SBill Paul {
10683ee5d7daSPyun YongHyeon 	int error, i;
106995d67482SBill Paul 
1070e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
107103e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1072e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1073943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
10743ee5d7daSPyun YongHyeon 			return (error);
107503e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
10761888f324SPyun YongHyeon 	}
107795d67482SBill Paul 
1078f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1079d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1080f41ac2beSBill Paul 
1081e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1082e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
108395d67482SBill Paul 
108495d67482SBill Paul 	return (0);
108595d67482SBill Paul }
108695d67482SBill Paul 
108795d67482SBill Paul static void
10883f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
108995d67482SBill Paul {
109095d67482SBill Paul 	int i;
109195d67482SBill Paul 
109295d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
109395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
10940ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1095e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1096e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
10970ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1098f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1099e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1100e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
110195d67482SBill Paul 		}
1102f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
110395d67482SBill Paul 		    sizeof(struct bge_rx_bd));
110495d67482SBill Paul 	}
110595d67482SBill Paul }
110695d67482SBill Paul 
110795d67482SBill Paul static int
11083f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
110995d67482SBill Paul {
111095d67482SBill Paul 	struct bge_rcb *rcb;
11113ee5d7daSPyun YongHyeon 	int error, i;
111295d67482SBill Paul 
1113e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
111403e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
111595d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1116943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
11173ee5d7daSPyun YongHyeon 			return (error);
111803e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
11191888f324SPyun YongHyeon 	}
112095d67482SBill Paul 
1121f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1122d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1123f41ac2beSBill Paul 
1124e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
112595d67482SBill Paul 
11268a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1127f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
11288a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
11298a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
113067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
113195d67482SBill Paul 
1132e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
113395d67482SBill Paul 
113495d67482SBill Paul 	return (0);
113595d67482SBill Paul }
113695d67482SBill Paul 
113795d67482SBill Paul static void
11383f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
113995d67482SBill Paul {
114095d67482SBill Paul 	int i;
114195d67482SBill Paul 
114295d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
114395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1144e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1145e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1146e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1147f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1148f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1149e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1150e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
115195d67482SBill Paul 		}
1152f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
11531be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
115495d67482SBill Paul 	}
115595d67482SBill Paul }
115695d67482SBill Paul 
115795d67482SBill Paul static void
11583f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
115995d67482SBill Paul {
116095d67482SBill Paul 	int i;
116195d67482SBill Paul 
1162f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
116395d67482SBill Paul 		return;
116495d67482SBill Paul 
116595d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
116695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
11670ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1168e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1169e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
11700ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1171f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1172e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1173e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
117495d67482SBill Paul 		}
1175f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
117695d67482SBill Paul 		    sizeof(struct bge_tx_bd));
117795d67482SBill Paul 	}
117895d67482SBill Paul }
117995d67482SBill Paul 
118095d67482SBill Paul static int
11813f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
118295d67482SBill Paul {
118395d67482SBill Paul 	sc->bge_txcnt = 0;
118495d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
11853927098fSPaul Saab 
1186e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1187e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
11885c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1189e6bf277eSPyun YongHyeon 
119014bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
119114bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
119238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
119314bbd30fSGleb Smirnoff 
11943927098fSPaul Saab 	/* 5700 b2 errata */
1195e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
119638cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
11973927098fSPaul Saab 
119814bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
119938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
12003927098fSPaul Saab 	/* 5700 b2 errata */
1201e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
120238cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
120395d67482SBill Paul 
120495d67482SBill Paul 	return (0);
120595d67482SBill Paul }
120695d67482SBill Paul 
120795d67482SBill Paul static void
12083e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
12093e9b1bcaSJung-uk Kim {
12103e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
12113e9b1bcaSJung-uk Kim 
12123e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
12133e9b1bcaSJung-uk Kim 
12143e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
12153e9b1bcaSJung-uk Kim 
121645ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
12173e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
121845ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12193e9b1bcaSJung-uk Kim 	else
122045ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12213e9b1bcaSJung-uk Kim }
12223e9b1bcaSJung-uk Kim 
12233e9b1bcaSJung-uk Kim static void
12243f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
122595d67482SBill Paul {
122695d67482SBill Paul 	struct ifnet *ifp;
122795d67482SBill Paul 	struct ifmultiaddr *ifma;
12283f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
122995d67482SBill Paul 	int h, i;
123095d67482SBill Paul 
12310f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
12320f9bd73bSSam Leffler 
1233fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
123495d67482SBill Paul 
123595d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
123695d67482SBill Paul 		for (i = 0; i < 4; i++)
12370c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
123895d67482SBill Paul 		return;
123995d67482SBill Paul 	}
124095d67482SBill Paul 
124195d67482SBill Paul 	/* First, zot all the existing filters. */
124295d67482SBill Paul 	for (i = 0; i < 4; i++)
124395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
124495d67482SBill Paul 
124595d67482SBill Paul 	/* Now program new ones. */
1246eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
124795d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
124895d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
124995d67482SBill Paul 			continue;
12500e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
12510c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
12520c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
125395d67482SBill Paul 	}
1254eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
125595d67482SBill Paul 
125695d67482SBill Paul 	for (i = 0; i < 4; i++)
125795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
125895d67482SBill Paul }
125995d67482SBill Paul 
12608cb1383cSDoug Ambrisko static void
1261cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1262cb2eacc7SYaroslav Tykhiy {
1263cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1264cb2eacc7SYaroslav Tykhiy 
1265cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1266cb2eacc7SYaroslav Tykhiy 
1267cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1268cb2eacc7SYaroslav Tykhiy 
1269cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1270cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1271cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1272cb2eacc7SYaroslav Tykhiy 	else
1273cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1274cb2eacc7SYaroslav Tykhiy }
1275cb2eacc7SYaroslav Tykhiy 
1276cb2eacc7SYaroslav Tykhiy static void
1277797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
12788cb1383cSDoug Ambrisko {
1279797ab05eSPyun YongHyeon 
12808cb1383cSDoug Ambrisko 	/*
12818cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
12828cb1383cSDoug Ambrisko 	 */
12838cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
12848cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
12858cb1383cSDoug Ambrisko 
12868cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
12878cb1383cSDoug Ambrisko 		switch (type) {
12888cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12898cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
12908cb1383cSDoug Ambrisko 			break;
12918cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12928cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
12938cb1383cSDoug Ambrisko 			break;
12948cb1383cSDoug Ambrisko 		}
12958cb1383cSDoug Ambrisko 	}
12968cb1383cSDoug Ambrisko }
12978cb1383cSDoug Ambrisko 
12988cb1383cSDoug Ambrisko static void
1299797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
13008cb1383cSDoug Ambrisko {
1301797ab05eSPyun YongHyeon 
13028cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
13038cb1383cSDoug Ambrisko 		switch (type) {
13048cb1383cSDoug Ambrisko 		case BGE_RESET_START:
13058cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
13068cb1383cSDoug Ambrisko 			/* START DONE */
13078cb1383cSDoug Ambrisko 			break;
13088cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
13098cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
13108cb1383cSDoug Ambrisko 			break;
13118cb1383cSDoug Ambrisko 		}
13128cb1383cSDoug Ambrisko 	}
13138cb1383cSDoug Ambrisko }
13148cb1383cSDoug Ambrisko 
13158cb1383cSDoug Ambrisko static void
1316797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
13178cb1383cSDoug Ambrisko {
1318797ab05eSPyun YongHyeon 
13198cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13208cb1383cSDoug Ambrisko 		switch (type) {
13218cb1383cSDoug Ambrisko 		case BGE_RESET_START:
13228cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
13238cb1383cSDoug Ambrisko 			break;
13248cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
13258cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
13268cb1383cSDoug Ambrisko 			break;
13278cb1383cSDoug Ambrisko 		}
13288cb1383cSDoug Ambrisko 	}
13298cb1383cSDoug Ambrisko }
13308cb1383cSDoug Ambrisko 
1331797ab05eSPyun YongHyeon static void
1332797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
13338cb1383cSDoug Ambrisko {
13348cb1383cSDoug Ambrisko 	int i;
13358cb1383cSDoug Ambrisko 
13368cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13378cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
13388cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
133939153c5aSJung-uk Kim 		    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
13408cb1383cSDoug Ambrisko 
13418cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
13428cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
13438cb1383cSDoug Ambrisko 				break;
13448cb1383cSDoug Ambrisko 			DELAY(10);
13458cb1383cSDoug Ambrisko 		}
13468cb1383cSDoug Ambrisko 	}
13478cb1383cSDoug Ambrisko }
13488cb1383cSDoug Ambrisko 
134995d67482SBill Paul /*
1350c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
135195d67482SBill Paul  */
135295d67482SBill Paul static int
13533f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
135495d67482SBill Paul {
13553f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
1356fbc374afSPyun YongHyeon 	uint16_t val;
135795d67482SBill Paul 	int i;
135895d67482SBill Paul 
13598cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
1360e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
136195d67482SBill Paul 
136295d67482SBill Paul 	/* Clear the MAC control register */
136395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
136495d67482SBill Paul 
136595d67482SBill Paul 	/*
136695d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
136795d67482SBill Paul 	 * internal memory.
136895d67482SBill Paul 	 */
136995d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
13703f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
137195d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
137295d67482SBill Paul 
137395d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
13743f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
137595d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
137695d67482SBill Paul 
1377fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1378fbc374afSPyun YongHyeon 		/*
1379d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1380fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1381fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1382fbc374afSPyun YongHyeon 		 */
1383fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1384fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1385fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1386fbc374afSPyun YongHyeon 	}
1387fbc374afSPyun YongHyeon 
1388186f842bSJung-uk Kim 	/*
1389186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1390186f842bSJung-uk Kim 	 */
1391186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1392186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1393652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
1394186f842bSJung-uk Kim 		/* Read watermark not used, 128 bytes for write. */
1395186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1396652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
13974c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1398186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1399186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1400186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1401186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1402186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1403186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1404cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1405cbb2b2feSPyun YongHyeon 			/*
1406cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1407cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1408cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1409cbb2b2feSPyun YongHyeon 			 * register.
1410cbb2b2feSPyun YongHyeon 			 */
1411cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1412cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1413186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1414186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1415186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1416186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1417186f842bSJung-uk Kim 		} else {
1418186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1419186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1420186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
14210c8aa4eaSJung-uk Kim 			    0x0F;
1422186f842bSJung-uk Kim 		}
1423e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1424e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
14253f74909aSGleb Smirnoff 			uint32_t tmp;
14265cba12d3SPaul Saab 
1427186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
14280c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1429186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1430186f842bSJung-uk Kim 				dma_rw_ctl |=
1431186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
14325cba12d3SPaul Saab 
1433186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1434186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1435186f842bSJung-uk Kim 		}
1436186f842bSJung-uk Kim 	} else {
1437186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1438186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1439186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1440186f842bSJung-uk Kim 
1441186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1442186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1443186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1444186f842bSJung-uk Kim 	}
1445186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1446186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1447186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1448186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1449e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1450186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
14515cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
14525cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
145395d67482SBill Paul 
145495d67482SBill Paul 	/*
145595d67482SBill Paul 	 * Set up general mode register.
145695d67482SBill Paul 	 */
1457e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
145895d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1459ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
146095d67482SBill Paul 
146195d67482SBill Paul 	/*
146290447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
146390447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
146490447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
146590447aadSMarius Strobl 	 * certain bridges.
146690447aadSMarius Strobl 	 */
146790447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
146890447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
146990447aadSMarius Strobl 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_FORCE_PCI32);
147090447aadSMarius Strobl 
147190447aadSMarius Strobl 	/*
14728cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
14738cb1383cSDoug Ambrisko 	 */
14748cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
14758cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
14768cb1383cSDoug Ambrisko 
14778cb1383cSDoug Ambrisko 	/*
1478ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1479c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1480c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
148195d67482SBill Paul 	 */
1482c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1483c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
148495d67482SBill Paul 
148595d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
14860c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
148795d67482SBill Paul 
148838cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
148938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
149038cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
149138cc658fSJohn Baldwin 
149238cc658fSJohn Baldwin 		/* Put PHY into ready state */
149338cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
149438cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
149538cc658fSJohn Baldwin 		DELAY(40);
149638cc658fSJohn Baldwin 	}
149738cc658fSJohn Baldwin 
149895d67482SBill Paul 	return (0);
149995d67482SBill Paul }
150095d67482SBill Paul 
150195d67482SBill Paul static int
15023f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
150395d67482SBill Paul {
150495d67482SBill Paul 	struct bge_rcb *rcb;
1505e907febfSPyun YongHyeon 	bus_size_t vrcb;
1506e907febfSPyun YongHyeon 	bge_hostaddr taddr;
15076f8718a3SScott Long 	uint32_t val;
15088a315a6dSPyun YongHyeon 	int i, limit;
150995d67482SBill Paul 
151095d67482SBill Paul 	/*
151195d67482SBill Paul 	 * Initialize the memory window pointer register so that
151295d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
151395d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
151495d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
151595d67482SBill Paul 	 */
151695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
151795d67482SBill Paul 
1518822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1519822f63fcSBill Paul 
15207ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
152195d67482SBill Paul 		/* Configure mbuf memory pool */
15220dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1523822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1524822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1525822f63fcSBill Paul 		else
152695d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
152795d67482SBill Paul 
152895d67482SBill Paul 		/* Configure DMA resource pool */
15290434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
15300434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
153195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
15320434d1b8SBill Paul 	}
153395d67482SBill Paul 
153495d67482SBill Paul 	/* Configure mbuf pool watermarks */
153538cc658fSJohn Baldwin 	if (!BGE_IS_5705_PLUS(sc)) {
1536fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1537fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1538fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
153938cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
154038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
154138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
154238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
154338cc658fSJohn Baldwin 	} else {
154438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
154538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
154638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
154738cc658fSJohn Baldwin 	}
154895d67482SBill Paul 
154995d67482SBill Paul 	/* Configure DMA resource watermarks */
155095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
155195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
155295d67482SBill Paul 
155395d67482SBill Paul 	/* Enable buffer manager */
15547ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
155595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
155695d67482SBill Paul 		    BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
155795d67482SBill Paul 
155895d67482SBill Paul 		/* Poll for buffer manager start indication */
155995d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
1560d5d23857SJung-uk Kim 			DELAY(10);
15610c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
156295d67482SBill Paul 				break;
156395d67482SBill Paul 		}
156495d67482SBill Paul 
156595d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1566fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1567fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
156895d67482SBill Paul 			return (ENXIO);
156995d67482SBill Paul 		}
15700434d1b8SBill Paul 	}
157195d67482SBill Paul 
157295d67482SBill Paul 	/* Enable flow-through queues */
15730c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
157495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
157595d67482SBill Paul 
157695d67482SBill Paul 	/* Wait until queue initialization is complete */
157795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1578d5d23857SJung-uk Kim 		DELAY(10);
157995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
158095d67482SBill Paul 			break;
158195d67482SBill Paul 	}
158295d67482SBill Paul 
158395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1584fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
158595d67482SBill Paul 		return (ENXIO);
158695d67482SBill Paul 	}
158795d67482SBill Paul 
15888a315a6dSPyun YongHyeon 	/*
15898a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
15908a315a6dSPyun YongHyeon 	 *
15918a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
15928a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
15938a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
15948a315a6dSPyun YongHyeon 	 *
15958a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
15968a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
15978a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
15988a315a6dSPyun YongHyeon 	 *   to the controller.
15998a315a6dSPyun YongHyeon 	 *
16008a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
16018a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
16028a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
16038a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
16048a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
16058a315a6dSPyun YongHyeon 	 *   be disabled.
16068a315a6dSPyun YongHyeon 	 *
16078a315a6dSPyun YongHyeon 	 * Receive Return Ring
16088a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
16098a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
16108a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
16118a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
16128a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
16138a315a6dSPyun YongHyeon 	 *
16148a315a6dSPyun YongHyeon 	 * Send Ring
16158a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
16168a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
16178a315a6dSPyun YongHyeon 	 */
16188a315a6dSPyun YongHyeon 
16198a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
1620f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1621f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1622f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1623f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1624f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1625f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1626f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
16278a315a6dSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc)) {
16288a315a6dSPyun YongHyeon 		/*
16298a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
16308a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
16318a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
16328a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
16338a315a6dSPyun YongHyeon 		 */
16340434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
16358a315a6dSPyun YongHyeon 	} else {
16368a315a6dSPyun YongHyeon 		/*
16378a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
16388a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
16398a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
16408a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
16418a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
16428a315a6dSPyun YongHyeon 		 */
16430434d1b8SBill Paul 		rcb->bge_maxlen_flags =
16440434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
16458a315a6dSPyun YongHyeon 	}
164695d67482SBill Paul 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
16478a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
16480c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
16490c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
165067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
165167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
165295d67482SBill Paul 
16538a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
16548a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
16558a315a6dSPyun YongHyeon 
165695d67482SBill Paul 	/*
16578a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
16588a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
16598a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
166095d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
166195d67482SBill Paul 	 * high enough to require it).
166295d67482SBill Paul 	 */
16634c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1664f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
16658a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
1666f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1667f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1668f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1669f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1670f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1671f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1672f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
16731be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
16741be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
167595d67482SBill Paul 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
167667111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
167767111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
167867111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
167967111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
16808a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
16810434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
16820434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
168367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
16848a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
16858a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
16868a315a6dSPyun YongHyeon 	}
168795d67482SBill Paul 
16888a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
16895e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
1690f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
169167111612SJohn Polstra 		rcb->bge_maxlen_flags =
169267111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
16930434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
16940434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
16958a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
16968a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
16970434d1b8SBill Paul 	}
169895d67482SBill Paul 
169995d67482SBill Paul 	/*
17008a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
17018a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
17028a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
17038a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
17048a315a6dSPyun YongHyeon 	 *
170595d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
170695d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
170795d67482SBill Paul 	 * each ring.
17089ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
17099ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
17109ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
17119ba784dbSScott Long 	 * are reports that it might not need to be so strict.
171238cc658fSJohn Baldwin 	 *
171338cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
171438cc658fSJohn Baldwin 	 * well.
171595d67482SBill Paul 	 */
17165345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
17176f8718a3SScott Long 		val = 8;
17186f8718a3SScott Long 	else
17196f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
17206f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
17212a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
17222a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
17232a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
172495d67482SBill Paul 
172595d67482SBill Paul 	/*
17268a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
17278a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
17288a315a6dSPyun YongHyeon 	 * located in NIC memory.
172995d67482SBill Paul 	 */
17308a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
17318a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
17328a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
17338a315a6dSPyun YongHyeon 	else
17348a315a6dSPyun YongHyeon 		limit = 1;
1735e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
17368a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1737e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1738e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1739e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1740e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
174195d67482SBill Paul 	}
174295d67482SBill Paul 
17438a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
1744e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1745e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1746e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1747e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1748e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1749e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1750e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1751e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
175295d67482SBill Paul 
17538a315a6dSPyun YongHyeon 	/*
17548a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
17558a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
17568a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
17578a315a6dSPyun YongHyeon 	 */
17588a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
17598a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
17608a315a6dSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755)
17618a315a6dSPyun YongHyeon 		limit = 4;
17628a315a6dSPyun YongHyeon 	else
17638a315a6dSPyun YongHyeon 		limit = 1;
17648a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
1765e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
17668a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1767e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1768e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1769e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
17708a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
1771e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
177238cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
17733f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1774e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
177595d67482SBill Paul 	}
177695d67482SBill Paul 
177795d67482SBill Paul 	/*
17788a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
17798a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
17808a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
178195d67482SBill Paul 	 */
1782e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1783e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1784e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1785e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
17868a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1787e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1788e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
178995d67482SBill Paul 
179095d67482SBill Paul 	/* Set random backoff seed for TX */
179195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
17924a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
17934a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
17944a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
179595d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
179695d67482SBill Paul 
179795d67482SBill Paul 	/* Set inter-packet gap */
179895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
179995d67482SBill Paul 
180095d67482SBill Paul 	/*
180195d67482SBill Paul 	 * Specify which ring to use for packets that don't match
180295d67482SBill Paul 	 * any RX rules.
180395d67482SBill Paul 	 */
180495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
180595d67482SBill Paul 
180695d67482SBill Paul 	/*
180795d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
180895d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
180995d67482SBill Paul 	 */
181095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
181195d67482SBill Paul 
181295d67482SBill Paul 	/* Inialize RX list placement stats mask. */
18130c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
181495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
181595d67482SBill Paul 
181695d67482SBill Paul 	/* Disable host coalescing until we get it set up */
181795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
181895d67482SBill Paul 
181995d67482SBill Paul 	/* Poll to make sure it's shut down. */
182095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1821d5d23857SJung-uk Kim 		DELAY(10);
182295d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
182395d67482SBill Paul 			break;
182495d67482SBill Paul 	}
182595d67482SBill Paul 
182695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1827fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1828fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
182995d67482SBill Paul 		return (ENXIO);
183095d67482SBill Paul 	}
183195d67482SBill Paul 
183295d67482SBill Paul 	/* Set up host coalescing defaults */
183395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
183495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
183595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
183695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
18377ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
183895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
183995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
18400434d1b8SBill Paul 	}
1841b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1842b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
184395d67482SBill Paul 
184495d67482SBill Paul 	/* Set up address of statistics block */
18457ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1846f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1847f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
184895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1849f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
18500434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
185195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
18520434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
18530434d1b8SBill Paul 	}
18540434d1b8SBill Paul 
18550434d1b8SBill Paul 	/* Set up address of status block */
1856f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1857f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
185895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1859f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
186095d67482SBill Paul 
186130f57f61SPyun YongHyeon 	/* Set up status block size. */
186230f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
1863864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
186430f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
1865864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1866864104feSPyun YongHyeon 	} else {
186730f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
1868864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
1869864104feSPyun YongHyeon 	}
1870864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
1871864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
1872864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
187330f57f61SPyun YongHyeon 
187495d67482SBill Paul 	/* Turn on host coalescing state machine */
187530f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
187695d67482SBill Paul 
187795d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
187895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
187995d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
188095d67482SBill Paul 
188195d67482SBill Paul 	/* Turn on RX list placement state machine */
188295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
188395d67482SBill Paul 
188495d67482SBill Paul 	/* Turn on RX list selector state machine. */
18857ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
188695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
188795d67482SBill Paul 
1888ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
1889ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
1890ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
1891ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
1892ea3b4127SPyun YongHyeon 
1893ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
1894ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
1895ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
1896ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
1897ea3b4127SPyun YongHyeon 	else
1898ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
1899ea3b4127SPyun YongHyeon 
190095d67482SBill Paul 	/* Turn on DMA, clear stats */
1901ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
190295d67482SBill Paul 
190395d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
190495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
190595d67482SBill Paul 
190695d67482SBill Paul #ifdef notdef
190795d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
190895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
190995d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
191095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
191195d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
191295d67482SBill Paul #endif
191395d67482SBill Paul 
191495d67482SBill Paul 	/* Turn on DMA completion state machine */
19157ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
191695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
191795d67482SBill Paul 
19186f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
19196f8718a3SScott Long 
19206f8718a3SScott Long 	/* Enable host coalescing bug fix. */
1921a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
19223889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
19236f8718a3SScott Long 
19247aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
19257aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
19267aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
19277aa4b937SPyun YongHyeon 
192895d67482SBill Paul 	/* Turn on write DMA state machine */
19296f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
19304f09c4c7SMarius Strobl 	DELAY(40);
193195d67482SBill Paul 
193295d67482SBill Paul 	/* Turn on read DMA state machine */
19334f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
1934a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
1935a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
1936a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
1937a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
1938a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
1939a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
19404f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
19414f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
194255a24a05SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO) {
1943ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
194455a24a05SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
194555a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
194655a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
194755a24a05SPyun YongHyeon 	}
1948d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
1949d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
1950d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
1951d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780) {
1952d255f2a9SPyun YongHyeon 		/*
1953d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
1954d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
1955d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
1956d255f2a9SPyun YongHyeon 		 */
1957d255f2a9SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL,
1958d255f2a9SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_RSRVCTRL) |
1959d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
1960d255f2a9SPyun YongHyeon 	}
19614f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
19624f09c4c7SMarius Strobl 	DELAY(40);
196395d67482SBill Paul 
196495d67482SBill Paul 	/* Turn on RX data completion state machine */
196595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
196695d67482SBill Paul 
196795d67482SBill Paul 	/* Turn on RX BD initiator state machine */
196895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
196995d67482SBill Paul 
197095d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
197195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
197295d67482SBill Paul 
197395d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
19747ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
197595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
197695d67482SBill Paul 
197795d67482SBill Paul 	/* Turn on send BD completion state machine */
197895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
197995d67482SBill Paul 
198095d67482SBill Paul 	/* Turn on send data completion state machine */
1981a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
1982a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
1983a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
1984a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
198595d67482SBill Paul 
198695d67482SBill Paul 	/* Turn on send data initiator state machine */
1987ca3f1187SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO)
1988ca3f1187SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE | 0x08);
1989ca3f1187SPyun YongHyeon 	else
199095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
199195d67482SBill Paul 
199295d67482SBill Paul 	/* Turn on send BD initiator state machine */
199395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
199495d67482SBill Paul 
199595d67482SBill Paul 	/* Turn on send BD selector state machine */
199695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
199795d67482SBill Paul 
19980c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
199995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
200095d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
200195d67482SBill Paul 
200295d67482SBill Paul 	/* ack/clear link change events */
200395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
20040434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
20050434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2006f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
200795d67482SBill Paul 
20086ede2cfaSPyun YongHyeon 	/*
20096ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
20106ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
20116ede2cfaSPyun YongHyeon 	 */
2012652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
201395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2014a1d52896SBill Paul 	} else {
20151f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
20164c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2017a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2018a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2019a1d52896SBill Paul 	}
202095d67482SBill Paul 
20211f313773SOleg Bulyzhin 	/*
20221f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
20231f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
20241f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
20251f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
20261f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
20271f313773SOleg Bulyzhin 	 */
20281f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
20291f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
20301f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
20311f313773SOleg Bulyzhin 
203295d67482SBill Paul 	/* Enable link state change attentions. */
203395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
203495d67482SBill Paul 
203595d67482SBill Paul 	return (0);
203695d67482SBill Paul }
203795d67482SBill Paul 
20384c0da0ffSGleb Smirnoff const struct bge_revision *
20394c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
20404c0da0ffSGleb Smirnoff {
20414c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
20424c0da0ffSGleb Smirnoff 
20434c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
20444c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
20454c0da0ffSGleb Smirnoff 			return (br);
20464c0da0ffSGleb Smirnoff 	}
20474c0da0ffSGleb Smirnoff 
20484c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
20494c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
20504c0da0ffSGleb Smirnoff 			return (br);
20514c0da0ffSGleb Smirnoff 	}
20524c0da0ffSGleb Smirnoff 
20534c0da0ffSGleb Smirnoff 	return (NULL);
20544c0da0ffSGleb Smirnoff }
20554c0da0ffSGleb Smirnoff 
20564c0da0ffSGleb Smirnoff const struct bge_vendor *
20574c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
20584c0da0ffSGleb Smirnoff {
20594c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
20604c0da0ffSGleb Smirnoff 
20614c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
20624c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
20634c0da0ffSGleb Smirnoff 			return (v);
20644c0da0ffSGleb Smirnoff 
20654c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
20664c0da0ffSGleb Smirnoff 	return (NULL);
20674c0da0ffSGleb Smirnoff }
20684c0da0ffSGleb Smirnoff 
206995d67482SBill Paul /*
207095d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
20714c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
20724c0da0ffSGleb Smirnoff  *
20734c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
20747c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
20757c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
20767c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
20777c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
207895d67482SBill Paul  */
207995d67482SBill Paul static int
20803f74909aSGleb Smirnoff bge_probe(device_t dev)
208195d67482SBill Paul {
2082852c67f9SMarius Strobl 	const struct bge_type *t = bge_devs;
20834c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
20847c929cf9SJung-uk Kim 	uint16_t vid, did;
208595d67482SBill Paul 
208695d67482SBill Paul 	sc->bge_dev = dev;
20877c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
20887c929cf9SJung-uk Kim 	did = pci_get_device(dev);
20894c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
20907c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
20917c929cf9SJung-uk Kim 			char model[64], buf[96];
20924c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
20934c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
20944c0da0ffSGleb Smirnoff 			uint32_t id;
20954c0da0ffSGleb Smirnoff 
2096a5779553SStanislav Sedov 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2097a5779553SStanislav Sedov 			    BGE_PCIMISCCTL_ASICREV_SHIFT;
2098a5779553SStanislav Sedov 			if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG)
2099a5779553SStanislav Sedov 				id = pci_read_config(dev,
2100a5779553SStanislav Sedov 				    BGE_PCI_PRODID_ASICREV, 4);
21014c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
21027c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
21034e35d186SJung-uk Kim 			{
21044e35d186SJung-uk Kim #if __FreeBSD_version > 700024
21054e35d186SJung-uk Kim 				const char *pname;
21064e35d186SJung-uk Kim 
2107852c67f9SMarius Strobl 				if (bge_has_eaddr(sc) &&
2108852c67f9SMarius Strobl 				    pci_get_vpd_ident(dev, &pname) == 0)
21094e35d186SJung-uk Kim 					snprintf(model, 64, "%s", pname);
21104e35d186SJung-uk Kim 				else
21114e35d186SJung-uk Kim #endif
21127c929cf9SJung-uk Kim 					snprintf(model, 64, "%s %s",
21137c929cf9SJung-uk Kim 					    v->v_name,
21147c929cf9SJung-uk Kim 					    br != NULL ? br->br_name :
21157c929cf9SJung-uk Kim 					    "NetXtreme Ethernet Controller");
21164e35d186SJung-uk Kim 			}
2117a5779553SStanislav Sedov 			snprintf(buf, 96, "%s, %sASIC rev. %#08x", model,
2118a5779553SStanislav Sedov 			    br != NULL ? "" : "unknown ", id);
21194c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
212095d67482SBill Paul 			return (0);
212195d67482SBill Paul 		}
212295d67482SBill Paul 		t++;
212395d67482SBill Paul 	}
212495d67482SBill Paul 
212595d67482SBill Paul 	return (ENXIO);
212695d67482SBill Paul }
212795d67482SBill Paul 
2128f41ac2beSBill Paul static void
21293f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2130f41ac2beSBill Paul {
2131f41ac2beSBill Paul 	int i;
2132f41ac2beSBill Paul 
21333f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2134f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2135f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
21360ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2137f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2138f41ac2beSBill Paul 	}
2139943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2140943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2141943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2142f41ac2beSBill Paul 
21433f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2144f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2145f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2146f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2147f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2148f41ac2beSBill Paul 	}
2149943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2150943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2151943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2152f41ac2beSBill Paul 
21533f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2154f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2155f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
21560ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2157f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2158f41ac2beSBill Paul 	}
2159f41ac2beSBill Paul 
21600ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
21610ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
21620ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
21630ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2164f41ac2beSBill Paul 
2165f41ac2beSBill Paul 
21663f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2167e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2168e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2169e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2170e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2171f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2172f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2173f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2174f41ac2beSBill Paul 
2175f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2176f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2177f41ac2beSBill Paul 
21783f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2179e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2180e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2181e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2182e65bed95SPyun YongHyeon 
2183e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2184e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2185f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2186f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2187f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2188f41ac2beSBill Paul 
2189f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2190f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2191f41ac2beSBill Paul 
21923f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2193e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2194e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2195e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2196e65bed95SPyun YongHyeon 
2197e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2198e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2199f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2200f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2201f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2202f41ac2beSBill Paul 
2203f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2204f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2205f41ac2beSBill Paul 
22063f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2207e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2208e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2209e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2210e65bed95SPyun YongHyeon 
2211e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2212f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2213f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2214f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2215f41ac2beSBill Paul 
2216f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2217f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2218f41ac2beSBill Paul 
22193f74909aSGleb Smirnoff 	/* Destroy status block. */
2220e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2221e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2222e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2223e65bed95SPyun YongHyeon 
2224e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2225f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2226f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2227f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2228f41ac2beSBill Paul 
2229f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2230f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2231f41ac2beSBill Paul 
22323f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2233e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2234e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2235e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2236e65bed95SPyun YongHyeon 
2237e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2238f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2239f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2240f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2241f41ac2beSBill Paul 
2242f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2243f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2244f41ac2beSBill Paul 
22455b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
22465b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
22475b610048SPyun YongHyeon 
22483f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2249f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2250f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2251f41ac2beSBill Paul }
2252f41ac2beSBill Paul 
2253f41ac2beSBill Paul static int
22545b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
22555b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
22565b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2257f41ac2beSBill Paul {
22583f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
2259f681b29aSPyun YongHyeon 	bus_addr_t lowaddr;
22605b610048SPyun YongHyeon 	bus_size_t ring_end;
22615b610048SPyun YongHyeon 	int error;
2262f41ac2beSBill Paul 
22635b610048SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
22645b610048SPyun YongHyeon again:
22655b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
22665b610048SPyun YongHyeon 	    alignment, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
22675b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
22685b610048SPyun YongHyeon 	if (error != 0) {
22695b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
22705b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
22715b610048SPyun YongHyeon 		return (ENOMEM);
22725b610048SPyun YongHyeon 	}
22735b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
22745b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
22755b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
22765b610048SPyun YongHyeon 	if (error != 0) {
22775b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
22785b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
22795b610048SPyun YongHyeon 		return (ENOMEM);
22805b610048SPyun YongHyeon 	}
22815b610048SPyun YongHyeon 	/* Load the address of the ring. */
22825b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
22835b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
22845b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
22855b610048SPyun YongHyeon 	if (error != 0) {
22865b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
22875b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
22885b610048SPyun YongHyeon 		return (ENOMEM);
22895b610048SPyun YongHyeon 	}
22905b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
22915b610048SPyun YongHyeon 	ring_end = *paddr + maxsize;
22925b610048SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0 &&
22935b610048SPyun YongHyeon 	    BGE_ADDR_HI(*paddr) != BGE_ADDR_HI(ring_end)) {
22945b610048SPyun YongHyeon 		/*
22955b610048SPyun YongHyeon 		 * 4GB boundary crossed.  Limit maximum allowable DMA
22965b610048SPyun YongHyeon 		 * address space to 32bit and try again.
22975b610048SPyun YongHyeon 		 */
22985b610048SPyun YongHyeon 		bus_dmamap_unload(*tag, *map);
22995b610048SPyun YongHyeon 		bus_dmamem_free(*tag, *ring, *map);
23005b610048SPyun YongHyeon 		bus_dma_tag_destroy(*tag);
23015b610048SPyun YongHyeon 		if (bootverbose)
23025b610048SPyun YongHyeon 			device_printf(sc->bge_dev, "4GB boundary crossed, "
23035b610048SPyun YongHyeon 			    "limit DMA address space to 32bit for %s\n", msg);
23045b610048SPyun YongHyeon 		*ring = NULL;
23055b610048SPyun YongHyeon 		*tag = NULL;
23065b610048SPyun YongHyeon 		*map = NULL;
23075b610048SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
23085b610048SPyun YongHyeon 		goto again;
23095b610048SPyun YongHyeon 	}
23105b610048SPyun YongHyeon 	return (0);
23115b610048SPyun YongHyeon }
23125b610048SPyun YongHyeon 
23135b610048SPyun YongHyeon static int
23145b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
23155b610048SPyun YongHyeon {
23165b610048SPyun YongHyeon 	bus_addr_t lowaddr;
23175b610048SPyun YongHyeon 	bus_size_t boundary, sbsz, txsegsz, txmaxsegsz;
23185b610048SPyun YongHyeon 	int i, error;
2319f41ac2beSBill Paul 
2320f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2321f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2322f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2323f41ac2beSBill Paul 	/*
2324f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2325f41ac2beSBill Paul 	 */
23264eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2327f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
23284eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
23294eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2330e65bed95SPyun YongHyeon 	if (error != 0) {
2331fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2332fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2333e65bed95SPyun YongHyeon 		return (ENOMEM);
2334e65bed95SPyun YongHyeon 	}
2335e65bed95SPyun YongHyeon 
23365b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
23375b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
23385b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
23395b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
23405b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
23415b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
23425b610048SPyun YongHyeon 	if (error)
23435b610048SPyun YongHyeon 		return (error);
23445b610048SPyun YongHyeon 
23455b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
23465b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
23475b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
23485b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
23495b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
23505b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
23515b610048SPyun YongHyeon 	if (error)
23525b610048SPyun YongHyeon 		return (error);
23535b610048SPyun YongHyeon 
23545b610048SPyun YongHyeon 	/* Create tag for TX ring. */
23555b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
23565b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
23575b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
23585b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
23595b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
23605b610048SPyun YongHyeon 	if (error)
23615b610048SPyun YongHyeon 		return (error);
23625b610048SPyun YongHyeon 
2363f41ac2beSBill Paul 	/*
23645b610048SPyun YongHyeon 	 * Create tag for status block.
23655b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
23665b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
23675b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
23685b610048SPyun YongHyeon 	 * of configured number of ring.
2369f41ac2beSBill Paul 	 */
23705b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
23715b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
23725b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
23735b610048SPyun YongHyeon 	else
23745b610048SPyun YongHyeon 		sbsz = 32;
23755b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
23765b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
23775b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
23785b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
23795b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
23805b610048SPyun YongHyeon 	if (error)
23815b610048SPyun YongHyeon 		return (error);
23825b610048SPyun YongHyeon 
238312c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
238412c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
238512c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
238612c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
238712c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
238812c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
238912c65daeSPyun YongHyeon 	if (error)
239012c65daeSPyun YongHyeon 		return (error);
239112c65daeSPyun YongHyeon 
23925b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
23935b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
23945b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
23955b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
23965b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
23975b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
23985b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
23995b610048SPyun YongHyeon 		if (error)
24005b610048SPyun YongHyeon 			return (error);
24015b610048SPyun YongHyeon 	}
24025b610048SPyun YongHyeon 
24035b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
24045b610048SPyun YongHyeon 	boundary = 0;
24055b610048SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0)
240638cc6151SPyun YongHyeon 		boundary = BGE_DMA_BNDRY;
24075b610048SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
24085b610048SPyun YongHyeon 	    1, boundary, lowaddr, BUS_SPACE_MAXADDR, NULL,
24095b610048SPyun YongHyeon 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
24105b610048SPyun YongHyeon 	    0, NULL, NULL, &sc->bge_cdata.bge_buffer_tag);
24115b610048SPyun YongHyeon 	if (error != 0) {
24125b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
24135b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
24145b610048SPyun YongHyeon 		return (ENOMEM);
24155b610048SPyun YongHyeon 	}
24165b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
2417ca3f1187SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO) {
2418ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
2419ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
2420ca3f1187SPyun YongHyeon 	} else {
2421ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
2422ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
2423ca3f1187SPyun YongHyeon 	}
24245b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
2425ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2426ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
2427ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
2428f41ac2beSBill Paul 
2429f41ac2beSBill Paul 	if (error) {
24300ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
24310ac56796SPyun YongHyeon 		return (ENOMEM);
24320ac56796SPyun YongHyeon 	}
24330ac56796SPyun YongHyeon 
24345b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
24355b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
24360ac56796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1,
2437ca3f1187SPyun YongHyeon 	    MCLBYTES, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
24380ac56796SPyun YongHyeon 
24390ac56796SPyun YongHyeon 	if (error) {
24400ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
2441f41ac2beSBill Paul 		return (ENOMEM);
2442f41ac2beSBill Paul 	}
2443f41ac2beSBill Paul 
24443f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
2445943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2446943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
2447943787f3SPyun YongHyeon 	if (error) {
2448943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
2449943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
2450943787f3SPyun YongHyeon 		return (ENOMEM);
2451943787f3SPyun YongHyeon 	}
2452f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
24530ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2454f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
2455f41ac2beSBill Paul 		if (error) {
2456fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
2457fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
2458f41ac2beSBill Paul 			return (ENOMEM);
2459f41ac2beSBill Paul 		}
2460f41ac2beSBill Paul 	}
2461f41ac2beSBill Paul 
24623f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
2463f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
24640ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
2465f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
2466f41ac2beSBill Paul 		if (error) {
2467fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
24680ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
2469f41ac2beSBill Paul 			return (ENOMEM);
2470f41ac2beSBill Paul 		}
2471f41ac2beSBill Paul 	}
2472f41ac2beSBill Paul 
24735b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
24744c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
24755b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
24768a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
24771be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
24781be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
2479f41ac2beSBill Paul 		if (error) {
2480fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
24813f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
2482f41ac2beSBill Paul 			return (ENOMEM);
2483f41ac2beSBill Paul 		}
24843f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
2485943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2486943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
2487943787f3SPyun YongHyeon 		if (error) {
2488943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
24891b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
2490943787f3SPyun YongHyeon 			return (ENOMEM);
2491943787f3SPyun YongHyeon 		}
2492f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2493f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2494f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2495f41ac2beSBill Paul 			if (error) {
2496fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
24973f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
2498f41ac2beSBill Paul 				return (ENOMEM);
2499f41ac2beSBill Paul 			}
2500f41ac2beSBill Paul 		}
2501f41ac2beSBill Paul 	}
2502f41ac2beSBill Paul 
2503f41ac2beSBill Paul 	return (0);
2504f41ac2beSBill Paul }
2505f41ac2beSBill Paul 
2506bf6ef57aSJohn Polstra /*
2507bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2508bf6ef57aSJohn Polstra  */
2509bf6ef57aSJohn Polstra static int
2510bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2511bf6ef57aSJohn Polstra {
2512bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
251355aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
2514bf6ef57aSJohn Polstra 
251555aaf894SMarius Strobl 	d = pci_get_domain(dev);
2516bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2517bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2518bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2519bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
252055aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2521bf6ef57aSJohn Polstra 			return (1);
2522bf6ef57aSJohn Polstra 	return (0);
2523bf6ef57aSJohn Polstra }
2524bf6ef57aSJohn Polstra 
2525bf6ef57aSJohn Polstra /*
2526bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2527bf6ef57aSJohn Polstra  */
2528bf6ef57aSJohn Polstra static int
2529bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2530bf6ef57aSJohn Polstra {
2531bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2532bf6ef57aSJohn Polstra 
2533bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2534a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
2535bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2536bf6ef57aSJohn Polstra 		/*
2537a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
2538a8376f70SMarius Strobl 		 * configured in single-port mode.
2539bf6ef57aSJohn Polstra 		 */
2540bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2541bf6ef57aSJohn Polstra 			can_use_msi = 1;
2542bf6ef57aSJohn Polstra 		break;
2543bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2544bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2545bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2546bf6ef57aSJohn Polstra 			can_use_msi = 1;
2547bf6ef57aSJohn Polstra 		break;
2548a8376f70SMarius Strobl 	default:
2549a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
2550bf6ef57aSJohn Polstra 			can_use_msi = 1;
2551bf6ef57aSJohn Polstra 	}
2552bf6ef57aSJohn Polstra 	return (can_use_msi);
2553bf6ef57aSJohn Polstra }
2554bf6ef57aSJohn Polstra 
255595d67482SBill Paul static int
25563f74909aSGleb Smirnoff bge_attach(device_t dev)
255795d67482SBill Paul {
255895d67482SBill Paul 	struct ifnet *ifp;
255995d67482SBill Paul 	struct bge_softc *sc;
25604f0794ffSBjoern A. Zeeb 	uint32_t hwcfg = 0, misccfg;
256108013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
2562d648358bSPyun YongHyeon 	int error, msicount, reg, rid, trys;
256395d67482SBill Paul 
256495d67482SBill Paul 	sc = device_get_softc(dev);
256595d67482SBill Paul 	sc->bge_dev = dev;
256695d67482SBill Paul 
2567dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
2568dfe0df9aSPyun YongHyeon 
256995d67482SBill Paul 	/*
257095d67482SBill Paul 	 * Map control/status registers.
257195d67482SBill Paul 	 */
257295d67482SBill Paul 	pci_enable_busmaster(dev);
257395d67482SBill Paul 
2574736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
25755f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
257644f8f2fcSMarius Strobl 	    RF_ACTIVE);
257795d67482SBill Paul 
257895d67482SBill Paul 	if (sc->bge_res == NULL) {
2579fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
258095d67482SBill Paul 		error = ENXIO;
258195d67482SBill Paul 		goto fail;
258295d67482SBill Paul 	}
258395d67482SBill Paul 
25844f09c4c7SMarius Strobl 	/* Save various chip information. */
2585e53d81eeSPaul Saab 	sc->bge_chipid =
2586a5779553SStanislav Sedov 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2587a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
2588a5779553SStanislav Sedov 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG)
2589a5779553SStanislav Sedov 		sc->bge_chipid = pci_read_config(dev, BGE_PCI_PRODID_ASICREV,
2590a5779553SStanislav Sedov 		    4);
2591e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2592e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2593e53d81eeSPaul Saab 
2594a813ed78SPyun YongHyeon 	/* Set default PHY address. */
2595a813ed78SPyun YongHyeon 	sc->bge_phy_addr = 1;
2596a813ed78SPyun YongHyeon 
259786543395SJung-uk Kim 	/*
259838cc658fSJohn Baldwin 	 * Don't enable Ethernet@WireSpeed for the 5700, 5906, or the
259986543395SJung-uk Kim 	 * 5705 A0 and A1 chips.
260086543395SJung-uk Kim 	 */
260186543395SJung-uk Kim 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
260238cc658fSJohn Baldwin 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
260386543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
260486543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)
2605757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_WIRESPEED;
260686543395SJung-uk Kim 
26075fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
26085fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
260908013fd3SMarius Strobl 
26100dae9719SJung-uk Kim 	/* Save chipset family. */
26110dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
2612a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
2613a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
2614a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
2615a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
2616a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
2617a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
2618a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
2619a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
2620a5779553SStanislav Sedov 		break;
26210dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
26220dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
26230dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
26240dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
26257ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
26260dae9719SJung-uk Kim 		break;
26270dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
26280dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
26290dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
26307ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5714_FAMILY /* | BGE_FLAG_JUMBO */;
26319fe569d8SXin LI 		/* FALLTHROUGH */
26320dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
26330dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
263438cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
26350dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
26369fe569d8SXin LI 		/* FALLTHROUGH */
26370dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
26380dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
26390dae9719SJung-uk Kim 		break;
26400dae9719SJung-uk Kim 	}
26410dae9719SJung-uk Kim 
2642757402fbSPyun YongHyeon 	/* Set various PHY bug flags. */
26431ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
26441ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
2645757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
26465ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
26475ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
2648757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
26495ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
2650757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
26514150ce6fSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
2652757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
2653eea8956aSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
2654eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
2655eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
2656eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780) {
26575ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2658a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2659a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
26604fcf220bSJohn Baldwin 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
2661f7d1b2ebSXin LI 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
2662f7d1b2ebSXin LI 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
2663757402fbSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
2664eea8956aSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
2665eea8956aSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
2666eea8956aSPyun YongHyeon 		} else
2667757402fbSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
26685ee49a3aSJung-uk Kim 	}
26695ee49a3aSJung-uk Kim 
2670a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
2671a813ed78SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2672a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2673a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2674a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2675a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
2676a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
2677a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
2678a813ed78SPyun YongHyeon 	else
2679a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
2680a813ed78SPyun YongHyeon 
2681f681b29aSPyun YongHyeon 	/*
2682f681b29aSPyun YongHyeon 	 * All controllers that are not 5755 or higher have 4GB
2683f681b29aSPyun YongHyeon 	 * boundary DMA bug.
2684f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
2685f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
2686f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
2687f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
2688f681b29aSPyun YongHyeon 	 */
2689f681b29aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) == 0)
2690f681b29aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
26914f0794ffSBjoern A. Zeeb 
269284ac96f8SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
26934f0794ffSBjoern A. Zeeb 		misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID;
26944f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
26954f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
26964f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
269784ac96f8SPyun YongHyeon 	}
26984f0794ffSBjoern A. Zeeb 
2699e53d81eeSPaul Saab 	/*
2700ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
2701ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
2702ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
2703ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
2704ca3f1187SPyun YongHyeon 	 * known bug which can't handle TSO if ethernet header + IP/TCP
2705ca3f1187SPyun YongHyeon 	 * header is greater than 80 bytes. The workaround for the TSO
2706ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
2707ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
2708ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
2709ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
2710ca3f1187SPyun YongHyeon 	 */
27114f4a16e1SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc)) {
27124f4a16e1SPyun YongHyeon 		/*
27134f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
27144f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
2715be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
27164f4a16e1SPyun YongHyeon 		 */
27174f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
2718be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
2719be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
2720ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
27214f4a16e1SPyun YongHyeon 	}
2722ca3f1187SPyun YongHyeon 
2723ca3f1187SPyun YongHyeon   	/*
27246f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
2725e53d81eeSPaul Saab   	 */
27266f8718a3SScott Long 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
27274c0da0ffSGleb Smirnoff 		/*
27286f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
27296f8718a3SScott Long 		 * must be a PCI Express device.
27306f8718a3SScott Long 		 */
27316f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
27320aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
2733d2b6e9a0SPyun YongHyeon 		if (pci_get_max_read_req(dev) != 4096)
2734d2b6e9a0SPyun YongHyeon 			pci_set_max_read_req(dev, 4096);
27356f8718a3SScott Long 	} else {
27366f8718a3SScott Long 		/*
27376f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
27386f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
27394c0da0ffSGleb Smirnoff 		 */
27400aaf1057SPyun YongHyeon 		if (pci_find_extcap(dev, PCIY_PCIX, &reg) == 0)
27410aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
274290447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
27434c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2744652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
27456f8718a3SScott Long 	}
27464c0da0ffSGleb Smirnoff 
2747bf6ef57aSJohn Polstra 	/*
2748fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
2749fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
2750fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
2751fd4d32feSPyun YongHyeon 	 */
2752fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
2753fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
2754fd4d32feSPyun YongHyeon 	/*
2755bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
2756bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
2757bf6ef57aSJohn Polstra 	 * normal operation.
2758bf6ef57aSJohn Polstra 	 */
27590aaf1057SPyun YongHyeon 	rid = 0;
27606a15578dSPyun YongHyeon 	if (pci_find_extcap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
27610aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
2762bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
2763bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
2764bf6ef57aSJohn Polstra 			if (msicount > 1)
2765bf6ef57aSJohn Polstra 				msicount = 1;
2766bf6ef57aSJohn Polstra 		} else
2767bf6ef57aSJohn Polstra 			msicount = 0;
2768bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2769bf6ef57aSJohn Polstra 			rid = 1;
2770bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
27710aaf1057SPyun YongHyeon 		}
27720aaf1057SPyun YongHyeon 	}
2773bf6ef57aSJohn Polstra 
2774bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2775bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
2776bf6ef57aSJohn Polstra 
2777bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
2778bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
2779bf6ef57aSJohn Polstra 		error = ENXIO;
2780bf6ef57aSJohn Polstra 		goto fail;
2781bf6ef57aSJohn Polstra 	}
2782bf6ef57aSJohn Polstra 
27834f09c4c7SMarius Strobl 	device_printf(dev,
27844f09c4c7SMarius Strobl 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; %s\n",
27854f09c4c7SMarius Strobl 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev,
27864f09c4c7SMarius Strobl 	    (sc->bge_flags & BGE_FLAG_PCIX) ? "PCI-X" :
27874f09c4c7SMarius Strobl 	    ((sc->bge_flags & BGE_FLAG_PCIE) ? "PCI-E" : "PCI"));
27884f09c4c7SMarius Strobl 
2789bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2790bf6ef57aSJohn Polstra 
279195d67482SBill Paul 	/* Try to reset the chip. */
27928cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
27938cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
27948cb1383cSDoug Ambrisko 		error = ENXIO;
27958cb1383cSDoug Ambrisko 		goto fail;
27968cb1383cSDoug Ambrisko 	}
27978cb1383cSDoug Ambrisko 
27988cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
2799f1a7e6d5SScott Long 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2800f1a7e6d5SScott Long 	    == BGE_MAGIC_NUMBER)) {
28018cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
28028cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
28038cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
28048cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
2805d67eba2fSPyun YongHyeon 			if (BGE_IS_575X_PLUS(sc))
28068cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
28078cb1383cSDoug Ambrisko 		}
28088cb1383cSDoug Ambrisko 	}
28098cb1383cSDoug Ambrisko 
28108cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
28118cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
28128cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
28138cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
28148cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
28158cb1383cSDoug Ambrisko 		error = ENXIO;
28168cb1383cSDoug Ambrisko 		goto fail;
28178cb1383cSDoug Ambrisko 	}
28188cb1383cSDoug Ambrisko 
28198cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
28208cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
282195d67482SBill Paul 
282295d67482SBill Paul 	if (bge_chipinit(sc)) {
2823fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
282495d67482SBill Paul 		error = ENXIO;
282595d67482SBill Paul 		goto fail;
282695d67482SBill Paul 	}
282795d67482SBill Paul 
282838cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
282938cc658fSJohn Baldwin 	if (error) {
283008013fd3SMarius Strobl 		device_printf(sc->bge_dev,
283108013fd3SMarius Strobl 		    "failed to read station address\n");
283295d67482SBill Paul 		error = ENXIO;
283395d67482SBill Paul 		goto fail;
283495d67482SBill Paul 	}
283595d67482SBill Paul 
2836f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
28377ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
2838f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2839f41ac2beSBill Paul 	else
2840f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2841f41ac2beSBill Paul 
28425b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
2843fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2844fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2845f41ac2beSBill Paul 		error = ENXIO;
2846f41ac2beSBill Paul 		goto fail;
2847f41ac2beSBill Paul 	}
2848f41ac2beSBill Paul 
284935f945cdSPyun YongHyeon 	bge_add_sysctls(sc);
285035f945cdSPyun YongHyeon 
285195d67482SBill Paul 	/* Set default tuneable values. */
285295d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
285395d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
285495d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
28556f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
28566f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
285795d67482SBill Paul 
285835f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
285935f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
286035f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
286135f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
286235f945cdSPyun YongHyeon 
286395d67482SBill Paul 	/* Set up ifnet structure */
2864fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2865fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2866fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2867fc74a9f9SBrooks Davis 		error = ENXIO;
2868fc74a9f9SBrooks Davis 		goto fail;
2869fc74a9f9SBrooks Davis 	}
287095d67482SBill Paul 	ifp->if_softc = sc;
28719bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
287295d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
287395d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
287495d67482SBill Paul 	ifp->if_start = bge_start;
287595d67482SBill Paul 	ifp->if_init = bge_init;
28764d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
28774d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
28784d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
287935f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
2880d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
28814e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
2882ca3f1187SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TSO) != 0) {
2883ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
288404bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
2885ca3f1187SPyun YongHyeon 	}
28864e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
28874e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
28884e35d186SJung-uk Kim #endif
288995d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
289075719184SGleb Smirnoff #ifdef DEVICE_POLLING
289175719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
289275719184SGleb Smirnoff #endif
289395d67482SBill Paul 
2894a1d52896SBill Paul 	/*
2895d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2896d375e524SGleb Smirnoff 	 * to hardware bugs.
2897d375e524SGleb Smirnoff 	 */
2898d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2899d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
29004d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
2901d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2902d375e524SGleb Smirnoff 	}
2903d375e524SGleb Smirnoff 
2904d375e524SGleb Smirnoff 	/*
2905a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
290641abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
290741abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
290841abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
290941abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
291041abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
291141abcc1bSPaul Saab 	 * SK-9D41.
2912a1d52896SBill Paul 	 */
291341abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
291441abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
29155fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
29165fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
2917f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2918f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2919fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2920f6789fbaSPyun YongHyeon 			error = ENXIO;
2921f6789fbaSPyun YongHyeon 			goto fail;
2922f6789fbaSPyun YongHyeon 		}
292341abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
292441abcc1bSPaul Saab 	}
292541abcc1bSPaul Saab 
292695d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
2927ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
2928ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
2929ea3b4127SPyun YongHyeon 		if (BGE_IS_5714_FAMILY(sc))
2930ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
2931ea3b4127SPyun YongHyeon 		else
2932652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
2933ea3b4127SPyun YongHyeon 	}
293495d67482SBill Paul 
2935652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
29360c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
29370c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
29380c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
29396098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
29406098821cSJung-uk Kim 		    0, NULL);
294195d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
294295d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
2943da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
294495d67482SBill Paul 	} else {
294595d67482SBill Paul 		/*
29468cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
29478cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
29488cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
29498cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
29508cb1383cSDoug Ambrisko 		 * the PHY.
295195d67482SBill Paul 		 */
29524012d104SMarius Strobl 		trys = 0;
29538cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
29548cb1383cSDoug Ambrisko again:
29558cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
29568cb1383cSDoug Ambrisko 
295795d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
295895d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
29598cb1383cSDoug Ambrisko 			if (trys++ < 4) {
29608cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
29614e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
29624e35d186SJung-uk Kim 				    BMCR_RESET);
29638cb1383cSDoug Ambrisko 				goto again;
29648cb1383cSDoug Ambrisko 			}
29658cb1383cSDoug Ambrisko 
2966fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
296795d67482SBill Paul 			error = ENXIO;
296895d67482SBill Paul 			goto fail;
296995d67482SBill Paul 		}
29708cb1383cSDoug Ambrisko 
29718cb1383cSDoug Ambrisko 		/*
29728cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
29738cb1383cSDoug Ambrisko 		 */
29748cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
29758cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
297695d67482SBill Paul 	}
297795d67482SBill Paul 
297895d67482SBill Paul 	/*
2979e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2980e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2981e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2982e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2983e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2984e255b776SJohn Polstra 	 * payloads by copying the received packets.
2985e255b776SJohn Polstra 	 */
2986652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2987652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2988652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2989e255b776SJohn Polstra 
2990e255b776SJohn Polstra 	/*
299195d67482SBill Paul 	 * Call MI attach routine.
299295d67482SBill Paul 	 */
2993fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
2994b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
29950f9bd73bSSam Leffler 
299661ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
299761ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
299861ccb9daSPyun YongHyeon 
29990f9bd73bSSam Leffler 	/*
30000f9bd73bSSam Leffler 	 * Hookup IRQ last.
30010f9bd73bSSam Leffler 	 */
30024e35d186SJung-uk Kim #if __FreeBSD_version > 700030
3003dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3004dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
30057e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
30067e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3007dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3008dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3009dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3010dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3011dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3012dfe0df9aSPyun YongHyeon 			error = ENXIO;
3013dfe0df9aSPyun YongHyeon 			goto fail;
3014dfe0df9aSPyun YongHyeon 		}
3015dfe0df9aSPyun YongHyeon 		taskqueue_start_threads(&sc->bge_tq, 1, PI_NET, "%s taskq",
3016dfe0df9aSPyun YongHyeon 		    device_get_nameunit(sc->bge_dev));
3017dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3018dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3019dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3020dfe0df9aSPyun YongHyeon 		if (error)
3021dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3022dfe0df9aSPyun YongHyeon 	} else
3023dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3024dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3025dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
30264e35d186SJung-uk Kim #else
30274e35d186SJung-uk Kim 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
30284e35d186SJung-uk Kim 	   bge_intr, sc, &sc->bge_intrhand);
30294e35d186SJung-uk Kim #endif
30300f9bd73bSSam Leffler 
30310f9bd73bSSam Leffler 	if (error) {
3032fc74a9f9SBrooks Davis 		bge_detach(dev);
3033fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
30340f9bd73bSSam Leffler 	}
303595d67482SBill Paul 
303608013fd3SMarius Strobl 	return (0);
303708013fd3SMarius Strobl 
303895d67482SBill Paul fail:
303908013fd3SMarius Strobl 	bge_release_resources(sc);
304008013fd3SMarius Strobl 
304195d67482SBill Paul 	return (error);
304295d67482SBill Paul }
304395d67482SBill Paul 
304495d67482SBill Paul static int
30453f74909aSGleb Smirnoff bge_detach(device_t dev)
304695d67482SBill Paul {
304795d67482SBill Paul 	struct bge_softc *sc;
304895d67482SBill Paul 	struct ifnet *ifp;
304995d67482SBill Paul 
305095d67482SBill Paul 	sc = device_get_softc(dev);
3051fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
305295d67482SBill Paul 
305375719184SGleb Smirnoff #ifdef DEVICE_POLLING
305475719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
305575719184SGleb Smirnoff 		ether_poll_deregister(ifp);
305675719184SGleb Smirnoff #endif
305775719184SGleb Smirnoff 
30580f9bd73bSSam Leffler 	BGE_LOCK(sc);
305995d67482SBill Paul 	bge_stop(sc);
306095d67482SBill Paul 	bge_reset(sc);
30610f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
30620f9bd73bSSam Leffler 
30635dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
30645dda8085SOleg Bulyzhin 
3065dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3066dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
30670f9bd73bSSam Leffler 	ether_ifdetach(ifp);
306895d67482SBill Paul 
3069652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
307095d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
307195d67482SBill Paul 	} else {
307295d67482SBill Paul 		bus_generic_detach(dev);
307395d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
307495d67482SBill Paul 	}
307595d67482SBill Paul 
307695d67482SBill Paul 	bge_release_resources(sc);
307795d67482SBill Paul 
307895d67482SBill Paul 	return (0);
307995d67482SBill Paul }
308095d67482SBill Paul 
308195d67482SBill Paul static void
30823f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
308395d67482SBill Paul {
308495d67482SBill Paul 	device_t dev;
308595d67482SBill Paul 
308695d67482SBill Paul 	dev = sc->bge_dev;
308795d67482SBill Paul 
3088dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3089dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3090dfe0df9aSPyun YongHyeon 
309195d67482SBill Paul 	if (sc->bge_intrhand != NULL)
309295d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
309395d67482SBill Paul 
309495d67482SBill Paul 	if (sc->bge_irq != NULL)
3095724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3096724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3097724bd939SJohn Polstra 
3098724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3099724bd939SJohn Polstra 		pci_release_msi(dev);
310095d67482SBill Paul 
310195d67482SBill Paul 	if (sc->bge_res != NULL)
310295d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3103736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
310495d67482SBill Paul 
3105ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3106ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3107ad61f896SRuslan Ermilov 
3108f41ac2beSBill Paul 	bge_dma_free(sc);
310995d67482SBill Paul 
31100f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
31110f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
311295d67482SBill Paul }
311395d67482SBill Paul 
31148cb1383cSDoug Ambrisko static int
31153f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
311695d67482SBill Paul {
311795d67482SBill Paul 	device_t dev;
31185fea260fSMarius Strobl 	uint32_t cachesize, command, pcistate, reset, val;
31196f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
31200aaf1057SPyun YongHyeon 	uint16_t devctl;
31215fea260fSMarius Strobl 	int i;
312295d67482SBill Paul 
312395d67482SBill Paul 	dev = sc->bge_dev;
312495d67482SBill Paul 
312538cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
312638cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
31276f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
31286f8718a3SScott Long 			write_op = bge_writemem_direct;
31296f8718a3SScott Long 		else
31306f8718a3SScott Long 			write_op = bge_writemem_ind;
31319ba784dbSScott Long 	} else
31326f8718a3SScott Long 		write_op = bge_writereg_ind;
31336f8718a3SScott Long 
313495d67482SBill Paul 	/* Save some important PCI state. */
313595d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
313695d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
313795d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
313895d67482SBill Paul 
313995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
314095d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3141e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
314295d67482SBill Paul 
31436f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
31446f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
3145a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
31466f8718a3SScott Long 		if (bootverbose)
3147333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
31486f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
31496f8718a3SScott Long 	}
31506f8718a3SScott Long 
31516f8718a3SScott Long 	/*
31526f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
31536f8718a3SScott Long 	 * When firmware finishes its initialization it will
31546f8718a3SScott Long 	 * write ~BGE_MAGIC_NUMBER to the same location.
31556f8718a3SScott Long 	 */
31566f8718a3SScott Long 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
31576f8718a3SScott Long 
31580c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
3159e53d81eeSPaul Saab 
3160e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3161652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
31620c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
31630c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
3164e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
3165e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
31660c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
31670c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
3168e53d81eeSPaul Saab 		}
3169e53d81eeSPaul Saab 	}
3170e53d81eeSPaul Saab 
317121c9e407SDavid Christensen 	/*
31726f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
31736f8718a3SScott Long 	 * powered up in D0 uninitialized.
31746f8718a3SScott Long 	 */
31755345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
3176caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
31776f8718a3SScott Long 
317895d67482SBill Paul 	/* Issue global reset */
31796f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
318095d67482SBill Paul 
318138cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
31825fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
318338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
31845fea260fSMarius Strobl 		    val | BGE_VCPU_STATUS_DRV_RESET);
31855fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
318638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
31875fea260fSMarius Strobl 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
318838cc658fSJohn Baldwin 	}
318938cc658fSJohn Baldwin 
319095d67482SBill Paul 	DELAY(1000);
319195d67482SBill Paul 
3192e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3193652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
3194e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
3195e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
31965fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
31975fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
3198e53d81eeSPaul Saab 		}
31990aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
32000aaf1057SPyun YongHyeon 		    sc->bge_expcap + PCIR_EXPRESS_DEVICE_CTL, 2);
32010aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
32029a6e301dSPyun YongHyeon 		devctl &= ~(PCIM_EXP_CTL_RELAXED_ORD_ENABLE |
32039a6e301dSPyun YongHyeon 		    PCIM_EXP_CTL_NOSNOOP_ENABLE);
32040aaf1057SPyun YongHyeon 		/* Set PCIE max payload size to 128. */
32050aaf1057SPyun YongHyeon 		devctl &= ~PCIM_EXP_CTL_MAX_PAYLOAD;
32060aaf1057SPyun YongHyeon 		pci_write_config(dev, sc->bge_expcap + PCIR_EXPRESS_DEVICE_CTL,
32070aaf1057SPyun YongHyeon 		    devctl, 2);
32080aaf1057SPyun YongHyeon 		/* Clear error status. */
32090aaf1057SPyun YongHyeon 		pci_write_config(dev, sc->bge_expcap + PCIR_EXPRESS_DEVICE_STA,
32109a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_CORRECTABLE_ERROR |
32119a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_NON_FATAL_ERROR | PCIM_EXP_STA_FATAL_ERROR |
32129a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_UNSUPPORTED_REQ, 2);
3213e53d81eeSPaul Saab 	}
3214e53d81eeSPaul Saab 
32153f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
321695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
321795d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3218e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
321995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
322095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
32210c8aa4eaSJung-uk Kim 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
3222cbb2b2feSPyun YongHyeon 	/*
3223cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
3224fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
3225cbb2b2feSPyun YongHyeon 	 * read stale status block.
3226cbb2b2feSPyun YongHyeon 	 */
3227cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
3228cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
3229cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
3230cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
3231cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
3232cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
3233cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3234cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3235cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
3236cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
3237cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3238cbb2b2feSPyun YongHyeon 		}
3239cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
3240cbb2b2feSPyun YongHyeon 		    devctl, 2);
3241cbb2b2feSPyun YongHyeon 	}
3242bf6ef57aSJohn Polstra 	/* Re-enable MSI, if neccesary, and enable the memory arbiter. */
32434c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
3244bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
3245bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
32460aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
32470aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
32480aaf1057SPyun YongHyeon 			pci_write_config(dev,
32490aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
3250bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
3251bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
3252bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
3253bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
3254bf6ef57aSJohn Polstra 		}
32554c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
32564c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
32574c0da0ffSGleb Smirnoff 	} else
3258a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3259a7b0c314SPaul Saab 
326038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
326138cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
326238cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
326338cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
326438cc658fSJohn Baldwin 				break;
326538cc658fSJohn Baldwin 			DELAY(100);
326638cc658fSJohn Baldwin 		}
326738cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
3268333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
326938cc658fSJohn Baldwin 			return (1);
327038cc658fSJohn Baldwin 		}
327138cc658fSJohn Baldwin 	} else {
327295d67482SBill Paul 		/*
32736f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
327408013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
32755fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
32765fea260fSMarius Strobl 		 * address is fitted though.
327795d67482SBill Paul 		 */
327895d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
3279d5d23857SJung-uk Kim 			DELAY(10);
328095d67482SBill Paul 			val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
328195d67482SBill Paul 			if (val == ~BGE_MAGIC_NUMBER)
328295d67482SBill Paul 				break;
328395d67482SBill Paul 		}
328495d67482SBill Paul 
32855fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
3286333704a3SPyun YongHyeon 			device_printf(dev,
3287333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
3288333704a3SPyun YongHyeon 			    val);
328938cc658fSJohn Baldwin 	}
329095d67482SBill Paul 
329195d67482SBill Paul 	/*
329295d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
329395d67482SBill Paul 	 * return to its original pre-reset state. This is a
329495d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
329595d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
329695d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
329795d67482SBill Paul 	 * results.
329895d67482SBill Paul 	 */
329995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
330095d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
330195d67482SBill Paul 			break;
330295d67482SBill Paul 		DELAY(10);
330395d67482SBill Paul 	}
330495d67482SBill Paul 
33053f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
3306e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
330795d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
330895d67482SBill Paul 
33098cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
33108cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
33118cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
33128cb1383cSDoug Ambrisko 
331395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
331495d67482SBill Paul 
3315da3003f0SBill Paul 	/*
3316da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
3317da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
3318da3003f0SBill Paul 	 * to 1.2V.
3319da3003f0SBill Paul 	 */
3320652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
3321652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
33225fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
33235fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
33245fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
3325da3003f0SBill Paul 	}
3326da3003f0SBill Paul 
3327e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3328652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
3329a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
3330a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
3331a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
33325fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
33335fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
3334e53d81eeSPaul Saab 	}
333595d67482SBill Paul 	DELAY(10000);
33368cb1383cSDoug Ambrisko 
33378cb1383cSDoug Ambrisko 	return (0);
333895d67482SBill Paul }
333995d67482SBill Paul 
3340e0b7b101SPyun YongHyeon static __inline void
3341e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
3342e0b7b101SPyun YongHyeon {
3343e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
3344e0b7b101SPyun YongHyeon 
3345e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
3346e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
3347e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
3348e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3349e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3350e0b7b101SPyun YongHyeon }
3351e0b7b101SPyun YongHyeon 
3352e0b7b101SPyun YongHyeon static __inline void
3353e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
3354e0b7b101SPyun YongHyeon {
3355e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
3356e0b7b101SPyun YongHyeon 
3357e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
3358e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
3359e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
3360e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
3361e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
3362e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
3363e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3364e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3365e0b7b101SPyun YongHyeon }
3366e0b7b101SPyun YongHyeon 
336795d67482SBill Paul /*
336895d67482SBill Paul  * Frame reception handling. This is called if there's a frame
336995d67482SBill Paul  * on the receive return list.
337095d67482SBill Paul  *
337195d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
33721be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
337395d67482SBill Paul  * 2) the frame is from the standard receive ring
337495d67482SBill Paul  */
337595d67482SBill Paul 
33761abcdbd1SAttilio Rao static int
3377dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
337895d67482SBill Paul {
337995d67482SBill Paul 	struct ifnet *ifp;
33801abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
3381b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
338295d67482SBill Paul 
33837f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
33840f9bd73bSSam Leffler 
33853f74909aSGleb Smirnoff 	/* Nothing to do. */
33867f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
33871abcdbd1SAttilio Rao 		return (rx_npkts);
3388cfcb5025SOleg Bulyzhin 
3389fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
339095d67482SBill Paul 
3391f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
3392e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
3393f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
339415eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
3395c215fd77SPyun YongHyeon 	if (ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
3396c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
3397f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
339815eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
3399f41ac2beSBill Paul 
34007f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
340195d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
34023f74909aSGleb Smirnoff 		uint32_t		rxidx;
340395d67482SBill Paul 		struct mbuf		*m = NULL;
34043f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
340595d67482SBill Paul 		int			have_tag = 0;
340695d67482SBill Paul 
340775719184SGleb Smirnoff #ifdef DEVICE_POLLING
340875719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
340975719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
341075719184SGleb Smirnoff 				break;
341175719184SGleb Smirnoff 			sc->rxcycles--;
341275719184SGleb Smirnoff 		}
341375719184SGleb Smirnoff #endif
341475719184SGleb Smirnoff 
34157f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
341695d67482SBill Paul 
341795d67482SBill Paul 		rxidx = cur_rx->bge_idx;
34187f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
341995d67482SBill Paul 
3420cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
3421cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
342295d67482SBill Paul 			have_tag = 1;
342395d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
342495d67482SBill Paul 		}
342595d67482SBill Paul 
342695d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
342795d67482SBill Paul 			jumbocnt++;
3428943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
342995d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3430e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
343195d67482SBill Paul 				continue;
343295d67482SBill Paul 			}
3433943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
3434e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
3435943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
343695d67482SBill Paul 				continue;
343795d67482SBill Paul 			}
343803e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
343995d67482SBill Paul 		} else {
344095d67482SBill Paul 			stdcnt++;
3441e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
344295d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3443e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
344495d67482SBill Paul 				continue;
344595d67482SBill Paul 			}
3446943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
3447e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
3448943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
344995d67482SBill Paul 				continue;
345095d67482SBill Paul 			}
345103e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
345295d67482SBill Paul 		}
345395d67482SBill Paul 
345495d67482SBill Paul 		ifp->if_ipackets++;
3455e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3456e255b776SJohn Polstra 		/*
3457e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
3458e65bed95SPyun YongHyeon 		 * the payload is aligned.
3459e255b776SJohn Polstra 		 */
3460652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
3461e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
3462e255b776SJohn Polstra 			    cur_rx->bge_len);
3463e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
3464e255b776SJohn Polstra 		}
3465e255b776SJohn Polstra #endif
3466473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
346795d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
346895d67482SBill Paul 
3469b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
347078178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
347195d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
34720c8aa4eaSJung-uk Kim 				if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
34730c8aa4eaSJung-uk Kim 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
347478178cd1SGleb Smirnoff 			}
3475d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
3476d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
347795d67482SBill Paul 				m->m_pkthdr.csum_data =
347895d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
3479ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
3480ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
348195d67482SBill Paul 			}
348295d67482SBill Paul 		}
348395d67482SBill Paul 
348495d67482SBill Paul 		/*
3485673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
3486673d9191SSam Leffler 		 * attach that information to the packet.
348795d67482SBill Paul 		 */
3488d147662cSGleb Smirnoff 		if (have_tag) {
34894e35d186SJung-uk Kim #if __FreeBSD_version > 700022
349078ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
349178ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
34924e35d186SJung-uk Kim #else
34934e35d186SJung-uk Kim 			VLAN_INPUT_TAG_NEW(ifp, m, vlan_tag);
34944e35d186SJung-uk Kim 			if (m == NULL)
34954e35d186SJung-uk Kim 				continue;
34964e35d186SJung-uk Kim #endif
3497d147662cSGleb Smirnoff 		}
349895d67482SBill Paul 
3499dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
35000f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
3501673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
35020f9bd73bSSam Leffler 			BGE_LOCK(sc);
3503dfe0df9aSPyun YongHyeon 		} else
3504dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
3505d4da719cSAttilio Rao 		rx_npkts++;
350625e13e68SXin LI 
350725e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
35088cf7d13dSAttilio Rao 			return (rx_npkts);
350995d67482SBill Paul 	}
351095d67482SBill Paul 
351115eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
351215eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
3513e65bed95SPyun YongHyeon 	if (stdcnt > 0)
3514f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3515e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
35164c0da0ffSGleb Smirnoff 
3517c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
3518f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
35194c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3520f41ac2beSBill Paul 
35217f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
352238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
352395d67482SBill Paul 	if (stdcnt)
3524767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
3525767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
352695d67482SBill Paul 	if (jumbocnt)
3527767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
3528767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
3529f5a034f9SPyun YongHyeon #ifdef notyet
3530f5a034f9SPyun YongHyeon 	/*
3531f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
3532f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
3533f5a034f9SPyun YongHyeon 	 */
3534f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
3535f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3536f5a034f9SPyun YongHyeon #endif
35371abcdbd1SAttilio Rao 	return (rx_npkts);
353895d67482SBill Paul }
353995d67482SBill Paul 
354095d67482SBill Paul static void
3541b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
354295d67482SBill Paul {
354395a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
354495d67482SBill Paul 	struct ifnet *ifp;
354595d67482SBill Paul 
35460f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
35470f9bd73bSSam Leffler 
35483f74909aSGleb Smirnoff 	/* Nothing to do. */
3549b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
3550cfcb5025SOleg Bulyzhin 		return;
3551cfcb5025SOleg Bulyzhin 
3552fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
355395d67482SBill Paul 
3554e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
35555c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
355695d67482SBill Paul 	/*
355795d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
355895d67482SBill Paul 	 * frames that have been sent.
355995d67482SBill Paul 	 */
3560b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
356195a0a340SPyun YongHyeon 		uint32_t		idx;
356295d67482SBill Paul 
356395d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
3564f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
356595d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
356695d67482SBill Paul 			ifp->if_opackets++;
356795d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
35680ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
3569e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
3570e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
35710ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
3572f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
3573e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
3574e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
357595d67482SBill Paul 		}
357695d67482SBill Paul 		sc->bge_txcnt--;
357795d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
357895d67482SBill Paul 	}
357995d67482SBill Paul 
358013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
35815b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
35825b01e77cSBruce Evans 		sc->bge_timer = 0;
358395d67482SBill Paul }
358495d67482SBill Paul 
358575719184SGleb Smirnoff #ifdef DEVICE_POLLING
35861abcdbd1SAttilio Rao static int
358775719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
358875719184SGleb Smirnoff {
358975719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
3590b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
3591366454f2SOleg Bulyzhin 	uint32_t statusword;
35921abcdbd1SAttilio Rao 	int rx_npkts = 0;
359375719184SGleb Smirnoff 
35943f74909aSGleb Smirnoff 	BGE_LOCK(sc);
35953f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
35963f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
35971abcdbd1SAttilio Rao 		return (rx_npkts);
35983f74909aSGleb Smirnoff 	}
359975719184SGleb Smirnoff 
3600dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3601b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3602b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3603b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3604b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3605dab5cd05SOleg Bulyzhin 
3606175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
3607175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3608dab5cd05SOleg Bulyzhin 
3609dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3610b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3611b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3612366454f2SOleg Bulyzhin 
36130c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
3614366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
3615366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
3616366454f2SOleg Bulyzhin 
3617366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
3618366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
36194c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3620652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3621366454f2SOleg Bulyzhin 			bge_link_upd(sc);
3622366454f2SOleg Bulyzhin 
3623366454f2SOleg Bulyzhin 	sc->rxcycles = count;
3624dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
362525e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
362625e13e68SXin LI 		BGE_UNLOCK(sc);
36278cf7d13dSAttilio Rao 		return (rx_npkts);
362825e13e68SXin LI 	}
3629b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
3630366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3631366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
36323f74909aSGleb Smirnoff 
36333f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
36341abcdbd1SAttilio Rao 	return (rx_npkts);
363575719184SGleb Smirnoff }
363675719184SGleb Smirnoff #endif /* DEVICE_POLLING */
363775719184SGleb Smirnoff 
3638dfe0df9aSPyun YongHyeon static int
3639dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
3640dfe0df9aSPyun YongHyeon {
3641dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
3642dfe0df9aSPyun YongHyeon 
3643dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
3644dfe0df9aSPyun YongHyeon 	/*
3645dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
3646dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
3647dfe0df9aSPyun YongHyeon 	 */
3648dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
3649dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
3650dfe0df9aSPyun YongHyeon }
3651dfe0df9aSPyun YongHyeon 
3652dfe0df9aSPyun YongHyeon static void
3653dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
3654dfe0df9aSPyun YongHyeon {
3655dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
3656dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
3657dfe0df9aSPyun YongHyeon 	uint32_t status;
3658dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
3659dfe0df9aSPyun YongHyeon 
3660dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
3661dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
3662dfe0df9aSPyun YongHyeon 
366366151edfSPyun YongHyeon 	BGE_LOCK(sc);
366466151edfSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
366566151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
3666dfe0df9aSPyun YongHyeon 		return;
366766151edfSPyun YongHyeon 	}
3668dfe0df9aSPyun YongHyeon 
3669dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
3670dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3671dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3672dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3673dfe0df9aSPyun YongHyeon 
3674dfe0df9aSPyun YongHyeon 	/* Save producer/consumer indexess. */
3675dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3676dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3677dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
3678dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3679dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3680dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3681dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
368266151edfSPyun YongHyeon 
368366151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
368466151edfSPyun YongHyeon 		bge_link_upd(sc);
368566151edfSPyun YongHyeon 
3686dfe0df9aSPyun YongHyeon 	/* Let controller work. */
3687dfe0df9aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3688dfe0df9aSPyun YongHyeon 
368966151edfSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
369066151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
3691dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
369266151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
3693dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
369466151edfSPyun YongHyeon 		BGE_LOCK(sc);
3695dfe0df9aSPyun YongHyeon 	}
3696dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3697dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
3698dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
3699dfe0df9aSPyun YongHyeon 	    	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3700dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
3701dfe0df9aSPyun YongHyeon 	}
370266151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
3703dfe0df9aSPyun YongHyeon }
3704dfe0df9aSPyun YongHyeon 
370595d67482SBill Paul static void
37063f74909aSGleb Smirnoff bge_intr(void *xsc)
370795d67482SBill Paul {
370895d67482SBill Paul 	struct bge_softc *sc;
370995d67482SBill Paul 	struct ifnet *ifp;
3710dab5cd05SOleg Bulyzhin 	uint32_t statusword;
3711b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
371295d67482SBill Paul 
371395d67482SBill Paul 	sc = xsc;
3714f41ac2beSBill Paul 
37150f9bd73bSSam Leffler 	BGE_LOCK(sc);
37160f9bd73bSSam Leffler 
3717dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
3718dab5cd05SOleg Bulyzhin 
371975719184SGleb Smirnoff #ifdef DEVICE_POLLING
372075719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
372175719184SGleb Smirnoff 		BGE_UNLOCK(sc);
372275719184SGleb Smirnoff 		return;
372375719184SGleb Smirnoff 	}
372475719184SGleb Smirnoff #endif
372575719184SGleb Smirnoff 
3726f30cbfc6SScott Long 	/*
3727b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
3728b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
3729b848e032SBruce Evans 	 * our current organization this just gives complications and
3730b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
3731b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
3732b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
3733b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
3734b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
3735b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
3736b848e032SBruce Evans 	 *
3737b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
3738b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
3739b848e032SBruce Evans 	 * changing later because it is more efficient to get another
3740b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
3741b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
3742b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
3743b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
3744b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
3745b848e032SBruce Evans 	 */
374638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3747b848e032SBruce Evans 
3748f584dfd1SPyun YongHyeon 	/*
3749f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
3750f584dfd1SPyun YongHyeon 	 */
3751f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
3752f584dfd1SPyun YongHyeon 
3753f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
3754f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3755f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3756f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3757f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3758f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3759f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3760f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3761f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3762f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3763f584dfd1SPyun YongHyeon 
37641f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
37654c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3766f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
3767dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
376895d67482SBill Paul 
376913f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
37703f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
3771dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
377225e13e68SXin LI 	}
377395d67482SBill Paul 
377425e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
37753f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
3776b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
377795d67482SBill Paul 	}
377895d67482SBill Paul 
377913f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
378013f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
37810f9bd73bSSam Leffler 		bge_start_locked(ifp);
37820f9bd73bSSam Leffler 
37830f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
378495d67482SBill Paul }
378595d67482SBill Paul 
378695d67482SBill Paul static void
37878cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
37888cb1383cSDoug Ambrisko {
37898cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
37908cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
37918cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
37928cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
37938cb1383cSDoug Ambrisko 		else {
3794899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
37958cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
37968cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
37978cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
37988cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
37998cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
380039153c5aSJung-uk Kim 			    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
38018cb1383cSDoug Ambrisko 		}
38028cb1383cSDoug Ambrisko 	}
38038cb1383cSDoug Ambrisko }
38048cb1383cSDoug Ambrisko 
38058cb1383cSDoug Ambrisko static void
3806b74e67fbSGleb Smirnoff bge_tick(void *xsc)
38070f9bd73bSSam Leffler {
3808b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
380995d67482SBill Paul 	struct mii_data *mii = NULL;
381095d67482SBill Paul 
38110f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
381295d67482SBill Paul 
38135dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
38145dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
38155dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
38165dda8085SOleg Bulyzhin 	    	return;
38175dda8085SOleg Bulyzhin 
38187ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
38190434d1b8SBill Paul 		bge_stats_update_regs(sc);
38200434d1b8SBill Paul 	else
382195d67482SBill Paul 		bge_stats_update(sc);
382295d67482SBill Paul 
3823652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
382495d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
382582b67c01SOleg Bulyzhin 		/*
382682b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
382782b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
382882b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
382982b67c01SOleg Bulyzhin 		 */
383082b67c01SOleg Bulyzhin 		if (!sc->bge_link)
383195d67482SBill Paul 			mii_tick(mii);
38327b97099dSOleg Bulyzhin 	} else {
38337b97099dSOleg Bulyzhin 		/*
38347b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
38357b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
38367b97099dSOleg Bulyzhin 		 * and trigger interrupt.
38377b97099dSOleg Bulyzhin 		 */
38387b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
38393f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
38407b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
38417b97099dSOleg Bulyzhin #endif
38427b97099dSOleg Bulyzhin 		{
38437b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
38444f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
38454f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
38467b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
38474f0794ffSBjoern A. Zeeb 		else
38484f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
38497b97099dSOleg Bulyzhin 		}
3850dab5cd05SOleg Bulyzhin 	}
385195d67482SBill Paul 
38528cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
3853b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
38548cb1383cSDoug Ambrisko 
3855dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
385695d67482SBill Paul }
385795d67482SBill Paul 
385895d67482SBill Paul static void
38593f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
38600434d1b8SBill Paul {
38613f74909aSGleb Smirnoff 	struct ifnet *ifp;
38622280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
38630434d1b8SBill Paul 
3864fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
38652280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
38660434d1b8SBill Paul 
38672280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
38682280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
38692280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
38702280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
38712280c16bSPyun YongHyeon 	stats->outXonSent +=
38722280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
38732280c16bSPyun YongHyeon 	stats->outXoffSent +=
38742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
38752280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
38762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
38772280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
38782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
38792280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
38802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
38812280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
38822280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
38832280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
38842280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
38852280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
38862280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
38872280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
38882280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
38892280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
38902280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
38912280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
38922280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
38937e6e2507SJung-uk Kim 
38942280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
38952280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
38962280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
38972280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
38982280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
38992280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
39002280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
39012280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
39022280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
39032280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
39042280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
39052280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
39062280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
39072280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
39082280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
39092280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
39102280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
39112280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
39122280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
39132280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
39142280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
39152280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
39162280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
39172280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
39182280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
39192280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
39202280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
39212280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
39222280c16bSPyun YongHyeon 
39232280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
39242280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
39252280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
39262280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
39272280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
39282280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
39292280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
39302280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
39312280c16bSPyun YongHyeon 	stats->InputDiscards +=
39322280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
39332280c16bSPyun YongHyeon 	stats->InputErrors +=
39342280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
39352280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
39362280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
39372280c16bSPyun YongHyeon 
39382280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
39392280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
39402280c16bSPyun YongHyeon 	    stats->InputErrors);
39412280c16bSPyun YongHyeon }
39422280c16bSPyun YongHyeon 
39432280c16bSPyun YongHyeon static void
39442280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
39452280c16bSPyun YongHyeon {
39462280c16bSPyun YongHyeon 
39472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
39482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
39492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
39502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
39512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
39522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
39532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
39542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
39552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
39562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
39572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
39582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
39592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
39602280c16bSPyun YongHyeon 
39612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
39622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
39632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
39642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
39652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
39662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
39672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
39682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
39692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
39702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
39712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
39722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
39732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
39742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
39752280c16bSPyun YongHyeon 
39762280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
39772280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
39782280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
39792280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
39802280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
39812280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
39822280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
39830434d1b8SBill Paul }
39840434d1b8SBill Paul 
39850434d1b8SBill Paul static void
39863f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
398795d67482SBill Paul {
398895d67482SBill Paul 	struct ifnet *ifp;
3989e907febfSPyun YongHyeon 	bus_size_t stats;
39907e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
399195d67482SBill Paul 
3992fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
399395d67482SBill Paul 
3994e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3995e907febfSPyun YongHyeon 
3996e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
3997e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
399895d67482SBill Paul 
39998634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
40006b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
40016fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
40026fb34dd2SOleg Bulyzhin 
40036fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
40046b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
40056fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
40066fb34dd2SOleg Bulyzhin 
40076fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
40086b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
40096fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
401095d67482SBill Paul 
4011e907febfSPyun YongHyeon #undef	READ_STAT
401295d67482SBill Paul }
401395d67482SBill Paul 
401495d67482SBill Paul /*
4015d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
4016d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
4017d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
4018d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
4019d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
4020d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
4021d375e524SGleb Smirnoff  */
4022d375e524SGleb Smirnoff static __inline int
4023d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
4024d375e524SGleb Smirnoff {
4025d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
4026d375e524SGleb Smirnoff 	struct mbuf *last;
4027d375e524SGleb Smirnoff 
4028d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
4029d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
4030d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
4031d375e524SGleb Smirnoff 		last = m;
4032d375e524SGleb Smirnoff 	} else {
4033d375e524SGleb Smirnoff 		/*
4034d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
4035d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
4036d375e524SGleb Smirnoff 		 */
4037d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
4038d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
4039d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
4040d375e524SGleb Smirnoff 			struct mbuf *n;
4041d375e524SGleb Smirnoff 
4042d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
4043d375e524SGleb Smirnoff 			if (n == NULL)
4044d375e524SGleb Smirnoff 				return (ENOBUFS);
4045d375e524SGleb Smirnoff 			n->m_len = 0;
4046d375e524SGleb Smirnoff 			last->m_next = n;
4047d375e524SGleb Smirnoff 			last = n;
4048d375e524SGleb Smirnoff 		}
4049d375e524SGleb Smirnoff 	}
4050d375e524SGleb Smirnoff 
4051d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
4052d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
4053d375e524SGleb Smirnoff 	last->m_len += padlen;
4054d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
4055d375e524SGleb Smirnoff 
4056d375e524SGleb Smirnoff 	return (0);
4057d375e524SGleb Smirnoff }
4058d375e524SGleb Smirnoff 
4059ca3f1187SPyun YongHyeon static struct mbuf *
4060ca3f1187SPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss)
4061ca3f1187SPyun YongHyeon {
4062ca3f1187SPyun YongHyeon 	struct ip *ip;
4063ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
4064ca3f1187SPyun YongHyeon 	struct mbuf *n;
4065ca3f1187SPyun YongHyeon 	uint16_t hlen;
40665b355c4fSPyun YongHyeon 	uint32_t poff;
4067ca3f1187SPyun YongHyeon 
4068ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
4069ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
4070ca3f1187SPyun YongHyeon 		n = m_dup(m, M_DONTWAIT);
4071ca3f1187SPyun YongHyeon 		m_freem(m);
4072ca3f1187SPyun YongHyeon 		if (n == NULL)
4073ca3f1187SPyun YongHyeon 			return (NULL);
4074ca3f1187SPyun YongHyeon 		m = n;
4075ca3f1187SPyun YongHyeon 	}
40765b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
4077ca3f1187SPyun YongHyeon 	if (m == NULL)
4078ca3f1187SPyun YongHyeon 		return (NULL);
40795b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
40805b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
4081ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
4082ca3f1187SPyun YongHyeon 	if (m == NULL)
4083ca3f1187SPyun YongHyeon 		return (NULL);
4084ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
40855b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
4086ca3f1187SPyun YongHyeon 	if (m == NULL)
4087ca3f1187SPyun YongHyeon 		return (NULL);
4088ca3f1187SPyun YongHyeon 	/*
4089ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
4090ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
4091ca3f1187SPyun YongHyeon 	 */
4092ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
4093ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
4094ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
4095ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
4096ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
4097ca3f1187SPyun YongHyeon 	/*
4098ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
4099ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
4100ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
4101ca3f1187SPyun YongHyeon 	 * we only support hardware based TSO which is applicable for
4102ca3f1187SPyun YongHyeon 	 * BCM5755 or newer controllers. Hardware based TSO uses 11
4103ca3f1187SPyun YongHyeon 	 * bits to store MSS and upper 5 bits are used to store IP/TCP
4104ca3f1187SPyun YongHyeon 	 * header length(including IP/TCP options). The header length
4105ca3f1187SPyun YongHyeon 	 * is expressed as 32 bits unit.
4106ca3f1187SPyun YongHyeon 	 */
4107ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
4108ca3f1187SPyun YongHyeon 	*mss |= (hlen << 11);
4109ca3f1187SPyun YongHyeon 	return (m);
4110ca3f1187SPyun YongHyeon }
4111ca3f1187SPyun YongHyeon 
4112d375e524SGleb Smirnoff /*
411395d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
411495d67482SBill Paul  * pointers to descriptors.
411595d67482SBill Paul  */
411695d67482SBill Paul static int
4117676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
411895d67482SBill Paul {
41197e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
4120f41ac2beSBill Paul 	bus_dmamap_t		map;
4121676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
4122676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
41237e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
4124ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
41257e27542aSGleb Smirnoff 	int			nsegs, i, error;
412695d67482SBill Paul 
41276909dc43SGleb Smirnoff 	csum_flags = 0;
4128ca3f1187SPyun YongHyeon 	mss = 0;
4129ca3f1187SPyun YongHyeon 	vlan_tag = 0;
4130ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
4131ca3f1187SPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss);
4132ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
4133ca3f1187SPyun YongHyeon 			return (ENOBUFS);
4134ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
4135ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
413635f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
41376909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
41386909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
41396909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
41406909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
41416909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
41426909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
41436909dc43SGleb Smirnoff 				m_freem(m);
41446909dc43SGleb Smirnoff 				*m_head = NULL;
41456909dc43SGleb Smirnoff 				return (error);
41466909dc43SGleb Smirnoff 			}
41476909dc43SGleb Smirnoff 		}
41486909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
41496909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
41506909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
41516909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
41526909dc43SGleb Smirnoff 	}
41536909dc43SGleb Smirnoff 
4154d94f2b85SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0 &&
4155beaa2ae1SPyun YongHyeon 	    sc->bge_forced_collapse > 0 &&
4156beaa2ae1SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
4157d94f2b85SPyun YongHyeon 		/*
4158d94f2b85SPyun YongHyeon 		 * Forcedly collapse mbuf chains to overcome hardware
4159d94f2b85SPyun YongHyeon 		 * limitation which only support a single outstanding
4160d94f2b85SPyun YongHyeon 		 * DMA read operation.
4161d94f2b85SPyun YongHyeon 		 */
4162beaa2ae1SPyun YongHyeon 		if (sc->bge_forced_collapse == 1)
4163d94f2b85SPyun YongHyeon 			m = m_defrag(m, M_DONTWAIT);
4164d94f2b85SPyun YongHyeon 		else
4165beaa2ae1SPyun YongHyeon 			m = m_collapse(m, M_DONTWAIT, sc->bge_forced_collapse);
4166261f04d6SPyun YongHyeon 		if (m == NULL)
4167261f04d6SPyun YongHyeon 			m = *m_head;
4168d94f2b85SPyun YongHyeon 		*m_head = m;
4169d94f2b85SPyun YongHyeon 	}
4170d94f2b85SPyun YongHyeon 
41717e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
41720ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
4173676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
41747e27542aSGleb Smirnoff 	if (error == EFBIG) {
41754eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
4176676ad2c9SGleb Smirnoff 		if (m == NULL) {
4177676ad2c9SGleb Smirnoff 			m_freem(*m_head);
4178676ad2c9SGleb Smirnoff 			*m_head = NULL;
41797e27542aSGleb Smirnoff 			return (ENOBUFS);
41807e27542aSGleb Smirnoff 		}
4181676ad2c9SGleb Smirnoff 		*m_head = m;
41820ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
41830ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
4184676ad2c9SGleb Smirnoff 		if (error) {
4185676ad2c9SGleb Smirnoff 			m_freem(m);
4186676ad2c9SGleb Smirnoff 			*m_head = NULL;
41877e27542aSGleb Smirnoff 			return (error);
41887e27542aSGleb Smirnoff 		}
4189676ad2c9SGleb Smirnoff 	} else if (error != 0)
4190676ad2c9SGleb Smirnoff 		return (error);
41917e27542aSGleb Smirnoff 
4192167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
4193167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
41940ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
419595d67482SBill Paul 		return (ENOBUFS);
41967e27542aSGleb Smirnoff 	}
41977e27542aSGleb Smirnoff 
41980ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
4199e65bed95SPyun YongHyeon 
4200ca3f1187SPyun YongHyeon #if __FreeBSD_version > 700022
4201ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
4202ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
4203ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
4204ca3f1187SPyun YongHyeon 	}
4205ca3f1187SPyun YongHyeon #else
4206ca3f1187SPyun YongHyeon 	{
4207ca3f1187SPyun YongHyeon 		struct m_tag		*mtag;
4208ca3f1187SPyun YongHyeon 
4209ca3f1187SPyun YongHyeon 		if ((mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m)) != NULL) {
4210ca3f1187SPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
4211ca3f1187SPyun YongHyeon 			vlan_tag = VLAN_TAG_VALUE(mtag);
4212ca3f1187SPyun YongHyeon 		}
4213ca3f1187SPyun YongHyeon 	}
4214ca3f1187SPyun YongHyeon #endif
42157e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
42167e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
42177e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
42187e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
42197e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
42207e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
4221ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
4222ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
42237e27542aSGleb Smirnoff 		if (i == nsegs - 1)
42247e27542aSGleb Smirnoff 			break;
42257e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
42267e27542aSGleb Smirnoff 	}
42277e27542aSGleb Smirnoff 
42287e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
42297e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
4230676ad2c9SGleb Smirnoff 
4231f41ac2beSBill Paul 	/*
4232f41ac2beSBill Paul 	 * Insure that the map for this transmission
4233f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
4234f41ac2beSBill Paul 	 * in this chain.
4235f41ac2beSBill Paul 	 */
42367e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
42377e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
4238676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
42397e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
424095d67482SBill Paul 
42417e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
42427e27542aSGleb Smirnoff 	*txidx = idx;
424395d67482SBill Paul 
424495d67482SBill Paul 	return (0);
424595d67482SBill Paul }
424695d67482SBill Paul 
424795d67482SBill Paul /*
424895d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
424995d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
425095d67482SBill Paul  */
425195d67482SBill Paul static void
42523f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
425395d67482SBill Paul {
425495d67482SBill Paul 	struct bge_softc *sc;
4255167fdb62SPyun YongHyeon 	struct mbuf *m_head;
425614bbd30fSGleb Smirnoff 	uint32_t prodidx;
4257167fdb62SPyun YongHyeon 	int count;
425895d67482SBill Paul 
425995d67482SBill Paul 	sc = ifp->if_softc;
4260167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
426195d67482SBill Paul 
4262167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
4263167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4264167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
426595d67482SBill Paul 		return;
426695d67482SBill Paul 
426714bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
426895d67482SBill Paul 
4269167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
4270167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
4271167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4272167fdb62SPyun YongHyeon 			break;
4273167fdb62SPyun YongHyeon 		}
42744d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
427595d67482SBill Paul 		if (m_head == NULL)
427695d67482SBill Paul 			break;
427795d67482SBill Paul 
427895d67482SBill Paul 		/*
427995d67482SBill Paul 		 * XXX
4280b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
4281b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
4282b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
4283b874fdd4SYaroslav Tykhiy 		 *
4284b874fdd4SYaroslav Tykhiy 		 * XXX
428595d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
428695d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
428795d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
428895d67482SBill Paul 		 * chain at once.
428995d67482SBill Paul 		 * (paranoia -- may not actually be needed)
429095d67482SBill Paul 		 */
429195d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
429295d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
429395d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
429495d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
42954d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
429613f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
429795d67482SBill Paul 				break;
429895d67482SBill Paul 			}
429995d67482SBill Paul 		}
430095d67482SBill Paul 
430195d67482SBill Paul 		/*
430295d67482SBill Paul 		 * Pack the data into the transmit ring. If we
430395d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
430495d67482SBill Paul 		 * for the NIC to drain the ring.
430595d67482SBill Paul 		 */
4306676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
4307676ad2c9SGleb Smirnoff 			if (m_head == NULL)
4308676ad2c9SGleb Smirnoff 				break;
43094d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
431013f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
431195d67482SBill Paul 			break;
431295d67482SBill Paul 		}
4313303a718cSDag-Erling Smørgrav 		++count;
431495d67482SBill Paul 
431595d67482SBill Paul 		/*
431695d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
431795d67482SBill Paul 		 * to him.
431895d67482SBill Paul 		 */
43194e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
432045ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
43214e35d186SJung-uk Kim #else
43224e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
43234e35d186SJung-uk Kim #endif
432495d67482SBill Paul 	}
432595d67482SBill Paul 
4326167fdb62SPyun YongHyeon 	if (count > 0) {
4327aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
43285c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
43293f74909aSGleb Smirnoff 		/* Transmit. */
433038cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
43313927098fSPaul Saab 		/* 5700 b2 errata */
4332e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
433338cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
433495d67482SBill Paul 
433514bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
433614bbd30fSGleb Smirnoff 
433795d67482SBill Paul 		/*
433895d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
433995d67482SBill Paul 		 */
4340b74e67fbSGleb Smirnoff 		sc->bge_timer = 5;
434195d67482SBill Paul 	}
4342167fdb62SPyun YongHyeon }
434395d67482SBill Paul 
43440f9bd73bSSam Leffler /*
43450f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
43460f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
43470f9bd73bSSam Leffler  */
434895d67482SBill Paul static void
43493f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
435095d67482SBill Paul {
43510f9bd73bSSam Leffler 	struct bge_softc *sc;
43520f9bd73bSSam Leffler 
43530f9bd73bSSam Leffler 	sc = ifp->if_softc;
43540f9bd73bSSam Leffler 	BGE_LOCK(sc);
43550f9bd73bSSam Leffler 	bge_start_locked(ifp);
43560f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
43570f9bd73bSSam Leffler }
43580f9bd73bSSam Leffler 
43590f9bd73bSSam Leffler static void
43603f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
43610f9bd73bSSam Leffler {
436295d67482SBill Paul 	struct ifnet *ifp;
43633f74909aSGleb Smirnoff 	uint16_t *m;
436495d67482SBill Paul 
43650f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
436695d67482SBill Paul 
4367fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
436895d67482SBill Paul 
436913f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
437095d67482SBill Paul 		return;
437195d67482SBill Paul 
437295d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
437395d67482SBill Paul 	bge_stop(sc);
43748cb1383cSDoug Ambrisko 
43758cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
43768cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
437795d67482SBill Paul 	bge_reset(sc);
43788cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
43798cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
43808cb1383cSDoug Ambrisko 
438195d67482SBill Paul 	bge_chipinit(sc);
438295d67482SBill Paul 
438395d67482SBill Paul 	/*
438495d67482SBill Paul 	 * Init the various state machines, ring
438595d67482SBill Paul 	 * control blocks and firmware.
438695d67482SBill Paul 	 */
438795d67482SBill Paul 	if (bge_blockinit(sc)) {
4388fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
438995d67482SBill Paul 		return;
439095d67482SBill Paul 	}
439195d67482SBill Paul 
4392fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
439395d67482SBill Paul 
439495d67482SBill Paul 	/* Specify MTU. */
439595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
4396cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
4397cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
439895d67482SBill Paul 
439995d67482SBill Paul 	/* Load our MAC address. */
44003f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
440195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
440295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
440395d67482SBill Paul 
44043e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
44053e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
440695d67482SBill Paul 
440795d67482SBill Paul 	/* Program multicast filter. */
440895d67482SBill Paul 	bge_setmulti(sc);
440995d67482SBill Paul 
4410cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
4411cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
4412cb2eacc7SYaroslav Tykhiy 
441335f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
441435f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
441535f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
441635f945cdSPyun YongHyeon 	else
441735f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
441835f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
441935f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
442035f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
442135f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
442235f945cdSPyun YongHyeon 	}
442335f945cdSPyun YongHyeon 
442495d67482SBill Paul 	/* Init RX ring. */
44253ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
44263ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
44273ee5d7daSPyun YongHyeon 		bge_stop(sc);
44283ee5d7daSPyun YongHyeon 		return;
44293ee5d7daSPyun YongHyeon 	}
443095d67482SBill Paul 
44310434d1b8SBill Paul 	/*
44320434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
44330434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
44340434d1b8SBill Paul 	 * entry of the ring.
44350434d1b8SBill Paul 	 */
44360434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
44373f74909aSGleb Smirnoff 		uint32_t		v, i;
44380434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
44390434d1b8SBill Paul 			DELAY(20);
44400434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
44410434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
44420434d1b8SBill Paul 				break;
44430434d1b8SBill Paul 		}
44440434d1b8SBill Paul 		if (i == 10)
4445fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
4446fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
44470434d1b8SBill Paul 	}
44480434d1b8SBill Paul 
444995d67482SBill Paul 	/* Init jumbo RX ring. */
4450c215fd77SPyun YongHyeon 	if (ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4451c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
44523ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
4453333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
4454b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
44553ee5d7daSPyun YongHyeon 			bge_stop(sc);
44563ee5d7daSPyun YongHyeon 			return;
44573ee5d7daSPyun YongHyeon 		}
44583ee5d7daSPyun YongHyeon 	}
445995d67482SBill Paul 
44603f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
446195d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
446295d67482SBill Paul 
44637e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
44647e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
44657e6e2507SJung-uk Kim 
446695d67482SBill Paul 	/* Init TX ring. */
446795d67482SBill Paul 	bge_init_tx_ring(sc);
446895d67482SBill Paul 
44693f74909aSGleb Smirnoff 	/* Turn on transmitter. */
447095d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
447195d67482SBill Paul 
44723f74909aSGleb Smirnoff 	/* Turn on receiver. */
447395d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
447495d67482SBill Paul 
4475dedcdf57SPyun YongHyeon 	/*
4476dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
4477dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
4478dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
4479dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
4480dedcdf57SPyun YongHyeon 	 */
4481dedcdf57SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
4482dedcdf57SPyun YongHyeon 
44832280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
44842280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
44852280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
44862280c16bSPyun YongHyeon 
448795d67482SBill Paul 	/* Tell firmware we're alive. */
448895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
448995d67482SBill Paul 
449075719184SGleb Smirnoff #ifdef DEVICE_POLLING
449175719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
449275719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
449375719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
449475719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
449538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
449675719184SGleb Smirnoff 	} else
449775719184SGleb Smirnoff #endif
449875719184SGleb Smirnoff 
449995d67482SBill Paul 	/* Enable host interrupts. */
450075719184SGleb Smirnoff 	{
450195d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
450295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
450338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
450475719184SGleb Smirnoff 	}
450595d67482SBill Paul 
450667d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
450795d67482SBill Paul 
450813f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
450913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
451095d67482SBill Paul 
45110f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
45120f9bd73bSSam Leffler }
45130f9bd73bSSam Leffler 
45140f9bd73bSSam Leffler static void
45153f74909aSGleb Smirnoff bge_init(void *xsc)
45160f9bd73bSSam Leffler {
45170f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
45180f9bd73bSSam Leffler 
45190f9bd73bSSam Leffler 	BGE_LOCK(sc);
45200f9bd73bSSam Leffler 	bge_init_locked(sc);
45210f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
452295d67482SBill Paul }
452395d67482SBill Paul 
452495d67482SBill Paul /*
452595d67482SBill Paul  * Set media options.
452695d67482SBill Paul  */
452795d67482SBill Paul static int
45283f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
452995d67482SBill Paul {
453067d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
453167d5e043SOleg Bulyzhin 	int res;
453267d5e043SOleg Bulyzhin 
453367d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
453467d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
453567d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
453667d5e043SOleg Bulyzhin 
453767d5e043SOleg Bulyzhin 	return (res);
453867d5e043SOleg Bulyzhin }
453967d5e043SOleg Bulyzhin 
454067d5e043SOleg Bulyzhin static int
454167d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
454267d5e043SOleg Bulyzhin {
454367d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
454495d67482SBill Paul 	struct mii_data *mii;
45454f09c4c7SMarius Strobl 	struct mii_softc *miisc;
454695d67482SBill Paul 	struct ifmedia *ifm;
454795d67482SBill Paul 
454867d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
454967d5e043SOleg Bulyzhin 
455095d67482SBill Paul 	ifm = &sc->bge_ifmedia;
455195d67482SBill Paul 
455295d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
4553652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
455495d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
455595d67482SBill Paul 			return (EINVAL);
455695d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
455795d67482SBill Paul 		case IFM_AUTO:
4558ff50922bSDoug White 			/*
4559ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
4560ff50922bSDoug White 			 * mechanism for programming the autoneg
4561ff50922bSDoug White 			 * advertisement registers in TBI mode.
4562ff50922bSDoug White 			 */
45630f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4564ff50922bSDoug White 				uint32_t sgdig;
45650f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
45660f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
4567ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
4568ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
4569ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
4570ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
4571ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
4572ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
4573ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
4574ff50922bSDoug White 					DELAY(5);
4575ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
4576ff50922bSDoug White 				}
45770f89fde2SJung-uk Kim 			}
457895d67482SBill Paul 			break;
457995d67482SBill Paul 		case IFM_1000_SX:
458095d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
458195d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
458295d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
458395d67482SBill Paul 			} else {
458495d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
458595d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
458695d67482SBill Paul 			}
458795d67482SBill Paul 			break;
458895d67482SBill Paul 		default:
458995d67482SBill Paul 			return (EINVAL);
459095d67482SBill Paul 		}
459195d67482SBill Paul 		return (0);
459295d67482SBill Paul 	}
459395d67482SBill Paul 
45941493e883SOleg Bulyzhin 	sc->bge_link_evt++;
459595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
45964f09c4c7SMarius Strobl 	if (mii->mii_instance)
45974f09c4c7SMarius Strobl 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
459895d67482SBill Paul 			mii_phy_reset(miisc);
459995d67482SBill Paul 	mii_mediachg(mii);
460095d67482SBill Paul 
4601902827f6SBjoern A. Zeeb 	/*
4602902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
4603902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
4604902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
4605902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
4606902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
4607902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
4608902827f6SBjoern A. Zeeb 	 * get an RX intr.
4609902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
4610902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
4611902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
4612902827f6SBjoern A. Zeeb 	 */
46134f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
46144f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
4615902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
46164f0794ffSBjoern A. Zeeb 	else
461763ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
4618902827f6SBjoern A. Zeeb 
461995d67482SBill Paul 	return (0);
462095d67482SBill Paul }
462195d67482SBill Paul 
462295d67482SBill Paul /*
462395d67482SBill Paul  * Report current media status.
462495d67482SBill Paul  */
462595d67482SBill Paul static void
46263f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
462795d67482SBill Paul {
462867d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
462995d67482SBill Paul 	struct mii_data *mii;
463095d67482SBill Paul 
463167d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
463295d67482SBill Paul 
4633652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
463495d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
463595d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
463695d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
463795d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
463895d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
46394c0da0ffSGleb Smirnoff 		else {
46404c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
464167d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
46424c0da0ffSGleb Smirnoff 			return;
46434c0da0ffSGleb Smirnoff 		}
464495d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
464595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
464695d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
464795d67482SBill Paul 		else
464895d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
464967d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
465095d67482SBill Paul 		return;
465195d67482SBill Paul 	}
465295d67482SBill Paul 
465395d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
465495d67482SBill Paul 	mii_pollstat(mii);
465595d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
465695d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
465767d5e043SOleg Bulyzhin 
465867d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
465995d67482SBill Paul }
466095d67482SBill Paul 
466195d67482SBill Paul static int
46623f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
466395d67482SBill Paul {
466495d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
466595d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
466695d67482SBill Paul 	struct mii_data *mii;
4667f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
466895d67482SBill Paul 
466995d67482SBill Paul 	switch (command) {
467095d67482SBill Paul 	case SIOCSIFMTU:
46713a429c8fSPyun YongHyeon 		BGE_LOCK(sc);
46724c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
46734c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
46744c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
46754c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
46764c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
467795d67482SBill Paul 			error = EINVAL;
46784c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
467995d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
46803a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
468113f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
46823a429c8fSPyun YongHyeon 				bge_init_locked(sc);
468395d67482SBill Paul 			}
46843a429c8fSPyun YongHyeon 		}
46853a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
468695d67482SBill Paul 		break;
468795d67482SBill Paul 	case SIOCSIFFLAGS:
46880f9bd73bSSam Leffler 		BGE_LOCK(sc);
468995d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
469095d67482SBill Paul 			/*
469195d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
469295d67482SBill Paul 			 * then just use the 'set promisc mode' command
469395d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
469495d67482SBill Paul 			 * a full re-init means reloading the firmware and
469595d67482SBill Paul 			 * waiting for it to start up, which may take a
4696d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
469795d67482SBill Paul 			 */
4698f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4699f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
47003e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
47013e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
4702f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
4703d183af7fSRuslan Ermilov 					bge_setmulti(sc);
470495d67482SBill Paul 			} else
47050f9bd73bSSam Leffler 				bge_init_locked(sc);
470695d67482SBill Paul 		} else {
470713f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
470895d67482SBill Paul 				bge_stop(sc);
470995d67482SBill Paul 			}
471095d67482SBill Paul 		}
471195d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
47120f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
471395d67482SBill Paul 		error = 0;
471495d67482SBill Paul 		break;
471595d67482SBill Paul 	case SIOCADDMULTI:
471695d67482SBill Paul 	case SIOCDELMULTI:
471713f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
47180f9bd73bSSam Leffler 			BGE_LOCK(sc);
471995d67482SBill Paul 			bge_setmulti(sc);
47200f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
472195d67482SBill Paul 			error = 0;
472295d67482SBill Paul 		}
472395d67482SBill Paul 		break;
472495d67482SBill Paul 	case SIOCSIFMEDIA:
472595d67482SBill Paul 	case SIOCGIFMEDIA:
4726652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
472795d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
472895d67482SBill Paul 			    &sc->bge_ifmedia, command);
472995d67482SBill Paul 		} else {
473095d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
473195d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
473295d67482SBill Paul 			    &mii->mii_media, command);
473395d67482SBill Paul 		}
473495d67482SBill Paul 		break;
473595d67482SBill Paul 	case SIOCSIFCAP:
473695d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
473775719184SGleb Smirnoff #ifdef DEVICE_POLLING
473875719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
473975719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
474075719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
474175719184SGleb Smirnoff 				if (error)
474275719184SGleb Smirnoff 					return (error);
474375719184SGleb Smirnoff 				BGE_LOCK(sc);
474475719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
474575719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
474638cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
474775719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
474875719184SGleb Smirnoff 				BGE_UNLOCK(sc);
474975719184SGleb Smirnoff 			} else {
475075719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
475175719184SGleb Smirnoff 				/* Enable interrupt even in error case */
475275719184SGleb Smirnoff 				BGE_LOCK(sc);
475375719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
475475719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
475538cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
475675719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
475775719184SGleb Smirnoff 				BGE_UNLOCK(sc);
475875719184SGleb Smirnoff 			}
475975719184SGleb Smirnoff 		}
476075719184SGleb Smirnoff #endif
4761d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
4762d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
4763d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
4764d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
476535f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
476695d67482SBill Paul 			else
476735f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
476895d67482SBill Paul 		}
4769cb2eacc7SYaroslav Tykhiy 
4770d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
4771d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
4772d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
4773d8b57f98SPyun YongHyeon 
4774ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
4775ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
4776ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
4777ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
4778ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
4779ca3f1187SPyun YongHyeon 			else
4780ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
4781ca3f1187SPyun YongHyeon 		}
4782ca3f1187SPyun YongHyeon 
4783cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
4784cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
4785cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4786cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
4787cb2eacc7SYaroslav Tykhiy 		}
4788cb2eacc7SYaroslav Tykhiy 
478904bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
479004bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
479104bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
479204bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
479304bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
4794cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
479504bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
479604bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
4797cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
4798cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
4799cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
480004bde852SPyun YongHyeon 		}
4801cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
4802cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
4803cb2eacc7SYaroslav Tykhiy #endif
480495d67482SBill Paul 		break;
480595d67482SBill Paul 	default:
4806673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
480795d67482SBill Paul 		break;
480895d67482SBill Paul 	}
480995d67482SBill Paul 
481095d67482SBill Paul 	return (error);
481195d67482SBill Paul }
481295d67482SBill Paul 
481395d67482SBill Paul static void
4814b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
481595d67482SBill Paul {
4816b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
481795d67482SBill Paul 
4818b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
4819b74e67fbSGleb Smirnoff 
4820b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
4821b74e67fbSGleb Smirnoff 		return;
4822b74e67fbSGleb Smirnoff 
4823b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
482495d67482SBill Paul 
4825fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
482695d67482SBill Paul 
482713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4828426742bfSGleb Smirnoff 	bge_init_locked(sc);
482995d67482SBill Paul 
483095d67482SBill Paul 	ifp->if_oerrors++;
483195d67482SBill Paul }
483295d67482SBill Paul 
483395d67482SBill Paul /*
483495d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
483595d67482SBill Paul  * RX and TX lists.
483695d67482SBill Paul  */
483795d67482SBill Paul static void
48383f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
483995d67482SBill Paul {
484095d67482SBill Paul 	struct ifnet *ifp;
484195d67482SBill Paul 
48420f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
48430f9bd73bSSam Leffler 
4844fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
484595d67482SBill Paul 
48460f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
484795d67482SBill Paul 
484844b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
484944b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
485044b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
485144b63691SBjoern A. Zeeb 
485244b63691SBjoern A. Zeeb 	/*
485344b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
485444b63691SBjoern A. Zeeb 	 */
485544b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
485644b63691SBjoern A. Zeeb 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
485744b63691SBjoern A. Zeeb 
485895d67482SBill Paul 	/*
48593f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
486095d67482SBill Paul 	 */
486195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
486295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
486395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
48647ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
486595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
486695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
486795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
486895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
486995d67482SBill Paul 
487095d67482SBill Paul 	/*
48713f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
487295d67482SBill Paul 	 */
487395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
487495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
487595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
487695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
487795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
48787ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
487995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
488095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
488195d67482SBill Paul 
488295d67482SBill Paul 	/*
488395d67482SBill Paul 	 * Shut down all of the memory managers and related
488495d67482SBill Paul 	 * state machines.
488595d67482SBill Paul 	 */
488695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
488795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
48887ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
488995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
48900c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
489195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
48927ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
489395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
489495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
48950434d1b8SBill Paul 	}
48962280c16bSPyun YongHyeon 	/* Update MAC statistics. */
48972280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
48982280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
489995d67482SBill Paul 
49008cb1383cSDoug Ambrisko 	bge_reset(sc);
49018cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
49028cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
49038cb1383cSDoug Ambrisko 
49048cb1383cSDoug Ambrisko 	/*
49058cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
49068cb1383cSDoug Ambrisko 	 */
49078cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
49088cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
49098cb1383cSDoug Ambrisko 	else
491095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
491195d67482SBill Paul 
491295d67482SBill Paul 	/* Free the RX lists. */
491395d67482SBill Paul 	bge_free_rx_ring_std(sc);
491495d67482SBill Paul 
491595d67482SBill Paul 	/* Free jumbo RX list. */
49164c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
491795d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
491895d67482SBill Paul 
491995d67482SBill Paul 	/* Free TX buffers. */
492095d67482SBill Paul 	bge_free_tx_ring(sc);
492195d67482SBill Paul 
492295d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
492395d67482SBill Paul 
49245dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
49251493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
49261493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
49271493e883SOleg Bulyzhin 	sc->bge_link = 0;
492895d67482SBill Paul 
49291493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
493095d67482SBill Paul }
493195d67482SBill Paul 
493295d67482SBill Paul /*
493395d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
493495d67482SBill Paul  * get confused by errant DMAs when rebooting.
493595d67482SBill Paul  */
4936b6c974e8SWarner Losh static int
49373f74909aSGleb Smirnoff bge_shutdown(device_t dev)
493895d67482SBill Paul {
493995d67482SBill Paul 	struct bge_softc *sc;
494095d67482SBill Paul 
494195d67482SBill Paul 	sc = device_get_softc(dev);
49420f9bd73bSSam Leffler 	BGE_LOCK(sc);
494395d67482SBill Paul 	bge_stop(sc);
494495d67482SBill Paul 	bge_reset(sc);
49450f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
4946b6c974e8SWarner Losh 
4947b6c974e8SWarner Losh 	return (0);
494895d67482SBill Paul }
494914afefa3SPawel Jakub Dawidek 
495014afefa3SPawel Jakub Dawidek static int
495114afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
495214afefa3SPawel Jakub Dawidek {
495314afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
495414afefa3SPawel Jakub Dawidek 
495514afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
495614afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
495714afefa3SPawel Jakub Dawidek 	bge_stop(sc);
495814afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
495914afefa3SPawel Jakub Dawidek 
496014afefa3SPawel Jakub Dawidek 	return (0);
496114afefa3SPawel Jakub Dawidek }
496214afefa3SPawel Jakub Dawidek 
496314afefa3SPawel Jakub Dawidek static int
496414afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
496514afefa3SPawel Jakub Dawidek {
496614afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
496714afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
496814afefa3SPawel Jakub Dawidek 
496914afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
497014afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
497114afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
497214afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
497314afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
497414afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
497514afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
497614afefa3SPawel Jakub Dawidek 	}
497714afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
497814afefa3SPawel Jakub Dawidek 
497914afefa3SPawel Jakub Dawidek 	return (0);
498014afefa3SPawel Jakub Dawidek }
4981dab5cd05SOleg Bulyzhin 
4982dab5cd05SOleg Bulyzhin static void
49833f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
4984dab5cd05SOleg Bulyzhin {
49851f313773SOleg Bulyzhin 	struct mii_data *mii;
49861f313773SOleg Bulyzhin 	uint32_t link, status;
4987dab5cd05SOleg Bulyzhin 
4988dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
49891f313773SOleg Bulyzhin 
49903f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
49917b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
49927b97099dSOleg Bulyzhin 
4993dab5cd05SOleg Bulyzhin 	/*
4994dab5cd05SOleg Bulyzhin 	 * Process link state changes.
4995dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
4996dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
4997dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
4998dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
4999dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
5000dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
5001dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
5002dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
50031f313773SOleg Bulyzhin 	 *
50041f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
50054c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
5006dab5cd05SOleg Bulyzhin 	 */
5007dab5cd05SOleg Bulyzhin 
50081f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
50094c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
5010dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
5011dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
50121f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
50135dda8085SOleg Bulyzhin 			mii_pollstat(mii);
50141f313773SOleg Bulyzhin 			if (!sc->bge_link &&
50151f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
50161f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
50171f313773SOleg Bulyzhin 				sc->bge_link++;
50181f313773SOleg Bulyzhin 				if (bootverbose)
50191f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
50201f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
50211f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
50221f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
50231f313773SOleg Bulyzhin 				sc->bge_link = 0;
50241f313773SOleg Bulyzhin 				if (bootverbose)
50251f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
50261f313773SOleg Bulyzhin 			}
50271f313773SOleg Bulyzhin 
50283f74909aSGleb Smirnoff 			/* Clear the interrupt. */
5029dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
5030dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
5031dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
5032dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
5033dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
5034dab5cd05SOleg Bulyzhin 		}
5035dab5cd05SOleg Bulyzhin 		return;
5036dab5cd05SOleg Bulyzhin 	}
5037dab5cd05SOleg Bulyzhin 
5038652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
50391f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
50407b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
50417b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
50421f313773SOleg Bulyzhin 				sc->bge_link++;
50431f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
50441f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
50451f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
50460c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
50471f313773SOleg Bulyzhin 				if (bootverbose)
50481f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
50493f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
50503f74909aSGleb Smirnoff 				    LINK_STATE_UP);
50517b97099dSOleg Bulyzhin 			}
50521f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
5053dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
50541f313773SOleg Bulyzhin 			if (bootverbose)
50551f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
50567b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
50571f313773SOleg Bulyzhin 		}
50586ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
50591f313773SOleg Bulyzhin 		/*
50600c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
50610c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
50620c8aa4eaSJung-uk Kim 		 * PHY link status directly.
50631f313773SOleg Bulyzhin 		 */
50641f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
50651f313773SOleg Bulyzhin 
50661f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
50671f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
50681f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
50695dda8085SOleg Bulyzhin 			mii_pollstat(mii);
50701f313773SOleg Bulyzhin 			if (!sc->bge_link &&
50711f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
50721f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
50731f313773SOleg Bulyzhin 				sc->bge_link++;
50741f313773SOleg Bulyzhin 				if (bootverbose)
50751f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
50761f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
50771f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
50781f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
50791f313773SOleg Bulyzhin 				sc->bge_link = 0;
50801f313773SOleg Bulyzhin 				if (bootverbose)
50811f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
50821f313773SOleg Bulyzhin 			}
50831f313773SOleg Bulyzhin 		}
50840c8aa4eaSJung-uk Kim 	} else {
50850c8aa4eaSJung-uk Kim 		/*
50866ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
50876ede2cfaSPyun YongHyeon 		 * link status.
50880c8aa4eaSJung-uk Kim 		 */
50896ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
50906ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
50916ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
5092dab5cd05SOleg Bulyzhin 	}
5093dab5cd05SOleg Bulyzhin 
50943f74909aSGleb Smirnoff 	/* Clear the attention. */
5095dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
5096dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
5097dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
5098dab5cd05SOleg Bulyzhin }
50996f8718a3SScott Long 
51006f8718a3SScott Long static void
51016f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
51026f8718a3SScott Long {
51036f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
51042280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
51057e32f79aSPyun YongHyeon 	char tn[32];
51067e32f79aSPyun YongHyeon 	int unit;
51076f8718a3SScott Long 
51086f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
51096f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
51106f8718a3SScott Long 
51116f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
51126f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
51136f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
51146f8718a3SScott Long 	    "Debug Information");
51156f8718a3SScott Long 
51166f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
51176f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
51186f8718a3SScott Long 	    "Register Read");
51196f8718a3SScott Long 
51206f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
51216f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
51226f8718a3SScott Long 	    "Memory Read");
51236f8718a3SScott Long 
51246f8718a3SScott Long #endif
5125763757b2SScott Long 
51267e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
5127beaa2ae1SPyun YongHyeon 	/*
5128beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
5129beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
5130beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
5131beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
5132beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
5133beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
5134beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
5135beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
5136beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
5137beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
5138beaa2ae1SPyun YongHyeon 	 */
51397e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
51407e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
51417e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
5142beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
5143beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
5144beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
5145beaa2ae1SPyun YongHyeon 	    "forced collapsing");
5146beaa2ae1SPyun YongHyeon 
514735f945cdSPyun YongHyeon 	/*
514835f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
514935f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
515035f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
515135f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
515235f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
515335f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
515435f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
515535f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
515635f945cdSPyun YongHyeon 	 */
515735f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
515835f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
515935f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
516035f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
516135f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
516235f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
516335f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
516435f945cdSPyun YongHyeon 
5165d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
51662280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
51672280c16bSPyun YongHyeon 	else
51682280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
51692280c16bSPyun YongHyeon }
5170d949071dSJung-uk Kim 
51712280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
51722280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
51732280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
51742280c16bSPyun YongHyeon 	    desc)
51752280c16bSPyun YongHyeon 
51762280c16bSPyun YongHyeon static void
51772280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
51782280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
51792280c16bSPyun YongHyeon {
51802280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
51812280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
51822280c16bSPyun YongHyeon 
51832280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
5184763757b2SScott Long 	    NULL, "BGE Statistics");
5185763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
5186763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
5187763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
5188763757b2SScott Long 	    "FramesDroppedDueToFilters");
5189763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
5190763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
5191763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
5192763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
5193763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
5194763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
519506e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
519606e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
519706e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
519806e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
5199763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
5200763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
5201763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
5202763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
5203763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
5204763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
5205763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
5206763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
5207763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
5208763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
5209763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
5210763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
5211763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
5212763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
5213763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
5214763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
5215763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
5216763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
5217763757b2SScott Long 
5218763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
5219763757b2SScott Long 	    NULL, "BGE RX Statistics");
5220763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5221763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
52221cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
5223763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
5224763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
5225763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
52261cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
5227763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
5228763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
5229763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
5230763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
5231763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
5232763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
5233763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
5234763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
5235763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
5236763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
5237763757b2SScott Long 	    "xoffPauseFramesReceived");
5238763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
5239763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
5240763757b2SScott Long 	    "ControlFramesReceived");
5241763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
5242763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
5243763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
5244763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
5245763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
5246763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
5247763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
5248763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
5249763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
525006e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
5251763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
525206e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
5253763757b2SScott Long 
5254763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
5255763757b2SScott Long 	    NULL, "BGE TX Statistics");
5256763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5257763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
52581cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
5259763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
5260763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
5261763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
5262763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
5263763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
5264763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
5265763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
5266763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
5267763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
5268763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
5269763757b2SScott Long 	    "InternalMacTransmitErrors");
5270763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
5271763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
5272763757b2SScott Long 	    "SingleCollisionFrames");
5273763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
5274763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
5275763757b2SScott Long 	    "MultipleCollisionFrames");
5276763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
5277763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
5278763757b2SScott Long 	    "DeferredTransmissions");
5279763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
5280763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
5281763757b2SScott Long 	    "ExcessiveCollisions");
5282763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
528306e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
528406e83c7eSScott Long 	    "LateCollisions");
5285763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
52861cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
5287763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
5288763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
5289763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
5290763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
5291763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
5292763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
5293763757b2SScott Long 	    "CarrierSenseErrors");
5294763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
5295763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
5296763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
5297763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
5298763757b2SScott Long }
5299763757b2SScott Long 
53002280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
53012280c16bSPyun YongHyeon 
53022280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
53032280c16bSPyun YongHyeon 	    SYSCTL_ADD_QUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
53042280c16bSPyun YongHyeon 
53052280c16bSPyun YongHyeon static void
53062280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
53072280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
53082280c16bSPyun YongHyeon {
53092280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
53102280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
53112280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
53122280c16bSPyun YongHyeon 
53132280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
53142280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
53152280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
53162280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
53172280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
53182280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
53192280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
53202280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
53212280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
53222280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
53232280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
53242280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
53252280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
53262280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
53272280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
53282280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
53292280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
53302280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
53312280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
53322280c16bSPyun YongHyeon 
53332280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
53342280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
53352280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
53362280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
53372280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
53382280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
53392280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
53401cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
53412280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
53422280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
53432280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
53442280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
53452280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
53462280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
53472280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
53482280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
53492280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
53502280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
53512280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
53522280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
53532280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
53542280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
53552280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
53562280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
53572280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
53582280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
53592280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
53602280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
53612280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
53622280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
53632280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
53642280c16bSPyun YongHyeon 
53652280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
53662280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
53672280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
53681cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
53692280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
53702280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
53712280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
53722280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
53732280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
53742280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
53752280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
53762280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
53772280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
53782280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
53792280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
53802280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
53812280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
53822280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
53832280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
53842280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
53852280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
53862280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
53872280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
53882280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
53892280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
53901cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
53912280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
53921cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
53932280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
53941cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
53952280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
53962280c16bSPyun YongHyeon }
53972280c16bSPyun YongHyeon 
53982280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
53992280c16bSPyun YongHyeon 
5400763757b2SScott Long static int
5401763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
5402763757b2SScott Long {
5403763757b2SScott Long 	struct bge_softc *sc;
540406e83c7eSScott Long 	uint32_t result;
5405d949071dSJung-uk Kim 	int offset;
5406763757b2SScott Long 
5407763757b2SScott Long 	sc = (struct bge_softc *)arg1;
5408763757b2SScott Long 	offset = arg2;
5409d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
5410d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
5411041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
54126f8718a3SScott Long }
54136f8718a3SScott Long 
54146f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
54156f8718a3SScott Long static int
54166f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
54176f8718a3SScott Long {
54186f8718a3SScott Long 	struct bge_softc *sc;
54196f8718a3SScott Long 	uint16_t *sbdata;
54206f8718a3SScott Long 	int error;
54216f8718a3SScott Long 	int result;
54226f8718a3SScott Long 	int i, j;
54236f8718a3SScott Long 
54246f8718a3SScott Long 	result = -1;
54256f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
54266f8718a3SScott Long 	if (error || (req->newptr == NULL))
54276f8718a3SScott Long 		return (error);
54286f8718a3SScott Long 
54296f8718a3SScott Long 	if (result == 1) {
54306f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
54316f8718a3SScott Long 
54326f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
54336f8718a3SScott Long 		printf("Status Block:\n");
54346f8718a3SScott Long 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
54356f8718a3SScott Long 			printf("%06x:", i);
54366f8718a3SScott Long 			for (j = 0; j < 8; j++) {
54376f8718a3SScott Long 				printf(" %04x", sbdata[i]);
54386f8718a3SScott Long 				i += 4;
54396f8718a3SScott Long 			}
54406f8718a3SScott Long 			printf("\n");
54416f8718a3SScott Long 		}
54426f8718a3SScott Long 
54436f8718a3SScott Long 		printf("Registers:\n");
54440c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
54456f8718a3SScott Long 			printf("%06x:", i);
54466f8718a3SScott Long 			for (j = 0; j < 8; j++) {
54476f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
54486f8718a3SScott Long 				i += 4;
54496f8718a3SScott Long 			}
54506f8718a3SScott Long 			printf("\n");
54516f8718a3SScott Long 		}
54526f8718a3SScott Long 
54536f8718a3SScott Long 		printf("Hardware Flags:\n");
5454a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
5455a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
54565345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
54576f8718a3SScott Long 			printf(" - 575X Plus\n");
54585345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
54596f8718a3SScott Long 			printf(" - 5705 Plus\n");
54605345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
54615345bad0SScott Long 			printf(" - 5714 Family\n");
54625345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
54635345bad0SScott Long 			printf(" - 5700 Family\n");
54646f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
54656f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
54666f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
54676f8718a3SScott Long 			printf(" - PCI-X Bus\n");
54686f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
54696f8718a3SScott Long 			printf(" - PCI Express Bus\n");
54707d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
54716f8718a3SScott Long 			printf(" - No 3 LEDs\n");
54726f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
54736f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
54746f8718a3SScott Long 	}
54756f8718a3SScott Long 
54766f8718a3SScott Long 	return (error);
54776f8718a3SScott Long }
54786f8718a3SScott Long 
54796f8718a3SScott Long static int
54806f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
54816f8718a3SScott Long {
54826f8718a3SScott Long 	struct bge_softc *sc;
54836f8718a3SScott Long 	int error;
54846f8718a3SScott Long 	uint16_t result;
54856f8718a3SScott Long 	uint32_t val;
54866f8718a3SScott Long 
54876f8718a3SScott Long 	result = -1;
54886f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
54896f8718a3SScott Long 	if (error || (req->newptr == NULL))
54906f8718a3SScott Long 		return (error);
54916f8718a3SScott Long 
54926f8718a3SScott Long 	if (result < 0x8000) {
54936f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
54946f8718a3SScott Long 		val = CSR_READ_4(sc, result);
54956f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
54966f8718a3SScott Long 	}
54976f8718a3SScott Long 
54986f8718a3SScott Long 	return (error);
54996f8718a3SScott Long }
55006f8718a3SScott Long 
55016f8718a3SScott Long static int
55026f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
55036f8718a3SScott Long {
55046f8718a3SScott Long 	struct bge_softc *sc;
55056f8718a3SScott Long 	int error;
55066f8718a3SScott Long 	uint16_t result;
55076f8718a3SScott Long 	uint32_t val;
55086f8718a3SScott Long 
55096f8718a3SScott Long 	result = -1;
55106f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
55116f8718a3SScott Long 	if (error || (req->newptr == NULL))
55126f8718a3SScott Long 		return (error);
55136f8718a3SScott Long 
55146f8718a3SScott Long 	if (result < 0x8000) {
55156f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
55166f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
55176f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
55186f8718a3SScott Long 	}
55196f8718a3SScott Long 
55206f8718a3SScott Long 	return (error);
55216f8718a3SScott Long }
55226f8718a3SScott Long #endif
552338cc658fSJohn Baldwin 
552438cc658fSJohn Baldwin static int
55255fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
55265fea260fSMarius Strobl {
55275fea260fSMarius Strobl 
55285fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
55295fea260fSMarius Strobl 		return (1);
55305fea260fSMarius Strobl 
55315fea260fSMarius Strobl #ifdef __sparc64__
55325fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
55335fea260fSMarius Strobl 	return (0);
55345fea260fSMarius Strobl #endif
55355fea260fSMarius Strobl 	return (1);
55365fea260fSMarius Strobl }
55375fea260fSMarius Strobl 
55385fea260fSMarius Strobl static int
553938cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
554038cc658fSJohn Baldwin {
554138cc658fSJohn Baldwin 	uint32_t mac_addr;
554238cc658fSJohn Baldwin 
554338cc658fSJohn Baldwin 	mac_addr = bge_readmem_ind(sc, 0x0c14);
554438cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
554538cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
554638cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
554738cc658fSJohn Baldwin 		mac_addr = bge_readmem_ind(sc, 0x0c18);
554838cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
554938cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
555038cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
555138cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
55525fea260fSMarius Strobl 		return (0);
555338cc658fSJohn Baldwin 	}
55545fea260fSMarius Strobl 	return (1);
555538cc658fSJohn Baldwin }
555638cc658fSJohn Baldwin 
555738cc658fSJohn Baldwin static int
555838cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
555938cc658fSJohn Baldwin {
556038cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
556138cc658fSJohn Baldwin 
556238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
556338cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
556438cc658fSJohn Baldwin 
55655fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
55665fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
556738cc658fSJohn Baldwin }
556838cc658fSJohn Baldwin 
556938cc658fSJohn Baldwin static int
557038cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
557138cc658fSJohn Baldwin {
557238cc658fSJohn Baldwin 
55735fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
55745fea260fSMarius Strobl 		return (1);
55755fea260fSMarius Strobl 
55765fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
55775fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
557838cc658fSJohn Baldwin }
557938cc658fSJohn Baldwin 
558038cc658fSJohn Baldwin static int
558138cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
558238cc658fSJohn Baldwin {
558338cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
558438cc658fSJohn Baldwin 		/* NOTE: Order is critical */
55855fea260fSMarius Strobl 		bge_get_eaddr_fw,
558638cc658fSJohn Baldwin 		bge_get_eaddr_mem,
558738cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
558838cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
558938cc658fSJohn Baldwin 		NULL
559038cc658fSJohn Baldwin 	};
559138cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
559238cc658fSJohn Baldwin 
559338cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
559438cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
559538cc658fSJohn Baldwin 			break;
559638cc658fSJohn Baldwin 	}
559738cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
559838cc658fSJohn Baldwin }
5599