xref: /freebsd/sys/dev/bge/if_bge.c (revision d255f2a9dfe233de5b1e166a1b014250568c0821)
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 
55395d67482SBill Paul 	dev = sc->bge_dev;
55495d67482SBill Paul 
55595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5566f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5576f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5586f8718a3SScott Long 	return (val);
55995d67482SBill Paul }
56095d67482SBill Paul 
56195d67482SBill Paul static void
5623f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
56395d67482SBill Paul {
56495d67482SBill Paul 	device_t dev;
56595d67482SBill Paul 
56695d67482SBill Paul 	dev = sc->bge_dev;
56795d67482SBill Paul 
56895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
56995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
5706f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
57195d67482SBill Paul }
57295d67482SBill Paul 
57395d67482SBill Paul #ifdef notdef
5743f74909aSGleb Smirnoff static uint32_t
5753f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
57695d67482SBill Paul {
57795d67482SBill Paul 	device_t dev;
57895d67482SBill Paul 
57995d67482SBill Paul 	dev = sc->bge_dev;
58095d67482SBill Paul 
58195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
58295d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
58395d67482SBill Paul }
58495d67482SBill Paul #endif
58595d67482SBill Paul 
58695d67482SBill Paul static void
5873f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
58895d67482SBill Paul {
58995d67482SBill Paul 	device_t dev;
59095d67482SBill Paul 
59195d67482SBill Paul 	dev = sc->bge_dev;
59295d67482SBill Paul 
59395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
59495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
59595d67482SBill Paul }
59695d67482SBill Paul 
5976f8718a3SScott Long static void
5986f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
5996f8718a3SScott Long {
6006f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6016f8718a3SScott Long }
6026f8718a3SScott Long 
60338cc658fSJohn Baldwin static void
60438cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
60538cc658fSJohn Baldwin {
60638cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
60738cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
60838cc658fSJohn Baldwin 
60938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
61038cc658fSJohn Baldwin }
61138cc658fSJohn Baldwin 
612f41ac2beSBill Paul /*
613f41ac2beSBill Paul  * Map a single buffer address.
614f41ac2beSBill Paul  */
615f41ac2beSBill Paul 
616f41ac2beSBill Paul static void
6173f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
618f41ac2beSBill Paul {
619f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
620f41ac2beSBill Paul 
621f41ac2beSBill Paul 	if (error)
622f41ac2beSBill Paul 		return;
623f41ac2beSBill Paul 
6245b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
6255b610048SPyun YongHyeon 
626f41ac2beSBill Paul 	ctx = arg;
627f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
628f41ac2beSBill Paul }
629f41ac2beSBill Paul 
63038cc658fSJohn Baldwin static uint8_t
63138cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
63238cc658fSJohn Baldwin {
63338cc658fSJohn Baldwin 	uint32_t access, byte = 0;
63438cc658fSJohn Baldwin 	int i;
63538cc658fSJohn Baldwin 
63638cc658fSJohn Baldwin 	/* Lock. */
63738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
63838cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
63938cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
64038cc658fSJohn Baldwin 			break;
64138cc658fSJohn Baldwin 		DELAY(20);
64238cc658fSJohn Baldwin 	}
64338cc658fSJohn Baldwin 	if (i == 8000)
64438cc658fSJohn Baldwin 		return (1);
64538cc658fSJohn Baldwin 
64638cc658fSJohn Baldwin 	/* Enable access. */
64738cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
64838cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
64938cc658fSJohn Baldwin 
65038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
65138cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
65238cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
65338cc658fSJohn Baldwin 		DELAY(10);
65438cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
65538cc658fSJohn Baldwin 			DELAY(10);
65638cc658fSJohn Baldwin 			break;
65738cc658fSJohn Baldwin 		}
65838cc658fSJohn Baldwin 	}
65938cc658fSJohn Baldwin 
66038cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
66138cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
66238cc658fSJohn Baldwin 		return (1);
66338cc658fSJohn Baldwin 	}
66438cc658fSJohn Baldwin 
66538cc658fSJohn Baldwin 	/* Get result. */
66638cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
66738cc658fSJohn Baldwin 
66838cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
66938cc658fSJohn Baldwin 
67038cc658fSJohn Baldwin 	/* Disable access. */
67138cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
67238cc658fSJohn Baldwin 
67338cc658fSJohn Baldwin 	/* Unlock. */
67438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
67538cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
67638cc658fSJohn Baldwin 
67738cc658fSJohn Baldwin 	return (0);
67838cc658fSJohn Baldwin }
67938cc658fSJohn Baldwin 
68038cc658fSJohn Baldwin /*
68138cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
68238cc658fSJohn Baldwin  */
68338cc658fSJohn Baldwin static int
68438cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
68538cc658fSJohn Baldwin {
68638cc658fSJohn Baldwin 	int err = 0, i;
68738cc658fSJohn Baldwin 	uint8_t byte = 0;
68838cc658fSJohn Baldwin 
68938cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
69038cc658fSJohn Baldwin 		return (1);
69138cc658fSJohn Baldwin 
69238cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
69338cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
69438cc658fSJohn Baldwin 		if (err)
69538cc658fSJohn Baldwin 			break;
69638cc658fSJohn Baldwin 		*(dest + i) = byte;
69738cc658fSJohn Baldwin 	}
69838cc658fSJohn Baldwin 
69938cc658fSJohn Baldwin 	return (err ? 1 : 0);
70038cc658fSJohn Baldwin }
70138cc658fSJohn Baldwin 
70295d67482SBill Paul /*
70395d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
70495d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
70595d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
70695d67482SBill Paul  * access method.
70795d67482SBill Paul  */
7083f74909aSGleb Smirnoff static uint8_t
7093f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
71095d67482SBill Paul {
71195d67482SBill Paul 	int i;
7123f74909aSGleb Smirnoff 	uint32_t byte = 0;
71395d67482SBill Paul 
71495d67482SBill Paul 	/*
71595d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
71695d67482SBill Paul 	 * having to use the bitbang method.
71795d67482SBill Paul 	 */
71895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
71995d67482SBill Paul 
72095d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
72195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
72295d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
72395d67482SBill Paul 	DELAY(20);
72495d67482SBill Paul 
72595d67482SBill Paul 	/* Issue the read EEPROM command. */
72695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
72795d67482SBill Paul 
72895d67482SBill Paul 	/* Wait for completion */
72995d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
73095d67482SBill Paul 		DELAY(10);
73195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
73295d67482SBill Paul 			break;
73395d67482SBill Paul 	}
73495d67482SBill Paul 
735d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
736fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
737f6789fbaSPyun YongHyeon 		return (1);
73895d67482SBill Paul 	}
73995d67482SBill Paul 
74095d67482SBill Paul 	/* Get result. */
74195d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
74295d67482SBill Paul 
7430c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
74495d67482SBill Paul 
74595d67482SBill Paul 	return (0);
74695d67482SBill Paul }
74795d67482SBill Paul 
74895d67482SBill Paul /*
74995d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
75095d67482SBill Paul  */
75195d67482SBill Paul static int
7523f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
75395d67482SBill Paul {
7543f74909aSGleb Smirnoff 	int i, error = 0;
7553f74909aSGleb Smirnoff 	uint8_t byte = 0;
75695d67482SBill Paul 
75795d67482SBill Paul 	for (i = 0; i < cnt; i++) {
7583f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
7593f74909aSGleb Smirnoff 		if (error)
76095d67482SBill Paul 			break;
76195d67482SBill Paul 		*(dest + i) = byte;
76295d67482SBill Paul 	}
76395d67482SBill Paul 
7643f74909aSGleb Smirnoff 	return (error ? 1 : 0);
76595d67482SBill Paul }
76695d67482SBill Paul 
76795d67482SBill Paul static int
7683f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
76995d67482SBill Paul {
77095d67482SBill Paul 	struct bge_softc *sc;
7713f74909aSGleb Smirnoff 	uint32_t val, autopoll;
77295d67482SBill Paul 	int i;
77395d67482SBill Paul 
77495d67482SBill Paul 	sc = device_get_softc(dev);
77595d67482SBill Paul 
7760434d1b8SBill Paul 	/*
7770434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
7780434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
7790434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
7800434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
7810434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
7820434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
7830434d1b8SBill Paul 	 * special-cased.
7840434d1b8SBill Paul 	 */
785b1265c1aSJohn Polstra 	if (phy != 1)
78698b28ee5SBill Paul 		return (0);
78798b28ee5SBill Paul 
78837ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
78937ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
79037ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
79137ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
79237ceeb4dSPaul Saab 		DELAY(40);
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 
79895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
799d5d23857SJung-uk Kim 		DELAY(10);
80095d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
80195d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
80295d67482SBill Paul 			break;
80395d67482SBill Paul 	}
80495d67482SBill Paul 
80595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
8065fea260fSMarius Strobl 		device_printf(sc->bge_dev,
8075fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
8085fea260fSMarius Strobl 		    phy, reg, val);
80937ceeb4dSPaul Saab 		val = 0;
81037ceeb4dSPaul Saab 		goto done;
81195d67482SBill Paul 	}
81295d67482SBill Paul 
81338cc658fSJohn Baldwin 	DELAY(5);
81495d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
81595d67482SBill Paul 
81637ceeb4dSPaul Saab done:
81737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
81837ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
81937ceeb4dSPaul Saab 		DELAY(40);
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;
8323f74909aSGleb Smirnoff 	uint32_t autopoll;
83395d67482SBill Paul 	int i;
83495d67482SBill Paul 
83595d67482SBill Paul 	sc = device_get_softc(dev);
83695d67482SBill Paul 
83738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
83838cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
83938cc658fSJohn Baldwin 		return (0);
84038cc658fSJohn Baldwin 
84137ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
84237ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
84337ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
84437ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
84537ceeb4dSPaul Saab 		DELAY(40);
84637ceeb4dSPaul Saab 	}
84737ceeb4dSPaul Saab 
84895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
84995d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
85095d67482SBill Paul 
85195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
852d5d23857SJung-uk Kim 		DELAY(10);
85338cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
85438cc658fSJohn Baldwin 			DELAY(5);
85538cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
85695d67482SBill Paul 			break;
857d5d23857SJung-uk Kim 		}
85838cc658fSJohn Baldwin 	}
859d5d23857SJung-uk Kim 
860d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT) {
86138cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
86238cc658fSJohn Baldwin 		    "PHY write timed out (phy %d, reg %d, val %d)\n",
86338cc658fSJohn Baldwin 		    phy, reg, val);
864d5d23857SJung-uk Kim 		return (0);
86595d67482SBill Paul 	}
86695d67482SBill Paul 
86737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
86837ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
86937ceeb4dSPaul Saab 		DELAY(40);
87037ceeb4dSPaul Saab 	}
87137ceeb4dSPaul Saab 
87295d67482SBill Paul 	return (0);
87395d67482SBill Paul }
87495d67482SBill Paul 
87595d67482SBill Paul static void
8763f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
87795d67482SBill Paul {
87895d67482SBill Paul 	struct bge_softc *sc;
87995d67482SBill Paul 	struct mii_data *mii;
88095d67482SBill Paul 	sc = device_get_softc(dev);
88195d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
88295d67482SBill Paul 
88395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
884ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
885ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
88695d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
8873f74909aSGleb Smirnoff 	else
88895d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
88995d67482SBill Paul 
8906854be25SPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0) {
89195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
8926854be25SPyun YongHyeon 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_FLAG1)
8936854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
8943f74909aSGleb Smirnoff 		else
8956854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
8966854be25SPyun YongHyeon 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_FLAG0)
8976854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
8986854be25SPyun YongHyeon 		else
8996854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9006854be25SPyun YongHyeon 	} else {
90195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
9026854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
9036854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9046854be25SPyun YongHyeon 	}
90595d67482SBill Paul }
90695d67482SBill Paul 
90795d67482SBill Paul /*
90895d67482SBill Paul  * Intialize a standard receive ring descriptor.
90995d67482SBill Paul  */
91095d67482SBill Paul static int
911943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
91295d67482SBill Paul {
913943787f3SPyun YongHyeon 	struct mbuf *m;
91495d67482SBill Paul 	struct bge_rx_bd *r;
915a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
916943787f3SPyun YongHyeon 	bus_dmamap_t map;
917a23634a1SPyun YongHyeon 	int error, nsegs;
91895d67482SBill Paul 
919943787f3SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
920943787f3SPyun YongHyeon 	if (m == NULL)
92195d67482SBill Paul 		return (ENOBUFS);
922943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
923652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
924943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
925943787f3SPyun YongHyeon 
9260ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
927943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
928a23634a1SPyun YongHyeon 	if (error != 0) {
929943787f3SPyun YongHyeon 		m_freem(m);
930a23634a1SPyun YongHyeon 		return (error);
931f41ac2beSBill Paul 	}
932943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
933943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
934943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
935943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
936943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
937943787f3SPyun YongHyeon 	}
938943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
939943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
940943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
941943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
942e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
943943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
944a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
945a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
946e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
947a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
948e907febfSPyun YongHyeon 	r->bge_idx = i;
949f41ac2beSBill Paul 
9500ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
951943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
95295d67482SBill Paul 
95395d67482SBill Paul 	return (0);
95495d67482SBill Paul }
95595d67482SBill Paul 
95695d67482SBill Paul /*
95795d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
95895d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
95995d67482SBill Paul  */
96095d67482SBill Paul static int
961943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
96295d67482SBill Paul {
9631be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
964943787f3SPyun YongHyeon 	bus_dmamap_t map;
9651be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
966943787f3SPyun YongHyeon 	struct mbuf *m;
967943787f3SPyun YongHyeon 	int error, nsegs;
96895d67482SBill Paul 
969943787f3SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
970943787f3SPyun YongHyeon 	if (m == NULL)
97195d67482SBill Paul 		return (ENOBUFS);
97295d67482SBill Paul 
973943787f3SPyun YongHyeon 	m_cljget(m, M_DONTWAIT, MJUM9BYTES);
974943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
975943787f3SPyun YongHyeon 		m_freem(m);
97695d67482SBill Paul 		return (ENOBUFS);
97795d67482SBill Paul 	}
978943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
979652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
980943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
9811be6acb7SGleb Smirnoff 
9821be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
983943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
984943787f3SPyun YongHyeon 	if (error != 0) {
985943787f3SPyun YongHyeon 		m_freem(m);
9861be6acb7SGleb Smirnoff 		return (error);
987f7cea149SGleb Smirnoff 	}
9881be6acb7SGleb Smirnoff 
989943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] == NULL) {
990943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
991943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
992943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
993943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
994943787f3SPyun YongHyeon 	}
995943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
996943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
997943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
998943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
999943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1000e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1001e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1002e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1003e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1004e0b7b101SPyun YongHyeon 
10051be6acb7SGleb Smirnoff 	/*
10061be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
10071be6acb7SGleb Smirnoff 	 */
1008943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
10094e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
10104e7ba1abSGleb Smirnoff 	r->bge_idx = i;
10114e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
10124e7ba1abSGleb Smirnoff 	switch (nsegs) {
10134e7ba1abSGleb Smirnoff 	case 4:
10144e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
10154e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
10164e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1017e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
10184e7ba1abSGleb Smirnoff 	case 3:
1019e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1020e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1021e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1022e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
10234e7ba1abSGleb Smirnoff 	case 2:
10244e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
10254e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
10264e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1027e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
10284e7ba1abSGleb Smirnoff 	case 1:
10294e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
10304e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
10314e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1032e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
10334e7ba1abSGleb Smirnoff 		break;
10344e7ba1abSGleb Smirnoff 	default:
10354e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
10364e7ba1abSGleb Smirnoff 	}
1037f41ac2beSBill Paul 
1038a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1039943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
104095d67482SBill Paul 
104195d67482SBill Paul 	return (0);
104295d67482SBill Paul }
104395d67482SBill Paul 
104495d67482SBill Paul static int
10453f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
104695d67482SBill Paul {
10473ee5d7daSPyun YongHyeon 	int error, i;
104895d67482SBill Paul 
1049e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
105003e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1051e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1052943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
10533ee5d7daSPyun YongHyeon 			return (error);
105403e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
10551888f324SPyun YongHyeon 	}
105695d67482SBill Paul 
1057f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1058d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1059f41ac2beSBill Paul 
1060e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1061e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
106295d67482SBill Paul 
106395d67482SBill Paul 	return (0);
106495d67482SBill Paul }
106595d67482SBill Paul 
106695d67482SBill Paul static void
10673f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
106895d67482SBill Paul {
106995d67482SBill Paul 	int i;
107095d67482SBill Paul 
107195d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
107295d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
10730ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1074e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1075e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
10760ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1077f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1078e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1079e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
108095d67482SBill Paul 		}
1081f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
108295d67482SBill Paul 		    sizeof(struct bge_rx_bd));
108395d67482SBill Paul 	}
108495d67482SBill Paul }
108595d67482SBill Paul 
108695d67482SBill Paul static int
10873f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
108895d67482SBill Paul {
108995d67482SBill Paul 	struct bge_rcb *rcb;
10903ee5d7daSPyun YongHyeon 	int error, i;
109195d67482SBill Paul 
1092e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
109303e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
109495d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1095943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
10963ee5d7daSPyun YongHyeon 			return (error);
109703e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
10981888f324SPyun YongHyeon 	}
109995d67482SBill Paul 
1100f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1101d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1102f41ac2beSBill Paul 
1103e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
110495d67482SBill Paul 
1105f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
11061be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
11071be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
110867111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
110995d67482SBill Paul 
1110e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
111195d67482SBill Paul 
111295d67482SBill Paul 	return (0);
111395d67482SBill Paul }
111495d67482SBill Paul 
111595d67482SBill Paul static void
11163f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
111795d67482SBill Paul {
111895d67482SBill Paul 	int i;
111995d67482SBill Paul 
112095d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
112195d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1122e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1123e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1124e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1125f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1126f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1127e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1128e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
112995d67482SBill Paul 		}
1130f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
11311be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
113295d67482SBill Paul 	}
113395d67482SBill Paul }
113495d67482SBill Paul 
113595d67482SBill Paul static void
11363f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
113795d67482SBill Paul {
113895d67482SBill Paul 	int i;
113995d67482SBill Paul 
1140f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
114195d67482SBill Paul 		return;
114295d67482SBill Paul 
114395d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
114495d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
11450ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1146e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1147e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
11480ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1149f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1150e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1151e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
115295d67482SBill Paul 		}
1153f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
115495d67482SBill Paul 		    sizeof(struct bge_tx_bd));
115595d67482SBill Paul 	}
115695d67482SBill Paul }
115795d67482SBill Paul 
115895d67482SBill Paul static int
11593f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
116095d67482SBill Paul {
116195d67482SBill Paul 	sc->bge_txcnt = 0;
116295d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
11633927098fSPaul Saab 
1164e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1165e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
11665c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1167e6bf277eSPyun YongHyeon 
116814bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
116914bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
117038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
117114bbd30fSGleb Smirnoff 
11723927098fSPaul Saab 	/* 5700 b2 errata */
1173e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
117438cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
11753927098fSPaul Saab 
117614bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
117738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
11783927098fSPaul Saab 	/* 5700 b2 errata */
1179e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
118038cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
118195d67482SBill Paul 
118295d67482SBill Paul 	return (0);
118395d67482SBill Paul }
118495d67482SBill Paul 
118595d67482SBill Paul static void
11863e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
11873e9b1bcaSJung-uk Kim {
11883e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
11893e9b1bcaSJung-uk Kim 
11903e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
11913e9b1bcaSJung-uk Kim 
11923e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
11933e9b1bcaSJung-uk Kim 
119445ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
11953e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
119645ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
11973e9b1bcaSJung-uk Kim 	else
119845ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
11993e9b1bcaSJung-uk Kim }
12003e9b1bcaSJung-uk Kim 
12013e9b1bcaSJung-uk Kim static void
12023f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
120395d67482SBill Paul {
120495d67482SBill Paul 	struct ifnet *ifp;
120595d67482SBill Paul 	struct ifmultiaddr *ifma;
12063f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
120795d67482SBill Paul 	int h, i;
120895d67482SBill Paul 
12090f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
12100f9bd73bSSam Leffler 
1211fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
121295d67482SBill Paul 
121395d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
121495d67482SBill Paul 		for (i = 0; i < 4; i++)
12150c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
121695d67482SBill Paul 		return;
121795d67482SBill Paul 	}
121895d67482SBill Paul 
121995d67482SBill Paul 	/* First, zot all the existing filters. */
122095d67482SBill Paul 	for (i = 0; i < 4; i++)
122195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
122295d67482SBill Paul 
122395d67482SBill Paul 	/* Now program new ones. */
1224eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
122595d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
122695d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
122795d67482SBill Paul 			continue;
12280e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
12290c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
12300c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
123195d67482SBill Paul 	}
1232eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
123395d67482SBill Paul 
123495d67482SBill Paul 	for (i = 0; i < 4; i++)
123595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
123695d67482SBill Paul }
123795d67482SBill Paul 
12388cb1383cSDoug Ambrisko static void
1239cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1240cb2eacc7SYaroslav Tykhiy {
1241cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1242cb2eacc7SYaroslav Tykhiy 
1243cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1244cb2eacc7SYaroslav Tykhiy 
1245cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1246cb2eacc7SYaroslav Tykhiy 
1247cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1248cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1249cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1250cb2eacc7SYaroslav Tykhiy 	else
1251cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1252cb2eacc7SYaroslav Tykhiy }
1253cb2eacc7SYaroslav Tykhiy 
1254cb2eacc7SYaroslav Tykhiy static void
1255797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
12568cb1383cSDoug Ambrisko {
1257797ab05eSPyun YongHyeon 
12588cb1383cSDoug Ambrisko 	/*
12598cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
12608cb1383cSDoug Ambrisko 	 */
12618cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
12628cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
12638cb1383cSDoug Ambrisko 
12648cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
12658cb1383cSDoug Ambrisko 		switch (type) {
12668cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12678cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
12688cb1383cSDoug Ambrisko 			break;
12698cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12708cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
12718cb1383cSDoug Ambrisko 			break;
12728cb1383cSDoug Ambrisko 		}
12738cb1383cSDoug Ambrisko 	}
12748cb1383cSDoug Ambrisko }
12758cb1383cSDoug Ambrisko 
12768cb1383cSDoug Ambrisko static void
1277797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
12788cb1383cSDoug Ambrisko {
1279797ab05eSPyun YongHyeon 
12808cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
12818cb1383cSDoug Ambrisko 		switch (type) {
12828cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12838cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
12848cb1383cSDoug Ambrisko 			/* START DONE */
12858cb1383cSDoug Ambrisko 			break;
12868cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12878cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
12888cb1383cSDoug Ambrisko 			break;
12898cb1383cSDoug Ambrisko 		}
12908cb1383cSDoug Ambrisko 	}
12918cb1383cSDoug Ambrisko }
12928cb1383cSDoug Ambrisko 
12938cb1383cSDoug Ambrisko static void
1294797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
12958cb1383cSDoug Ambrisko {
1296797ab05eSPyun YongHyeon 
12978cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
12988cb1383cSDoug Ambrisko 		switch (type) {
12998cb1383cSDoug Ambrisko 		case BGE_RESET_START:
13008cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
13018cb1383cSDoug Ambrisko 			break;
13028cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
13038cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
13048cb1383cSDoug Ambrisko 			break;
13058cb1383cSDoug Ambrisko 		}
13068cb1383cSDoug Ambrisko 	}
13078cb1383cSDoug Ambrisko }
13088cb1383cSDoug Ambrisko 
1309797ab05eSPyun YongHyeon static void
1310797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
13118cb1383cSDoug Ambrisko {
13128cb1383cSDoug Ambrisko 	int i;
13138cb1383cSDoug Ambrisko 
13148cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13158cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
13168cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
131739153c5aSJung-uk Kim 		    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
13188cb1383cSDoug Ambrisko 
13198cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
13208cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
13218cb1383cSDoug Ambrisko 				break;
13228cb1383cSDoug Ambrisko 			DELAY(10);
13238cb1383cSDoug Ambrisko 		}
13248cb1383cSDoug Ambrisko 	}
13258cb1383cSDoug Ambrisko }
13268cb1383cSDoug Ambrisko 
132795d67482SBill Paul /*
1328c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
132995d67482SBill Paul  */
133095d67482SBill Paul static int
13313f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
133295d67482SBill Paul {
13333f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
1334fbc374afSPyun YongHyeon 	uint16_t val;
133595d67482SBill Paul 	int i;
133695d67482SBill Paul 
13378cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
1338e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
133995d67482SBill Paul 
134095d67482SBill Paul 	/* Clear the MAC control register */
134195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
134295d67482SBill Paul 
134395d67482SBill Paul 	/*
134495d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
134595d67482SBill Paul 	 * internal memory.
134695d67482SBill Paul 	 */
134795d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
13483f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
134995d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
135095d67482SBill Paul 
135195d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
13523f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
135395d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
135495d67482SBill Paul 
1355fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1356fbc374afSPyun YongHyeon 		/*
1357d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1358fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1359fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1360fbc374afSPyun YongHyeon 		 */
1361fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1362fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1363fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1364fbc374afSPyun YongHyeon 	}
1365fbc374afSPyun YongHyeon 
1366186f842bSJung-uk Kim 	/*
1367186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1368186f842bSJung-uk Kim 	 */
1369186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1370186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1371652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
1372186f842bSJung-uk Kim 		/* Read watermark not used, 128 bytes for write. */
1373186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1374652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
13754c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1376186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1377186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1378186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1379186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1380186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1381186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1382cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1383cbb2b2feSPyun YongHyeon 			/*
1384cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1385cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1386cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1387cbb2b2feSPyun YongHyeon 			 * register.
1388cbb2b2feSPyun YongHyeon 			 */
1389cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1390cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1391186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1392186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1393186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1394186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1395186f842bSJung-uk Kim 		} else {
1396186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1397186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1398186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
13990c8aa4eaSJung-uk Kim 			    0x0F;
1400186f842bSJung-uk Kim 		}
1401e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1402e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
14033f74909aSGleb Smirnoff 			uint32_t tmp;
14045cba12d3SPaul Saab 
1405186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
14060c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1407186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1408186f842bSJung-uk Kim 				dma_rw_ctl |=
1409186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
14105cba12d3SPaul Saab 
1411186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1412186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1413186f842bSJung-uk Kim 		}
1414186f842bSJung-uk Kim 	} else {
1415186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1416186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1417186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1418186f842bSJung-uk Kim 
1419186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1420186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1421186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1422186f842bSJung-uk Kim 	}
1423186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1424186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1425186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1426186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1427e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1428186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
14295cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
14305cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
143195d67482SBill Paul 
143295d67482SBill Paul 	/*
143395d67482SBill Paul 	 * Set up general mode register.
143495d67482SBill Paul 	 */
1435e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
143695d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1437ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
143895d67482SBill Paul 
143995d67482SBill Paul 	/*
144090447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
144190447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
144290447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
144390447aadSMarius Strobl 	 * certain bridges.
144490447aadSMarius Strobl 	 */
144590447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
144690447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
144790447aadSMarius Strobl 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_FORCE_PCI32);
144890447aadSMarius Strobl 
144990447aadSMarius Strobl 	/*
14508cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
14518cb1383cSDoug Ambrisko 	 */
14528cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
14538cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
14548cb1383cSDoug Ambrisko 
14558cb1383cSDoug Ambrisko 	/*
1456ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1457c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1458c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
145995d67482SBill Paul 	 */
1460c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1461c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
146295d67482SBill Paul 
146395d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
14640c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
146595d67482SBill Paul 
146638cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
146738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
146838cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
146938cc658fSJohn Baldwin 
147038cc658fSJohn Baldwin 		/* Put PHY into ready state */
147138cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
147238cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
147338cc658fSJohn Baldwin 		DELAY(40);
147438cc658fSJohn Baldwin 	}
147538cc658fSJohn Baldwin 
147695d67482SBill Paul 	return (0);
147795d67482SBill Paul }
147895d67482SBill Paul 
147995d67482SBill Paul static int
14803f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
148195d67482SBill Paul {
148295d67482SBill Paul 	struct bge_rcb *rcb;
1483e907febfSPyun YongHyeon 	bus_size_t vrcb;
1484e907febfSPyun YongHyeon 	bge_hostaddr taddr;
14856f8718a3SScott Long 	uint32_t val;
148695d67482SBill Paul 	int i;
148795d67482SBill Paul 
148895d67482SBill Paul 	/*
148995d67482SBill Paul 	 * Initialize the memory window pointer register so that
149095d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
149195d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
149295d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
149395d67482SBill Paul 	 */
149495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
149595d67482SBill Paul 
1496822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1497822f63fcSBill Paul 
14987ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
149995d67482SBill Paul 		/* Configure mbuf memory pool */
15000dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1501822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1502822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1503822f63fcSBill Paul 		else
150495d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
150595d67482SBill Paul 
150695d67482SBill Paul 		/* Configure DMA resource pool */
15070434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
15080434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
150995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
15100434d1b8SBill Paul 	}
151195d67482SBill Paul 
151295d67482SBill Paul 	/* Configure mbuf pool watermarks */
151338cc658fSJohn Baldwin 	if (!BGE_IS_5705_PLUS(sc)) {
1514fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1515fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1516fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
151738cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
151838cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
151938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
152038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
152138cc658fSJohn Baldwin 	} else {
152238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
152338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
152438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
152538cc658fSJohn Baldwin 	}
152695d67482SBill Paul 
152795d67482SBill Paul 	/* Configure DMA resource watermarks */
152895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
152995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
153095d67482SBill Paul 
153195d67482SBill Paul 	/* Enable buffer manager */
15327ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
153395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
153495d67482SBill Paul 		    BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
153595d67482SBill Paul 
153695d67482SBill Paul 		/* Poll for buffer manager start indication */
153795d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
1538d5d23857SJung-uk Kim 			DELAY(10);
15390c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
154095d67482SBill Paul 				break;
154195d67482SBill Paul 		}
154295d67482SBill Paul 
154395d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1544fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1545fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
154695d67482SBill Paul 			return (ENXIO);
154795d67482SBill Paul 		}
15480434d1b8SBill Paul 	}
154995d67482SBill Paul 
155095d67482SBill Paul 	/* Enable flow-through queues */
15510c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
155295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
155395d67482SBill Paul 
155495d67482SBill Paul 	/* Wait until queue initialization is complete */
155595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1556d5d23857SJung-uk Kim 		DELAY(10);
155795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
155895d67482SBill Paul 			break;
155995d67482SBill Paul 	}
156095d67482SBill Paul 
156195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1562fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
156395d67482SBill Paul 		return (ENXIO);
156495d67482SBill Paul 	}
156595d67482SBill Paul 
156695d67482SBill Paul 	/* Initialize the standard RX ring control block */
1567f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1568f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1569f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1570f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1571f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1572f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1573f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
15747ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
15750434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
15760434d1b8SBill Paul 	else
15770434d1b8SBill Paul 		rcb->bge_maxlen_flags =
15780434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
157995d67482SBill Paul 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
15800c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
15810c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1582f41ac2beSBill Paul 
158367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
158467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
158595d67482SBill Paul 
158695d67482SBill Paul 	/*
158795d67482SBill Paul 	 * Initialize the jumbo RX ring control block
158895d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
158995d67482SBill Paul 	 * field until we're actually ready to start
159095d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
159195d67482SBill Paul 	 * high enough to require it).
159295d67482SBill Paul 	 */
15934c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1594f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1595f41ac2beSBill Paul 
1596f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1597f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1598f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1599f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1600f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1601f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1602f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
16031be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
16041be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
160595d67482SBill Paul 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
160667111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
160767111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
160867111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
160967111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1610f41ac2beSBill Paul 
16110434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
16120434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
161367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
161495d67482SBill Paul 
161595d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1616f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
161767111612SJohn Polstra 		rcb->bge_maxlen_flags =
161867111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
16190434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
16200434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
16210434d1b8SBill Paul 	}
162295d67482SBill Paul 
162395d67482SBill Paul 	/*
162495d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
162595d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
162695d67482SBill Paul 	 * each ring.
16279ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
16289ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
16299ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
16309ba784dbSScott Long 	 * are reports that it might not need to be so strict.
163138cc658fSJohn Baldwin 	 *
163238cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
163338cc658fSJohn Baldwin 	 * well.
163495d67482SBill Paul 	 */
16355345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
16366f8718a3SScott Long 		val = 8;
16376f8718a3SScott Long 	else
16386f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
16396f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
16402a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
16412a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
16422a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
164395d67482SBill Paul 
164495d67482SBill Paul 	/*
164595d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
164695d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
164795d67482SBill Paul 	 * These are located in NIC memory.
164895d67482SBill Paul 	 */
1649e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
165095d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1651e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1652e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1653e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1654e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
165595d67482SBill Paul 	}
165695d67482SBill Paul 
165795d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1658e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1659e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1660e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1661e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1662e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1663e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
16647ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
1665e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1666e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
166795d67482SBill Paul 
166895d67482SBill Paul 	/* Disable all unused RX return rings */
1669e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
167095d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1671e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1672e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1673e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
16740434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1675e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1676e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
167738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
16783f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1679e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
168095d67482SBill Paul 	}
168195d67482SBill Paul 
168295d67482SBill Paul 	/* Initialize RX ring indexes */
168338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
16842a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
168538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
16862a141b94SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700)
168738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
168895d67482SBill Paul 
168995d67482SBill Paul 	/*
169095d67482SBill Paul 	 * Set up RX return ring 0
169195d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
169295d67482SBill Paul 	 * The return rings live entirely within the host, so the
169395d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
169495d67482SBill Paul 	 */
1695e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1696e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1697e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1698e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1699e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1700e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1701e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
170295d67482SBill Paul 
170395d67482SBill Paul 	/* Set random backoff seed for TX */
170495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
17054a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
17064a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
17074a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
170895d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
170995d67482SBill Paul 
171095d67482SBill Paul 	/* Set inter-packet gap */
171195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
171295d67482SBill Paul 
171395d67482SBill Paul 	/*
171495d67482SBill Paul 	 * Specify which ring to use for packets that don't match
171595d67482SBill Paul 	 * any RX rules.
171695d67482SBill Paul 	 */
171795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
171895d67482SBill Paul 
171995d67482SBill Paul 	/*
172095d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
172195d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
172295d67482SBill Paul 	 */
172395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
172495d67482SBill Paul 
172595d67482SBill Paul 	/* Inialize RX list placement stats mask. */
17260c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
172795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
172895d67482SBill Paul 
172995d67482SBill Paul 	/* Disable host coalescing until we get it set up */
173095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
173195d67482SBill Paul 
173295d67482SBill Paul 	/* Poll to make sure it's shut down. */
173395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1734d5d23857SJung-uk Kim 		DELAY(10);
173595d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
173695d67482SBill Paul 			break;
173795d67482SBill Paul 	}
173895d67482SBill Paul 
173995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1740fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1741fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
174295d67482SBill Paul 		return (ENXIO);
174395d67482SBill Paul 	}
174495d67482SBill Paul 
174595d67482SBill Paul 	/* Set up host coalescing defaults */
174695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
174795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
174895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
174995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
17507ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
175195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
175295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
17530434d1b8SBill Paul 	}
1754b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1755b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
175695d67482SBill Paul 
175795d67482SBill Paul 	/* Set up address of statistics block */
17587ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1759f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1760f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
176195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1762f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
17630434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
176495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
17650434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
17660434d1b8SBill Paul 	}
17670434d1b8SBill Paul 
17680434d1b8SBill Paul 	/* Set up address of status block */
1769f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1770f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
177195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1772f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
177395d67482SBill Paul 
177430f57f61SPyun YongHyeon 	/* Set up status block size. */
177530f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
1776864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
177730f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
1778864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1779864104feSPyun YongHyeon 	} else {
178030f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
1781864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
1782864104feSPyun YongHyeon 	}
1783864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
1784864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
1785864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
178630f57f61SPyun YongHyeon 
178795d67482SBill Paul 	/* Turn on host coalescing state machine */
178830f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
178995d67482SBill Paul 
179095d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
179195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
179295d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
179395d67482SBill Paul 
179495d67482SBill Paul 	/* Turn on RX list placement state machine */
179595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
179695d67482SBill Paul 
179795d67482SBill Paul 	/* Turn on RX list selector state machine. */
17987ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
179995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
180095d67482SBill Paul 
1801ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
1802ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
1803ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
1804ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
1805ea3b4127SPyun YongHyeon 
1806ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
1807ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
1808ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
1809ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
1810ea3b4127SPyun YongHyeon 	else
1811ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
1812ea3b4127SPyun YongHyeon 
181395d67482SBill Paul 	/* Turn on DMA, clear stats */
1814ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
181595d67482SBill Paul 
181695d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
181795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
181895d67482SBill Paul 
181995d67482SBill Paul #ifdef notdef
182095d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
182195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
182295d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
182395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
182495d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
182595d67482SBill Paul #endif
182695d67482SBill Paul 
182795d67482SBill Paul 	/* Turn on DMA completion state machine */
18287ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
182995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
183095d67482SBill Paul 
18316f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
18326f8718a3SScott Long 
18336f8718a3SScott Long 	/* Enable host coalescing bug fix. */
1834a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
18353889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
18366f8718a3SScott Long 
18377aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
18387aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
18397aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
18407aa4b937SPyun YongHyeon 
184195d67482SBill Paul 	/* Turn on write DMA state machine */
18426f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
18434f09c4c7SMarius Strobl 	DELAY(40);
184495d67482SBill Paul 
184595d67482SBill Paul 	/* Turn on read DMA state machine */
18464f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
1847a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
1848a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
1849a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
1850a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
1851a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
1852a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
18534f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
18544f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
185555a24a05SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO) {
1856ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
185755a24a05SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
185855a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
185955a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
186055a24a05SPyun YongHyeon 	}
1861*d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
1862*d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
1863*d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
1864*d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780) {
1865*d255f2a9SPyun YongHyeon 		/*
1866*d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
1867*d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
1868*d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
1869*d255f2a9SPyun YongHyeon 		 */
1870*d255f2a9SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL,
1871*d255f2a9SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_RSRVCTRL) |
1872*d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
1873*d255f2a9SPyun YongHyeon 	}
18744f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
18754f09c4c7SMarius Strobl 	DELAY(40);
187695d67482SBill Paul 
187795d67482SBill Paul 	/* Turn on RX data completion state machine */
187895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
187995d67482SBill Paul 
188095d67482SBill Paul 	/* Turn on RX BD initiator state machine */
188195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
188295d67482SBill Paul 
188395d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
188495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
188595d67482SBill Paul 
188695d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
18877ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
188895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
188995d67482SBill Paul 
189095d67482SBill Paul 	/* Turn on send BD completion state machine */
189195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
189295d67482SBill Paul 
189395d67482SBill Paul 	/* Turn on send data completion state machine */
1894a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
1895a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
1896a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
1897a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
189895d67482SBill Paul 
189995d67482SBill Paul 	/* Turn on send data initiator state machine */
1900ca3f1187SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO)
1901ca3f1187SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE | 0x08);
1902ca3f1187SPyun YongHyeon 	else
190395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
190495d67482SBill Paul 
190595d67482SBill Paul 	/* Turn on send BD initiator state machine */
190695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
190795d67482SBill Paul 
190895d67482SBill Paul 	/* Turn on send BD selector state machine */
190995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
191095d67482SBill Paul 
19110c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
191295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
191395d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
191495d67482SBill Paul 
191595d67482SBill Paul 	/* ack/clear link change events */
191695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
19170434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
19180434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1919f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
192095d67482SBill Paul 
192195d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
1922652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
192395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1924a1d52896SBill Paul 	} else {
19256098821cSJung-uk Kim 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL | (10 << 16));
19261f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
19274c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1928a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1929a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1930a1d52896SBill Paul 	}
193195d67482SBill Paul 
19321f313773SOleg Bulyzhin 	/*
19331f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
19341f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
19351f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
19361f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
19371f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
19381f313773SOleg Bulyzhin 	 */
19391f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
19401f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
19411f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
19421f313773SOleg Bulyzhin 
194395d67482SBill Paul 	/* Enable link state change attentions. */
194495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
194595d67482SBill Paul 
194695d67482SBill Paul 	return (0);
194795d67482SBill Paul }
194895d67482SBill Paul 
19494c0da0ffSGleb Smirnoff const struct bge_revision *
19504c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
19514c0da0ffSGleb Smirnoff {
19524c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
19534c0da0ffSGleb Smirnoff 
19544c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
19554c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
19564c0da0ffSGleb Smirnoff 			return (br);
19574c0da0ffSGleb Smirnoff 	}
19584c0da0ffSGleb Smirnoff 
19594c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
19604c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
19614c0da0ffSGleb Smirnoff 			return (br);
19624c0da0ffSGleb Smirnoff 	}
19634c0da0ffSGleb Smirnoff 
19644c0da0ffSGleb Smirnoff 	return (NULL);
19654c0da0ffSGleb Smirnoff }
19664c0da0ffSGleb Smirnoff 
19674c0da0ffSGleb Smirnoff const struct bge_vendor *
19684c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
19694c0da0ffSGleb Smirnoff {
19704c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
19714c0da0ffSGleb Smirnoff 
19724c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
19734c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
19744c0da0ffSGleb Smirnoff 			return (v);
19754c0da0ffSGleb Smirnoff 
19764c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
19774c0da0ffSGleb Smirnoff 	return (NULL);
19784c0da0ffSGleb Smirnoff }
19794c0da0ffSGleb Smirnoff 
198095d67482SBill Paul /*
198195d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
19824c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
19834c0da0ffSGleb Smirnoff  *
19844c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
19857c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
19867c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
19877c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
19887c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
198995d67482SBill Paul  */
199095d67482SBill Paul static int
19913f74909aSGleb Smirnoff bge_probe(device_t dev)
199295d67482SBill Paul {
1993852c67f9SMarius Strobl 	const struct bge_type *t = bge_devs;
19944c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
19957c929cf9SJung-uk Kim 	uint16_t vid, did;
199695d67482SBill Paul 
199795d67482SBill Paul 	sc->bge_dev = dev;
19987c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
19997c929cf9SJung-uk Kim 	did = pci_get_device(dev);
20004c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
20017c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
20027c929cf9SJung-uk Kim 			char model[64], buf[96];
20034c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
20044c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
20054c0da0ffSGleb Smirnoff 			uint32_t id;
20064c0da0ffSGleb Smirnoff 
2007a5779553SStanislav Sedov 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2008a5779553SStanislav Sedov 			    BGE_PCIMISCCTL_ASICREV_SHIFT;
2009a5779553SStanislav Sedov 			if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG)
2010a5779553SStanislav Sedov 				id = pci_read_config(dev,
2011a5779553SStanislav Sedov 				    BGE_PCI_PRODID_ASICREV, 4);
20124c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
20137c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
20144e35d186SJung-uk Kim 			{
20154e35d186SJung-uk Kim #if __FreeBSD_version > 700024
20164e35d186SJung-uk Kim 				const char *pname;
20174e35d186SJung-uk Kim 
2018852c67f9SMarius Strobl 				if (bge_has_eaddr(sc) &&
2019852c67f9SMarius Strobl 				    pci_get_vpd_ident(dev, &pname) == 0)
20204e35d186SJung-uk Kim 					snprintf(model, 64, "%s", pname);
20214e35d186SJung-uk Kim 				else
20224e35d186SJung-uk Kim #endif
20237c929cf9SJung-uk Kim 					snprintf(model, 64, "%s %s",
20247c929cf9SJung-uk Kim 					    v->v_name,
20257c929cf9SJung-uk Kim 					    br != NULL ? br->br_name :
20267c929cf9SJung-uk Kim 					    "NetXtreme Ethernet Controller");
20274e35d186SJung-uk Kim 			}
2028a5779553SStanislav Sedov 			snprintf(buf, 96, "%s, %sASIC rev. %#08x", model,
2029a5779553SStanislav Sedov 			    br != NULL ? "" : "unknown ", id);
20304c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
203195d67482SBill Paul 			return (0);
203295d67482SBill Paul 		}
203395d67482SBill Paul 		t++;
203495d67482SBill Paul 	}
203595d67482SBill Paul 
203695d67482SBill Paul 	return (ENXIO);
203795d67482SBill Paul }
203895d67482SBill Paul 
2039f41ac2beSBill Paul static void
20403f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2041f41ac2beSBill Paul {
2042f41ac2beSBill Paul 	int i;
2043f41ac2beSBill Paul 
20443f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2045f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2046f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
20470ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2048f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2049f41ac2beSBill Paul 	}
2050943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2051943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2052943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2053f41ac2beSBill Paul 
20543f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2055f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2056f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2057f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2058f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2059f41ac2beSBill Paul 	}
2060943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2061943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2062943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2063f41ac2beSBill Paul 
20643f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2065f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2066f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
20670ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2068f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2069f41ac2beSBill Paul 	}
2070f41ac2beSBill Paul 
20710ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
20720ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
20730ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
20740ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2075f41ac2beSBill Paul 
2076f41ac2beSBill Paul 
20773f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2078e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2079e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2080e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2081e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2082f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2083f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2084f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2085f41ac2beSBill Paul 
2086f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2087f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2088f41ac2beSBill Paul 
20893f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2090e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2091e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2092e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2093e65bed95SPyun YongHyeon 
2094e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2095e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2096f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2097f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2098f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2099f41ac2beSBill Paul 
2100f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2101f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2102f41ac2beSBill Paul 
21033f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2104e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2105e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2106e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2107e65bed95SPyun YongHyeon 
2108e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2109e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2110f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2111f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2112f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2113f41ac2beSBill Paul 
2114f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2115f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2116f41ac2beSBill Paul 
21173f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2118e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2119e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2120e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2121e65bed95SPyun YongHyeon 
2122e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2123f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2124f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2125f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2126f41ac2beSBill Paul 
2127f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2128f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2129f41ac2beSBill Paul 
21303f74909aSGleb Smirnoff 	/* Destroy status block. */
2131e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2132e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2133e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2134e65bed95SPyun YongHyeon 
2135e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2136f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2137f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2138f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2139f41ac2beSBill Paul 
2140f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2141f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2142f41ac2beSBill Paul 
21433f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2144e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2145e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2146e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2147e65bed95SPyun YongHyeon 
2148e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2149f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2150f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2151f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2152f41ac2beSBill Paul 
2153f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2154f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2155f41ac2beSBill Paul 
21565b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
21575b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
21585b610048SPyun YongHyeon 
21593f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2160f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2161f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2162f41ac2beSBill Paul }
2163f41ac2beSBill Paul 
2164f41ac2beSBill Paul static int
21655b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
21665b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
21675b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2168f41ac2beSBill Paul {
21693f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
2170f681b29aSPyun YongHyeon 	bus_addr_t lowaddr;
21715b610048SPyun YongHyeon 	bus_size_t ring_end;
21725b610048SPyun YongHyeon 	int error;
2173f41ac2beSBill Paul 
21745b610048SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
21755b610048SPyun YongHyeon again:
21765b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
21775b610048SPyun YongHyeon 	    alignment, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
21785b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
21795b610048SPyun YongHyeon 	if (error != 0) {
21805b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
21815b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
21825b610048SPyun YongHyeon 		return (ENOMEM);
21835b610048SPyun YongHyeon 	}
21845b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
21855b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
21865b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
21875b610048SPyun YongHyeon 	if (error != 0) {
21885b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
21895b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
21905b610048SPyun YongHyeon 		return (ENOMEM);
21915b610048SPyun YongHyeon 	}
21925b610048SPyun YongHyeon 	/* Load the address of the ring. */
21935b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
21945b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
21955b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
21965b610048SPyun YongHyeon 	if (error != 0) {
21975b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
21985b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
21995b610048SPyun YongHyeon 		return (ENOMEM);
22005b610048SPyun YongHyeon 	}
22015b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
22025b610048SPyun YongHyeon 	ring_end = *paddr + maxsize;
22035b610048SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0 &&
22045b610048SPyun YongHyeon 	    BGE_ADDR_HI(*paddr) != BGE_ADDR_HI(ring_end)) {
22055b610048SPyun YongHyeon 		/*
22065b610048SPyun YongHyeon 		 * 4GB boundary crossed.  Limit maximum allowable DMA
22075b610048SPyun YongHyeon 		 * address space to 32bit and try again.
22085b610048SPyun YongHyeon 		 */
22095b610048SPyun YongHyeon 		bus_dmamap_unload(*tag, *map);
22105b610048SPyun YongHyeon 		bus_dmamem_free(*tag, *ring, *map);
22115b610048SPyun YongHyeon 		bus_dma_tag_destroy(*tag);
22125b610048SPyun YongHyeon 		if (bootverbose)
22135b610048SPyun YongHyeon 			device_printf(sc->bge_dev, "4GB boundary crossed, "
22145b610048SPyun YongHyeon 			    "limit DMA address space to 32bit for %s\n", msg);
22155b610048SPyun YongHyeon 		*ring = NULL;
22165b610048SPyun YongHyeon 		*tag = NULL;
22175b610048SPyun YongHyeon 		*map = NULL;
22185b610048SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
22195b610048SPyun YongHyeon 		goto again;
22205b610048SPyun YongHyeon 	}
22215b610048SPyun YongHyeon 	return (0);
22225b610048SPyun YongHyeon }
22235b610048SPyun YongHyeon 
22245b610048SPyun YongHyeon static int
22255b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
22265b610048SPyun YongHyeon {
22275b610048SPyun YongHyeon 	bus_addr_t lowaddr;
22285b610048SPyun YongHyeon 	bus_size_t boundary, sbsz, txsegsz, txmaxsegsz;
22295b610048SPyun YongHyeon 	int i, error;
2230f41ac2beSBill Paul 
2231f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2232f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2233f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2234f41ac2beSBill Paul 	/*
2235f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2236f41ac2beSBill Paul 	 */
22374eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2238f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
22394eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
22404eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2241e65bed95SPyun YongHyeon 	if (error != 0) {
2242fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2243fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2244e65bed95SPyun YongHyeon 		return (ENOMEM);
2245e65bed95SPyun YongHyeon 	}
2246e65bed95SPyun YongHyeon 
22475b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
22485b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
22495b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
22505b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
22515b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
22525b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
22535b610048SPyun YongHyeon 	if (error)
22545b610048SPyun YongHyeon 		return (error);
22555b610048SPyun YongHyeon 
22565b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
22575b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
22585b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
22595b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
22605b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
22615b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
22625b610048SPyun YongHyeon 	if (error)
22635b610048SPyun YongHyeon 		return (error);
22645b610048SPyun YongHyeon 
22655b610048SPyun YongHyeon 	/* Create tag for TX ring. */
22665b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
22675b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
22685b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
22695b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
22705b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
22715b610048SPyun YongHyeon 	if (error)
22725b610048SPyun YongHyeon 		return (error);
22735b610048SPyun YongHyeon 
2274f41ac2beSBill Paul 	/*
22755b610048SPyun YongHyeon 	 * Create tag for status block.
22765b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
22775b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
22785b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
22795b610048SPyun YongHyeon 	 * of configured number of ring.
2280f41ac2beSBill Paul 	 */
22815b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
22825b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
22835b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
22845b610048SPyun YongHyeon 	else
22855b610048SPyun YongHyeon 		sbsz = 32;
22865b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
22875b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
22885b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
22895b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
22905b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
22915b610048SPyun YongHyeon 	if (error)
22925b610048SPyun YongHyeon 		return (error);
22935b610048SPyun YongHyeon 
229412c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
229512c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
229612c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
229712c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
229812c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
229912c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
230012c65daeSPyun YongHyeon 	if (error)
230112c65daeSPyun YongHyeon 		return (error);
230212c65daeSPyun YongHyeon 
23035b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
23045b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
23055b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
23065b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
23075b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
23085b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
23095b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
23105b610048SPyun YongHyeon 		if (error)
23115b610048SPyun YongHyeon 			return (error);
23125b610048SPyun YongHyeon 	}
23135b610048SPyun YongHyeon 
23145b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
23155b610048SPyun YongHyeon 	boundary = 0;
23165b610048SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0)
231738cc6151SPyun YongHyeon 		boundary = BGE_DMA_BNDRY;
23185b610048SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
23195b610048SPyun YongHyeon 	    1, boundary, lowaddr, BUS_SPACE_MAXADDR, NULL,
23205b610048SPyun YongHyeon 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
23215b610048SPyun YongHyeon 	    0, NULL, NULL, &sc->bge_cdata.bge_buffer_tag);
23225b610048SPyun YongHyeon 	if (error != 0) {
23235b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
23245b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
23255b610048SPyun YongHyeon 		return (ENOMEM);
23265b610048SPyun YongHyeon 	}
23275b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
2328ca3f1187SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO) {
2329ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
2330ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
2331ca3f1187SPyun YongHyeon 	} else {
2332ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
2333ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
2334ca3f1187SPyun YongHyeon 	}
23355b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
2336ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2337ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
2338ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
2339f41ac2beSBill Paul 
2340f41ac2beSBill Paul 	if (error) {
23410ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
23420ac56796SPyun YongHyeon 		return (ENOMEM);
23430ac56796SPyun YongHyeon 	}
23440ac56796SPyun YongHyeon 
23455b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
23465b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
23470ac56796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1,
2348ca3f1187SPyun YongHyeon 	    MCLBYTES, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
23490ac56796SPyun YongHyeon 
23500ac56796SPyun YongHyeon 	if (error) {
23510ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
2352f41ac2beSBill Paul 		return (ENOMEM);
2353f41ac2beSBill Paul 	}
2354f41ac2beSBill Paul 
23553f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
2356943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2357943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
2358943787f3SPyun YongHyeon 	if (error) {
2359943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
2360943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
2361943787f3SPyun YongHyeon 		return (ENOMEM);
2362943787f3SPyun YongHyeon 	}
2363f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
23640ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2365f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
2366f41ac2beSBill Paul 		if (error) {
2367fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
2368fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
2369f41ac2beSBill Paul 			return (ENOMEM);
2370f41ac2beSBill Paul 		}
2371f41ac2beSBill Paul 	}
2372f41ac2beSBill Paul 
23733f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
2374f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
23750ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
2376f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
2377f41ac2beSBill Paul 		if (error) {
2378fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
23790ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
2380f41ac2beSBill Paul 			return (ENOMEM);
2381f41ac2beSBill Paul 		}
2382f41ac2beSBill Paul 	}
2383f41ac2beSBill Paul 
23845b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
23854c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
23865b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
23878a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
23881be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
23891be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
2390f41ac2beSBill Paul 		if (error) {
2391fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
23923f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
2393f41ac2beSBill Paul 			return (ENOMEM);
2394f41ac2beSBill Paul 		}
23953f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
2396943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2397943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
2398943787f3SPyun YongHyeon 		if (error) {
2399943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
24001b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
2401943787f3SPyun YongHyeon 			return (ENOMEM);
2402943787f3SPyun YongHyeon 		}
2403f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2404f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2405f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2406f41ac2beSBill Paul 			if (error) {
2407fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
24083f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
2409f41ac2beSBill Paul 				return (ENOMEM);
2410f41ac2beSBill Paul 			}
2411f41ac2beSBill Paul 		}
2412f41ac2beSBill Paul 	}
2413f41ac2beSBill Paul 
2414f41ac2beSBill Paul 	return (0);
2415f41ac2beSBill Paul }
2416f41ac2beSBill Paul 
2417bf6ef57aSJohn Polstra /*
2418bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2419bf6ef57aSJohn Polstra  */
2420bf6ef57aSJohn Polstra static int
2421bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2422bf6ef57aSJohn Polstra {
2423bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
242455aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
2425bf6ef57aSJohn Polstra 
242655aaf894SMarius Strobl 	d = pci_get_domain(dev);
2427bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2428bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2429bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2430bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
243155aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2432bf6ef57aSJohn Polstra 			return (1);
2433bf6ef57aSJohn Polstra 	return (0);
2434bf6ef57aSJohn Polstra }
2435bf6ef57aSJohn Polstra 
2436bf6ef57aSJohn Polstra /*
2437bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2438bf6ef57aSJohn Polstra  */
2439bf6ef57aSJohn Polstra static int
2440bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2441bf6ef57aSJohn Polstra {
2442bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2443bf6ef57aSJohn Polstra 
2444bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2445a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
2446bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2447bf6ef57aSJohn Polstra 		/*
2448a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
2449a8376f70SMarius Strobl 		 * configured in single-port mode.
2450bf6ef57aSJohn Polstra 		 */
2451bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2452bf6ef57aSJohn Polstra 			can_use_msi = 1;
2453bf6ef57aSJohn Polstra 		break;
2454bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2455bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2456bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2457bf6ef57aSJohn Polstra 			can_use_msi = 1;
2458bf6ef57aSJohn Polstra 		break;
2459a8376f70SMarius Strobl 	default:
2460a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
2461bf6ef57aSJohn Polstra 			can_use_msi = 1;
2462bf6ef57aSJohn Polstra 	}
2463bf6ef57aSJohn Polstra 	return (can_use_msi);
2464bf6ef57aSJohn Polstra }
2465bf6ef57aSJohn Polstra 
246695d67482SBill Paul static int
24673f74909aSGleb Smirnoff bge_attach(device_t dev)
246895d67482SBill Paul {
246995d67482SBill Paul 	struct ifnet *ifp;
247095d67482SBill Paul 	struct bge_softc *sc;
24714f0794ffSBjoern A. Zeeb 	uint32_t hwcfg = 0, misccfg;
247208013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
2473d648358bSPyun YongHyeon 	int error, msicount, reg, rid, trys;
247495d67482SBill Paul 
247595d67482SBill Paul 	sc = device_get_softc(dev);
247695d67482SBill Paul 	sc->bge_dev = dev;
247795d67482SBill Paul 
2478dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
2479dfe0df9aSPyun YongHyeon 
248095d67482SBill Paul 	/*
248195d67482SBill Paul 	 * Map control/status registers.
248295d67482SBill Paul 	 */
248395d67482SBill Paul 	pci_enable_busmaster(dev);
248495d67482SBill Paul 
2485736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
24865f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
248744f8f2fcSMarius Strobl 	    RF_ACTIVE);
248895d67482SBill Paul 
248995d67482SBill Paul 	if (sc->bge_res == NULL) {
2490fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
249195d67482SBill Paul 		error = ENXIO;
249295d67482SBill Paul 		goto fail;
249395d67482SBill Paul 	}
249495d67482SBill Paul 
24954f09c4c7SMarius Strobl 	/* Save various chip information. */
2496e53d81eeSPaul Saab 	sc->bge_chipid =
2497a5779553SStanislav Sedov 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2498a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
2499a5779553SStanislav Sedov 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG)
2500a5779553SStanislav Sedov 		sc->bge_chipid = pci_read_config(dev, BGE_PCI_PRODID_ASICREV,
2501a5779553SStanislav Sedov 		    4);
2502e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2503e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2504e53d81eeSPaul Saab 
250586543395SJung-uk Kim 	/*
250638cc658fSJohn Baldwin 	 * Don't enable Ethernet@WireSpeed for the 5700, 5906, or the
250786543395SJung-uk Kim 	 * 5705 A0 and A1 chips.
250886543395SJung-uk Kim 	 */
250986543395SJung-uk Kim 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
251038cc658fSJohn Baldwin 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
251186543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
251286543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)
251386543395SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_WIRESPEED;
251486543395SJung-uk Kim 
25155fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
25165fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
251708013fd3SMarius Strobl 
25180dae9719SJung-uk Kim 	/* Save chipset family. */
25190dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
2520a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
2521a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
2522a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
2523a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
2524a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
2525a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
2526a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
2527a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
2528a5779553SStanislav Sedov 		break;
25290dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
25300dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
25310dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
25320dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
25337ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
25340dae9719SJung-uk Kim 		break;
25350dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
25360dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
25370dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
25387ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5714_FAMILY /* | BGE_FLAG_JUMBO */;
25399fe569d8SXin LI 		/* FALLTHROUGH */
25400dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
25410dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
254238cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
25430dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
25449fe569d8SXin LI 		/* FALLTHROUGH */
25450dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
25460dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
25470dae9719SJung-uk Kim 		break;
25480dae9719SJung-uk Kim 	}
25490dae9719SJung-uk Kim 
25505ee49a3aSJung-uk Kim 	/* Set various bug flags. */
25511ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
25521ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
25531ec4c3a8SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_CRC_BUG;
25545ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
25555ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
25565ee49a3aSJung-uk Kim 		sc->bge_flags |= BGE_FLAG_ADC_BUG;
25575ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
25585ee49a3aSJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5704_A0_BUG;
25594150ce6fSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
25604150ce6fSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_NO_3LED;
25614150ce6fSPyun YongHyeon 	if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
25624150ce6fSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_ADJUST_TRIM;
256308bf8bb7SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc) &&
256408bf8bb7SJung-uk Kim 	    !(sc->bge_flags & BGE_FLAG_ADJUST_TRIM)) {
25655ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2566a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2567a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
25684fcf220bSJohn Baldwin 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
2569f7d1b2ebSXin LI 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
2570f7d1b2ebSXin LI 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
25715ee49a3aSJung-uk Kim 				sc->bge_flags |= BGE_FLAG_JITTER_BUG;
257238cc658fSJohn Baldwin 		} else if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
25735ee49a3aSJung-uk Kim 			sc->bge_flags |= BGE_FLAG_BER_BUG;
25745ee49a3aSJung-uk Kim 	}
25755ee49a3aSJung-uk Kim 
2576f681b29aSPyun YongHyeon 	/*
2577f681b29aSPyun YongHyeon 	 * All controllers that are not 5755 or higher have 4GB
2578f681b29aSPyun YongHyeon 	 * boundary DMA bug.
2579f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
2580f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
2581f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
2582f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
2583f681b29aSPyun YongHyeon 	 */
2584f681b29aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) == 0)
2585f681b29aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
25864f0794ffSBjoern A. Zeeb 
25874f0794ffSBjoern A. Zeeb 	/*
25884f0794ffSBjoern A. Zeeb 	 * We could possibly check for BCOM_DEVICEID_BCM5788 in bge_probe()
25894f0794ffSBjoern A. Zeeb 	 * but I do not know the DEVICEID for the 5788M.
25904f0794ffSBjoern A. Zeeb 	 */
25914f0794ffSBjoern A. Zeeb 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID;
25924f0794ffSBjoern A. Zeeb 	if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
25934f0794ffSBjoern A. Zeeb 	    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
25944f0794ffSBjoern A. Zeeb 		sc->bge_flags |= BGE_FLAG_5788;
25954f0794ffSBjoern A. Zeeb 
2596e53d81eeSPaul Saab 	/*
2597ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
2598ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
2599ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
2600ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
2601ca3f1187SPyun YongHyeon 	 * known bug which can't handle TSO if ethernet header + IP/TCP
2602ca3f1187SPyun YongHyeon 	 * header is greater than 80 bytes. The workaround for the TSO
2603ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
2604ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
2605ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
2606ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
2607ca3f1187SPyun YongHyeon 	 */
26084f4a16e1SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc)) {
26094f4a16e1SPyun YongHyeon 		/*
26104f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
26114f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
2612be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
26134f4a16e1SPyun YongHyeon 		 */
26144f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
2615be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
2616be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
2617ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
26184f4a16e1SPyun YongHyeon 	}
2619ca3f1187SPyun YongHyeon 
2620ca3f1187SPyun YongHyeon   	/*
26216f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
2622e53d81eeSPaul Saab   	 */
26236f8718a3SScott Long 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
26244c0da0ffSGleb Smirnoff 		/*
26256f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
26266f8718a3SScott Long 		 * must be a PCI Express device.
26276f8718a3SScott Long 		 */
26286f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
26290aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
2630d2b6e9a0SPyun YongHyeon 		if (pci_get_max_read_req(dev) != 4096)
2631d2b6e9a0SPyun YongHyeon 			pci_set_max_read_req(dev, 4096);
26326f8718a3SScott Long 	} else {
26336f8718a3SScott Long 		/*
26346f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
26356f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
26364c0da0ffSGleb Smirnoff 		 */
26370aaf1057SPyun YongHyeon 		if (pci_find_extcap(dev, PCIY_PCIX, &reg) == 0)
26380aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
263990447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
26404c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2641652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
26426f8718a3SScott Long 	}
26434c0da0ffSGleb Smirnoff 
2644bf6ef57aSJohn Polstra 	/*
2645fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
2646fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
2647fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
2648fd4d32feSPyun YongHyeon 	 */
2649fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
2650fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
2651fd4d32feSPyun YongHyeon 	/*
2652bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
2653bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
2654bf6ef57aSJohn Polstra 	 * normal operation.
2655bf6ef57aSJohn Polstra 	 */
26560aaf1057SPyun YongHyeon 	rid = 0;
26576a15578dSPyun YongHyeon 	if (pci_find_extcap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
26580aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
2659bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
2660bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
2661bf6ef57aSJohn Polstra 			if (msicount > 1)
2662bf6ef57aSJohn Polstra 				msicount = 1;
2663bf6ef57aSJohn Polstra 		} else
2664bf6ef57aSJohn Polstra 			msicount = 0;
2665bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2666bf6ef57aSJohn Polstra 			rid = 1;
2667bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
26680aaf1057SPyun YongHyeon 		}
26690aaf1057SPyun YongHyeon 	}
2670bf6ef57aSJohn Polstra 
2671bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2672bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
2673bf6ef57aSJohn Polstra 
2674bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
2675bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
2676bf6ef57aSJohn Polstra 		error = ENXIO;
2677bf6ef57aSJohn Polstra 		goto fail;
2678bf6ef57aSJohn Polstra 	}
2679bf6ef57aSJohn Polstra 
26804f09c4c7SMarius Strobl 	device_printf(dev,
26814f09c4c7SMarius Strobl 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; %s\n",
26824f09c4c7SMarius Strobl 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev,
26834f09c4c7SMarius Strobl 	    (sc->bge_flags & BGE_FLAG_PCIX) ? "PCI-X" :
26844f09c4c7SMarius Strobl 	    ((sc->bge_flags & BGE_FLAG_PCIE) ? "PCI-E" : "PCI"));
26854f09c4c7SMarius Strobl 
2686bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2687bf6ef57aSJohn Polstra 
268895d67482SBill Paul 	/* Try to reset the chip. */
26898cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
26908cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
26918cb1383cSDoug Ambrisko 		error = ENXIO;
26928cb1383cSDoug Ambrisko 		goto fail;
26938cb1383cSDoug Ambrisko 	}
26948cb1383cSDoug Ambrisko 
26958cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
2696f1a7e6d5SScott Long 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2697f1a7e6d5SScott Long 	    == BGE_MAGIC_NUMBER)) {
26988cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
26998cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
27008cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
27018cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
2702d67eba2fSPyun YongHyeon 			if (BGE_IS_575X_PLUS(sc))
27038cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
27048cb1383cSDoug Ambrisko 		}
27058cb1383cSDoug Ambrisko 	}
27068cb1383cSDoug Ambrisko 
27078cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
27088cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
27098cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
27108cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
27118cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
27128cb1383cSDoug Ambrisko 		error = ENXIO;
27138cb1383cSDoug Ambrisko 		goto fail;
27148cb1383cSDoug Ambrisko 	}
27158cb1383cSDoug Ambrisko 
27168cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
27178cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
271895d67482SBill Paul 
271995d67482SBill Paul 	if (bge_chipinit(sc)) {
2720fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
272195d67482SBill Paul 		error = ENXIO;
272295d67482SBill Paul 		goto fail;
272395d67482SBill Paul 	}
272495d67482SBill Paul 
272538cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
272638cc658fSJohn Baldwin 	if (error) {
272708013fd3SMarius Strobl 		device_printf(sc->bge_dev,
272808013fd3SMarius Strobl 		    "failed to read station address\n");
272995d67482SBill Paul 		error = ENXIO;
273095d67482SBill Paul 		goto fail;
273195d67482SBill Paul 	}
273295d67482SBill Paul 
2733f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
27347ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
2735f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2736f41ac2beSBill Paul 	else
2737f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2738f41ac2beSBill Paul 
27395b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
2740fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2741fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2742f41ac2beSBill Paul 		error = ENXIO;
2743f41ac2beSBill Paul 		goto fail;
2744f41ac2beSBill Paul 	}
2745f41ac2beSBill Paul 
274635f945cdSPyun YongHyeon 	bge_add_sysctls(sc);
274735f945cdSPyun YongHyeon 
274895d67482SBill Paul 	/* Set default tuneable values. */
274995d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
275095d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
275195d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
27526f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
27536f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
275495d67482SBill Paul 
275535f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
275635f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
275735f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
275835f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
275935f945cdSPyun YongHyeon 
276095d67482SBill Paul 	/* Set up ifnet structure */
2761fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2762fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2763fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2764fc74a9f9SBrooks Davis 		error = ENXIO;
2765fc74a9f9SBrooks Davis 		goto fail;
2766fc74a9f9SBrooks Davis 	}
276795d67482SBill Paul 	ifp->if_softc = sc;
27689bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
276995d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
277095d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
277195d67482SBill Paul 	ifp->if_start = bge_start;
277295d67482SBill Paul 	ifp->if_init = bge_init;
27734d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
27744d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
27754d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
277635f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
2777d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
27784e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
2779ca3f1187SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TSO) != 0) {
2780ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
278104bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
2782ca3f1187SPyun YongHyeon 	}
27834e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
27844e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
27854e35d186SJung-uk Kim #endif
278695d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
278775719184SGleb Smirnoff #ifdef DEVICE_POLLING
278875719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
278975719184SGleb Smirnoff #endif
279095d67482SBill Paul 
2791a1d52896SBill Paul 	/*
2792d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2793d375e524SGleb Smirnoff 	 * to hardware bugs.
2794d375e524SGleb Smirnoff 	 */
2795d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2796d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
27974d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
2798d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2799d375e524SGleb Smirnoff 	}
2800d375e524SGleb Smirnoff 
2801d375e524SGleb Smirnoff 	/*
2802a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
280341abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
280441abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
280541abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
280641abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
280741abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
280841abcc1bSPaul Saab 	 * SK-9D41.
2809a1d52896SBill Paul 	 */
281041abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
281141abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
28125fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
28135fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
2814f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2815f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2816fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2817f6789fbaSPyun YongHyeon 			error = ENXIO;
2818f6789fbaSPyun YongHyeon 			goto fail;
2819f6789fbaSPyun YongHyeon 		}
282041abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
282141abcc1bSPaul Saab 	}
282241abcc1bSPaul Saab 
282395d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
2824ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
2825ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
2826ea3b4127SPyun YongHyeon 		if (BGE_IS_5714_FAMILY(sc))
2827ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
2828ea3b4127SPyun YongHyeon 		else
2829652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
2830ea3b4127SPyun YongHyeon 	}
283195d67482SBill Paul 
2832652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
28330c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
28340c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
28350c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
28366098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
28376098821cSJung-uk Kim 		    0, NULL);
283895d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
283995d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
2840da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
284195d67482SBill Paul 	} else {
284295d67482SBill Paul 		/*
28438cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
28448cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
28458cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
28468cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
28478cb1383cSDoug Ambrisko 		 * the PHY.
284895d67482SBill Paul 		 */
28494012d104SMarius Strobl 		trys = 0;
28508cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
28518cb1383cSDoug Ambrisko again:
28528cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
28538cb1383cSDoug Ambrisko 
285495d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
285595d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
28568cb1383cSDoug Ambrisko 			if (trys++ < 4) {
28578cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
28584e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
28594e35d186SJung-uk Kim 				    BMCR_RESET);
28608cb1383cSDoug Ambrisko 				goto again;
28618cb1383cSDoug Ambrisko 			}
28628cb1383cSDoug Ambrisko 
2863fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
286495d67482SBill Paul 			error = ENXIO;
286595d67482SBill Paul 			goto fail;
286695d67482SBill Paul 		}
28678cb1383cSDoug Ambrisko 
28688cb1383cSDoug Ambrisko 		/*
28698cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
28708cb1383cSDoug Ambrisko 		 */
28718cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
28728cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
287395d67482SBill Paul 	}
287495d67482SBill Paul 
287595d67482SBill Paul 	/*
2876e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2877e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2878e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2879e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2880e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2881e255b776SJohn Polstra 	 * payloads by copying the received packets.
2882e255b776SJohn Polstra 	 */
2883652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2884652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2885652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2886e255b776SJohn Polstra 
2887e255b776SJohn Polstra 	/*
288895d67482SBill Paul 	 * Call MI attach routine.
288995d67482SBill Paul 	 */
2890fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
2891b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
28920f9bd73bSSam Leffler 
289361ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
289461ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
289561ccb9daSPyun YongHyeon 
28960f9bd73bSSam Leffler 	/*
28970f9bd73bSSam Leffler 	 * Hookup IRQ last.
28980f9bd73bSSam Leffler 	 */
28994e35d186SJung-uk Kim #if __FreeBSD_version > 700030
2900dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
2901dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
29027e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
29037e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
2904dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
2905dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
2906dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
2907dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
2908dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
2909dfe0df9aSPyun YongHyeon 			error = ENXIO;
2910dfe0df9aSPyun YongHyeon 			goto fail;
2911dfe0df9aSPyun YongHyeon 		}
2912dfe0df9aSPyun YongHyeon 		taskqueue_start_threads(&sc->bge_tq, 1, PI_NET, "%s taskq",
2913dfe0df9aSPyun YongHyeon 		    device_get_nameunit(sc->bge_dev));
2914dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
2915dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
2916dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
2917dfe0df9aSPyun YongHyeon 		if (error)
2918dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
2919dfe0df9aSPyun YongHyeon 	} else
2920dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
2921dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
2922dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
29234e35d186SJung-uk Kim #else
29244e35d186SJung-uk Kim 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
29254e35d186SJung-uk Kim 	   bge_intr, sc, &sc->bge_intrhand);
29264e35d186SJung-uk Kim #endif
29270f9bd73bSSam Leffler 
29280f9bd73bSSam Leffler 	if (error) {
2929fc74a9f9SBrooks Davis 		bge_detach(dev);
2930fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
29310f9bd73bSSam Leffler 	}
293295d67482SBill Paul 
293308013fd3SMarius Strobl 	return (0);
293408013fd3SMarius Strobl 
293595d67482SBill Paul fail:
293608013fd3SMarius Strobl 	bge_release_resources(sc);
293708013fd3SMarius Strobl 
293895d67482SBill Paul 	return (error);
293995d67482SBill Paul }
294095d67482SBill Paul 
294195d67482SBill Paul static int
29423f74909aSGleb Smirnoff bge_detach(device_t dev)
294395d67482SBill Paul {
294495d67482SBill Paul 	struct bge_softc *sc;
294595d67482SBill Paul 	struct ifnet *ifp;
294695d67482SBill Paul 
294795d67482SBill Paul 	sc = device_get_softc(dev);
2948fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
294995d67482SBill Paul 
295075719184SGleb Smirnoff #ifdef DEVICE_POLLING
295175719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
295275719184SGleb Smirnoff 		ether_poll_deregister(ifp);
295375719184SGleb Smirnoff #endif
295475719184SGleb Smirnoff 
29550f9bd73bSSam Leffler 	BGE_LOCK(sc);
295695d67482SBill Paul 	bge_stop(sc);
295795d67482SBill Paul 	bge_reset(sc);
29580f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
29590f9bd73bSSam Leffler 
29605dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
29615dda8085SOleg Bulyzhin 
2962dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
2963dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
29640f9bd73bSSam Leffler 	ether_ifdetach(ifp);
296595d67482SBill Paul 
2966652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
296795d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
296895d67482SBill Paul 	} else {
296995d67482SBill Paul 		bus_generic_detach(dev);
297095d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
297195d67482SBill Paul 	}
297295d67482SBill Paul 
297395d67482SBill Paul 	bge_release_resources(sc);
297495d67482SBill Paul 
297595d67482SBill Paul 	return (0);
297695d67482SBill Paul }
297795d67482SBill Paul 
297895d67482SBill Paul static void
29793f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
298095d67482SBill Paul {
298195d67482SBill Paul 	device_t dev;
298295d67482SBill Paul 
298395d67482SBill Paul 	dev = sc->bge_dev;
298495d67482SBill Paul 
2985dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
2986dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
2987dfe0df9aSPyun YongHyeon 
298895d67482SBill Paul 	if (sc->bge_intrhand != NULL)
298995d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
299095d67482SBill Paul 
299195d67482SBill Paul 	if (sc->bge_irq != NULL)
2992724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
2993724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
2994724bd939SJohn Polstra 
2995724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
2996724bd939SJohn Polstra 		pci_release_msi(dev);
299795d67482SBill Paul 
299895d67482SBill Paul 	if (sc->bge_res != NULL)
299995d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3000736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
300195d67482SBill Paul 
3002ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3003ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3004ad61f896SRuslan Ermilov 
3005f41ac2beSBill Paul 	bge_dma_free(sc);
300695d67482SBill Paul 
30070f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
30080f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
300995d67482SBill Paul }
301095d67482SBill Paul 
30118cb1383cSDoug Ambrisko static int
30123f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
301395d67482SBill Paul {
301495d67482SBill Paul 	device_t dev;
30155fea260fSMarius Strobl 	uint32_t cachesize, command, pcistate, reset, val;
30166f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
30170aaf1057SPyun YongHyeon 	uint16_t devctl;
30185fea260fSMarius Strobl 	int i;
301995d67482SBill Paul 
302095d67482SBill Paul 	dev = sc->bge_dev;
302195d67482SBill Paul 
302238cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
302338cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
30246f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
30256f8718a3SScott Long 			write_op = bge_writemem_direct;
30266f8718a3SScott Long 		else
30276f8718a3SScott Long 			write_op = bge_writemem_ind;
30289ba784dbSScott Long 	} else
30296f8718a3SScott Long 		write_op = bge_writereg_ind;
30306f8718a3SScott Long 
303195d67482SBill Paul 	/* Save some important PCI state. */
303295d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
303395d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
303495d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
303595d67482SBill Paul 
303695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
303795d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3038e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
303995d67482SBill Paul 
30406f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
30416f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
3042a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
30436f8718a3SScott Long 		if (bootverbose)
3044333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
30456f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
30466f8718a3SScott Long 	}
30476f8718a3SScott Long 
30486f8718a3SScott Long 	/*
30496f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
30506f8718a3SScott Long 	 * When firmware finishes its initialization it will
30516f8718a3SScott Long 	 * write ~BGE_MAGIC_NUMBER to the same location.
30526f8718a3SScott Long 	 */
30536f8718a3SScott Long 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
30546f8718a3SScott Long 
30550c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
3056e53d81eeSPaul Saab 
3057e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3058652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
30590c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
30600c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
3061e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
3062e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
30630c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
30640c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
3065e53d81eeSPaul Saab 		}
3066e53d81eeSPaul Saab 	}
3067e53d81eeSPaul Saab 
306821c9e407SDavid Christensen 	/*
30696f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
30706f8718a3SScott Long 	 * powered up in D0 uninitialized.
30716f8718a3SScott Long 	 */
30725345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
3073caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
30746f8718a3SScott Long 
307595d67482SBill Paul 	/* Issue global reset */
30766f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
307795d67482SBill Paul 
307838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
30795fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
308038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
30815fea260fSMarius Strobl 		    val | BGE_VCPU_STATUS_DRV_RESET);
30825fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
308338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
30845fea260fSMarius Strobl 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
308538cc658fSJohn Baldwin 	}
308638cc658fSJohn Baldwin 
308795d67482SBill Paul 	DELAY(1000);
308895d67482SBill Paul 
3089e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3090652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
3091e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
3092e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
30935fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
30945fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
3095e53d81eeSPaul Saab 		}
30960aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
30970aaf1057SPyun YongHyeon 		    sc->bge_expcap + PCIR_EXPRESS_DEVICE_CTL, 2);
30980aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
30999a6e301dSPyun YongHyeon 		devctl &= ~(PCIM_EXP_CTL_RELAXED_ORD_ENABLE |
31009a6e301dSPyun YongHyeon 		    PCIM_EXP_CTL_NOSNOOP_ENABLE);
31010aaf1057SPyun YongHyeon 		/* Set PCIE max payload size to 128. */
31020aaf1057SPyun YongHyeon 		devctl &= ~PCIM_EXP_CTL_MAX_PAYLOAD;
31030aaf1057SPyun YongHyeon 		pci_write_config(dev, sc->bge_expcap + PCIR_EXPRESS_DEVICE_CTL,
31040aaf1057SPyun YongHyeon 		    devctl, 2);
31050aaf1057SPyun YongHyeon 		/* Clear error status. */
31060aaf1057SPyun YongHyeon 		pci_write_config(dev, sc->bge_expcap + PCIR_EXPRESS_DEVICE_STA,
31079a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_CORRECTABLE_ERROR |
31089a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_NON_FATAL_ERROR | PCIM_EXP_STA_FATAL_ERROR |
31099a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_UNSUPPORTED_REQ, 2);
3110e53d81eeSPaul Saab 	}
3111e53d81eeSPaul Saab 
31123f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
311395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
311495d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3115e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
311695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
311795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
31180c8aa4eaSJung-uk Kim 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
3119cbb2b2feSPyun YongHyeon 	/*
3120cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
3121fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
3122cbb2b2feSPyun YongHyeon 	 * read stale status block.
3123cbb2b2feSPyun YongHyeon 	 */
3124cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
3125cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
3126cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
3127cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
3128cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
3129cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
3130cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3131cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3132cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
3133cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
3134cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3135cbb2b2feSPyun YongHyeon 		}
3136cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
3137cbb2b2feSPyun YongHyeon 		    devctl, 2);
3138cbb2b2feSPyun YongHyeon 	}
3139bf6ef57aSJohn Polstra 	/* Re-enable MSI, if neccesary, and enable the memory arbiter. */
31404c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
3141bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
3142bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
31430aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
31440aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
31450aaf1057SPyun YongHyeon 			pci_write_config(dev,
31460aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
3147bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
3148bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
3149bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
3150bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
3151bf6ef57aSJohn Polstra 		}
31524c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
31534c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
31544c0da0ffSGleb Smirnoff 	} else
3155a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3156a7b0c314SPaul Saab 
315738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
315838cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
315938cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
316038cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
316138cc658fSJohn Baldwin 				break;
316238cc658fSJohn Baldwin 			DELAY(100);
316338cc658fSJohn Baldwin 		}
316438cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
3165333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
316638cc658fSJohn Baldwin 			return (1);
316738cc658fSJohn Baldwin 		}
316838cc658fSJohn Baldwin 	} else {
316995d67482SBill Paul 		/*
31706f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
317108013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
31725fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
31735fea260fSMarius Strobl 		 * address is fitted though.
317495d67482SBill Paul 		 */
317595d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
3176d5d23857SJung-uk Kim 			DELAY(10);
317795d67482SBill Paul 			val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
317895d67482SBill Paul 			if (val == ~BGE_MAGIC_NUMBER)
317995d67482SBill Paul 				break;
318095d67482SBill Paul 		}
318195d67482SBill Paul 
31825fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
3183333704a3SPyun YongHyeon 			device_printf(dev,
3184333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
3185333704a3SPyun YongHyeon 			    val);
318638cc658fSJohn Baldwin 	}
318795d67482SBill Paul 
318895d67482SBill Paul 	/*
318995d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
319095d67482SBill Paul 	 * return to its original pre-reset state. This is a
319195d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
319295d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
319395d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
319495d67482SBill Paul 	 * results.
319595d67482SBill Paul 	 */
319695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
319795d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
319895d67482SBill Paul 			break;
319995d67482SBill Paul 		DELAY(10);
320095d67482SBill Paul 	}
320195d67482SBill Paul 
32023f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
3203e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
320495d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
320595d67482SBill Paul 
32068cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
32078cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
32088cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
32098cb1383cSDoug Ambrisko 
321095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
321195d67482SBill Paul 
3212da3003f0SBill Paul 	/*
3213da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
3214da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
3215da3003f0SBill Paul 	 * to 1.2V.
3216da3003f0SBill Paul 	 */
3217652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
3218652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
32195fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
32205fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
32215fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
3222da3003f0SBill Paul 	}
3223da3003f0SBill Paul 
3224e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3225652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
3226a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
3227a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
3228a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
32295fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
32305fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
3231e53d81eeSPaul Saab 	}
323295d67482SBill Paul 	DELAY(10000);
32338cb1383cSDoug Ambrisko 
32348cb1383cSDoug Ambrisko 	return (0);
323595d67482SBill Paul }
323695d67482SBill Paul 
3237e0b7b101SPyun YongHyeon static __inline void
3238e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
3239e0b7b101SPyun YongHyeon {
3240e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
3241e0b7b101SPyun YongHyeon 
3242e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
3243e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
3244e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
3245e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3246e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3247e0b7b101SPyun YongHyeon }
3248e0b7b101SPyun YongHyeon 
3249e0b7b101SPyun YongHyeon static __inline void
3250e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
3251e0b7b101SPyun YongHyeon {
3252e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
3253e0b7b101SPyun YongHyeon 
3254e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
3255e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
3256e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
3257e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
3258e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
3259e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
3260e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3261e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3262e0b7b101SPyun YongHyeon }
3263e0b7b101SPyun YongHyeon 
326495d67482SBill Paul /*
326595d67482SBill Paul  * Frame reception handling. This is called if there's a frame
326695d67482SBill Paul  * on the receive return list.
326795d67482SBill Paul  *
326895d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
32691be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
327095d67482SBill Paul  * 2) the frame is from the standard receive ring
327195d67482SBill Paul  */
327295d67482SBill Paul 
32731abcdbd1SAttilio Rao static int
3274dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
327595d67482SBill Paul {
327695d67482SBill Paul 	struct ifnet *ifp;
32771abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
3278b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
327995d67482SBill Paul 
32807f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
32810f9bd73bSSam Leffler 
32823f74909aSGleb Smirnoff 	/* Nothing to do. */
32837f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
32841abcdbd1SAttilio Rao 		return (rx_npkts);
3285cfcb5025SOleg Bulyzhin 
3286fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
328795d67482SBill Paul 
3288f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
3289e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
3290f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
329115eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
3292c215fd77SPyun YongHyeon 	if (ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
3293c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
3294f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
329515eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
3296f41ac2beSBill Paul 
32977f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
329895d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
32993f74909aSGleb Smirnoff 		uint32_t		rxidx;
330095d67482SBill Paul 		struct mbuf		*m = NULL;
33013f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
330295d67482SBill Paul 		int			have_tag = 0;
330395d67482SBill Paul 
330475719184SGleb Smirnoff #ifdef DEVICE_POLLING
330575719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
330675719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
330775719184SGleb Smirnoff 				break;
330875719184SGleb Smirnoff 			sc->rxcycles--;
330975719184SGleb Smirnoff 		}
331075719184SGleb Smirnoff #endif
331175719184SGleb Smirnoff 
33127f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
331395d67482SBill Paul 
331495d67482SBill Paul 		rxidx = cur_rx->bge_idx;
33157f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
331695d67482SBill Paul 
3317cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
3318cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
331995d67482SBill Paul 			have_tag = 1;
332095d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
332195d67482SBill Paul 		}
332295d67482SBill Paul 
332395d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
332495d67482SBill Paul 			jumbocnt++;
3325943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
332695d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3327e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
332895d67482SBill Paul 				continue;
332995d67482SBill Paul 			}
3330943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
3331e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
3332943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
333395d67482SBill Paul 				continue;
333495d67482SBill Paul 			}
333503e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
333695d67482SBill Paul 		} else {
333795d67482SBill Paul 			stdcnt++;
3338e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
333995d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3340e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
334195d67482SBill Paul 				continue;
334295d67482SBill Paul 			}
3343943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
3344e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
3345943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
334695d67482SBill Paul 				continue;
334795d67482SBill Paul 			}
334803e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
334995d67482SBill Paul 		}
335095d67482SBill Paul 
335195d67482SBill Paul 		ifp->if_ipackets++;
3352e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3353e255b776SJohn Polstra 		/*
3354e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
3355e65bed95SPyun YongHyeon 		 * the payload is aligned.
3356e255b776SJohn Polstra 		 */
3357652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
3358e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
3359e255b776SJohn Polstra 			    cur_rx->bge_len);
3360e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
3361e255b776SJohn Polstra 		}
3362e255b776SJohn Polstra #endif
3363473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
336495d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
336595d67482SBill Paul 
3366b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
336778178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
336895d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
33690c8aa4eaSJung-uk Kim 				if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
33700c8aa4eaSJung-uk Kim 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
337178178cd1SGleb Smirnoff 			}
3372d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
3373d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
337495d67482SBill Paul 				m->m_pkthdr.csum_data =
337595d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
3376ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
3377ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
337895d67482SBill Paul 			}
337995d67482SBill Paul 		}
338095d67482SBill Paul 
338195d67482SBill Paul 		/*
3382673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
3383673d9191SSam Leffler 		 * attach that information to the packet.
338495d67482SBill Paul 		 */
3385d147662cSGleb Smirnoff 		if (have_tag) {
33864e35d186SJung-uk Kim #if __FreeBSD_version > 700022
338778ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
338878ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
33894e35d186SJung-uk Kim #else
33904e35d186SJung-uk Kim 			VLAN_INPUT_TAG_NEW(ifp, m, vlan_tag);
33914e35d186SJung-uk Kim 			if (m == NULL)
33924e35d186SJung-uk Kim 				continue;
33934e35d186SJung-uk Kim #endif
3394d147662cSGleb Smirnoff 		}
339595d67482SBill Paul 
3396dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
33970f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
3398673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
33990f9bd73bSSam Leffler 			BGE_LOCK(sc);
3400dfe0df9aSPyun YongHyeon 		} else
3401dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
3402d4da719cSAttilio Rao 		rx_npkts++;
340325e13e68SXin LI 
340425e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
34058cf7d13dSAttilio Rao 			return (rx_npkts);
340695d67482SBill Paul 	}
340795d67482SBill Paul 
340815eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
340915eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
3410e65bed95SPyun YongHyeon 	if (stdcnt > 0)
3411f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3412e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
34134c0da0ffSGleb Smirnoff 
3414c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
3415f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
34164c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3417f41ac2beSBill Paul 
34187f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
341938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
342095d67482SBill Paul 	if (stdcnt)
3421767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
3422767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
342395d67482SBill Paul 	if (jumbocnt)
3424767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
3425767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
3426f5a034f9SPyun YongHyeon #ifdef notyet
3427f5a034f9SPyun YongHyeon 	/*
3428f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
3429f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
3430f5a034f9SPyun YongHyeon 	 */
3431f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
3432f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3433f5a034f9SPyun YongHyeon #endif
34341abcdbd1SAttilio Rao 	return (rx_npkts);
343595d67482SBill Paul }
343695d67482SBill Paul 
343795d67482SBill Paul static void
3438b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
343995d67482SBill Paul {
344095a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
344195d67482SBill Paul 	struct ifnet *ifp;
344295d67482SBill Paul 
34430f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
34440f9bd73bSSam Leffler 
34453f74909aSGleb Smirnoff 	/* Nothing to do. */
3446b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
3447cfcb5025SOleg Bulyzhin 		return;
3448cfcb5025SOleg Bulyzhin 
3449fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
345095d67482SBill Paul 
3451e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
34525c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
345395d67482SBill Paul 	/*
345495d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
345595d67482SBill Paul 	 * frames that have been sent.
345695d67482SBill Paul 	 */
3457b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
345895a0a340SPyun YongHyeon 		uint32_t		idx;
345995d67482SBill Paul 
346095d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
3461f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
346295d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
346395d67482SBill Paul 			ifp->if_opackets++;
346495d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
34650ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
3466e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
3467e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
34680ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
3469f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
3470e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
3471e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
347295d67482SBill Paul 		}
347395d67482SBill Paul 		sc->bge_txcnt--;
347495d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
347595d67482SBill Paul 	}
347695d67482SBill Paul 
347713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
34785b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
34795b01e77cSBruce Evans 		sc->bge_timer = 0;
348095d67482SBill Paul }
348195d67482SBill Paul 
348275719184SGleb Smirnoff #ifdef DEVICE_POLLING
34831abcdbd1SAttilio Rao static int
348475719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
348575719184SGleb Smirnoff {
348675719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
3487b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
3488366454f2SOleg Bulyzhin 	uint32_t statusword;
34891abcdbd1SAttilio Rao 	int rx_npkts = 0;
349075719184SGleb Smirnoff 
34913f74909aSGleb Smirnoff 	BGE_LOCK(sc);
34923f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
34933f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
34941abcdbd1SAttilio Rao 		return (rx_npkts);
34953f74909aSGleb Smirnoff 	}
349675719184SGleb Smirnoff 
3497dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3498b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3499b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3500b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3501b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3502dab5cd05SOleg Bulyzhin 
3503175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
3504175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3505dab5cd05SOleg Bulyzhin 
3506dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3507b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3508b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3509366454f2SOleg Bulyzhin 
35100c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
3511366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
3512366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
3513366454f2SOleg Bulyzhin 
3514366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
3515366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
35164c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3517652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3518366454f2SOleg Bulyzhin 			bge_link_upd(sc);
3519366454f2SOleg Bulyzhin 
3520366454f2SOleg Bulyzhin 	sc->rxcycles = count;
3521dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
352225e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
352325e13e68SXin LI 		BGE_UNLOCK(sc);
35248cf7d13dSAttilio Rao 		return (rx_npkts);
352525e13e68SXin LI 	}
3526b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
3527366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3528366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
35293f74909aSGleb Smirnoff 
35303f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
35311abcdbd1SAttilio Rao 	return (rx_npkts);
353275719184SGleb Smirnoff }
353375719184SGleb Smirnoff #endif /* DEVICE_POLLING */
353475719184SGleb Smirnoff 
3535dfe0df9aSPyun YongHyeon static int
3536dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
3537dfe0df9aSPyun YongHyeon {
3538dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
3539dfe0df9aSPyun YongHyeon 
3540dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
3541dfe0df9aSPyun YongHyeon 	/*
3542dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
3543dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
3544dfe0df9aSPyun YongHyeon 	 */
3545dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
3546dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
3547dfe0df9aSPyun YongHyeon }
3548dfe0df9aSPyun YongHyeon 
3549dfe0df9aSPyun YongHyeon static void
3550dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
3551dfe0df9aSPyun YongHyeon {
3552dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
3553dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
3554dfe0df9aSPyun YongHyeon 	uint32_t status;
3555dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
3556dfe0df9aSPyun YongHyeon 
3557dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
3558dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
3559dfe0df9aSPyun YongHyeon 
3560dfe0df9aSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3561dfe0df9aSPyun YongHyeon 		return;
3562dfe0df9aSPyun YongHyeon 
3563dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
3564dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3565dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3566dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3567dfe0df9aSPyun YongHyeon 
3568dfe0df9aSPyun YongHyeon 	/* Save producer/consumer indexess. */
3569dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3570dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3571dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
3572dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3573dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3574dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3575dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3576dfe0df9aSPyun YongHyeon 	/* Let controller work. */
3577dfe0df9aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3578dfe0df9aSPyun YongHyeon 
3579dfe0df9aSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0) {
3580dfe0df9aSPyun YongHyeon 		BGE_LOCK(sc);
3581dfe0df9aSPyun YongHyeon 		bge_link_upd(sc);
3582dfe0df9aSPyun YongHyeon 		BGE_UNLOCK(sc);
3583dfe0df9aSPyun YongHyeon 	}
3584dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3585dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
3586dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
3587dfe0df9aSPyun YongHyeon 	}
3588dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3589dfe0df9aSPyun YongHyeon 		BGE_LOCK(sc);
3590dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
3591dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
3592dfe0df9aSPyun YongHyeon 	    	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3593dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
3594dfe0df9aSPyun YongHyeon 		BGE_UNLOCK(sc);
3595dfe0df9aSPyun YongHyeon 	}
3596dfe0df9aSPyun YongHyeon }
3597dfe0df9aSPyun YongHyeon 
359895d67482SBill Paul static void
35993f74909aSGleb Smirnoff bge_intr(void *xsc)
360095d67482SBill Paul {
360195d67482SBill Paul 	struct bge_softc *sc;
360295d67482SBill Paul 	struct ifnet *ifp;
3603dab5cd05SOleg Bulyzhin 	uint32_t statusword;
3604b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
360595d67482SBill Paul 
360695d67482SBill Paul 	sc = xsc;
3607f41ac2beSBill Paul 
36080f9bd73bSSam Leffler 	BGE_LOCK(sc);
36090f9bd73bSSam Leffler 
3610dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
3611dab5cd05SOleg Bulyzhin 
361275719184SGleb Smirnoff #ifdef DEVICE_POLLING
361375719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
361475719184SGleb Smirnoff 		BGE_UNLOCK(sc);
361575719184SGleb Smirnoff 		return;
361675719184SGleb Smirnoff 	}
361775719184SGleb Smirnoff #endif
361875719184SGleb Smirnoff 
3619f30cbfc6SScott Long 	/*
3620b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
3621b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
3622b848e032SBruce Evans 	 * our current organization this just gives complications and
3623b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
3624b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
3625b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
3626b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
3627b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
3628b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
3629b848e032SBruce Evans 	 *
3630b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
3631b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
3632b848e032SBruce Evans 	 * changing later because it is more efficient to get another
3633b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
3634b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
3635b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
3636b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
3637b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
3638b848e032SBruce Evans 	 */
363938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3640b848e032SBruce Evans 
3641f584dfd1SPyun YongHyeon 	/*
3642f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
3643f584dfd1SPyun YongHyeon 	 */
3644f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
3645f584dfd1SPyun YongHyeon 
3646f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
3647f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3648f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3649f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3650f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3651f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3652f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3653f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3654f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3655f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3656f584dfd1SPyun YongHyeon 
36571f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
36584c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3659f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
3660dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
366195d67482SBill Paul 
366213f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
36633f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
3664dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
366525e13e68SXin LI 	}
366695d67482SBill Paul 
366725e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
36683f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
3669b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
367095d67482SBill Paul 	}
367195d67482SBill Paul 
367213f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
367313f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
36740f9bd73bSSam Leffler 		bge_start_locked(ifp);
36750f9bd73bSSam Leffler 
36760f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
367795d67482SBill Paul }
367895d67482SBill Paul 
367995d67482SBill Paul static void
36808cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
36818cb1383cSDoug Ambrisko {
36828cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
36838cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
36848cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
36858cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
36868cb1383cSDoug Ambrisko 		else {
3687899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
36888cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
36898cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
36908cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
36918cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
36928cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
369339153c5aSJung-uk Kim 			    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
36948cb1383cSDoug Ambrisko 		}
36958cb1383cSDoug Ambrisko 	}
36968cb1383cSDoug Ambrisko }
36978cb1383cSDoug Ambrisko 
36988cb1383cSDoug Ambrisko static void
3699b74e67fbSGleb Smirnoff bge_tick(void *xsc)
37000f9bd73bSSam Leffler {
3701b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
370295d67482SBill Paul 	struct mii_data *mii = NULL;
370395d67482SBill Paul 
37040f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
370595d67482SBill Paul 
37065dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
37075dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
37085dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
37095dda8085SOleg Bulyzhin 	    	return;
37105dda8085SOleg Bulyzhin 
37117ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
37120434d1b8SBill Paul 		bge_stats_update_regs(sc);
37130434d1b8SBill Paul 	else
371495d67482SBill Paul 		bge_stats_update(sc);
371595d67482SBill Paul 
3716652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
371795d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
371882b67c01SOleg Bulyzhin 		/*
371982b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
372082b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
372182b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
372282b67c01SOleg Bulyzhin 		 */
372382b67c01SOleg Bulyzhin 		if (!sc->bge_link)
372495d67482SBill Paul 			mii_tick(mii);
37257b97099dSOleg Bulyzhin 	} else {
37267b97099dSOleg Bulyzhin 		/*
37277b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
37287b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
37297b97099dSOleg Bulyzhin 		 * and trigger interrupt.
37307b97099dSOleg Bulyzhin 		 */
37317b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
37323f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
37337b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
37347b97099dSOleg Bulyzhin #endif
37357b97099dSOleg Bulyzhin 		{
37367b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
37374f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
37384f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
37397b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
37404f0794ffSBjoern A. Zeeb 		else
37414f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
37427b97099dSOleg Bulyzhin 		}
3743dab5cd05SOleg Bulyzhin 	}
374495d67482SBill Paul 
37458cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
3746b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
37478cb1383cSDoug Ambrisko 
3748dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
374995d67482SBill Paul }
375095d67482SBill Paul 
375195d67482SBill Paul static void
37523f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
37530434d1b8SBill Paul {
37543f74909aSGleb Smirnoff 	struct ifnet *ifp;
37552280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
37560434d1b8SBill Paul 
3757fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
37582280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
37590434d1b8SBill Paul 
37602280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
37612280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
37622280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
37632280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
37642280c16bSPyun YongHyeon 	stats->outXonSent +=
37652280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
37662280c16bSPyun YongHyeon 	stats->outXoffSent +=
37672280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
37682280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
37692280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
37702280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
37712280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
37722280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
37732280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
37742280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
37752280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
37762280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
37772280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
37782280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
37792280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
37802280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
37812280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
37822280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
37832280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
37842280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
37852280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
37867e6e2507SJung-uk Kim 
37872280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
37882280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
37892280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
37902280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
37912280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
37922280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
37932280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
37942280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
37952280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
37962280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
37972280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
37982280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
37992280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
38002280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
38012280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
38022280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
38032280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
38042280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
38052280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
38062280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
38072280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
38082280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
38092280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
38102280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
38112280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
38122280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
38132280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
38142280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
38152280c16bSPyun YongHyeon 
38162280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
38172280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
38182280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
38192280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
38202280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
38212280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
38222280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
38232280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
38242280c16bSPyun YongHyeon 	stats->InputDiscards +=
38252280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
38262280c16bSPyun YongHyeon 	stats->InputErrors +=
38272280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
38282280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
38292280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
38302280c16bSPyun YongHyeon 
38312280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
38322280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
38332280c16bSPyun YongHyeon 	    stats->InputErrors);
38342280c16bSPyun YongHyeon }
38352280c16bSPyun YongHyeon 
38362280c16bSPyun YongHyeon static void
38372280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
38382280c16bSPyun YongHyeon {
38392280c16bSPyun YongHyeon 
38402280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
38412280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
38422280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
38432280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
38442280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
38452280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
38462280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
38472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
38482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
38492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
38502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
38512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
38522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
38532280c16bSPyun YongHyeon 
38542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
38552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
38562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
38572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
38582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
38592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
38602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
38612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
38622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
38632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
38642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
38652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
38662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
38672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
38682280c16bSPyun YongHyeon 
38692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
38702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
38712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
38722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
38732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
38742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
38752280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
38760434d1b8SBill Paul }
38770434d1b8SBill Paul 
38780434d1b8SBill Paul static void
38793f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
388095d67482SBill Paul {
388195d67482SBill Paul 	struct ifnet *ifp;
3882e907febfSPyun YongHyeon 	bus_size_t stats;
38837e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
388495d67482SBill Paul 
3885fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
388695d67482SBill Paul 
3887e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3888e907febfSPyun YongHyeon 
3889e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
3890e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
389195d67482SBill Paul 
38928634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
38936b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
38946fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
38956fb34dd2SOleg Bulyzhin 
38966fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
38976b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
38986fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
38996fb34dd2SOleg Bulyzhin 
39006fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
39016b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
39026fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
390395d67482SBill Paul 
3904e907febfSPyun YongHyeon #undef	READ_STAT
390595d67482SBill Paul }
390695d67482SBill Paul 
390795d67482SBill Paul /*
3908d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3909d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3910d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
3911d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
3912d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3913d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
3914d375e524SGleb Smirnoff  */
3915d375e524SGleb Smirnoff static __inline int
3916d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
3917d375e524SGleb Smirnoff {
3918d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
3919d375e524SGleb Smirnoff 	struct mbuf *last;
3920d375e524SGleb Smirnoff 
3921d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
3922d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
3923d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
3924d375e524SGleb Smirnoff 		last = m;
3925d375e524SGleb Smirnoff 	} else {
3926d375e524SGleb Smirnoff 		/*
3927d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
3928d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
3929d375e524SGleb Smirnoff 		 */
3930d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
3931d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
3932d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
3933d375e524SGleb Smirnoff 			struct mbuf *n;
3934d375e524SGleb Smirnoff 
3935d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
3936d375e524SGleb Smirnoff 			if (n == NULL)
3937d375e524SGleb Smirnoff 				return (ENOBUFS);
3938d375e524SGleb Smirnoff 			n->m_len = 0;
3939d375e524SGleb Smirnoff 			last->m_next = n;
3940d375e524SGleb Smirnoff 			last = n;
3941d375e524SGleb Smirnoff 		}
3942d375e524SGleb Smirnoff 	}
3943d375e524SGleb Smirnoff 
3944d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
3945d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
3946d375e524SGleb Smirnoff 	last->m_len += padlen;
3947d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
3948d375e524SGleb Smirnoff 
3949d375e524SGleb Smirnoff 	return (0);
3950d375e524SGleb Smirnoff }
3951d375e524SGleb Smirnoff 
3952ca3f1187SPyun YongHyeon static struct mbuf *
3953ca3f1187SPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss)
3954ca3f1187SPyun YongHyeon {
3955ca3f1187SPyun YongHyeon 	struct ip *ip;
3956ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
3957ca3f1187SPyun YongHyeon 	struct mbuf *n;
3958ca3f1187SPyun YongHyeon 	uint16_t hlen;
39595b355c4fSPyun YongHyeon 	uint32_t poff;
3960ca3f1187SPyun YongHyeon 
3961ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
3962ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
3963ca3f1187SPyun YongHyeon 		n = m_dup(m, M_DONTWAIT);
3964ca3f1187SPyun YongHyeon 		m_freem(m);
3965ca3f1187SPyun YongHyeon 		if (n == NULL)
3966ca3f1187SPyun YongHyeon 			return (NULL);
3967ca3f1187SPyun YongHyeon 		m = n;
3968ca3f1187SPyun YongHyeon 	}
39695b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
3970ca3f1187SPyun YongHyeon 	if (m == NULL)
3971ca3f1187SPyun YongHyeon 		return (NULL);
39725b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
39735b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
3974ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
3975ca3f1187SPyun YongHyeon 	if (m == NULL)
3976ca3f1187SPyun YongHyeon 		return (NULL);
3977ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
39785b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
3979ca3f1187SPyun YongHyeon 	if (m == NULL)
3980ca3f1187SPyun YongHyeon 		return (NULL);
3981ca3f1187SPyun YongHyeon 	/*
3982ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
3983ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
3984ca3f1187SPyun YongHyeon 	 */
3985ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
3986ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
3987ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
3988ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
3989ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
3990ca3f1187SPyun YongHyeon 	/*
3991ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
3992ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
3993ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
3994ca3f1187SPyun YongHyeon 	 * we only support hardware based TSO which is applicable for
3995ca3f1187SPyun YongHyeon 	 * BCM5755 or newer controllers. Hardware based TSO uses 11
3996ca3f1187SPyun YongHyeon 	 * bits to store MSS and upper 5 bits are used to store IP/TCP
3997ca3f1187SPyun YongHyeon 	 * header length(including IP/TCP options). The header length
3998ca3f1187SPyun YongHyeon 	 * is expressed as 32 bits unit.
3999ca3f1187SPyun YongHyeon 	 */
4000ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
4001ca3f1187SPyun YongHyeon 	*mss |= (hlen << 11);
4002ca3f1187SPyun YongHyeon 	return (m);
4003ca3f1187SPyun YongHyeon }
4004ca3f1187SPyun YongHyeon 
4005d375e524SGleb Smirnoff /*
400695d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
400795d67482SBill Paul  * pointers to descriptors.
400895d67482SBill Paul  */
400995d67482SBill Paul static int
4010676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
401195d67482SBill Paul {
40127e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
4013f41ac2beSBill Paul 	bus_dmamap_t		map;
4014676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
4015676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
40167e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
4017ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
40187e27542aSGleb Smirnoff 	int			nsegs, i, error;
401995d67482SBill Paul 
40206909dc43SGleb Smirnoff 	csum_flags = 0;
4021ca3f1187SPyun YongHyeon 	mss = 0;
4022ca3f1187SPyun YongHyeon 	vlan_tag = 0;
4023ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
4024ca3f1187SPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss);
4025ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
4026ca3f1187SPyun YongHyeon 			return (ENOBUFS);
4027ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
4028ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
402935f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
40306909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
40316909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
40326909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
40336909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
40346909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
40356909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
40366909dc43SGleb Smirnoff 				m_freem(m);
40376909dc43SGleb Smirnoff 				*m_head = NULL;
40386909dc43SGleb Smirnoff 				return (error);
40396909dc43SGleb Smirnoff 			}
40406909dc43SGleb Smirnoff 		}
40416909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
40426909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
40436909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
40446909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
40456909dc43SGleb Smirnoff 	}
40466909dc43SGleb Smirnoff 
4047d94f2b85SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0 &&
4048beaa2ae1SPyun YongHyeon 	    sc->bge_forced_collapse > 0 &&
4049beaa2ae1SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
4050d94f2b85SPyun YongHyeon 		/*
4051d94f2b85SPyun YongHyeon 		 * Forcedly collapse mbuf chains to overcome hardware
4052d94f2b85SPyun YongHyeon 		 * limitation which only support a single outstanding
4053d94f2b85SPyun YongHyeon 		 * DMA read operation.
4054d94f2b85SPyun YongHyeon 		 */
4055beaa2ae1SPyun YongHyeon 		if (sc->bge_forced_collapse == 1)
4056d94f2b85SPyun YongHyeon 			m = m_defrag(m, M_DONTWAIT);
4057d94f2b85SPyun YongHyeon 		else
4058beaa2ae1SPyun YongHyeon 			m = m_collapse(m, M_DONTWAIT, sc->bge_forced_collapse);
4059261f04d6SPyun YongHyeon 		if (m == NULL)
4060261f04d6SPyun YongHyeon 			m = *m_head;
4061d94f2b85SPyun YongHyeon 		*m_head = m;
4062d94f2b85SPyun YongHyeon 	}
4063d94f2b85SPyun YongHyeon 
40647e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
40650ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
4066676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
40677e27542aSGleb Smirnoff 	if (error == EFBIG) {
40684eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
4069676ad2c9SGleb Smirnoff 		if (m == NULL) {
4070676ad2c9SGleb Smirnoff 			m_freem(*m_head);
4071676ad2c9SGleb Smirnoff 			*m_head = NULL;
40727e27542aSGleb Smirnoff 			return (ENOBUFS);
40737e27542aSGleb Smirnoff 		}
4074676ad2c9SGleb Smirnoff 		*m_head = m;
40750ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
40760ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
4077676ad2c9SGleb Smirnoff 		if (error) {
4078676ad2c9SGleb Smirnoff 			m_freem(m);
4079676ad2c9SGleb Smirnoff 			*m_head = NULL;
40807e27542aSGleb Smirnoff 			return (error);
40817e27542aSGleb Smirnoff 		}
4082676ad2c9SGleb Smirnoff 	} else if (error != 0)
4083676ad2c9SGleb Smirnoff 		return (error);
40847e27542aSGleb Smirnoff 
4085167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
4086167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
40870ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
408895d67482SBill Paul 		return (ENOBUFS);
40897e27542aSGleb Smirnoff 	}
40907e27542aSGleb Smirnoff 
40910ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
4092e65bed95SPyun YongHyeon 
4093ca3f1187SPyun YongHyeon #if __FreeBSD_version > 700022
4094ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
4095ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
4096ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
4097ca3f1187SPyun YongHyeon 	}
4098ca3f1187SPyun YongHyeon #else
4099ca3f1187SPyun YongHyeon 	{
4100ca3f1187SPyun YongHyeon 		struct m_tag		*mtag;
4101ca3f1187SPyun YongHyeon 
4102ca3f1187SPyun YongHyeon 		if ((mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m)) != NULL) {
4103ca3f1187SPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
4104ca3f1187SPyun YongHyeon 			vlan_tag = VLAN_TAG_VALUE(mtag);
4105ca3f1187SPyun YongHyeon 		}
4106ca3f1187SPyun YongHyeon 	}
4107ca3f1187SPyun YongHyeon #endif
41087e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
41097e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
41107e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
41117e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
41127e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
41137e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
4114ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
4115ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
41167e27542aSGleb Smirnoff 		if (i == nsegs - 1)
41177e27542aSGleb Smirnoff 			break;
41187e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
41197e27542aSGleb Smirnoff 	}
41207e27542aSGleb Smirnoff 
41217e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
41227e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
4123676ad2c9SGleb Smirnoff 
4124f41ac2beSBill Paul 	/*
4125f41ac2beSBill Paul 	 * Insure that the map for this transmission
4126f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
4127f41ac2beSBill Paul 	 * in this chain.
4128f41ac2beSBill Paul 	 */
41297e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
41307e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
4131676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
41327e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
413395d67482SBill Paul 
41347e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
41357e27542aSGleb Smirnoff 	*txidx = idx;
413695d67482SBill Paul 
413795d67482SBill Paul 	return (0);
413895d67482SBill Paul }
413995d67482SBill Paul 
414095d67482SBill Paul /*
414195d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
414295d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
414395d67482SBill Paul  */
414495d67482SBill Paul static void
41453f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
414695d67482SBill Paul {
414795d67482SBill Paul 	struct bge_softc *sc;
4148167fdb62SPyun YongHyeon 	struct mbuf *m_head;
414914bbd30fSGleb Smirnoff 	uint32_t prodidx;
4150167fdb62SPyun YongHyeon 	int count;
415195d67482SBill Paul 
415295d67482SBill Paul 	sc = ifp->if_softc;
4153167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
415495d67482SBill Paul 
4155167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
4156167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4157167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
415895d67482SBill Paul 		return;
415995d67482SBill Paul 
416014bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
416195d67482SBill Paul 
4162167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
4163167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
4164167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4165167fdb62SPyun YongHyeon 			break;
4166167fdb62SPyun YongHyeon 		}
41674d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
416895d67482SBill Paul 		if (m_head == NULL)
416995d67482SBill Paul 			break;
417095d67482SBill Paul 
417195d67482SBill Paul 		/*
417295d67482SBill Paul 		 * XXX
4173b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
4174b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
4175b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
4176b874fdd4SYaroslav Tykhiy 		 *
4177b874fdd4SYaroslav Tykhiy 		 * XXX
417895d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
417995d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
418095d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
418195d67482SBill Paul 		 * chain at once.
418295d67482SBill Paul 		 * (paranoia -- may not actually be needed)
418395d67482SBill Paul 		 */
418495d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
418595d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
418695d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
418795d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
41884d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
418913f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
419095d67482SBill Paul 				break;
419195d67482SBill Paul 			}
419295d67482SBill Paul 		}
419395d67482SBill Paul 
419495d67482SBill Paul 		/*
419595d67482SBill Paul 		 * Pack the data into the transmit ring. If we
419695d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
419795d67482SBill Paul 		 * for the NIC to drain the ring.
419895d67482SBill Paul 		 */
4199676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
4200676ad2c9SGleb Smirnoff 			if (m_head == NULL)
4201676ad2c9SGleb Smirnoff 				break;
42024d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
420313f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
420495d67482SBill Paul 			break;
420595d67482SBill Paul 		}
4206303a718cSDag-Erling Smørgrav 		++count;
420795d67482SBill Paul 
420895d67482SBill Paul 		/*
420995d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
421095d67482SBill Paul 		 * to him.
421195d67482SBill Paul 		 */
42124e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
421345ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
42144e35d186SJung-uk Kim #else
42154e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
42164e35d186SJung-uk Kim #endif
421795d67482SBill Paul 	}
421895d67482SBill Paul 
4219167fdb62SPyun YongHyeon 	if (count > 0) {
4220aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
42215c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
42223f74909aSGleb Smirnoff 		/* Transmit. */
422338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
42243927098fSPaul Saab 		/* 5700 b2 errata */
4225e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
422638cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
422795d67482SBill Paul 
422814bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
422914bbd30fSGleb Smirnoff 
423095d67482SBill Paul 		/*
423195d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
423295d67482SBill Paul 		 */
4233b74e67fbSGleb Smirnoff 		sc->bge_timer = 5;
423495d67482SBill Paul 	}
4235167fdb62SPyun YongHyeon }
423695d67482SBill Paul 
42370f9bd73bSSam Leffler /*
42380f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
42390f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
42400f9bd73bSSam Leffler  */
424195d67482SBill Paul static void
42423f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
424395d67482SBill Paul {
42440f9bd73bSSam Leffler 	struct bge_softc *sc;
42450f9bd73bSSam Leffler 
42460f9bd73bSSam Leffler 	sc = ifp->if_softc;
42470f9bd73bSSam Leffler 	BGE_LOCK(sc);
42480f9bd73bSSam Leffler 	bge_start_locked(ifp);
42490f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
42500f9bd73bSSam Leffler }
42510f9bd73bSSam Leffler 
42520f9bd73bSSam Leffler static void
42533f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
42540f9bd73bSSam Leffler {
425595d67482SBill Paul 	struct ifnet *ifp;
42563f74909aSGleb Smirnoff 	uint16_t *m;
425795d67482SBill Paul 
42580f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
425995d67482SBill Paul 
4260fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
426195d67482SBill Paul 
426213f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
426395d67482SBill Paul 		return;
426495d67482SBill Paul 
426595d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
426695d67482SBill Paul 	bge_stop(sc);
42678cb1383cSDoug Ambrisko 
42688cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
42698cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
427095d67482SBill Paul 	bge_reset(sc);
42718cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
42728cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
42738cb1383cSDoug Ambrisko 
427495d67482SBill Paul 	bge_chipinit(sc);
427595d67482SBill Paul 
427695d67482SBill Paul 	/*
427795d67482SBill Paul 	 * Init the various state machines, ring
427895d67482SBill Paul 	 * control blocks and firmware.
427995d67482SBill Paul 	 */
428095d67482SBill Paul 	if (bge_blockinit(sc)) {
4281fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
428295d67482SBill Paul 		return;
428395d67482SBill Paul 	}
428495d67482SBill Paul 
4285fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
428695d67482SBill Paul 
428795d67482SBill Paul 	/* Specify MTU. */
428895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
4289cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
4290cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
429195d67482SBill Paul 
429295d67482SBill Paul 	/* Load our MAC address. */
42933f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
429495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
429595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
429695d67482SBill Paul 
42973e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
42983e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
429995d67482SBill Paul 
430095d67482SBill Paul 	/* Program multicast filter. */
430195d67482SBill Paul 	bge_setmulti(sc);
430295d67482SBill Paul 
4303cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
4304cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
4305cb2eacc7SYaroslav Tykhiy 
430635f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
430735f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
430835f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
430935f945cdSPyun YongHyeon 	else
431035f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
431135f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
431235f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
431335f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
431435f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
431535f945cdSPyun YongHyeon 	}
431635f945cdSPyun YongHyeon 
431795d67482SBill Paul 	/* Init RX ring. */
43183ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
43193ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
43203ee5d7daSPyun YongHyeon 		bge_stop(sc);
43213ee5d7daSPyun YongHyeon 		return;
43223ee5d7daSPyun YongHyeon 	}
432395d67482SBill Paul 
43240434d1b8SBill Paul 	/*
43250434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
43260434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
43270434d1b8SBill Paul 	 * entry of the ring.
43280434d1b8SBill Paul 	 */
43290434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
43303f74909aSGleb Smirnoff 		uint32_t		v, i;
43310434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
43320434d1b8SBill Paul 			DELAY(20);
43330434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
43340434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
43350434d1b8SBill Paul 				break;
43360434d1b8SBill Paul 		}
43370434d1b8SBill Paul 		if (i == 10)
4338fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
4339fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
43400434d1b8SBill Paul 	}
43410434d1b8SBill Paul 
434295d67482SBill Paul 	/* Init jumbo RX ring. */
4343c215fd77SPyun YongHyeon 	if (ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4344c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
43453ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
4346333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
4347b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
43483ee5d7daSPyun YongHyeon 			bge_stop(sc);
43493ee5d7daSPyun YongHyeon 			return;
43503ee5d7daSPyun YongHyeon 		}
43513ee5d7daSPyun YongHyeon 	}
435295d67482SBill Paul 
43533f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
435495d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
435595d67482SBill Paul 
43567e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
43577e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
43587e6e2507SJung-uk Kim 
435995d67482SBill Paul 	/* Init TX ring. */
436095d67482SBill Paul 	bge_init_tx_ring(sc);
436195d67482SBill Paul 
43623f74909aSGleb Smirnoff 	/* Turn on transmitter. */
436395d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
436495d67482SBill Paul 
43653f74909aSGleb Smirnoff 	/* Turn on receiver. */
436695d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
436795d67482SBill Paul 
4368dedcdf57SPyun YongHyeon 	/*
4369dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
4370dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
4371dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
4372dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
4373dedcdf57SPyun YongHyeon 	 */
4374dedcdf57SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
4375dedcdf57SPyun YongHyeon 
43762280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
43772280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
43782280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
43792280c16bSPyun YongHyeon 
438095d67482SBill Paul 	/* Tell firmware we're alive. */
438195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
438295d67482SBill Paul 
438375719184SGleb Smirnoff #ifdef DEVICE_POLLING
438475719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
438575719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
438675719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
438775719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
438838cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
438975719184SGleb Smirnoff 	} else
439075719184SGleb Smirnoff #endif
439175719184SGleb Smirnoff 
439295d67482SBill Paul 	/* Enable host interrupts. */
439375719184SGleb Smirnoff 	{
439495d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
439595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
439638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
439775719184SGleb Smirnoff 	}
439895d67482SBill Paul 
439967d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
440095d67482SBill Paul 
440113f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
440213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
440395d67482SBill Paul 
44040f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
44050f9bd73bSSam Leffler }
44060f9bd73bSSam Leffler 
44070f9bd73bSSam Leffler static void
44083f74909aSGleb Smirnoff bge_init(void *xsc)
44090f9bd73bSSam Leffler {
44100f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
44110f9bd73bSSam Leffler 
44120f9bd73bSSam Leffler 	BGE_LOCK(sc);
44130f9bd73bSSam Leffler 	bge_init_locked(sc);
44140f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
441595d67482SBill Paul }
441695d67482SBill Paul 
441795d67482SBill Paul /*
441895d67482SBill Paul  * Set media options.
441995d67482SBill Paul  */
442095d67482SBill Paul static int
44213f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
442295d67482SBill Paul {
442367d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
442467d5e043SOleg Bulyzhin 	int res;
442567d5e043SOleg Bulyzhin 
442667d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
442767d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
442867d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
442967d5e043SOleg Bulyzhin 
443067d5e043SOleg Bulyzhin 	return (res);
443167d5e043SOleg Bulyzhin }
443267d5e043SOleg Bulyzhin 
443367d5e043SOleg Bulyzhin static int
443467d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
443567d5e043SOleg Bulyzhin {
443667d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
443795d67482SBill Paul 	struct mii_data *mii;
44384f09c4c7SMarius Strobl 	struct mii_softc *miisc;
443995d67482SBill Paul 	struct ifmedia *ifm;
444095d67482SBill Paul 
444167d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
444267d5e043SOleg Bulyzhin 
444395d67482SBill Paul 	ifm = &sc->bge_ifmedia;
444495d67482SBill Paul 
444595d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
4446652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
444795d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
444895d67482SBill Paul 			return (EINVAL);
444995d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
445095d67482SBill Paul 		case IFM_AUTO:
4451ff50922bSDoug White 			/*
4452ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
4453ff50922bSDoug White 			 * mechanism for programming the autoneg
4454ff50922bSDoug White 			 * advertisement registers in TBI mode.
4455ff50922bSDoug White 			 */
44560f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4457ff50922bSDoug White 				uint32_t sgdig;
44580f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
44590f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
4460ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
4461ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
4462ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
4463ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
4464ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
4465ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
4466ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
4467ff50922bSDoug White 					DELAY(5);
4468ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
4469ff50922bSDoug White 				}
44700f89fde2SJung-uk Kim 			}
447195d67482SBill Paul 			break;
447295d67482SBill Paul 		case IFM_1000_SX:
447395d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
447495d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
447595d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
447695d67482SBill Paul 			} else {
447795d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
447895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
447995d67482SBill Paul 			}
448095d67482SBill Paul 			break;
448195d67482SBill Paul 		default:
448295d67482SBill Paul 			return (EINVAL);
448395d67482SBill Paul 		}
448495d67482SBill Paul 		return (0);
448595d67482SBill Paul 	}
448695d67482SBill Paul 
44871493e883SOleg Bulyzhin 	sc->bge_link_evt++;
448895d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
44894f09c4c7SMarius Strobl 	if (mii->mii_instance)
44904f09c4c7SMarius Strobl 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
449195d67482SBill Paul 			mii_phy_reset(miisc);
449295d67482SBill Paul 	mii_mediachg(mii);
449395d67482SBill Paul 
4494902827f6SBjoern A. Zeeb 	/*
4495902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
4496902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
4497902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
4498902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
4499902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
4500902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
4501902827f6SBjoern A. Zeeb 	 * get an RX intr.
4502902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
4503902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
4504902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
4505902827f6SBjoern A. Zeeb 	 */
45064f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
45074f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
4508902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
45094f0794ffSBjoern A. Zeeb 	else
451063ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
4511902827f6SBjoern A. Zeeb 
451295d67482SBill Paul 	return (0);
451395d67482SBill Paul }
451495d67482SBill Paul 
451595d67482SBill Paul /*
451695d67482SBill Paul  * Report current media status.
451795d67482SBill Paul  */
451895d67482SBill Paul static void
45193f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
452095d67482SBill Paul {
452167d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
452295d67482SBill Paul 	struct mii_data *mii;
452395d67482SBill Paul 
452467d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
452595d67482SBill Paul 
4526652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
452795d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
452895d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
452995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
453095d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
453195d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
45324c0da0ffSGleb Smirnoff 		else {
45334c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
453467d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
45354c0da0ffSGleb Smirnoff 			return;
45364c0da0ffSGleb Smirnoff 		}
453795d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
453895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
453995d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
454095d67482SBill Paul 		else
454195d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
454267d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
454395d67482SBill Paul 		return;
454495d67482SBill Paul 	}
454595d67482SBill Paul 
454695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
454795d67482SBill Paul 	mii_pollstat(mii);
454895d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
454995d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
455067d5e043SOleg Bulyzhin 
455167d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
455295d67482SBill Paul }
455395d67482SBill Paul 
455495d67482SBill Paul static int
45553f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
455695d67482SBill Paul {
455795d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
455895d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
455995d67482SBill Paul 	struct mii_data *mii;
4560f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
456195d67482SBill Paul 
456295d67482SBill Paul 	switch (command) {
456395d67482SBill Paul 	case SIOCSIFMTU:
45644c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
45654c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
45664c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
45674c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
45684c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
456995d67482SBill Paul 			error = EINVAL;
45704c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
457195d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
457213f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
457395d67482SBill Paul 			bge_init(sc);
457495d67482SBill Paul 		}
457595d67482SBill Paul 		break;
457695d67482SBill Paul 	case SIOCSIFFLAGS:
45770f9bd73bSSam Leffler 		BGE_LOCK(sc);
457895d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
457995d67482SBill Paul 			/*
458095d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
458195d67482SBill Paul 			 * then just use the 'set promisc mode' command
458295d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
458395d67482SBill Paul 			 * a full re-init means reloading the firmware and
458495d67482SBill Paul 			 * waiting for it to start up, which may take a
4585d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
458695d67482SBill Paul 			 */
4587f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4588f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
45893e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
45903e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
4591f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
4592d183af7fSRuslan Ermilov 					bge_setmulti(sc);
459395d67482SBill Paul 			} else
45940f9bd73bSSam Leffler 				bge_init_locked(sc);
459595d67482SBill Paul 		} else {
459613f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
459795d67482SBill Paul 				bge_stop(sc);
459895d67482SBill Paul 			}
459995d67482SBill Paul 		}
460095d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
46010f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
460295d67482SBill Paul 		error = 0;
460395d67482SBill Paul 		break;
460495d67482SBill Paul 	case SIOCADDMULTI:
460595d67482SBill Paul 	case SIOCDELMULTI:
460613f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46070f9bd73bSSam Leffler 			BGE_LOCK(sc);
460895d67482SBill Paul 			bge_setmulti(sc);
46090f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
461095d67482SBill Paul 			error = 0;
461195d67482SBill Paul 		}
461295d67482SBill Paul 		break;
461395d67482SBill Paul 	case SIOCSIFMEDIA:
461495d67482SBill Paul 	case SIOCGIFMEDIA:
4615652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
461695d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
461795d67482SBill Paul 			    &sc->bge_ifmedia, command);
461895d67482SBill Paul 		} else {
461995d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
462095d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
462195d67482SBill Paul 			    &mii->mii_media, command);
462295d67482SBill Paul 		}
462395d67482SBill Paul 		break;
462495d67482SBill Paul 	case SIOCSIFCAP:
462595d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
462675719184SGleb Smirnoff #ifdef DEVICE_POLLING
462775719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
462875719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
462975719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
463075719184SGleb Smirnoff 				if (error)
463175719184SGleb Smirnoff 					return (error);
463275719184SGleb Smirnoff 				BGE_LOCK(sc);
463375719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
463475719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
463538cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
463675719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
463775719184SGleb Smirnoff 				BGE_UNLOCK(sc);
463875719184SGleb Smirnoff 			} else {
463975719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
464075719184SGleb Smirnoff 				/* Enable interrupt even in error case */
464175719184SGleb Smirnoff 				BGE_LOCK(sc);
464275719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
464375719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
464438cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
464575719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
464675719184SGleb Smirnoff 				BGE_UNLOCK(sc);
464775719184SGleb Smirnoff 			}
464875719184SGleb Smirnoff 		}
464975719184SGleb Smirnoff #endif
4650d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
4651d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
4652d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
4653d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
465435f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
465595d67482SBill Paul 			else
465635f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
465795d67482SBill Paul 		}
4658cb2eacc7SYaroslav Tykhiy 
4659d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
4660d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
4661d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
4662d8b57f98SPyun YongHyeon 
4663ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
4664ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
4665ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
4666ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
4667ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
4668ca3f1187SPyun YongHyeon 			else
4669ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
4670ca3f1187SPyun YongHyeon 		}
4671ca3f1187SPyun YongHyeon 
4672cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
4673cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
4674cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4675cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
4676cb2eacc7SYaroslav Tykhiy 		}
4677cb2eacc7SYaroslav Tykhiy 
467804bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
467904bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
468004bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
468104bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
468204bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
4683cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
468404bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
468504bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
4686cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
4687cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
4688cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
468904bde852SPyun YongHyeon 		}
4690cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
4691cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
4692cb2eacc7SYaroslav Tykhiy #endif
469395d67482SBill Paul 		break;
469495d67482SBill Paul 	default:
4695673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
469695d67482SBill Paul 		break;
469795d67482SBill Paul 	}
469895d67482SBill Paul 
469995d67482SBill Paul 	return (error);
470095d67482SBill Paul }
470195d67482SBill Paul 
470295d67482SBill Paul static void
4703b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
470495d67482SBill Paul {
4705b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
470695d67482SBill Paul 
4707b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
4708b74e67fbSGleb Smirnoff 
4709b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
4710b74e67fbSGleb Smirnoff 		return;
4711b74e67fbSGleb Smirnoff 
4712b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
471395d67482SBill Paul 
4714fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
471595d67482SBill Paul 
471613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4717426742bfSGleb Smirnoff 	bge_init_locked(sc);
471895d67482SBill Paul 
471995d67482SBill Paul 	ifp->if_oerrors++;
472095d67482SBill Paul }
472195d67482SBill Paul 
472295d67482SBill Paul /*
472395d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
472495d67482SBill Paul  * RX and TX lists.
472595d67482SBill Paul  */
472695d67482SBill Paul static void
47273f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
472895d67482SBill Paul {
472995d67482SBill Paul 	struct ifnet *ifp;
473095d67482SBill Paul 
47310f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
47320f9bd73bSSam Leffler 
4733fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
473495d67482SBill Paul 
47350f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
473695d67482SBill Paul 
473744b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
473844b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
473944b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
474044b63691SBjoern A. Zeeb 
474144b63691SBjoern A. Zeeb 	/*
474244b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
474344b63691SBjoern A. Zeeb 	 */
474444b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
474544b63691SBjoern A. Zeeb 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
474644b63691SBjoern A. Zeeb 
474795d67482SBill Paul 	/*
47483f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
474995d67482SBill Paul 	 */
475095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
475195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
475295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
47537ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
475495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
475595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
475695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
475795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
475895d67482SBill Paul 
475995d67482SBill Paul 	/*
47603f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
476195d67482SBill Paul 	 */
476295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
476395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
476495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
476595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
476695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
47677ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
476895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
476995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
477095d67482SBill Paul 
477195d67482SBill Paul 	/*
477295d67482SBill Paul 	 * Shut down all of the memory managers and related
477395d67482SBill Paul 	 * state machines.
477495d67482SBill Paul 	 */
477595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
477695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
47777ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
477895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
47790c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
478095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
47817ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
478295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
478395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
47840434d1b8SBill Paul 	}
47852280c16bSPyun YongHyeon 	/* Update MAC statistics. */
47862280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
47872280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
478895d67482SBill Paul 
47898cb1383cSDoug Ambrisko 	bge_reset(sc);
47908cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
47918cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
47928cb1383cSDoug Ambrisko 
47938cb1383cSDoug Ambrisko 	/*
47948cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
47958cb1383cSDoug Ambrisko 	 */
47968cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
47978cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
47988cb1383cSDoug Ambrisko 	else
479995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
480095d67482SBill Paul 
480195d67482SBill Paul 	/* Free the RX lists. */
480295d67482SBill Paul 	bge_free_rx_ring_std(sc);
480395d67482SBill Paul 
480495d67482SBill Paul 	/* Free jumbo RX list. */
48054c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
480695d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
480795d67482SBill Paul 
480895d67482SBill Paul 	/* Free TX buffers. */
480995d67482SBill Paul 	bge_free_tx_ring(sc);
481095d67482SBill Paul 
481195d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
481295d67482SBill Paul 
48135dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
48141493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
48151493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
48161493e883SOleg Bulyzhin 	sc->bge_link = 0;
481795d67482SBill Paul 
48181493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
481995d67482SBill Paul }
482095d67482SBill Paul 
482195d67482SBill Paul /*
482295d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
482395d67482SBill Paul  * get confused by errant DMAs when rebooting.
482495d67482SBill Paul  */
4825b6c974e8SWarner Losh static int
48263f74909aSGleb Smirnoff bge_shutdown(device_t dev)
482795d67482SBill Paul {
482895d67482SBill Paul 	struct bge_softc *sc;
482995d67482SBill Paul 
483095d67482SBill Paul 	sc = device_get_softc(dev);
48310f9bd73bSSam Leffler 	BGE_LOCK(sc);
483295d67482SBill Paul 	bge_stop(sc);
483395d67482SBill Paul 	bge_reset(sc);
48340f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
4835b6c974e8SWarner Losh 
4836b6c974e8SWarner Losh 	return (0);
483795d67482SBill Paul }
483814afefa3SPawel Jakub Dawidek 
483914afefa3SPawel Jakub Dawidek static int
484014afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
484114afefa3SPawel Jakub Dawidek {
484214afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
484314afefa3SPawel Jakub Dawidek 
484414afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
484514afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
484614afefa3SPawel Jakub Dawidek 	bge_stop(sc);
484714afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
484814afefa3SPawel Jakub Dawidek 
484914afefa3SPawel Jakub Dawidek 	return (0);
485014afefa3SPawel Jakub Dawidek }
485114afefa3SPawel Jakub Dawidek 
485214afefa3SPawel Jakub Dawidek static int
485314afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
485414afefa3SPawel Jakub Dawidek {
485514afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
485614afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
485714afefa3SPawel Jakub Dawidek 
485814afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
485914afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
486014afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
486114afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
486214afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
486314afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
486414afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
486514afefa3SPawel Jakub Dawidek 	}
486614afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
486714afefa3SPawel Jakub Dawidek 
486814afefa3SPawel Jakub Dawidek 	return (0);
486914afefa3SPawel Jakub Dawidek }
4870dab5cd05SOleg Bulyzhin 
4871dab5cd05SOleg Bulyzhin static void
48723f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
4873dab5cd05SOleg Bulyzhin {
48741f313773SOleg Bulyzhin 	struct mii_data *mii;
48751f313773SOleg Bulyzhin 	uint32_t link, status;
4876dab5cd05SOleg Bulyzhin 
4877dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
48781f313773SOleg Bulyzhin 
48793f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
48807b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
48817b97099dSOleg Bulyzhin 
4882dab5cd05SOleg Bulyzhin 	/*
4883dab5cd05SOleg Bulyzhin 	 * Process link state changes.
4884dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
4885dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
4886dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
4887dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
4888dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
4889dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
4890dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
4891dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
48921f313773SOleg Bulyzhin 	 *
48931f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
48944c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
4895dab5cd05SOleg Bulyzhin 	 */
4896dab5cd05SOleg Bulyzhin 
48971f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
48984c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
4899dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
4900dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
49011f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
49025dda8085SOleg Bulyzhin 			mii_pollstat(mii);
49031f313773SOleg Bulyzhin 			if (!sc->bge_link &&
49041f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
49051f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
49061f313773SOleg Bulyzhin 				sc->bge_link++;
49071f313773SOleg Bulyzhin 				if (bootverbose)
49081f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
49091f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
49101f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
49111f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
49121f313773SOleg Bulyzhin 				sc->bge_link = 0;
49131f313773SOleg Bulyzhin 				if (bootverbose)
49141f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
49151f313773SOleg Bulyzhin 			}
49161f313773SOleg Bulyzhin 
49173f74909aSGleb Smirnoff 			/* Clear the interrupt. */
4918dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
4919dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
4920dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
4921dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
4922dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
4923dab5cd05SOleg Bulyzhin 		}
4924dab5cd05SOleg Bulyzhin 		return;
4925dab5cd05SOleg Bulyzhin 	}
4926dab5cd05SOleg Bulyzhin 
4927652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
49281f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
49297b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
49307b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
49311f313773SOleg Bulyzhin 				sc->bge_link++;
49321f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
49331f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
49341f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
49350c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
49361f313773SOleg Bulyzhin 				if (bootverbose)
49371f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
49383f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
49393f74909aSGleb Smirnoff 				    LINK_STATE_UP);
49407b97099dSOleg Bulyzhin 			}
49411f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
4942dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
49431f313773SOleg Bulyzhin 			if (bootverbose)
49441f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
49457b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
49461f313773SOleg Bulyzhin 		}
49471493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
49481f313773SOleg Bulyzhin 		/*
49490c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
49500c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
49510c8aa4eaSJung-uk Kim 		 * PHY link status directly.
49521f313773SOleg Bulyzhin 		 */
49531f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
49541f313773SOleg Bulyzhin 
49551f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
49561f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
49571f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
49585dda8085SOleg Bulyzhin 			mii_pollstat(mii);
49591f313773SOleg Bulyzhin 			if (!sc->bge_link &&
49601f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
49611f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
49621f313773SOleg Bulyzhin 				sc->bge_link++;
49631f313773SOleg Bulyzhin 				if (bootverbose)
49641f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
49651f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
49661f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
49671f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
49681f313773SOleg Bulyzhin 				sc->bge_link = 0;
49691f313773SOleg Bulyzhin 				if (bootverbose)
49701f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
49711f313773SOleg Bulyzhin 			}
49721f313773SOleg Bulyzhin 		}
49730c8aa4eaSJung-uk Kim 	} else {
49740c8aa4eaSJung-uk Kim 		/*
49750c8aa4eaSJung-uk Kim 		 * Discard link events for MII/GMII controllers
49760c8aa4eaSJung-uk Kim 		 * if MI auto-polling is disabled.
49770c8aa4eaSJung-uk Kim 		 */
4978dab5cd05SOleg Bulyzhin 	}
4979dab5cd05SOleg Bulyzhin 
49803f74909aSGleb Smirnoff 	/* Clear the attention. */
4981dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
4982dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
4983dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
4984dab5cd05SOleg Bulyzhin }
49856f8718a3SScott Long 
49866f8718a3SScott Long static void
49876f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
49886f8718a3SScott Long {
49896f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
49902280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
49917e32f79aSPyun YongHyeon 	char tn[32];
49927e32f79aSPyun YongHyeon 	int unit;
49936f8718a3SScott Long 
49946f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
49956f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
49966f8718a3SScott Long 
49976f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
49986f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
49996f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
50006f8718a3SScott Long 	    "Debug Information");
50016f8718a3SScott Long 
50026f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
50036f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
50046f8718a3SScott Long 	    "Register Read");
50056f8718a3SScott Long 
50066f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
50076f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
50086f8718a3SScott Long 	    "Memory Read");
50096f8718a3SScott Long 
50106f8718a3SScott Long #endif
5011763757b2SScott Long 
50127e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
5013beaa2ae1SPyun YongHyeon 	/*
5014beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
5015beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
5016beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
5017beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
5018beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
5019beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
5020beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
5021beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
5022beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
5023beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
5024beaa2ae1SPyun YongHyeon 	 */
50257e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
50267e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
50277e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
5028beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
5029beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
5030beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
5031beaa2ae1SPyun YongHyeon 	    "forced collapsing");
5032beaa2ae1SPyun YongHyeon 
503335f945cdSPyun YongHyeon 	/*
503435f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
503535f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
503635f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
503735f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
503835f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
503935f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
504035f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
504135f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
504235f945cdSPyun YongHyeon 	 */
504335f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
504435f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
504535f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
504635f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
504735f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
504835f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
504935f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
505035f945cdSPyun YongHyeon 
5051d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
50522280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
50532280c16bSPyun YongHyeon 	else
50542280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
50552280c16bSPyun YongHyeon }
5056d949071dSJung-uk Kim 
50572280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
50582280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
50592280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
50602280c16bSPyun YongHyeon 	    desc)
50612280c16bSPyun YongHyeon 
50622280c16bSPyun YongHyeon static void
50632280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
50642280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
50652280c16bSPyun YongHyeon {
50662280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
50672280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
50682280c16bSPyun YongHyeon 
50692280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
5070763757b2SScott Long 	    NULL, "BGE Statistics");
5071763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
5072763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
5073763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
5074763757b2SScott Long 	    "FramesDroppedDueToFilters");
5075763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
5076763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
5077763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
5078763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
5079763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
5080763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
508106e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
508206e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
508306e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
508406e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
5085763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
5086763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
5087763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
5088763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
5089763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
5090763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
5091763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
5092763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
5093763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
5094763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
5095763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
5096763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
5097763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
5098763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
5099763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
5100763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
5101763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
5102763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
5103763757b2SScott Long 
5104763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
5105763757b2SScott Long 	    NULL, "BGE RX Statistics");
5106763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5107763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
51081cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
5109763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
5110763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
5111763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
51121cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
5113763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
5114763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
5115763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
5116763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
5117763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
5118763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
5119763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
5120763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
5121763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
5122763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
5123763757b2SScott Long 	    "xoffPauseFramesReceived");
5124763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
5125763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
5126763757b2SScott Long 	    "ControlFramesReceived");
5127763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
5128763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
5129763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
5130763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
5131763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
5132763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
5133763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
5134763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
5135763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
513606e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
5137763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
513806e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
5139763757b2SScott Long 
5140763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
5141763757b2SScott Long 	    NULL, "BGE TX Statistics");
5142763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5143763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
51441cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
5145763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
5146763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
5147763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
5148763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
5149763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
5150763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
5151763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
5152763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
5153763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
5154763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
5155763757b2SScott Long 	    "InternalMacTransmitErrors");
5156763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
5157763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
5158763757b2SScott Long 	    "SingleCollisionFrames");
5159763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
5160763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
5161763757b2SScott Long 	    "MultipleCollisionFrames");
5162763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
5163763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
5164763757b2SScott Long 	    "DeferredTransmissions");
5165763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
5166763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
5167763757b2SScott Long 	    "ExcessiveCollisions");
5168763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
516906e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
517006e83c7eSScott Long 	    "LateCollisions");
5171763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
51721cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
5173763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
5174763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
5175763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
5176763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
5177763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
5178763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
5179763757b2SScott Long 	    "CarrierSenseErrors");
5180763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
5181763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
5182763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
5183763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
5184763757b2SScott Long }
5185763757b2SScott Long 
51862280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
51872280c16bSPyun YongHyeon 
51882280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
51892280c16bSPyun YongHyeon 	    SYSCTL_ADD_QUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
51902280c16bSPyun YongHyeon 
51912280c16bSPyun YongHyeon static void
51922280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
51932280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
51942280c16bSPyun YongHyeon {
51952280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
51962280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
51972280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
51982280c16bSPyun YongHyeon 
51992280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
52002280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
52012280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
52022280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
52032280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
52042280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
52052280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
52062280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
52072280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
52082280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
52092280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
52102280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
52112280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
52122280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
52132280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
52142280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
52152280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
52162280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
52172280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
52182280c16bSPyun YongHyeon 
52192280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
52202280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
52212280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
52222280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
52232280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
52242280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
52252280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
52261cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
52272280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
52282280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
52292280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
52302280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
52312280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
52322280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
52332280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
52342280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
52352280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
52362280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
52372280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
52382280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
52392280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
52402280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
52412280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
52422280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
52432280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
52442280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
52452280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
52462280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
52472280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
52482280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
52492280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
52502280c16bSPyun YongHyeon 
52512280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
52522280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
52532280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
52541cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
52552280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
52562280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
52572280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
52582280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
52592280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
52602280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
52612280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
52622280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
52632280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
52642280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
52652280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
52662280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
52672280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
52682280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
52692280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
52702280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
52712280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
52722280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
52732280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
52742280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
52752280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
52761cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
52772280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
52781cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
52792280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
52801cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
52812280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
52822280c16bSPyun YongHyeon }
52832280c16bSPyun YongHyeon 
52842280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
52852280c16bSPyun YongHyeon 
5286763757b2SScott Long static int
5287763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
5288763757b2SScott Long {
5289763757b2SScott Long 	struct bge_softc *sc;
529006e83c7eSScott Long 	uint32_t result;
5291d949071dSJung-uk Kim 	int offset;
5292763757b2SScott Long 
5293763757b2SScott Long 	sc = (struct bge_softc *)arg1;
5294763757b2SScott Long 	offset = arg2;
5295d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
5296d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
5297041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
52986f8718a3SScott Long }
52996f8718a3SScott Long 
53006f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
53016f8718a3SScott Long static int
53026f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
53036f8718a3SScott Long {
53046f8718a3SScott Long 	struct bge_softc *sc;
53056f8718a3SScott Long 	uint16_t *sbdata;
53066f8718a3SScott Long 	int error;
53076f8718a3SScott Long 	int result;
53086f8718a3SScott Long 	int i, j;
53096f8718a3SScott Long 
53106f8718a3SScott Long 	result = -1;
53116f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
53126f8718a3SScott Long 	if (error || (req->newptr == NULL))
53136f8718a3SScott Long 		return (error);
53146f8718a3SScott Long 
53156f8718a3SScott Long 	if (result == 1) {
53166f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
53176f8718a3SScott Long 
53186f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
53196f8718a3SScott Long 		printf("Status Block:\n");
53206f8718a3SScott Long 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
53216f8718a3SScott Long 			printf("%06x:", i);
53226f8718a3SScott Long 			for (j = 0; j < 8; j++) {
53236f8718a3SScott Long 				printf(" %04x", sbdata[i]);
53246f8718a3SScott Long 				i += 4;
53256f8718a3SScott Long 			}
53266f8718a3SScott Long 			printf("\n");
53276f8718a3SScott Long 		}
53286f8718a3SScott Long 
53296f8718a3SScott Long 		printf("Registers:\n");
53300c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
53316f8718a3SScott Long 			printf("%06x:", i);
53326f8718a3SScott Long 			for (j = 0; j < 8; j++) {
53336f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
53346f8718a3SScott Long 				i += 4;
53356f8718a3SScott Long 			}
53366f8718a3SScott Long 			printf("\n");
53376f8718a3SScott Long 		}
53386f8718a3SScott Long 
53396f8718a3SScott Long 		printf("Hardware Flags:\n");
5340a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
5341a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
53425345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
53436f8718a3SScott Long 			printf(" - 575X Plus\n");
53445345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
53456f8718a3SScott Long 			printf(" - 5705 Plus\n");
53465345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
53475345bad0SScott Long 			printf(" - 5714 Family\n");
53485345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
53495345bad0SScott Long 			printf(" - 5700 Family\n");
53506f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
53516f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
53526f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
53536f8718a3SScott Long 			printf(" - PCI-X Bus\n");
53546f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
53556f8718a3SScott Long 			printf(" - PCI Express Bus\n");
53565ee49a3aSJung-uk Kim 		if (sc->bge_flags & BGE_FLAG_NO_3LED)
53576f8718a3SScott Long 			printf(" - No 3 LEDs\n");
53586f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
53596f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
53606f8718a3SScott Long 	}
53616f8718a3SScott Long 
53626f8718a3SScott Long 	return (error);
53636f8718a3SScott Long }
53646f8718a3SScott Long 
53656f8718a3SScott Long static int
53666f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
53676f8718a3SScott Long {
53686f8718a3SScott Long 	struct bge_softc *sc;
53696f8718a3SScott Long 	int error;
53706f8718a3SScott Long 	uint16_t result;
53716f8718a3SScott Long 	uint32_t val;
53726f8718a3SScott Long 
53736f8718a3SScott Long 	result = -1;
53746f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
53756f8718a3SScott Long 	if (error || (req->newptr == NULL))
53766f8718a3SScott Long 		return (error);
53776f8718a3SScott Long 
53786f8718a3SScott Long 	if (result < 0x8000) {
53796f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
53806f8718a3SScott Long 		val = CSR_READ_4(sc, result);
53816f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
53826f8718a3SScott Long 	}
53836f8718a3SScott Long 
53846f8718a3SScott Long 	return (error);
53856f8718a3SScott Long }
53866f8718a3SScott Long 
53876f8718a3SScott Long static int
53886f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
53896f8718a3SScott Long {
53906f8718a3SScott Long 	struct bge_softc *sc;
53916f8718a3SScott Long 	int error;
53926f8718a3SScott Long 	uint16_t result;
53936f8718a3SScott Long 	uint32_t val;
53946f8718a3SScott Long 
53956f8718a3SScott Long 	result = -1;
53966f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
53976f8718a3SScott Long 	if (error || (req->newptr == NULL))
53986f8718a3SScott Long 		return (error);
53996f8718a3SScott Long 
54006f8718a3SScott Long 	if (result < 0x8000) {
54016f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
54026f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
54036f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
54046f8718a3SScott Long 	}
54056f8718a3SScott Long 
54066f8718a3SScott Long 	return (error);
54076f8718a3SScott Long }
54086f8718a3SScott Long #endif
540938cc658fSJohn Baldwin 
541038cc658fSJohn Baldwin static int
54115fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
54125fea260fSMarius Strobl {
54135fea260fSMarius Strobl 
54145fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
54155fea260fSMarius Strobl 		return (1);
54165fea260fSMarius Strobl 
54175fea260fSMarius Strobl #ifdef __sparc64__
54185fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
54195fea260fSMarius Strobl 	return (0);
54205fea260fSMarius Strobl #endif
54215fea260fSMarius Strobl 	return (1);
54225fea260fSMarius Strobl }
54235fea260fSMarius Strobl 
54245fea260fSMarius Strobl static int
542538cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
542638cc658fSJohn Baldwin {
542738cc658fSJohn Baldwin 	uint32_t mac_addr;
542838cc658fSJohn Baldwin 
542938cc658fSJohn Baldwin 	mac_addr = bge_readmem_ind(sc, 0x0c14);
543038cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
543138cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
543238cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
543338cc658fSJohn Baldwin 		mac_addr = bge_readmem_ind(sc, 0x0c18);
543438cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
543538cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
543638cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
543738cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
54385fea260fSMarius Strobl 		return (0);
543938cc658fSJohn Baldwin 	}
54405fea260fSMarius Strobl 	return (1);
544138cc658fSJohn Baldwin }
544238cc658fSJohn Baldwin 
544338cc658fSJohn Baldwin static int
544438cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
544538cc658fSJohn Baldwin {
544638cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
544738cc658fSJohn Baldwin 
544838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
544938cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
545038cc658fSJohn Baldwin 
54515fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
54525fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
545338cc658fSJohn Baldwin }
545438cc658fSJohn Baldwin 
545538cc658fSJohn Baldwin static int
545638cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
545738cc658fSJohn Baldwin {
545838cc658fSJohn Baldwin 
54595fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
54605fea260fSMarius Strobl 		return (1);
54615fea260fSMarius Strobl 
54625fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
54635fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
546438cc658fSJohn Baldwin }
546538cc658fSJohn Baldwin 
546638cc658fSJohn Baldwin static int
546738cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
546838cc658fSJohn Baldwin {
546938cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
547038cc658fSJohn Baldwin 		/* NOTE: Order is critical */
54715fea260fSMarius Strobl 		bge_get_eaddr_fw,
547238cc658fSJohn Baldwin 		bge_get_eaddr_mem,
547338cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
547438cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
547538cc658fSJohn Baldwin 		NULL
547638cc658fSJohn Baldwin 	};
547738cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
547838cc658fSJohn Baldwin 
547938cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
548038cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
548138cc658fSJohn Baldwin 			break;
548238cc658fSJohn Baldwin 	}
548338cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
548438cc658fSJohn Baldwin }
5485